/** This java script file is used to validate the form fields.*/

/**This function is used to validate the form fields
 based on the screen name.*/

function fnValidate(thisform,screenName)
{
 	// Invoking the form validation function by checking proper screen name.
 	if(screenName == "simServiceInput") {
 		return fnValidateSimServiceInput(thisform);
 	}
 	else if(screenName == "expIServiceInput"){
 		return fnValidateIExpServiceInput(thisform);
 	}
 	else if(screenName == "expCServiceInput"){
 		return fnValidateCExpServiceInput(thisform);
 	} 	
}

/** This function is for checking proper inputs of 
simplified Carload/Intermodal Service Input screens*/
function fnValidateSimServiceInput(thisform) {
	
	//to validate the form fields
	if(checkAlphabets(thisform.txtOrgCity.value,"")) {
	
		if(checkAlphabets(thisform.txtDestCity.value,"")) {
		
			if(checkNumeric(thisform.txtOrgZip.value,"")) {
			
				if(checkNumeric(thisform.txtDestZip.value,"")) {
					return true;
				}
				else {
					alert("Please enter only numeric entries for destination zipcode.");
					thisform.txtDestZip.select();
					thisform.txtDestZip.focus();
					return false;
				}
			}
			else {
				alert("Please enter only numeric entries for origin zipcode.");
				thisform.txtOrgZip.select();
				thisform.txtOrgZip.focus();
				return false;
			}
		}
		else {
			alert("Please enter only letters or only numbers for destination city.");
			thisform.txtDestCity.select();
			thisform.txtDestCity.focus();
			return false;
		}
	}
	else {
		alert("Please enter only letters or only numbers for origin city.");
		thisform.txtOrgCity.select();
		thisform.txtOrgCity.focus();
		return false;
	}	
}

/** This function is for checking proper inputs of 
expanded Intermodal Service Input screen*/
function fnValidateIExpServiceInput(thisform) {
//to validate the form fields
	if(checkAlphabets(thisform.txtOrgCity.value,"")) {

	    if(checkAlphabets(thisform.txtDestCity.value,"")) {

	        if(checkNumeric(thisform.txtOrgZip.value,"")) {

	            if(checkNumeric(thisform.txtDestZip.value,"")) {

		        	if(checkNumeric(thisform.txtPicUpTime.value,"")) {
		        	
						if(checkNumeric(thisform.txtDelTime.value,"")) {
							 					
							if(checkNumeric(thisform.txtServNeedDay.value,".")) {

								if(checkNumeric(thisform.txtLaneOTP.value,"")) {
									
									if(thisform.txtLaneOTP.value != ""){	
													
										if ((thisform.txtLaneOTP.value >0) && (thisform.txtLaneOTP.value <= 100)) {
													return true;
										}
										else {
											alert
												("Lane otp values should be between 1 to 100");
												thisform.txtLaneOTP.select();
												thisform.txtLaneOTP.focus();
												return false;								
										}
									}
									else {
											return true;
									}
								}
								else {
									alert
									("Please enter only numeric entries for lane otp.");
									thisform.txtLaneOTP.select();
									thisform.txtLaneOTP.focus();
									return false;								
								}
							}
							else {
								alert("Please enter only numeric entries for service needed in days.");
								thisform.txtServNeedDay.select();
								thisform.txtServNeedDay.focus();	   
								return false;
							}
						}
						else {
							alert("Please enter numeric entries in 24 hour HHMM format for delivery time.");
							//thisform.txtDelTime.select();
							//thisform.txtDelTime.focus();
							return false;
						}
					}
					else {
						alert("Please enter numeric entries in 24 hour HHMM format for pickup time.");
						//thisform.txtPicUpTime.select();
						//thisform.txtPicUpTime.focus();
						return false;
					}
				}
				else {
					alert("Please enter only numeric entries for destination zipcode.");
					thisform.txtDestZip.select();
					thisform.txtDestZip.focus();
					return false;
				}
			}
			else {
				alert("Please enter only numeric entries for origin zipcode.");
				thisform.txtOrgZip.select();
				thisform.txtOrgZip.focus();
				return false;
			}
		}
		else {
			alert("Please enter only alphabets for destination city.");
			thisform.txtDestCity.select();
			thisform.txtDestCity.focus();
			return false;
		}
	}
	else {
		alert("Please enter only alphabets for origin city.");
		thisform.txtOrgCity.select();
		thisform.txtOrgCity.focus();
		return false;
	}
}

/** This function is for checking proper inputs of 
expanded Carload Service Input screen*/
function fnValidateCExpServiceInput(thisform) {
//to validate the form fields
	if(checkAlphabets(thisform.txtOrgCity.value,"")) {

	    if(checkAlphabets(thisform.txtDestCity.value,"")) {

	        if(checkNumeric(thisform.txtOrgZip.value,"")) {

	            if(checkNumeric(thisform.txtDestZip.value,"")) {

		        	if(true){//checkNumeric(thisform.txtPicUpTime.value,"")) {
		        	
						if(true){//checkNumeric(thisform.txtDelTime.value,"")) {
							 					
							if(checkNumeric(thisform.txtServNeedDay.value,".")) {

								if(checkNumeric(thisform.txtLaneOTP.value,"")) {
									
									if(thisform.txtLaneOTP.value != ""){	
													
										if ((thisform.txtLaneOTP.value >0) && (thisform.txtLaneOTP.value <= 100)) {
													return true;
										}
										else {
											alert
												("Lane otp values should be between 1 to 100");
												thisform.txtLaneOTP.select();
												thisform.txtLaneOTP.focus();
												return false;								
										}
									}
									else {
											return true;
									}
								}
								else {
									alert
									("Please enter only numeric entries for lane otp.");
									thisform.txtLaneOTP.select();
									thisform.txtLaneOTP.focus();
									return false;								
								}
							}
							else {
								alert("Please enter only numeric entries for service needed in days.");
								thisform.txtServNeedDay.select();
								thisform.txtServNeedDay.focus();	   
								return false;
							}
						}
						else {
							alert("Please enter numeric entries in 24 hour HHMM format for delivery time.");
							//thisform.txtDelTime.select();
							//thisform.txtDelTime.focus();
							return false;
						}
					}
					else {
						alert("Please enter numeric entries in 24 hour HHMM format for pickup time.");
						//thisform.txtPicUpTime.select();
						//thisform.txtPicUpTime.focus();
						return false;
					}
				}
				else {
					alert("Please enter only numeric entries for destination zipcode.");
					thisform.txtDestZip.select();
					thisform.txtDestZip.focus();
					return false;
				}
			}
			else {
				alert("Please enter only numeric entries for origin zipcode.");
				thisform.txtOrgZip.select();
				thisform.txtOrgZip.focus();
				return false;
			}
		}
		else {
			alert("Please enter only alphabets for destination city.");
			thisform.txtDestCity.select();
			thisform.txtDestCity.focus();
			return false;
		}
	}
	else {
		alert("Please enter only alphabets for origin city.");
		thisform.txtOrgCity.select();
		thisform.txtOrgCity.focus();
		return false;
	}
}	
	

/** This function is used to validate
	the time format entered by the user.
	The format is "HHMM"*/
function fnCheckTime(time) {
		hours=time.substr(0,2);
		mins=time.substr(2,2);
		//alert(hours);
		//alert(mins);
		if (hours==24) {
			if (mins > 0) {
				alert("Please enter numeric entries in 24 hour HHMM format and cannot be greater than 2400 hour.");
				return false;			
			}
			else {
				return true;
			}
		}
		else {
			if(!(hours < 0 || hours > 23)) {
				if(!(mins < 0 || mins > 59)) {
					return true;
				}   			
				else {
					alert("Please enter numeric entries in 24 hour HHMM format for pickup time/delivery time.");
					return false;
				}
			}
			else {
				alert("Please enter numeric entries in 24 hour HHMM format for pickup time/delivery time.");
				return false;
			}
		}
}

/** Function to validate the PickUp time 
enterd by the user is proper or not.*/
function fnValidatePickUpTime(thisform) { 
		
	if(!fnCheckTime(thisform.txtPicUpTime.value)) {
		thisform.txtPicUpTime.select();
		thisform.txtPicUpTime.focus();
		return false;
	} 
	else {
		return true;
	}							
}

/** Function to validate the Delivery time 
enterd by the user is proper or not.*/
function fnValidateDelTime(thisform) {

	if(!fnCheckTime(thisform.txtDelTime.value)) {
			thisform.txtDelTime.select();
			thisform.txtDelTime.focus();
			return false;
	}
	else {
		return true;
	}
}
/** Function used to validate the Forced Rail Origin/Destination
fields based on the Transload value passed*/
function fnValidateForcedRail(thisform) {

	if(thisform.selTransload.value == 'NEITHER') {
		if(thisform.selFOrgSt.value!="" && thisform.selFDestSt.value!="") {
			return true;
		}
		else { 
			return false;
		}
	}
	else if(thisform.selTransload.value == 'ORIGIN') {
		if(thisform.selFDestSt.value != "") {
			return true;
		}
		else { 
			return false;
		}
	}
	else if(thisform.selTransload.value == 'DESTINATION') {
		if(thisform.selFOrgSt.value != "") {
			return true;
		}
		else { 
			return false;
		}
	}
	else if(thisform.selTransload.value == 'BOTH') {
		return true;
	}
}

function validateOrigin(thisform) {
	if(((thisform.txtOrgCity.value!="") && 
			(thisform.selOrgSt.value!="")) ||
			(thisform.txtOrgZip.value!="")) 
	{
		if(checkAlphabets(thisform.txtOrgCity.value,"")) 
		{
		  if(checkAlphabets(thisform.selOrgSt.value,""))
		  {
			if(checkNumeric(thisform.txtOrgZip.value,"")) 
			{
				// if cond to check whether user entered 
				//either the City,State or Zip values.
				if(((thisform.txtOrgCity.value!="") ||
						(thisform.selOrgSt.value!="")) && 
						(thisform.txtOrgZip.value !=""))
				{
					alert("Please select either city-state or zip for origin location.");
					return false;
				}
				return true;
			}
			else {
				alert("Please enter only numbers for origin zipcode.");
				thisform.txtOrgZip.select();
				thisform.txtOrgZip.focus();
				return false;
			}
		  }
		  else
		  {
			  alert("Please enter only letters for origin state.");
				thisform.selOrgSt.select();
				thisform.selOrgSt.focus();
				return false;
		  }
		}
		else {
			alert("Please enter only letters for origin city.");
			thisform.txtOrgCity.select();
			thisform.txtOrgCity.focus();
			return false;
		}
	}
	else {
		alert("Please enter all mandatory fields for a valid search."); 
		return false;
	}
}

function validateDest(thisform) {
	if(((thisform.txtDestCity.value!="") && 
			(thisform.selDestSt.value!="")) ||
			(thisform.txtDestZip.value!="")) 
	{
		if(checkAlphabets(thisform.txtDestCity.value,"")) 
		{
		  if(checkAlphabets(thisform.selDestSt.value,""))
		  {
			if(checkNumeric(thisform.txtDestZip.value,"")) {
			
				// if cond to check whether user entered 
				//either the City,State or Zip values.
				if(((thisform.txtDestCity.value!="") ||
				    (thisform.selDestSt.value!="")) && 
					(thisform.txtDestZip.value !=""))
				{
					alert("Please select either city-state or zip for destination location.");
					return false;
				}
				return true;
			}
			else {
				alert("Please enter only numeric entries for destination zipcode.");
				thisform.txtDestZip.select();
				thisform.txtDestZip.focus();
				return false;
			}
		  }
		  else
		  {
			alert("Please enter only letters for destination state.");
			thisform.selDestSt.select();
			thisform.selDestSt.focus();
			return false;
		  }
		}
		else {
			alert("Please enter only letters for destination city.");
			thisform.txtDestCity.select();
			thisform.txtDestCity.focus();
			return false;
		}
	}
	else {
		alert("Please enter all mandatory fields for a valid search."); 
		return false;
	}
}

function validateSegmentation(thisform) {
	if(thisform.selSegmentation.value!="") {
		return true;
	}
	else {
		alert("Please select a value for segmentation field"); 
		return false;
	}
}
			


