
/*************************************************
 Client side validations for General Reward Search.
**************************************************/
function fnGeneralSearchValidation(form) {
	
	var bPointsCheck = fnValidatePointRange(form);
	if (!bPointsCheck) {
		return false;

	}// end if
	
	// If validations successful, return true.
	return true;
	
} // end function fnGeneralSearchValidation


/************************************************
 Client side validations for Flight Reward Search.
*************************************************/
function fnFlightSearchValidation(form) {

	// All the input fields should not be empty.
	if (isEmpty(form.day) &&
		isEmpty(form.month) &&
		isEmpty(form.year) &&
		form.departingFrom.value == '' &&
		form.goingTo.value == '' &&
		isEmpty(form.pointsFrom) &&
		isEmpty(form.pointsTo)){
	
		alert(errorAllEmpty);
		form.day.select();
		form.day.focus();
		return false;
	}

	// The mandatory fields should not be empty.
	if ( isEmpty(form.day) || 
		 isEmpty(form.month) || 
		 isEmpty(form.year) ||
		 form.departingFrom.value == '') {
	 
		alert(errorMandatoryMissing);
		if (isEmpty(form.day)) {
			form.day.focus();
			return;
		}
		
		if (isEmpty(form.month)) {
			form.month.focus();
			return;
		}
		if (isEmpty(form.year)) {
			form.year.focus();
			return;
		}
		if (form.departingFrom.value == '') {
			form.departingFrom.focus();
			return;
		}
		return false;
	}
	
	if ( isEmpty(form.day) || 
		 isEmpty(form.month) || 
		 isEmpty(form.year) ||
		 form.departingFrom.value == '' ) {
		 
		 alert("One of the mandatory field is missing.");
		 if (isEmpty(form.day)) {
			 form.day.focus();
			 return;
		 }
		 if (isEmpty(form.month)) {
			 form.month.focus();
			 return;
		 }
		 if (isEmpty(form.year)) {
			 form.year.focus();
			 return;
		 }
		 if (form.departingFrom.value == '') {
			 form.departingFrom.focus();
			 return;
		 }
		 
		 return false;
	}
	

	// The entered date should be valid.
	if (!chkdate(form, form.day, form.month, form.year)) {
		return false;
	}	

	if (!doDateCheckConcat(currentDate,form.day.value+'/'+form.month.value+'/'+form.year.value)) {
		alert(errorImproperDate);
		form.day.select();		
		form.day.focus();	
		return false;	
	}

	// If the Point range is entered, it should be valid.
	if( (!isEmpty(form.pointsFrom)) ||
		(!isEmpty(form.pointsTo)) ) {
		var bPointsCheck = fnValidatePointRange(form);
		if (!bPointsCheck) {
			return false;
		}
	}
	
	return true;
	
} // end function fnFlightSearchValidation


/************************************************
 Client side validations for Car Reward Search.
*************************************************/

function fnCarSearchValidation(form)
{
						
	form.pointsFrom.value=fnTrimMiddleAll(form.pointsFrom.value);
	form.pointsTo.value=fnTrimMiddleAll(form.pointsTo.value);	
	// All the input fields should not be empty.
	if (isEmpty(form.day) &&
		isEmpty(form.month) &&
		isEmpty(form.year) &&
		form.countryRegion.value == '' &&
		form.location.value == '' &&
		isEmpty(form.pointsFrom) &&
		isEmpty(form.pointsTo)){
	
		alert(errorAllEmpty);
		form.day.select();
		form.day.focus();
		return false;
	}
	
	// The mandatory fields should not be empty.
	if ( isEmpty(form.day) || 
		 isEmpty(form.month) || 
		 isEmpty(form.year) ||
		 form.countryRegion.value == '') {
	 
		alert(errorMandatoryMissing);
		if (isEmpty(form.day)) {
			form.day.focus();
			return;
		}
		
		if (isEmpty(form.month)) {
			form.month.focus();
			return;
		}
		if (isEmpty(form.year)) {
			form.year.focus();
			return;
		}
		if (form.countryRegion.value == '') {
			form.countryRegion.focus();
			return;
		}
		return false;
	}

	// The entered date should be valid.
	if (!chkdate(form, form.day, form.month, form.year)) {
		return false;
	}

	if (!doDateCheckConcat(currentDate,form.day.value+'/'+form.month.value+'/'+form.year.value)) {
		alert(errorImproperDate);
		form.day.select();		
		form.day.focus();	
		return false;	
	}

	// If the Point range is entered, it should be valid.
	if( (!isEmpty(form.pointsFrom)) ||
		(!isEmpty(form.pointsTo)) ) {
		var bPointsCheck = fnValidatePointRange(form);
		if (!bPointsCheck) {
			return false;
		}
	}

	return true; 		 		
}


/*************************************************
 Client side validations for Hotel Reward Search.
**************************************************/
function fnHotelSearchValidation(form)
{

	// All the input fields should not be empty.
	if (isEmpty(form.day) &&
		isEmpty(form.month) &&
		isEmpty(form.year) &&
		form.countryRegion.value == '' &&
		form.location.value == '' &&
		isEmpty(form.pointsFrom) &&
		isEmpty(form.pointsTo)){
	
		alert(errorAllEmpty);
		form.day.select();
		form.day.focus();
		return false;
	}
	
	// The mandatory fields should not be empty.
	if ( isEmpty(form.day) || 
		 isEmpty(form.month) || 
		 isEmpty(form.year) ||
		 form.countryRegion.value == '') {
	 
		alert(errorMandatoryMissing);
		if (isEmpty(form.day)) {
			form.day.focus();
			return;
		}
		
		if (isEmpty(form.month)) {
			form.month.focus();
			return;
		}
		if (isEmpty(form.year)) {
			form.year.focus();
			return;
		}
		if (form.countryRegion.value == '') {
			form.countryRegion.focus();
			return;
		}
		return false;
	}

	// The entered date should be valid.
	if (!chkdate(form, form.day, form.month, form.year)) {
		return false;
	}

	if (!doDateCheckConcat(currentDate,form.day.value+'/'+form.month.value+'/'+form.year.value)) {
		alert(errorImproperDate);
		form.day.select();		
		form.day.focus();	
		return false;	
	}

	// If the Point range is entered, it should be valid.
	if( (!isEmpty(form.pointsFrom)) ||
		(!isEmpty(form.pointsTo)) ) {
		var bPointsCheck = fnValidatePointRange(form);
		if (!bPointsCheck) {
			return false;
		}
	}

	return true; 		 		
}

/****************************************************************
Function to validate that the user has selected a country/region.
****************************************************************/
function fnSelectLocation(form) {
	
	if (form.countryRegion.value == '') {
		alert("Please select a country/region.");
		form.countryRegion.focus();
		return false;
	}
	
	return true;
}


/*****************************************************************
Function to validate that the user has selected an arrival airport.
******************************************************************/
function fnSelectArrivalAirport(form) {
	
	if (form.departingFrom.value == '') {
		alert("Please select a departing airport.");
		form.departingFrom.focus();
		return false;
	}
	
	return true;
}


/****************************************************************
 Common function to validate the points range entered by the user.
*****************************************************************/
function fnValidatePointRange(form) {

   		if( (!isEmpty(form.pointsFrom)) &&
   		    (!isEmpty(form.pointsTo)) ) {

   		    	if (!(validateNumber(form.pointsFrom.value)) ) {
   		    	
   		    		alert(errorInvalidPointRange);
   		    		form.pointsFrom.select();
   		    		form.pointsFrom.focus();
   		    		return false;
   		    	}
   		    	if (!(validateNumber(form.pointsTo.value)) ) {
   		    	
   		    		alert(errorInvalidPointRange);
   		    		form.pointsTo.select();
   		    		form.pointsTo.focus();
   		    		return false;
   		    	}

		   		var iPointsFrom = parseInt(form.pointsFrom.value);
   				var iPointsTo   = parseInt(form.pointsTo.value);

   				if ((iPointsFrom == 0) && (iPointsTo == 0)) {
   					alert(errorZeroPointsInvalid);
   					form.pointsTo.select();
   					form.pointsTo.focus();
	   				return false;
   				}
   			
   				if (iPointsFrom > iPointsTo) {
   					alert(errorWrongPointRange);
   					form.pointsFrom.select();
   					form.pointsFrom.focus();
	   				return false;
   				}

   		}// end if
		
		if( (!isEmpty(form.pointsFrom)) &&
			  isEmpty(form.pointsTo) ) {

				alert(errorPointsEmpty);
				form.pointsTo.select();
   				form.pointsTo.focus();
				return false;
		}

		if( (!isEmpty(form.pointsTo)) &&
			  isEmpty(form.pointsFrom) ) {

				alert(errorPointsEmpty);
				form.pointsFrom.select();
   				form.pointsFrom.focus();
				return false;
		}
		
		return true;
		
}// end function fnValidatePointRange.



function fnKeywordSearchValidation(form) {
	if (isEmpty(form.searchTerms)) {
		alert(errorKeywordMissing);
		form.searchTerms.select();
		form.searchTerms.focus();
		return false;
	}
	if (!validateDescription(form.searchTerms.value)) {
		alert(errorInvalidKeyword);
		form.searchTerms.select();		
		form.searchTerms.focus();
		return false;
	}	
	return true;
}
var agent  = navigator.userAgent.toLowerCase();
   var isNN = (agent.indexOf("netscape")!=-1);
   var isOpera = (agent.indexOf("opera")!=-1);
   var isIE = (agent.indexOf("msie") != -1);

   function autoTaab(eCtrl,len, e,prevObj,curObj,nextObj)
   {
      var keyCode;
      var filter;
      c = curObj.value;
      
      if(e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 36 || e.keyCode == 35 || e.keyCode == 40 || e.keyCode == 38)
      {
         curObj.select();
      }

      if (isOpera | isIE)
      {
         keyCode = e.keyCode;
         filter = [0,8,9,16,17,18,37,38,39,40,46,35,36];
      }
      else if (isNN)
      {
         keyCode = e.which;
         filter = [0,8,9,16,17,18,37,38,39,40,46,35,36];
      }
	  if((c.length==0) && (prevObj!=curObj) && keyCode== 8)
		{
			prevObj.focus();
			prevObj.select();			
			
		}
	   
      if(eCtrl.value.length >= len && !containsElement(filter,keyCode))
      {
         eCtrl.value = eCtrl.value.slice(0, len);
         eCtrl.form[(getIndex(eCtrl)+1) % eCtrl.form.length].focus();
         eCtrl.form[(getIndex(eCtrl)+1) % eCtrl.form.length].select();
      }
	 
      function containsElement(arr, ele)
      {

         var found = false, index = 0;

         while(!found && index < arr.length)

            if(arr[index] == ele)
               found = true;
            else
               index++;

         return found;
      }

      function getIndex(eCtrl)
      {

         var index = -1, i = 0, found = false;

         while (i < eCtrl.form.length && index == -1)

            if (eCtrl.form[i] == eCtrl) index = i;
            else i++;

         return index;
      }
      return true;
   }
