// js file used to validate the booking form search criteria
// file created by machine user AshishNet
// Date	: Feb 09, 2005
// do not change the code, it will effect the validation rules.


		function trim(str)
		{

			var trimmedString = new String(str);

			if ((0 < str.length) && ((str.charCodeAt(0) <= 32) || (str.charCodeAt(str.length-1) <= 32)))
			{
				
				var startSlice = 0;
				var endSlice   = -1;


				for (var i = 0; i < str.length-1; i++)
				{
					if (str.charCodeAt(i) > 32)
					{
						startSlice = i;
						break;
					}
				}
				
				for (var i = str.length-1; startSlice <= i; i--)
				{
					if (str.charCodeAt(i) > 32)
					{
						endSlice = i;
						break;
					}
				}
				
				trimmedString = str.slice(startSlice, endSlice+1);
			}
			return trimmedString;
		}


		// valid form before goes to server	
		function validate(frm)
		{
			// call checkDate function to valid selected date	
			/* var s = checkDate(frm);
			if (!s)
			{
				return s;
			}
			*/				
			
			
		    // Code for Rooms > Adults Checking: By Krunal
			if(document.forms["frm"].lstRooms != null && document.forms["frm"].lstAdults != null)
			{
			    if (parseInt(document.forms["frm"].lstRooms.options[document.forms["frm"].lstRooms.selectedIndex].value) > parseInt(document.forms["frm"].lstAdults.options[document.forms["frm"].lstAdults.selectedIndex].value) )
			    {
				    alert("There should be one Adult Per Room.");
				    document.forms["frm"].lstAdults.focus();
				    return false;
			    }
			}    
			// Code ends for Rooms > Adults Checking Ends: By Krunal
									
								
			// call function to toggle layers
			changeDIV(this);
		}

		// function used to check valid date for selected month
		function checkDate(frm)
		{
			var dDate;
			var iYear;
			var iMonth;
			var iDay;
			var aMonthDays = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

			dDate = new Date();
			iDay = dDate.getDate();
			iMonth = dDate.getMonth();
			iYear = dDate.getFullYear();

			/*
			if ( frm.lstArrivalYear.options[frm.lstArrivalYear.selectedIndex].value == iYear)
			{
				if ( frm.lstArrivalMonth.options[frm.lstArrivalMonth.selectedIndex].value < iMonth + 1)
				{
					alert( "Please select the Arrival Date greater than or equal to Today.");
					return false;
				}
				else if ( frm.lstArrivalMonth.options[frm.lstArrivalMonth.selectedIndex].value == iMonth + 1)
				{
					if ( frm.lstArrivalDay.options[frm.lstArrivalDay.selectedIndex].value < iDay)
					{
						alert( "Please select the Arrival Date greater than or equal to Today.");
						return false;
					}
				}
			}

			// check that the upper date limit is OK
			if ( frm.lstArrivalMonth.options[frm.lstArrivalMonth.selectedIndex].value != 2)
			{
				if (frm.lstArrivalDay.options[frm.lstArrivalDay.selectedIndex].value > aMonthDays[ frm.lstArrivalMonth.options[frm.lstArrivalMonth.selectedIndex].value])
				{
					alert( "Please select a Valid Date of the selected Month.");
					return false;
				}
			}

			// check the date in february is correct
			if ( frm.lstArrivalMonth.options[frm.lstArrivalMonth.selectedIndex].value == 2)
			{
				if (frm.lstArrivalDay.options[frm.lstArrivalDay.selectedIndex].value > daysInFebruary( frm.lstArrivalYear.options[frm.lstArrivalYear.selectedIndex].value))
				{
					alert( "Please select a Valid Date in month of February.");
					return false;
				}
			}
			*/
			return true;
		}

		// function used to return total days in February month for current year
		function daysInFebruary (year)
		{
			return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
		}

		// function used to toggle layers
		var currDivObj = null;
		function changeDIV(selObj)	
		{
			var divObj;
			if (document.getElementById)
				divObj = document.getElementById("div1");
			else if(document.all)
				divObj = document.all("div1");
			else if (document.layers)
				divObj = document.layers["div1"];

			if(document.getElementById || document.all)
			{
				divObj.style.display = "block";
			}
			currDivObj = divObj;

			var objDiv;

			if (document.getElementById)
				objDiv = document.getElementById("div0");
			else if(document.all)
				objDiv = document.all("div0");
			else if (document.layers)
				objDiv = document.layers["div0"];

			if(document.getElementById || document.all)
			{
				objDiv.style.display = "none";
			}

			// call progressbar function to generate progressbar 			
			progress();
		}			

		// function used to generate progressbar on the page
		function progress()
		{
			intWidth = parseInt(document.getElementById("container").style.width) + 1;
			if(intWidth <= 440){
				document.getElementById("container").style.width = intWidth;
			}else{
				document.getElementById("container").style.width = 0;
			}
			setTimeout("progress()",150);
		}


		// function used to validate different type of feild
		function validate_field(field, type, autoplace)
		{
			if (field.value == null || field.value.length == 0)
			{
				window.isvalid = true;
				return true;
			}
			var validflag = true;
			if (type == "currency")
			{
				var val = field.value.replace(/\$/g,"");
				val = val.replace(/,/g,"");
				if (val.charAt(0) == '=')
				{
					val = eval(val.substr(1));
					autoplace = false;
				}

				else if (val.substr(1).search(/[\+\-\*\/]/g) != -1)
				{
					if (new String("abcdefghijklmnopqrstuvwxyz").indexOf(val.toLowerCase().substr(0,0)) != -1)
					{
						value = "error";
					}
					else
					{
						val = eval(val);
						autoplace = false;
					}
				}
				numval = parseFloat(val);
				if (isNaN(numval) || Math.abs(numval)>1.0e+10)
				{
					alert("Invalid currency value. Values must be numbers up to 9,999,999,999.99");
					validflag = false;
				}
				else
				{
					if (autoplace && val.indexOf(".") == -1) numval/=100;
					field.value=format_currency(numval.toString());
					validflag = true;
				}
			}
			else if (type == "date")
			{
				var month;
				var day;
				var year;

				if (field.value.indexOf("/") == -1 && field.value.indexOf(".") == -1 && field.value.indexOf("-") == -1)
				{
					var l = field.value.length;
					day = parseInt(field.value.substr(0,2-l%2),10);
					month = parseInt(field.value.substr(2-l%2,2),10);
					year = parseInt(field.value.substr(4-l%2),10);
				}
				else
				{
					var comps;
					if (field.value.indexOf("/") != -1 ){
						comps = field.value.split("/");
					}else if(field.value.indexOf(".") != -1 ){
						comps = field.value.split(".");
					}else if( field.value.indexOf("-") != -1 ){
						comps = field.value.split("-");
					}

					day = parseInt(comps[0],10);
					month = parseInt(comps[1],10);
					year = parseInt(comps[2],10);

				}
				if (month >= 1 && month <= 12 && day >= 1 && day <=31 && ((year >= 0 && year < 100) || (year > 1900 && year <2100)))
				{
					if (year < 50)
					year += 2000;
					else if (year < 100)
					year += 1900;
					if(year <= 1900 ){
						alert(" The Year Must Be Greater then 1900");
						validflag = false;
					}else{
						field.value = getDateString(new Date(year, month-1, day),'ddmmyy');
						validflag = true;
					}
				}
				else
				{
					alert("Invalid date value must be  DD/MM/YY or DD/MM/YYYY");
					validflag = false;
				}
			}
			else if (type == "mmyydate")
			{
				var month;
				var day = 0;
				var year;

				if (field.value.indexOf("/") == -1)
				{
					var l = field.value.length;
					month = parseInt(field.value.substr(0,2-l%2),10);
					year = parseInt(field.value.substr(2-l%2),10);
				}
				else
				{
					var comps = field.value.split("/");
					month = parseInt(comps[0],10);
					if (comps[2] != null)
					{
						day  = parseInt(comps[1],10);
						year = parseInt(comps[2],10);
					}
					else
						year = parseInt(comps[1],10);
				}
				if (month >= 1 && month <= 12 && ((year >= 0 && year < 100) || (year > 1900 && year <2100)))
				{
					if (year < 50)
						year += 2000;
					else if (year < 100)
						year += 1900;
					if (day == 0 || day > 31)
					{
						if (month == 2)
						{
							if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
								day = 29;
							else
								day = 28;
			 			}
						else if (month == 4 || month == 6 || month == 9 || month == 11)
							day = 30;
						else
							day = 31;
					}
					field.value = getmmyydatestring(new Date(year, month-1, day));
					validflag = true;
				}
				else
				{
					alert("Invalid date value (must be MMYY, MMYYYY, MM/DD/YY, MM/DD/YYYY)");
					validflag = false;
				}
			}
			else if (type == "mmyy")
			{
				var month;
				var day = 0;
				var year;

				if (field.value.indexOf("/") == -1)
				{
					var l = field.value.length;
					month = parseInt(field.value.substr(0,2-l%2),10);
					year = parseInt(field.value.substr(2-l%2),10);
				}
				else
				{
					var comps = field.value.split("/");
					month = parseInt(comps[0],10);
					if (comps.length == 2)
					{
						year = parseInt(comps[1],10);
					}
					else{
						alert("Invalid date value (must be MM/YY)");
						validflag = false;
					}
				}
				
				
				if(validflag){
					if (month >= 1 && month <= 12 && ((year >= 0 && year < 100) || (year > 1900 && year <2100)))
					{
						if (year < 50)
							year += 2000;
						else if (year < 100)
							year += 1900;
						day =1;
						field.value = getmmyydatestring(new Date(year, month-1, day));
						validflag = true;
					}
					else
					{	
						alert("Invalid date value (must be MM/YY)");
						validflag = false;
					}
				}
			}
			else if (type == "rate")
			{
				var numval;
				var minclip=-10000000;
				var maxclip=10000000;

				var val = field.value;
				val = val.replace(/,/g,"");
				var pctidx = val.lastIndexOf("%");
				if (pctidx!=-1)
				val = val.substr(0,pctidx);

				numval = parseFloat(val);
				if (isNaN(numval) || numval >= maxclip || numval <= minclip)
				{
					alert("Invalid number or percentage");
					validflag = false;
				}
				else
				{
					field.value = format_rate(numval,pctidx!=-1);
					validflag = true;
				}
			}
			else if (type == "integer" || type == "posinteger" || type == "float" || type == "posfloat")
			{
				var numval;
				var minclip=-Math.pow(2,32);
				var maxclip=Math.pow(2,64);
				var val = field.value;
				val = val.replace(/,/g,"");

				if (type == "integer")
					numval = parseInt(val,10);
				else if (type == "posinteger")
				{
					numval = parseInt(val,10);
					minclip=0;
				}
				else if (type == "posfloat")
				{
					numval = parseFloat(val);
					minclip=0;
				}
				else
					numval = parseFloat(val);
				if (isNaN(numval) || numval >= maxclip || numval <= minclip)
				{
					if (type=="posinteger" || type=="posfloat")
						alert("Invalid number (must be positive and less than 1.845E19)");
					else if (type=="integer" || type=="float")
						alert("Illegal number: " + numval);
					else
						alert("Invalid number (must be greater than -4.29B");
					validflag = false;
				}
				else
				{
					field.value = numval;
					validflag = true;
				}
			}
			else if (type == "address")
			{
				var err = '';
				if (field.value.length>999)
				{
					err = "Address too long (truncated at 1000 characters)";
					newval = field.value.substr(0,999);
				}
				if (err != '')
				{
					alert(err);
					field.value = newval;
				}
			}
			else if (type == "visiblepassword")
			{
				if (checkpassword(field, field))
					validflag = true;
				else
					validflag = false;
			}
			else if (type == "email")
			{
				if (checkemail(field, true, true))
					validflag = true;
				else
				validflag = false;
			}
			if (!validflag)
			{
				field.focus();
				field.select();
			}
			window.isvalid = validflag;
			return validflag;
		}


		// used to validate CC Numbers
		function isValidCreditCardNumber(formField,ccType)
		{
		
			var result = true;
 			var ccNum = formField.value;

  			if (result && (formField.value.length>0))
 			{
 				if (!allDigits(ccNum))
 				{
 					alert('Please enter only numbers (no dashes or spaces) for the Credit Card No.');
					formField.focus();
					formField.select();
					result = false;
				}

				if (result)
 				{
					var toValidate = 0;

					if (trim(ccType) == "Visa Card" || trim(ccType) == "Amex" || trim(ccType) == "Amex Optima" || trim(ccType) == "Master Card" || trim(ccType) == "JCB" || trim(ccType) == "Discover" || trim(ccType) == "Diners Card" || trim(ccType) == "C'e Blanche" || trim(ccType) == "Barclaycard"  )
					{
						toValidate = 1;
					}
					
					if ( toValidate == 1 ) 
					{
	 					if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
	 					{
	 						alert('Please enter valid Credit Card No. for ' + ccType + ' to confirm booking.');
							formField.focus();
							formField.select();
							result = false;
						}
					}
					else
					{
						result = true;
					}
				}
			}
			return result;
		}

		function LuhnCheck(str)
		{
		var result = true;

		var sum = 0;
		var mul = 1;
		var strLen = str.length;

		for (i = 0; i < strLen; i++)
		{
			var digit = str.substring(strLen-i-1,strLen-i);
			var tproduct = parseInt(digit ,10)*mul;
			if (tproduct >= 10)
			sum += (tproduct % 10) + 1;
			else
			sum += tproduct;
			if (mul == 1)
			mul++;
			else
			mul--;
		}
		if ((sum % 10) != 0)
			result = false;

		return result;
		}


		function validateCCNum(cardType,cardNum)
		{
			var result = false;
			//cardType = cardType.toUpperCase();

			var cardLen = cardNum.length;
			var firstdig = cardNum.substring(0,1);
			var seconddig = cardNum.substring(1,2);
			var first2dig = cardNum.substring(0,2);
			var first3dig = cardNum.substring(0,3);
			var first4digs = cardNum.substring(0,4);

			switch (trim(cardType))
			{
				case "Visa Card":
					result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
					break;
				case "Amex":
					var validNums = "47";
					result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
					break;
				case "Amex Optima":
					var validNums = "47";
					result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
					break;
				case "Master Card":
					var validNums = "12345";
					result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
					break;
				case "JCB":
					result = ( ((cardLen == 16) && ((parseInt(first4digs) >= 3528 ) && (parseInt(first4digs) <= 3589 ))   )  || ((cardLen == 15) && ((first4digs == "2131") || (first4digs == "1800")) ) ) ;
					break;
				case "Discover":
					result = (cardLen == 16) && (first4digs == "6011");
					break;
				case "Diners Card":
					result = (cardLen == 14) && ( (first3dig == "300") || (first3dig == "301") || (first3dig == "302")  || (first3dig == "303") || (first3dig == "304") || (first3dig == "305") || (first2dig == "36") || (first2dig == "38") );
					break;
				case "C'e Blanche":
					result = ((cardLen == 14) && (first3dig == "389")  ) ;
					break;
				case "Barclaycard":
					var validNums = "12345";
					result = ( ((cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0)) || (((cardLen == 16) || (cardLen == 13)) && (firstdig == "4"))  ) ;
					break;
			}
			return result;
		}

		
		// get Data in JS
		function getJsDateValue(fieldVal){

				var month;
				var day;
				var year;

				if (fieldVal.indexOf("/") == -1 && fieldVal.indexOf(".") == -1 && fieldVal.indexOf("-") == -1)
				{
					var l = fieldVal.length;
					day = parseInt(fieldVal.substr(0,2-l%2),10);
					month = parseInt(fieldVal.substr(2-l%2,2),10);
					year = parseInt(fieldVal.substr(4-l%2),10);
				}
				else
				{
					var comps;
					if (fieldVal.indexOf("/") != -1 ){
						comps = fieldVal.split("/");
					}else if(fieldVal.indexOf(".") != -1 ){
						comps = fieldVal.split(".");
					}else if( fieldVal.indexOf("-") != -1 ){
						comps = fieldVal.split("-");
					}

					day = parseInt(comps[0],10);
					month = parseInt(comps[1],10);
					year = parseInt(comps[2],10);

				}

				if (year < 50)
					year += 2000;
				else if (year < 100)
					year += 1900;
				
				return ( new Date(year, month-1, day)) ;
		}
		 
		 
		function getJsDate(field){
				var month;
				var day;
				var year;

				if (field.value.indexOf("/") == -1 && field.value.indexOf(".") == -1 && field.value.indexOf("-") == -1)
				{
					var l = field.value.length;
					day = parseInt(field.value.substr(0,2-l%2),10);
					month = parseInt(field.value.substr(2-l%2,2),10);
					year = parseInt(field.value.substr(4-l%2),10);
				}
				else
				{
					var comps;
					if (field.value.indexOf("/") != -1 ){
						comps = field.value.split("/");
					}else if(field.value.indexOf(".") != -1 ){
						comps = field.value.split(".");
					}else if( field.value.indexOf("-") != -1 ){
						comps = field.value.split("-");
					}

					day = parseInt(comps[0],10);
					month = parseInt(comps[1],10);
					year = parseInt(comps[2],10);

				}

				if (year < 50)
					year += 2000;
				else if (year < 100)
					year += 1900;

				return ( new Date(year, month-1, day) ) ;
		}


		// get datadiff in JS Date
		function DateDiff( startField, endField, interval, rounding ) {

			var iOut = 0;

			var bufferA = startField ;
			var bufferB = endField ;

			var number = bufferB-bufferA ;

			// what kind of add to do?
			switch (interval.charAt(0))
			{
				case 'd': case 'D':
					iOut = parseInt(number / 86400000) ;
					if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
					break ;
				case 'h': case 'H':
					iOut = parseInt(number / 3600000 ) ;
					if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
					break ;
				case 'm': case 'M':
					iOut = parseInt(number / 60000 ) ;
					if(rounding) iOut += parseInt((number % 60000)/30001) ;
					break ;
				case 's': case 'S':
					iOut = parseInt(number / 1000 ) ;
					if(rounding) iOut += parseInt((number % 1000)/501) ;
					break ;
				default:
				// If we get to here then the interval parameter
				// didn't meet the d,h,m,s criteria.  Handle
				// the error.
				return null ;
			}
			return iOut ;
		}


		// valid proper email
		function checkemail(fld1,emptyok,alrt)
		{

			fld1.value = fld1.value;
			return checkemail2(fld1,fld1,emptyok,alrt);
		}

		// valid proper email attribs		
		function checkemail2(fld1,fld2,emptyok,alrt)
		{
				var s_email = fld1.value;

				if (s_email != fld2.value)
				{
					alert('E-mail addresses must match');
					return false;
				}
				if (emptyok && s_email.length==0)
				{
					return true;
				}

			/* 	shortest possible email is a@a.aa ; can't start with @; only one @ allowed ;
			should be min of 4 chars after @, 2 for '.', min of 3 chars before first '.', etc.....
			*/
			if ((s_email.length < 6) ||
				(s_email.indexOf('@',0) < 1) ||
				(s_email.lastIndexOf('@') != s_email.indexOf('@',0)) ||
				(s_email.lastIndexOf('@') > (s_email.length - 5)) ||
				(s_email.lastIndexOf('.') > (s_email.length - 3)) ||
				(s_email.lastIndexOf('.') < (s_email.length - 4)) ||
				(s_email.indexOf('..',0) > -1) ||
				(s_email.indexOf('@.',0) > -1) ||
				(s_email.indexOf('.@',0) > -1) ||
				(s_email.indexOf(',',0) > -1)  ||
				(s_email.indexOf(' ',0) > -1))
			{
				if (alrt)
				{
					alert('Please enter a valid e-mail address.');
					return false;
				}
			}
			return true;
		}

		// CC Digits
		function allDigits(str)
		{
			return inValidCharSet(str,"0123456789");
		}

		function inValidCharSet(str,charset)
		{
			var result = true;

			for (var i=0;i<str.length;i++)
				if (charset.indexOf(str.substr(i,1))<0)
				{
					result = false;
					break;
				}

			return result;
		}

		function getmmyydatestring(d)
		{
			return ((d.getMonth()+1) < 10 ? "0" : "") + (d.getMonth()+1) + "/" + getFullYear(d).toString().substring(2,4);
		}

		function getFullYear(d)
		{
			if (navigator.appName == "Netscape")
			{
				if (d.getFullYear=="undefined")
				return d.getYear();
			}
			return d.getFullYear();
		}


		// righting string
		function Right(str, n)
				/***
						IN: str - the string we are RIGHTing
							n - the number of characters we want to return

						RETVAL: n characters from the right side of the string
				***/
				{
						if (n <= 0)     // Invalid bound, return blank string
						return "";
						else if (n > String(str).length)   // Invalid bound, return
						return str;                     // entire string
						else { // Valid bound, return appropriate substring
						var iLen = String(str).length;
						return String(str).substring(iLen, iLen - n);
						}
				}

        
        // For Numeric checking : By Krunal
		function isNumberKey(evt,objField) 
         {
			var charCode = (evt.which) ? evt.which : event.keyCode			
            if (charCode > 31 && (charCode < 48 || charCode > 57))
				return false; 
            else
				return true;
            
         }
         
         // For Expiry date allow only digits and slash charater code is 47. : By Krunal
         function isNumberKeyExpiry(evt,objField) 
         {            
			var charCode = (evt.which) ? evt.which : event.keyCode			
            if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode !=47)
				return false; 
            else
				return true;
            
         }
         
         
       
