/**
	* @ Title:					book_common.js
	* @ Version:			1.0
	* @ Copyright:		Copyright (c) 2002-2004
	* @ Author:			Deepali Parikh	
	* @ Company:		Avenues (I) Pvt. Limited.
	* @ Description:	JavaScript file for common functions
	* @ Created:			1-Nov-2004
	* @ Modified:		
*/

// Added by Shailesh 9/26/2008 
//To check Credit card number 
function chkCardByLuhnAlgo(cardNumber,CardType)
{      
		
			var CCNum =cardNumber; //Card Number
			var CCNum =CCNum.replace(/ /g, "");
			var dashindex=CardType.indexOf("_");
			var CCType=CardType.substring(0,dashindex);		
			var msg = 'Card Number does not match with Card Type.\nPlease enter proper Card Number that matches with the card type you have selected.';
			var finalmsg='Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.';

			//Checking Card Type 
			if(CCType=='')
			{
				alert('Card Type is not selected.\nPlease select one of the credit cards from the Card Type dropdown.');
				return false;
			}

			//Checking for valid card types supported by this script
			//Discover Novus(DSNV) has not been validated but ignored by script for now
			if(CCType !='VISA' && CCType !='MSTR'  && CCType !='AMEX' && CCType!='JPCB' && CCType!='ECRD' && CCType !='DCLB' && CCType !='DSNV' && CCType !='CTBL' &&CCType !='AXISDB' &&  CCType !='INGDB'  && CCType !='ICICDB'  && CCType !='IOBDB'  && CCType !='KVBDB'  && CCType !='DSBDB'  && CCType !='HDFCDB'  && CCType !='COBDB'  && CCType !='DCDB') //// added condition by gauri for debit card validation
			{
				alert('Card Type selected is not supported.\nPlease contact the System Administrator to report this error.')
				return false;
			}

			//Check for Card Number entered.
			if(CCNum=='' || CCNum==0)
			{
				  alert('Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
				  return false;
			} 
			if(CCNum !='')
			{  
				for( var i = 0; i <CCNum.length; ++i ) 
					{
						var c = CCNum.charAt(i);
						if( c < '0' || c > '9' ) 
						{
							alert('Card Number you have provided is not correct.\nPlease ensure that the Card Number does not contain any alphabets, spaces or special characters.');
							return false;
						} 
					}  
			 }
			//Check Visa 
			if(CCType.indexOf('VISA') !=-1)
			{
				if ((CCNum.length !=13 && CCNum.length != 16) && (CCNum.substring(0,1) == 4))
					{
						alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
						return false;
					}
				if ((CCNum.length == 13 || CCNum.length == 16) && (CCNum.substring(0,1) == 4))
				{
					if(!cardval(cardNumber))
					{
				 
							alert(finalmsg);  
							return false; 
					}
				 }
	    
				else
				{ 
					alert(msg);  
					return false;
				}
			}
// added by gauri for debit card validation
			
			if((CCType.indexOf('AXISDB') !=-1) || (CCType.indexOf('INGDB') !=-1) || (CCType.indexOf('ICICDB') !=-1) ||(CCType.indexOf('IOBDB') !=-1) || (CCType.indexOf('KVBDB') !=-1) || (CCType.indexOf('DSBDB') !=-1) ||(CCType.indexOf('HDFCDB') !=-1) || (CCType.indexOf('COBDB') !=-1) || (CCType.indexOf('DCDB') !=-1))
			{
				if ((CCNum.length != 16) && (CCNum.substring(0,1) == 4))
					{
						alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
						return false;
					}
				if ((CCNum.length == 16) && (CCNum.substring(0,1) == 4))
				{
					if(!cardval(cardNumber))
					{
							alert(finalmsg);  
							return false; 
					}
				 }   
				else
				{ 
					alert(msg);  
					return false;
				}
			}
// end of debit card validation 1/27/2009
			//Check Mastercard  and Citibank ECard
			if((CCType.indexOf('MSTR') !=-1) || (CCType.indexOf('ECRD') !=-1))
			{
				var firstdig=CCNum.substring(0,1);
				var seconddig=CCNum.substring(1,2);
				if ((CCNum.length !=16 && CCNum.length != 19) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) 
				{ 
					alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
					return false;
				}
        
				if ((CCNum.length == 16 || CCNum.length == 19) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
				{
					if(!cardval(cardNumber))
					{
						alert(finalmsg);  
						return false; 
					}
					
					}
			
					else
					{
						alert(msg);  
						return false;
					}
	
			}

			//Check American Express 
			if(CCType.indexOf('AMEX') !=-1)
			{
				firstdig = CCNum.substring(0,1);
				seconddig = CCNum.substring(1,2);
				if ((CCNum.length !=15) && (firstdig == 3) && ((seconddig == 4)||(seconddig == 7)))
				{ 
					alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
					return false;
				}
				if (((CCNum.length == 15)  || (CCNum.length == 18)) && (firstdig == 3) && ((seconddig == 4) ||(seconddig == 7)))
				{
					if(!cardval(cardNumber))
					{  
						alert(finalmsg);  
						return false; 
					}
				}
				else
				{
					alert(msg);  
					return false;
				}
			}		

			//Check JCB  card 
			if(CCType.indexOf('JPCB') !=-1)
			{
				if ((CCNum.length != 16) && (CCNum.match(/^(3088|3096|3112|3158|3337|3528|3566|3530|3540|3541)/)))
				{
					alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
					return false;
				}
				if ((CCNum.length==16) && (CCNum.match(/^(3088|3096|3112|3158|3337|3528|3566|3530|3540|3541)/))) 
				{
					if(!cardval(cardNumber))
					{  
						alert(finalmsg);  
						return false; 
					}
				}
				else
				{
					alert(msg);  
					return false;
				}
			}
 
			
			//Check Diners
 
			if(CCType.indexOf('DCLB') !=-1)
		    {   
				firstdig = CCNum.substring(0,1);
				seconddig = CCNum.substring(1,2);
				if ((CCNum.length !=14 && CCNum.length != 17) && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
				{
					alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
					return false;
				}

				if((CCNum.length == 14 || CCNum.length == 17)  && (firstdig == 3) && ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
				{
					if(!cardval(cardNumber))
					{
						alert(finalmsg);  
						return false; 
					}
				}
				else
				{
					alert(msg);  
					return false;
				}
			}

			//Check CarteBlanche
			if(CCType.indexOf('CTBL') !=-1)
			{
				firstdig = CCNum.substring(0,1);
				seconddig = CCNum.substring(1,2);
				if ((CCNum.length !=14) && (firstdig == 3) && ((seconddig == 0)||(seconddig == 6)||(seconddig == 8)))
				{ 
					alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
					return false;
				}
				if (((CCNum.length == 14)) && (firstdig == 3) && ((seconddig == 0)||(seconddig == 6)||(seconddig == 8)))
				{
					if(!cardval(cardNumber))
					{  
						alert(finalmsg);  
						return false; 
					}
				}
				else
				{
					alert(msg);  
					return false;
				}
			}

			//Check Discover
			if(CCType.indexOf('DSNV') !=-1)
			{
				firstdig = CCNum.substring(0,1);
				seconddig = CCNum.substring(1,2);
				if ((CCNum.length !=16) && (firstdig == 6) && ((seconddig == 0)||(seconddig == 5)))
				{ 
					alert('Length of Card Number you have provided is not correct.\nPlease ensure that the Card Number you enter is same as it appears on your credit card.');
					return false;
				}
				if (((CCNum.length == 16)) && (firstdig == 6) && ((seconddig == 0)||(seconddig == 5)))
				{
					if(!cardval(cardNumber))
					{  
						alert(finalmsg);  
						return false; 
					}
				}
				else
				{
					alert(msg);  
					return false;
				}
			}	
	return true;
}//method is closed.




function cardval(cardNum){
	var s=cardNum;
	var v = "0123456789";
	var w = "";
	if(s !="")
	{ 
	   for (i=0; i < s.length; i++) 
		{
			x = s.charAt(i);
			if (v.indexOf(x,0) != -1)
			w += x;
		}
		j = w.length / 2;
		k = Math.floor(j);
		m = Math.ceil(j) - k;
		c = 0;
		for (i=0; i<k; i++) 
		{
			a = w.charAt(i*2+m) * 2;
			c += a > 9 ? Math.floor(a/10 + a%10) : a;
		}

		for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
		return (c%10 == 0);  //returns true here.
	}
}// method is closed

function chkBlank(formelement,text){
		var eleVal = formelement.value;
		var len = eleVal.length;
		while(eleVal.substring(0,1) == ' '){
			eleVal = eleVal.substring(1, len);
		}
		
		if (eleVal == ''){
			alert(alrt_ent + text);
//			alert('Enter '+text);		
			formelement.focus();
			return false;
	   }else{
			return true;
	   }
}

/* This function checks for non numeric numbers. */

function chkNaN(formelement,text){
	var pattern = "^[0-9 .]+$";	
	var regex = new RegExp(pattern)
	if (regex.test(formelement.value)) return true;
	else{
		alert(alrt_num + text);
	//	alert('Enter Numeric '+text);
		formelement.value="";
		formelement.focus();
		return false; 
	}
}

function chkAlpha(formelement, text){			
		var pattern = "^[a-z A-Z ]+$";			
		var checkAlpha = new RegExp(pattern);
	
		if(checkAlpha.test(formelement.value)){
			return true;
		}else{	
			alert(alrt_char + text);
			//alert("Invalid Characters in "+text);
			formelement.focus();
			return false;
		}
}

/*Function to check if a float eg. 20.22 is in proper format.
parameters :
	formElementObject - Object of the form element
	displayText - Name of field (String)
	isRequired - true if this field should not be empty, false otherwise.(boolean)

	Note : More suitable for onBlur event
*/
function isDigit(formElementObject, displayText, isRequired) {	
	var eleValue = trim(formElementObject.value);

	if(formElementObject == null || displayText == '') {
		alert(alrt_dig);
	//	alert('Incorrect use of function isDigit');
		return false;
	}
	if(eleValue == '' && isRequired == true) {
		alert(alrt_prov + displayText);
		//alert('Please provide value for '+displayText);
		if(formElementObject.type != "hidden"){
			formElementObject.focus();
		}		
		return false;
	}

	if(eleValue != '') {
		if(eleValue.indexOf('.') != -1) {
			var eleArr = eleValue.split('.');			
			if(eleArr.length > 2) {				
				alert(alrt_valid + displayText);
				//alert('Enter a valid ' + displayText);
				if(formElementObject.type != "hidden"){
					formElementObject.focus();
				}
				return false;
			} else {
				var pattern = "^[0-9]+$";	
				var regex = new RegExp(pattern);
				if (!regex.test(eleArr[0])){
					alert(alrt_num +displayText);
					//alert('Enter Numeric '+displayText);
					formelement.focus();
					return false; 
				}

				if (!regex.test(eleArr[1])){
					alert(alrt_num + displayText);
					//alert('Enter Numeric '+displayText);
					formelement.focus();
					return false; 
				}				
			}
		} else {
			if(!chkNaN(formElementObject, displayText)){
				return false;			
			}else{
				return true;
			}
		}
	}
	return true;
}

/* This function checks for a valid email address. */

function chkEmail(formelement,text){	
		if(formelement.value!=''){
		var pattern = ".+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)"
//		var pattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
		var regex = new RegExp(pattern)
		if (regex.test(formelement.value)){
			return true;
		}else{
			alert(alrt_valid + text);
			//alert("Enter a valid "+text);
			formelement.focus();
			return false;
		}
	}else{
		alert(alrt_valid + text);
		//alert("Enter a valid "+text);
		formelement.focus();
		return false;
	}
}

/* This function checks for the minimum characters. */

function chkLessLen(formelement,text,len){
	if(formelement.value.length<parseInt(len)){
		alert(alrt_the + text + alrt_shld + len + alrt_chrs);
		//alert('The '+text+' should not be less than '+len+' characters');
		formelement.focus();
		return false;
	}else{
		return true;
	}
}

/* This function checks for the maximum characters. */

function chkGreaterLen(formelement,text,len){
	if(formelement.value.length>parseInt(len)){
		alert(alrt_the + text + alrt_gt + len + alrt_chrs);
		//alert('The '+text+' should not be Greater than '+len+' characters');
		formelement.focus();
		return false;
	}else{
		return true;
	}
}

/* This function checks for special characters. */

function chkSpecialChar(formelement,text){
	var msg='true';
	var a=formelement.value;
	var b=a.length;
	var cha='`~!@#$%^&()+-[]{}/|;:,<>.?';
	var ch=cha.length;
	var i,j;

	for(i=0;i<ch;i++){
		var ch1=cha.substring(i,i+1);
		for(j=0;j<b;j++){
			var a1=a.substring(j,j+1);
			if(a1==ch1){
				//msg='Special Characters like ' +cha+ 'are not allowed in '+text;
				//alert(msg);
				alert(alrt_spc +cha+ alrt_notall + text);
				formelement.focus();
				return false;
			}
		}
	}
	if (msg=='true'){
		return true;
	}
}

/* This function checks for spaces in the field. */

function chkSpace(formelement,text){
	var msg = 'true';
	var a = formelement.value;
	var b = a.length;
	var i,j;

	for(j=0;j<b;j++){
		var a1=a.substring(j,j+1);
		if(a1==' '){
				//msg='Spaces are not allowed in '+text;
				alert(alrt_spaces + text);
				formelement.focus();
				return false;
		}
	}

	if (msg=='true'){
			return true;
	}
}

/* This function compels the user to enter number greater than 0. */

function chkLEZero(formelement,text){
	if (formelement.value <= 0){
		//alert('Enter the '+text+' greater than 0.');
		alert(alrt_ent + text + alrt_grzero);
		formelement.focus();
		return false;
	}else{
		return true;
	}
}

/* This function checks for identical username & passwords. */

function chkUserPass(formelement1,formelement2){
	var user=formelement1.value;
	var pass=formelement2.value;

	if (user == pass){
		//alert('Username & password cannot be the same.');
		alert(alrt_usern);
		formelement2.value = "";
		if(formelement2.type != "hidden"){
			formelement2.focus();
		}		
		return false;
	}else{
		return true;
	}
}

/* This function checks for identical passwords. */

function chkPassword(formelement1,formelement2){
	var password1=formelement1.value;
	var password2=formelement2.value;

	if (password1 != password2){
		//alert('Password do not match.');
		alert(alrt_pass);
		if(formelement1.type != "hidden"){
			formelement1.focus();
		}		
		return false;
	}else{
		return true;
	}
}

/* This function compels the user to select a value in the list box. */

function chkListBlank(formelement,text){	
	if (formelement.options[formelement.selectedIndex].value==''){
		alert(alrt_sel + text);
//		alert('Select '+text);
		formelement.focus();
		return false;
	}else{
		return true;
	}
	return false;
}

/* This function compels the user to select atleast one radio button. */

function chkRadio(formelement,text){
	var flag='false';
	if(formelement.checked==true){
		flag='true';
	}else{
		for (var i =0;i<formelement.length;i++ ){
			if(formelement[i].checked==true){
				flag='true';
			}
		}
	}
	if (flag=='false'){
		alert( alrt_sel +text);
		//alert('Select '+text);
		return false;
	}else{
		return true;
	}
}

/* This function is used to open a new pop-up window. */

function OpenWin (link, windowName){
    loc =   link;
    win =   window.open (loc,windowName,'top=150,left=75,height=300,width=600,scrollbars=yes,status=no,toolbar=no,resizable=yes') ;
    win.focus();
}

/*function to get the date after the given period
  currentYear - String giving current year in yyyy format
  currentMonth - String giving current month in mm format
  currentDay - String giving current day in dd format
  addDays - Integer showing no of days after the current date
  return next date as mm/dd/yyyy string
*/
function getNextDate(currentYear, currentMonth, currentDay, addDays) {
	var nextDate = '';
	
	var currDateObj = new Date(parseInt(currentYear),
				parseInt(currentMonth),
				parseInt(currentDay));
		
	currDateObj.setDate(parseInt(currentDay)+addDays);
	nextDate = currDateObj.getMonth()+'/'+currDateObj.getDate()+'/'+currDateObj.getYear();
	return nextDate;
}

//Validate URL data entered
function validateURL(formelement,text){

	if(!chkBlank(formelement, text)) return false;
	if(!chkSpace(formelement, text)) return false;

	var URL = formelement.value;
	if(URL.length == 7){
		alert(alrt_valid + text);
		formelement.focus();
		return false;
	}

	if(URL.indexOf("http://") > 0){
		alert(alrt_valid + text);
		formelement.focus();
		return false;
	}
	
	if(URL.lastIndexOf(".") < 8){
		alert(alrt_valid + text);
		formelement.focus();
		return false;
	}

	if(URL.length <= URL.lastIndexOf(".")+2){
		alert(alrt_valid + text);
		formelement.focus();
		return false;
	}

	if(URL.length > URL.lastIndexOf(".")+4){
		alert(alrt_valid +text);
		formelement.focus();
		return false;
	}
	return true;
}

function chkDate(formelement, text, delim){
	var monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var ddVal = formelement.value;	
	if(ddVal.indexOf(delim) < 1){		
		badDateAlert(formelement);
		return false;
	}
	var dateArray = ddVal.split(delim);
	if(dateArray.length != 3){
		badDateAlert(formelement);
		return false;
	}
	if(dateArray[2].length != 4){
		badDateAlert(formelement);
		return false;
	}
	if(parseInt(dateArray[1]) > 12){
		badDateAlert(formelement);
		return false;
	}
	
	if((parseInt(dateArray[1]) == 2) && (parseInt(dateArray[2])%4 == 0)){
		if((parseInt(dateArray[0])) > (monthDays[parseInt(dateArray[1]-1)])+1){
			badDateAlert(formelement);
			return false;
		}
	}else if((parseInt(dateArray[0])) > (monthDays[parseInt(dateArray[1]-1)])){
		badDateAlert(formelement);
		return false;
	}
	return true;
}

function badDateAlert(ele){
	alert(alrt_invdate);
	//alert("Invalid Date");
	ele.value = "";
	if(ele.type != "hidden"){
		ele.focus();
	}		
}

function sqlDate(srcEle, destEle, delim){
	if(!chkDate(srcEle, 'Date', delim)) return false;	
	var temp = (srcEle.value).split(delim);	
	destEle.value = temp[2] +"/"+ temp[1] +"/"+ temp[0];
	return true;
}

function goToError(err){
	document.err_frm.actionPage.value="/errorPages/error.jsp?err="+err;
	document.err_frm.submit();
}

function help(anc){
	if(anc == 5){
		alert(anc);
		OpenWin("/helpfiles/bedTypeHelpfile.jsp", "ResAvenue_Help");
	}else{
		OpenWin("/helpfiles/helpfile.jsp"+anc, "ResAvenue_Help");
	}
}

//---- Added by Ankit. The above function is used only in includes/adm_brief_help.jsp. 17-Sep-04----
function goToPage(mPage){
	document.incfrm.actionPage.value=mPage;
	document.incfrm.submit();
}

/*
	Function to call pop-up for displaying Secure Certificate.
	Called from req_booking_nights, getAvailability, setRooms
*/
function popUpVsign(url){
	var url = 'https://digitalid.verisign.com/as2/46c9d61e98b639b5252af54f0575b41b';
	window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
}

/* This function checks for special characters other than characters allowed. */

function chkAllowSpecialChar(formelement, text, allow_numeric, char_allow)
{
	var msg='true';
	var allow_char = "";

	for(c=0;c<char_allow.length;c++){
		allow_char = allow_char + char_allow.charCodeAt(c) + ",";
	}

	var spl_char = allow_char.split(",");	

	var user_val = formelement.value;
	if(user_val != ""){
		user_val_con :
		for(t=0;t<user_val.length;t++){
			if(user_val.charCodeAt(t) < 65 ||  user_val.charCodeAt(t) > 122){
				if(allow_numeric == "Y" && ( user_val.charCodeAt(t) > 47 &&  user_val.charCodeAt(t) < 58)){
					continue;
				}else if(allow_numeric == "N" && ( user_val.charCodeAt(t) > 47 &&  user_val.charCodeAt(t) < 58)){
					msg = "Numeric Characters is not allowed in "+ text;
					break;
				}

				for(p=0;p<spl_char.length-1;p++){
					if(user_val.charCodeAt(t) == spl_char[p]){
						continue user_val_con;
					}
				}
				
				if(msg == 'true'){
					msg = "Character "+user_val.charAt(t)+" is not allowed in "+ text;
					alert(msg);
					formelement.focus();
					return false;
				}				
			}else if(user_val.charCodeAt(t) > 90 &&  user_val.charCodeAt(t) < 97){
				for(p=0;p<spl_char.length-1;p++){
					if(user_val.charCodeAt(t) == spl_char[p]){
						continue user_val_con;
					}
				}
				msg = "Character "+user_val.charAt(t)+" is not allowed in "+ text;
				alert(msg);
				formelement.focus();
				return false;
			}
		}
	}	
	
	if (msg == 'true'){
		return true;
	}else{
		alert(msg);
		formelement.focus();
		return false;
	}
}


function chkTxtArea(formelement){
	var str=formelement.value;
	var flag=true;
	var sample="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ._#,";
	for(var i=0;i<str.length;i++)
	{
		//alert(sample.indexOf(str.charAt(i)));
		if(sample.indexOf(str.charAt(i))==-1)
		{
			flag=false;
			break;
		}
		else
		{
			flag=true;
		}
	}
	if (flag)
	{
		return true;
	}
	else
	{
		alert("Enter a characters present in  - ._#-,A-Z a-z 0-9  only");
		formelement.focus();
		return false;
	}
}
