//************************************************************************
//
// File Name			: CommonScript.js
// Module name			: 5eserpraise Java Script Functionality
// Description			: Java script for user input validation
// Author			: Danny
// Date of creation		: 5-Apr-2004
// Last update			: 5-Apr-2004
// Updates			: None
// Tables involved		: None
// Additional information	: None
//
//****************************************************************************

//ALERT MESSAGES
var EMAIL_ADDRESS="Please enter Recipient's Emails"
var EMAIL="Email Address is Mandatory !"
var VALID_EMAIL = "Please enter valid email address"
var AMOUNT = "Please enter the Amount"
var VALID_AMOUNT = "Amount should be Numeric"
var CHEQUE_NO="Please enter the cheque Number"
var VALID_CHEQUE_NO = "Cheque Number should be Numeric"
var MO_NO="Please enter the Money Order Number"
var VALID_MO_NO = "Money Order Number should be Numeric"
var BANK_NAME="Please enter the Bank Name"
var BANK_ADDRESS="Please enter the Bank Address"
var AMOUNT="Please enter the Amount"
var DAY="Please enter the Date"
var MONTH="Please enter the Month"
var YEAR="Please enter the Year"
var EMAIL_ID="Please enter Email ID"
var EMAIL_RECEIVED_ID="Please enter Email ID"
var COMMISSION="Please enter Email ID"
var VALID_URL="Please enter the valid URL"
var ITEM_NAME="Please enter the Item Name"
var PRICE="Please the Item Price"
var VALID_PRICE="Price should be Numeric"
var USER_NAME="Please enter the User Name"
var VALID_USER_NAME="Please enter the valid User Name"
var PASSWORD="Please enter the password"
var SPACES="Spaces are not allowed in password"
var CON_PASSWORD="Please enter the Confirm password"
var VALID_CON_PASSWORD="Check the confirmed Password"
var FIST_NAME="Please enter the First Name"
var LAST_NAME="Please enter the Last Name"
var CARD_NUMBER="Please enter the Credit Card Number"
var CV_NUMBER="Please enter the Card verification Number"
var CR_NUMERIC="Card and Verification Number should be Numeric"

var SUB_NAME="Please enter the Subscription Name"
var VALID_TRIAL="Please enter valid bill"
var BILL_TRAIL="Bill should be Numeric"

var TRAIL_P="Please select the Trail period from drop down menu"

var ADDNOALLOW="`~!#@$%^*()_+=|\\}}{{\"':;<>?/";
var NOALLOW="`~!$%#^*()_+=|}{\"';<>?/&"; 
var PASSNOALLOW="`~@$%#^()-+=|*!\\}}{{\"':;<,>.?/";//Allowing _ & * !
var EMAIL_NOT_EQUAL="Email and Confirm Email should be same !";



//To prevent specified characters

function fnNoAllowThisChar(txtfld, spChar, mes)
{
	var txtString= fnTrim(txtfld.value);
	for(var i=0; i<txtString.length; i++)
	{
	if(spChar.indexOf(txtString.charAt(i))!= -1)
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return false;
	}
	}
	return true;		
}

//To trim the white spaces

function fnTrim(string)
{
	var cnt;	
	len = string.length;
	str = string;
	begin = -1;
	for(cnt=0;cnt<len;cnt++)
	{   
		if (str.charAt(cnt) == " " )
		{	
			begin = cnt;
		}	
		else
		break;
	}
	str = str.slice(begin+1,len);
	len = str.length;
	end = len;
	for(cnt=len-1;cnt>=0;cnt--)
	{
		if (str.charAt(cnt) == " ")
		{	
			end = cnt;
		}	
		else
		break;
	}
	str = str.slice(0,end);
	return str;
}

//To Check for null

function fnIsNull(txtfld, mes)
{
	   var string=txtfld.value;
       if (fnTrim(string) == null || fnTrim(string) == "" )
       {
	   	alert(mes);
		txtfld.focus();
		txtfld.select();
     	return true;
       }
		return false;
}

//To check whether it is a integer value

function checkInt(txtfld,msg)
{
	var val=txtfld.value;
	if(isNaN(val))
	{
		alert(msg);
		txtfld.focus();
		txtfld.select();
		return false;
	}
	return true;
}

//To check whether email is valid 

function fnIsEmail(txtfld, mes)
{
	var strString=fnTrim(txtfld.value);
	if(strString.length==0)
	{
	return false
	}
	else if(strString.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)== -1)
    {
	alert(mes);
	txtfld.select();
	txtfld.focus()
	return false;		
	}
	return true;
	
}

//-------------
//check multiple email address
function fnMultipleEmail(txtfld)
{
	var strString=fnTrim(txtfld);
	if(strString.length==0)
	{
	return false
	}
	else if(strString.search(/^\w+((-\w+)|(\.\w+))*\@[a-z0-9]+((\.|-_)[a-z0-9]+)*\.[a-z0-9]+$/)== -1)
    {
	alert("' "+txtfld + "' is not a valid email address");
	return true;		
	}
	return false;
}

//------------
//To check whether the 2 fields are same

function checkEquality(txtfld1,txtfld2,msg)
{
	if(txtfld1.length>0)
	  var txtString1= fnTrim(txtfld1.value);
	if(txtfld2.length>0)
      var txtString2= fnTrim(txtfld2.value);
	if(txtfld1.value != txtfld2.value)
		{
			alert(msg);
			txtfld2.focus();
			txtfld2.select();
			return false;
		}
			return true;
}

//This function performs multiple actions.
//Checks for null, special characters, minimum size and uses the passed name to
//generate dynamic error messages.

function checkSpecial(txtfld,nonull,nospecial,min,name)
{
	if((nonull)&&(fnIsNull(txtfld,name +" is Mandatory !")))
		return false;
	else if((nospecial.length>0)&&(!fnNoAllowThisChar(txtfld,nospecial,
		name +" should not contain special characters !")))
		return false;
	else if((min > 0)&&(!checkLength(txtfld,(name + " should be " + min + " characters or greater !"),min)))
		return false;
	return true;
}

//Function which will check whether the Email is null & valid
function checkEmail(txtfld,nonull,name)
{
	if((nonull)&&(fnIsNull(txtfld,name +" is Mandatory !")))
		return false;
	else if(!fnIsEmail(txtfld,name + " is Invalid !"))
		return false;
	return true;
}

//Function which will check whether the data is null & integer
function checkSpecialInt(txtfld,nonull,onlyint,name)
{
	if((nonull)&&(fnIsNull(txtfld,name +" is Mandatory !")))
		return false;
	else if((onlyint)&&(!checkInt(txtfld,name + " should contain only numbers. [0-9]")))
		return false;
	return true;
}

//To check the length 
function checkLength(txtfld,msg,min)
{
	var txtString=fnTrim(txtfld.value);
	if(txtString.length < min)
	{	
		alert(msg);
		txtfld.focus();
		txtfld.select();
		return false;
	}
	return true;
}
//To check a list/combo box selection
function fnCheckSelectedIndex(cmbfld, mes)
{
	if(cmbfld.selectedIndex==-1)
	{
		alert(mes)
		cmbfld.focus();
		return true
	}
	else
		return false
}

//To Check whether 2 list/combo have the same value.
//Eg: Security Questions
function checkSelection(txtfld1,txtfld2,msg)
{
	if(txtfld1.selectedIndex==txtfld2.selectedIndex)
	{
		alert(msg);
		txtfld2.focus();
		return false;	
	}
return true;
}

function fnvalidateURL(txtfld, mes)
{
	var strString=fnTrim(txtfld.value);
	arr=strString.split("/");
	var len=arr[arr.length-1].length;
	var str=strString.substr(0,(strString.length-len));
	if(str.search(/^(\http\:\/\/)[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9/]+$/)== -1)
  
	{
	alert(mes);
	txtfld.select();
	txtfld.focus()
	return false;		
	}
	return true;
	
}

function fnvalidateURLImage(txtfld,mes)
{
	var strString=fnTrim(txtfld.value);
	arr=strString.split("/");
	var len=arr[arr.length-1].length;
	var str=strString.substr(0,(strString.length-len)); 
	if(str.search(/^(\http\:\/\/)[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9/]+$/)== -1)
	{
		alert(mes);
		txtfld.select();
		txtfld.focus();
		return false;
	}
	strString = strString.substr(strString.length-3,strString.length); 
	if(!(strString == 'gif' || strString == 'jpg' || strString == 'png'))
	{
		alert (mes);
		txtfld.select();
		txtfld.focus();
		return false;
	}
 return true;

}
 //Allow Numbers only to enter

function fnNumberOnly(txtfld, mes)
{
	var Num="0123456789";
	var txtString= fnTrim(txtfld.value)
	for(var i=0; i < txtString.length; i++)
	{
	if(Num.indexOf(txtString.charAt(i))== -1)
	{	
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	return false;
	}
			
}


 //----------------------------
//Month validation
function fnValidMonth(txtfld)
{
	var Num="0123456789";
	var txtString= fnTrim(txtfld.value)
	for(var i=0; i < txtString.length; i++)
	{
	if(Num.indexOf(txtString.charAt(i))== -1)
	{	
	alert("Month should be Numaric")
	txtfld.focus();
	txtfld.select();
	return true;
	}
	}
	if(txtfld.value>12 || txtfld.value<=0)
	{
	alert("Please enter valid month")
	txtfld.focus();
	txtfld.select();	
	return true;
	}
	return false;
			
}

//--------------------------------
// date validation
function fnValidDate(txtfld)
{
	var Num="0123456789";
	var txtString= fnTrim(txtfld.value)
	for(var i=0; i < txtString.length; i++)
	{
	if(Num.indexOf(txtString.charAt(i))== -1)
	{	
	alert("Day should be Numaric")
	txtfld.focus();
	txtfld.select();
	return true;
	}
	}
	if(txtfld.value>31 || txtfld.value<=0)
	{
	alert("Please enter valid Day")
	txtfld.focus();
	txtfld.select();	
	return true;
	}
	return false;
			
}

//---------------------------------------------------------------------------------------------------
//Validates website.
// here value is removed in txtfld becoz its consider as variable
function fnIsWebsite(txtfld,Fname,mes)
{
	var strString=fnTrim(txtfld);
	if(strString.length==0)
	{
		return false
	}
	else if(strString.search(/^(\bwww\b)*\.[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9/]+$/)== -1)
    {
	alert(mes);
	Fname.select();
	Fname.focus()
	return true;		
	}
	return false;
	
}

//-----------------------------------
//check the spaces

function fnCheckSpace(txtfld, mes)
{
	var string = txtfld.value;
	for(i=0; i<=string.length; i++)
	{
	if(string.charAt(i)==" ")
	{
	alert(mes)
	txtfld.focus();
	txtfld.select();
	return true;
	}
	}
	return false;
		
}
//---------------------------------
//compare passwords
function fncompare(PW,CP,mes) {
frm=document.form1;
var np = PW.value;
var cp = CP.value;
if (np != cp)
	{
	alert(mes);
	CP.select();
	return true;
	}
else return false;
}

//chech whether its numeric or not
function AllowNumeric(textfield, msg)
{
var val=textfield.value;
	if(isNaN(val))
	{
		alert(msg);
		textfield.focus();
		textfield.select();
		return true;
	}
	return false;
}


//-------------------------------------------------
//dropdown menu check
function dropDownMenu(menu, mes) 
   {
   var myindex=menu.selectedIndex;
   if (myindex==0)
      {
      alert(mes);
      menu.focus();
	  return true;
	  }
	else 
		{
		return false;
		}
   }



  