function notNull(str) {
	if (str.length == 0)
    	return false;
  	else
    	return true;
}

function notBlank(str) {
  	for (i = 0; i < str.length; i++) {
    	if (str.charAt(i) != " ")
      		return true;
  	}
  	return false;
}

function isSize(str, size) {
  	if (str.length == size)
    	return true;
  	else
    	return false;
}

function isDigits(str) {
  	var i;
  	for (i = 0; i < str.length; i++) {
    	mychar = str.charAt(i);
    	if (mychar < "0" || mychar > "9")
      		return false;
  	}
  	return true;
}

function isNumber(str) {
  	numdecs = 0;
  	for (i = 0; i < str.length; i++) {
    	mychar = str.charAt(i);
    	if ((mychar >= "0" && mychar <= "9") || mychar == ".") {
      		if (mychar == ".")
        		numdecs++;
    	}
    	else
      		return false;
  	}
  	if (numdecs > 1)
    	return false;
   	return true;
}

function isInRange(str, num1, num2) {
  	var i = parseInt(str);
  	return((i >= num1) && (i <= num2));
}

function stripNonDigits(str) {
  	var i;
  	var newstring = "";
  	for (i = 0;  i < str.length; i++) {
    	mychar = str.charAt(i);
    	if (isDigits(mychar))
      		newstring += mychar;
  	}
  	return newstring;
}

function stripChars(str, chars) {
  	var i;
  	var newstring = "";
  	for (i = 0;  i < str.length; i++) {
    	mychar = str.charAt(i);
    	if (chars.indexOf(mychar) == -1)
      		newstring += mychar;
  	}
  	return newstring;
}

function switchChars(str, chars, subchar) {
  	var i;
  	var newstring = "";
  	for (i = 0;  i < str.length; i++) {
    	mychar = str.charAt(i);
    	if (chars.indexOf(mychar) == -1)
      		newstring += mychar;
    	else
      		newstring += subchar;
  	}
  	return newstring;
}

var emptyString = " field is blank. Please enter a ";

function validateRequired(fld, fldname) {

	if (notNull(fld.value) && notBlank(fld.value))
    	return true;
  	else {
    	alert("The " + fldname + emptyString + fldname);
    	fld.focus();
    	return false;
  	}
}

function validateZip(fld, fldname) {
	var zip = stripNonDigits(fld.value);
  	if (isSize(zip, 5)) {
    	fld.value = zip;
    	return true;
  	} else {
    	alert("Please enter a valid " + fldname);
    	fld.focus();
    	return false;
  	}
}

function validateUSPhone(fld, fldname) {
    var phone = stripNonDigits(fld.value);
  	if (isSize(phone, 10)) {
		fld.value = phone;
    	return true;
  	} else {
    	alert("The " + fldname + "field must be 10 digits including area code");
    	fld.focus();
    	return false;
  	}
}

function validateEmail(fld, fldname) {
  	var i;
  	var cnt;

  	if (fld.value.length > 0) {
    	for (i = 0, cnt = 0; i < fld.value.length; i++) {
      		if (fld.value.charAt(i) == '@')
        		cnt++;
    	}
    	if (cnt != 1 || fld.value.charAt(0) == '@' || fld.value.charAt(fld.value.length - 1) == '@') {
      		alert("The " + fldname + " field should be in the form 'username@host'");
      		fld.focus();
      		return false;
    	}
  	}
  	return true;
}

function validateSTCC(fld) {
  	var checkOK 	= "0123456789";
  	var checkStr 	= fld.value;
  	var allValid 	= true;
  	
  	for (i = 0; i < checkStr.length; i++)
  	{
    	ch = checkStr.charAt(i);
    	for (j = 0; j < checkOK.length; j++)
    	{
      		if (ch == checkOK.charAt(j))
        		break;
    	}
    	if (j == checkOK.length)
    	{
      		allValid = false;
      		break;
    	}
  	}
  	if (!allValid)
  	{
    	alert("Please enter only digits in the STCC field.");
    	fld.focus();
    	return (false);
  	}
  	return (true);
}

function fmtDateTime(dt) {
	if (dt.length != 0)
      	return(dt.substr(5, 2) + '/' + dt.substr(8, 2) + '/' + dt.substr(0,4) + ' ' + dt.substr(11,5));
}

function fmtDate(dt) {
  	if (dt.length != 0)
      	return(dt.substr(5, 2) + '/' + dt.substr(8, 2) + '/' + dt.substr(0,4));
}

function fmtTime(dt) {
  	if (dt.length != 0)
      	return(dt.substr(11,5));
}

function fmtFloat(fExpr, iDecPlaces) {
  	var sRawNum 	= fExpr.toString();
  	var sFmtNum 	= "";
  	var iPoint 		= sRawNum.indexOf(".", 0);
  	var iWholeDigits;

  	if (iPoint > 0)
    	iWholeDigits = iPoint;
  	else
    	iWholeDigits = sRawNum.length;

  	var iPlaceCnt = iWholeDigits;

  	for (var idx = 0; idx < iWholeDigits; idx++) {
    	sFmtNum = sFmtNum + sRawNum.substr(idx, 1);
    	if (iPlaceCnt-- % 3 == 1 && iPlaceCnt)
      		sFmtNum = sFmtNum + ',';
  	}
  	if (iDecPlaces) {
    	sFmtNum = sFmtNum + '.';

    for (var iDecCnt = 0; iDecCnt < iDecPlaces; iDecCnt++) {
      	if (iPoint > 0)
        	sFmtNum = sFmtNum + sRawNum.substr(iPoint + iDecCnt + 1, 1);
      	else
        	sFmtNum = sFmtNum + '0';
    	}
  	}
   	return(sFmtNum);
}

function checkdate(objName) {
  	var datefield = objName;

  	if (chkdate(objName) == false) {
    	datefield.select();
    	alert("That date is invalid.  Please try again.");
    	datefield.focus();
    	return false;
  	} else {
    	return true;
   	}
}

function chkdate(objName) {
  	var strDatestyle 		= "US"; //United States date style
  	var strDate;
  	var strDateArray;
  	var strDay;
  	var strMonth;
  	var strYear;
  	var intday;
  	var intMonth;
  	var intYear;
  	var booFound 			= false;
  	var datefield 			= objName;
  	var strSeparatorArray 	= new Array("-"," ","/",".");
  	var intElementNr;
  	var err 				= 0;
  	
  	var strMonthArray 	= new Array(12);
  	strMonthArray[0] 	= "Jan";
  	strMonthArray[1] 	= "Feb";
  	strMonthArray[2] 	= "Mar";
  	strMonthArray[3] 	= "Apr";
  	strMonthArray[4] 	= "May";
  	strMonthArray[5] 	= "Jun";
  	strMonthArray[6] 	= "Jul";
  	strMonthArray[7] 	= "Aug";
  	strMonthArray[8] 	= "Sep";
  	strMonthArray[9] 	= "Oct";
  	strMonthArray[10] 	= "Nov";
  	strMonthArray[11] 	= "Dec";
  	
  	strDate = datefield.value;
  	if (strDate.length < 1) {
		return true;
  	}
  	
  	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
    	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
      		strDateArray = strDate.split(strSeparatorArray[intElementNr]);
      		if (strDateArray.length != 3) {
        		err = 1;
        		return false;
      		} else {
        		strDay 		= strDateArray[0];
        		strMonth 	= strDateArray[1];
        		strYear 	= strDateArray[2];
      		}
      		booFound = true;
     	}
  	}
  	
  	if (booFound == false) {
    	if (strDate.length > 5) {
      		strDay 		= strDate.substr(0, 2);
      		strMonth 	= strDate.substr(2, 2);
      		strYear 	= strDate.substr(4);
    	}
  	}
  	
  	if (strYear.length == 2) {
		strYear = '20' + strYear;
  	}
  	
  	// US style
  	if (strDatestyle == "US") {
    	strTemp 	= strDay;
    	strDay 		= strMonth;
    	strMonth 	= strTemp;
  	}
  	
  	intday = parseInt(strDay, 10);
  	if (isNaN(intday)) {
    	err = 2;
    	return false;
  	}
  	
  	intMonth = parseInt(strMonth, 10);
  	if (isNaN(intMonth)) {
    	for (i = 0; i < 12; i++) {
      		if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
        		intMonth 	= i+1;
        		strMonth 	= strMonthArray[i];
        		i 			= 12;
       		}
    	}
    	if (isNaN(intMonth)) {
      		err = 3;
      		return false;
    	}
  	}
  	
  	intYear = parseInt(strYear, 10);
  	if (isNaN(intYear)) {
    	err = 4;
    	return false;
  	}
  	
  	if (intMonth > 12 || intMonth < 1) {
    	err = 5;
    	return false;
  	}
  	
  	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
    	err = 6;
   	 	return false;
  	}
  	
  	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
    	err = 7;
   	 	return false;
  	}
  	
  	if (intMonth == 2) {
    	if (intday < 1) {
      		err = 8;
      		return false;
    	}
    	if (LeapYear(intYear) == true) {
      		if (intday > 29) {
        		err = 9;
        		return false;
      		}
    	} else {
      		if (intday > 28) {
        		err = 10;
        		return false;
      		}
    	}
  	}
  	
  	if (strDatestyle == "US") {
    	datefield.value = intMonth + "/" + intday + "/" + strYear;
  	}
  	else if (strDatestyle == "EU") {
    	datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
  	} else {
    	datefield.value = intMonth + "/" + intday + "/" + strYear;
  	}
 	return true;
}

function LeapYear(intYear) {
  	if (intYear % 100 == 0) {
    	if (intYear % 400 == 0) { return true; }
  	} else {
    	if ((intYear % 4) == 0) { return true; }
  	}
  	return false;
}

function doDateCheck(from, to) {
	if (Date.parse(from.value) <= Date.parse(to.value)) {
    	alert("The dates are valid.");
  	} else {
    	if (from.value == "" || to.value == "")
      		alert("Both dates must be entered.");
    	else
      		alert("To date must occur after " + from);
  	}
}

function rtrim(inString) {
  	while (1) {
    	if (inString.substring(inString.length - 1, inString.length) != " ")
      		break;
    	inString = inString.substring(0, inString.length - 1);
  	}
  	return inString;
}

function ltrim(inString) {
	while (1) {
    	if (inString.substring(0, 1) != " ")
      		break;
    	inString = inString.substring(1, inString.length);
  	}
  	return inString;
}

function trim(inString) {
  	var tmpStr = ltrim(inString);
  	return rtrim(tmpStr);
}

function getFormName() {
	var frmName = null;
  	var frm 	= null;
  	var oForm;
  	
  	frm = document.forms["frmCommodity"];

  	if (frm == null) {
    	frm = document.forms["frmOdTransload"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmCityState"];
  	}
  	if (frm == null) {
    	frm = document.forms["frmRates"];
  	}

  	if (frm == null) {
    	frm = document.forms["frmZipResults"];
  	}

  	if (frm == null) {
    	frm = document.forms["frmCityStateEntry"];
  	}

  	if (frm == null) {
   	 	frm = document.forms["frmPricesForm"];
  	}

  	if (frm == null) {
    	frm = document.forms["frmErrorPage"];
  	}

  	if (frm == null) {
    	frm = document.forms["frmNewPriceReq"];
  	}

  	if (frm == null) {
    	frm = document.forms["frmEmailFriend"];
  	}

  	if (frm == null) {
    	frm = document.forms["frmEmailUs"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmRailRates"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmCommodityDesc"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmCitiesList"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmTNTHiddenRates"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmTNTRates"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmHiddenStccDesc"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmPriceDetail"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmEquipTypeDesc"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmEquipDesc"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmRateDetails"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmContact"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmGlossary"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmRail101"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmSiteTour"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmAbout"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmRpConfirmation"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmCityList"];
  	}
  	
  	if (frm == null) {
    	frm = document.forms["frmRatesForEquip"];
  	}
  	
  	if (frm != null) {
    	frmName = frm.name;
  	}
  	
  	if (frmName == "frmCityState") {
    	frmName = "frmHidden";
 	}
 	
  	if (frmName == "frmTNTRates") {
   	 	frmName = "frmTNTHiddenRates";
  	}
  	
  	oForm = eval("document."+frmName);
  	return oForm;
}

function callOriginPage(showLinkClicked, mode, originTnldNeeded, ctxroot) {
	var oForm = getFormName();
    if (originTnldNeeded == "null") {
		openDialog('origin', 450, 120, loadNextPage, '', ctxroot);
        return;
    } else {
         if (oForm.txtShipVol != null) {            
            if (isValidShipments(oForm)) {
            	if(!CheckCustomerZipCode("origin")) {
                	return;
                } else {
	                oForm.showLinkClicked.value = showLinkClicked;
	                oForm.mode.value			= mode;
	                oForm.submit();
                }
            }
        } else {
            oForm.showLinkClicked.value = showLinkClicked;
            oForm.mode.value 			= mode;
            oForm.submit();
        }
    }
}

function callDestPage(showLinkClicked, mode, destTnldNeeded, ctxroot) {
    var oForm 		= getFormName();
    var submitForm 	= true;
    
    if (oForm.txtZip != null) {
        submitForm = isValidZips(oForm);
    }
    if (submitForm) {
      	if (destTnldNeeded == "null") {
        	openDialog('dest', 450, 120, loadNextPage, '', ctxroot);
        	return;
      	} else {
        	oForm.showLinkClicked.value = showLinkClicked;
        	oForm.mode.value 			= mode;
        	oForm.submit();
      	}
	}
}

function callCommodityPage(showLinkClicked, mode) {
  	var oForm 		= getFormName();
  	var submitForm 	= true;
  	
  	if (oForm.txtZip != null) {
      	submitForm = isValidZips(oForm);
  	}
  	if (submitForm) {
     	oForm.showLinkClicked.value = showLinkClicked;
     	oForm.mode.value 			= mode;
     	oForm.submit();
  	}
}

function callPricesPage(showLinkClicked, mode) {
  	var oForm 		= getFormName();
  	var submitForm 	= true;
  	
  	if (oForm.txtZip != null) {
    	submitForm = isValidZips(oForm);
  	}
  	if (submitForm) {
    	oForm.showLinkClicked.value = showLinkClicked;
    	oForm.mode.value 			= mode;
    	oForm.submit();
  	}
}

function isValidShipments(oForm) {
    var count = 0;
    
    for (count = 0; count < oForm.txtShipVol.length; count++) {
        var innerCount 	= 0;
        var shipVol 	= oForm.txtShipVol[count].value;
        var zip 		= oForm.txtZip[count].value;
        
        if(oForm.hdnCommodityCategory.value == "T")
        {
	        if (trim(zip) != "" || trim(shipVol) != "") {
	            if (shipVol <= 0 || shipVol != parseInt(shipVol)) {
	                alert("Please enter a valid Shipment Volume.");
	                oForm.txtShipVol[count].focus();
	                return false;
	            } 
	            else if(shipVol > 999999)
	            {
	            	alert("Please enter a Shipment Volume that is less than 999999");
	                oForm.txtShipVol[count].focus();
	                return false;
	            }
	            if (trim(zip) == "") {
	                alert("Please enter a Zip.");
	                oForm.txtZip[count].focus();
	                return false;
	            }
	        }
        }
        else if(oForm.hdnCommodityCategory.value == "W")
        {
        	noOfTrucks = oForm.txtTruck[count].value;
        	if (trim(zip) != "" || trim(shipVol) != "" || trim(noOfTrucks) != "") {
	            if (shipVol <= 0 || shipVol != parseFloat(shipVol)) {
	                alert("Please enter a valid Shipment Volume.");
	                oForm.txtShipVol[count].focus();
	                return false;
	            } 
	            else if(Math.floor(shipVol) > 999999)
	            {
	            	alert("Please enter a Shipment Volume that is less than 999999");
	                oForm.txtShipVol[count].focus();
	                return false;
	            }
	            if (trim(zip) == "") {
	                alert("Please enter a Zip.");
	                oForm.txtZip[count].focus();
	                return false;
	            }
	            if (trim(noOfTrucks) == "") {
	                alert("Please enter number of Trucks.");
	                oForm.txtTruck[count].focus();
	                return false;
	            }
	        }
        }
        else
        {
        	noOfTrucks = oForm.txtTruck[count].value;
        	if (trim(zip) != "" || trim(shipVol) != "" || trim(noOfTrucks) != "") {
	            if (shipVol <= 0 || shipVol != parseInt(shipVol)) {
	                alert("Please enter a valid Shipment Volume.");
	                oForm.txtShipVol[count].focus();
	                return false;
	            } 
	            else if(shipVol > 999999)
	            {
	            	alert("Please enter a Shipment Volume that is less than 999999");
	                oForm.txtShipVol[count].focus();
	                return false;
	            }
	            if (trim(zip) == "") {
	                alert("Please enter a Zip.");
	                oForm.txtZip[count].focus();
	                return false;
	            }
	            if (trim(noOfTrucks) == "") {
	                alert("Please enter number of Trucks.");
	                oForm.txtTruck[count].focus();
	                return false;
	            }
	        }
        }
        
        if (trim(zip) != "") {
			for (innerCount = count+1; innerCount < oForm.txtZip.length; innerCount++) {
				if (trim(oForm.txtZip[count].value) == trim(oForm.txtZip[innerCount].value)) {
					alert("One or more zips have been duplicated.");
					return false;
				}
			}
		}        
    }
    
    if(oForm.hdnCommodityCategory.value == "W")
    {
    	if(Math.floor(parseFloat(oForm.txtTotalShipment.value)) > 999999)
        {
        	alert("Total Shipment Volume cannot be more than 999999. Please adjust the Shipment Volumes accordingly.");
            return false;
        }
    }
    else
    {
    	if(parseInt(oForm.txtTotalShipment.value) > 999999)
        {
        	alert("Total Shipment Volume cannot be more than 999999. Please adjust the Shipment Volumes accordingly.");
            return false;
        }
    }
    return true;
}

function isValidZips(oForm) {
  	var count 		= 0;
  	var innerCount	= 0;
  	
  	for (count = 0; count < oForm.txtZip.length; count++) {       
		if (trim(oForm.txtZip[count].value) != "") {
          	for (innerCount = count + 1; innerCount < oForm.txtZip.length; innerCount++) {
             	if (trim(oForm.txtZip[count].value) == trim(oForm.txtZip[innerCount].value)) {
               		alert("One or more zips have been duplicated.");
               		return false;
             	}
          	}
       	}
	}
    return true;
}

//TTR Price Capture Change Starts

function fnCheckQueryName(strCheckStr)
{
	var oForm 		= getFormName();
	var intCount 	= 0;
    var intCounter 	= 0;
    var varChar;
    var strCheckOK 	= "`%&+'?";
    var blnAllValid = true;
	
	if(strCheckStr.length == 0)
	{
		alert("Please enter a Query Name");
		oForm.txtQueryName.focus();
		return false;
	}
	if(strCheckStr.length > 80)
	{
		alert("Query Name cannot be more than 80 characters");
		oForm.txtQueryName.focus();
		return false;
	}
	
    for (intCount = 0; intCount < strCheckStr.length; intCount++)
    {
        varChar = strCheckStr.charAt(intCount);
        if(varChar == '"' || varChar == '\\')
        {
        	blnAllValid = false;
            break;
        }
        for (intCounter = 0; intCounter < strCheckOK.length; intCounter++)
        {
            if (varChar == strCheckOK.charAt(intCounter))
            {
            	blnAllValid = false;
                break;
            }
        }
        if (blnAllValid == false) 
        {
            break;
        }
    }
    if (!blnAllValid)
    {
        alert("The characters ` % & + ' ? \" \\ are not allowed in query names. Please rename your query without using these characters.");
        oForm.txtQueryName.focus();
        return (false);
    }
    return (true);
}

function fnSaveQueryDetails(action)
{
	var oForm 					= getFormName();
	oForm.txtQueryName.value 	= trim(oForm.txtQueryName.value);
	var queryName 				= oForm.txtQueryName.value;
	
	if(!fnCheckQueryName(queryName))
	{
		return false;
	}
	oForm.action 				= action;
	oForm.method 				= "post";
	oForm.showLinkClicked.value = "saveandstay";
    oForm.submit();
}

function fnShipQueryDetails(action)
{
	var oForm 					= getFormName();
	oForm.txtQueryName.value 	= trim(oForm.txtQueryName.value);
	var queryName 				= oForm.txtQueryName.value;
	
	if(!fnCheckQueryName(queryName))
	{
		return false;
	}
	oForm.action 				= action;
	oForm.method 				= "post";
	oForm.showLinkClicked.value = "saveandship";
    oForm.submit();
}

//TTR Price Capture Change Ends

