// allow only numbers to be entered in a field
function AllowOnlyNumeric() {

	// Get the ASCII value of the key that the user entered
	var key = window.event.keyCode;

	// Verify if the key entered was a numeric character (0-9)
	if (key > 47 && key < 58) {

		// If it was, then allow the entry to continue
		return;

	} else {
	
		// If it was not, then dispose the key and continue with entry
		window.event.returnValue = null;
	}
}

// allow numbers and decimal point to be entered in a field
function AllowCurrency() {

	// Get the ASCII value of the key that the user entered
	var key = window.event.keyCode;

	// Verify if the key entered was a numeric character (0-9)
	if ((key > 47 && key < 58) || key == 46) {

		// If it was, then allow the entry to continue
		return;

	} else {

		// If it was not, then dispose the key and continue with entry
		window.event.returnValue = null;
	}
}

// trim a string
function trim(strIn) { 
	var i = 0;
	for (i=0;i<strIn.length;i++) {
		if ((strIn.charCodeAt(i)  != 32) && (strIn.charCodeAt(i)  != 10) && (strIn.charCodeAt(i)  != 13)) break;
	}
	strIn = strIn.slice(i);
	for (i=strIn.length-1;i>=0;i--) {
		if ((strIn.charCodeAt(i)  != 32) && (strIn.charCodeAt(i)  != 10) && (strIn.charCodeAt(i)  != 13)) break;
	}
	strIn = strIn.slice(0,i+1);
	return strIn;
}

// count number of tildas in the string (this is an indication of how many tracing's they are trying to do)
function countTildas(strIn) {
	var count = 0;
	var i = 0;
	for (i=0;i<strIn.length;i++) {
		if (strIn.substring(i,i+1) == '~') count++;
	}
	return count;
}

// validate trace id entered, trim it, put in upper case, etc.
function valTraceID() {
	var quotedoc = document.forms[0];
	var strTraceId = quotedoc.TraceID.value;
	strTraceId = rmvTildas(strTraceId);
	strTraceId = trim(strTraceId);
	quotedoc.TraceID.value = strTraceId;
	if (strTraceId.length == 0) {
		alert('Please enter a trace id number');
		return false;
	} else {

    		 // regular expression to replace all occurances of carriage returns with a tilda
		re = /\n/gi;
		strTraceId = strTraceId.toUpperCase();
		quotedoc.HiddenTraceID.value = strTraceId.replace(re, '~');
		if (countTildas(quotedoc.HiddenTraceID.value) > 9) {
			if (quotedoc.Language.value == 'E') {
	     			alert('Please enter 10 or less Trace ID numbers');
			} else {
				alert('Veuillez entrer 1-10 numéros d\'expédition avant de soumettre');
			}
    			return false;
		}

		// if it's a trace by pro number, trim unnecessary characters from pro# and cut to a max of 10 in length
		if (quotedoc.TypeOfTrace.selectedIndex == 0) {
			var strPros = '';
			var strWork = '';
			var strHT = '';
			var i = 0;
			strPros = quotedoc.HiddenTraceID.value;
			for (i=0; i < strPros.length; i++) {
				if (strPros.charAt(i) == '~') {
					if (strWork != '') {
						strWork = proTrim(strWork);
						if (strHT != '') {
							strHT = strHT + '~' + strWork;
						} else {
							strHT = strWork;
						}	
						strWork = '';
					}	
				} else {
					strWork = strWork + strPros.charAt(i);
				}
			}

			// end of loop, take care of last entry and load back to hidden trace id
			strWork = proTrim(strWork);
			if (strHT != '') {
				strHT = strHT + '~' + strWork;
			} else {
				strHT = strWork;
			}	
			quotedoc.HiddenTraceID.value = strHT;
		}

		return true;
	}
}

// remove any tildas from any strings
function rmvTildas(strIn) {
	var strWork = "";
	var intX = 0;
	var intY = 0;
	strWork = strIn;
	intX = strIn.indexOf("~")
	while (intX != -1) {
		intY = strIn.length;
		strWork = strIn.substring(0, intX) + strIn.substring(intX+1, intY);
		strIn = strWork;
		intX = strIn.indexOf("~",intX);
	}
return strWork;	
}

// make sure only numbers entered in a field
function isInt(strWorkIn) {
	for (i=0; i < strWorkIn.length; i++) {
		if (strWorkIn.charAt(i) >= '0' && strWorkIn.charAt(i) <= '9') {
		} else {
			return false;
		}
	}
	return true;
}

// validate zip/postal code(s)
function valZip(strIZip) {
	// zip code mandatory
	if (strIZip.length == 0) {
		alert('We\'re sorry, postal/zip code(s) are mandatory.');
		return false;
	}
	// make sure length is at least 5 or 6
	if (strIZip.length < 5 || strIZip.length > 6) {
		alert ('Invalid postal/zip code format.\nPlease enter a valid postal/zip code.');
		return false;
	}
	// assume zip code
	if (strIZip.length == 5) {
		if (!isInt(strIZip)) {
			alert ('Invalid postal/zip code format.\nPlease enter a valid postal/zip code.\nZip code length is 5, postal code is 6 (no spaces please).');
			return false;
		} else {
			return true;	
		}	
	}
	// assume postal code
	if (strIZip.length == 6) {
		for (i=0; i < strIZip.length; i++) {
			if (i == 1 | i == 3 | i == 5) {
				if (strIZip.charAt(i) >= '0' && strIZip.charAt(i) <= '9') {
				} else {
					alert ('Invalid postal/zip code format.\nPlease enter a valid postal/zip code (no spaces please).\nAn example of a valid postal code is L4W5T9.');
					return false;
				}
			} else {
				if (i == 0 | i == 2 | i == 4) {
					if (strIZip.charAt(i) >= 'A' && strIZip.charAt(i) <= 'Z') {
					} else {
						alert ('Invalid postal/zip code format.\nPlease enter a valid postal/zip code (no spaces please).\nAn example of a valid postal code is L4W5T9.');

						return false;
					}
				}	
			}	
		}
	}
return true;	
}

// validate fields on transit request
function valTransitRequest() {
  	var quotedoc = document.forms[0];

	// nothing is required for total solutions and gold
	var strUL = quotedoc.ServiceLevel.options[quotedoc.ServiceLevel.selectedIndex].value;
	if (strUL == 'G1' || strUL == 'T1') {
		return (true);
	}

	// figure out if by city or just zip/postal
	var cityElement = document.getElementById("ftCity");
	if (cityElement == null) {
		cityInUse = false;
	} else {
		cityInUse = true;
		quotedoc.FCity.value = quotedoc.SS_FromCity_1.value.toUpperCase();
		quotedoc.TCity.value = quotedoc.SS_ToCity_1.value.toUpperCase();
		quotedoc.FCity.value = trim(quotedoc.FCity.value);
		quotedoc.TCity.value = trim(quotedoc.TCity.value);
		// set up from state or province	
		quotedoc.FST.value  = quotedoc.SS_FromProv.options[quotedoc.SS_FromProv.selectedIndex].value;
		// set up to state or province
		quotedoc.TST.value  = quotedoc.SS_ToProv.options[quotedoc.SS_ToProv.selectedIndex].value;
	}

	// set up hidden fields trimming out blank characters
	quotedoc.FZip.value = trim(quotedoc.SS_FromPostal.value);
	quotedoc.TZip.value = trim(quotedoc.SS_ToPostal.value);

	// change to upper case
	quotedoc.FZip.value = quotedoc.FZip.value.toUpperCase();
	quotedoc.TZip.value = quotedoc.TZip.value.toUpperCase();
	if (cityInUse == true) {

		// cities and state/provinces are required information if postal code not entered
		if(quotedoc.FZip.value == '' && quotedoc.FCity.value == '') {
			if (quotedoc.Language.value == 'E') {
				alert('Please enter a From City or Postal/Zip Code');
			} else {
				alert('S.V.P. veuillez entrer toute l\'information requise');
			}
			return (false);
		}
		if(quotedoc.TZip.value == '' && quotedoc.TCity.value == '') {
			if (quotedoc.Language.value == 'E') {
				alert('Please enter a To City or Postal/Zip Code');
			} else {
				alert('S.V.P. veuillez entrer toute l\'information requise');
			}
			return (false);
		}

		if (quotedoc.FZip.value != '') {
			if(!valZip(quotedoc.FZip.value)) return (false);
		}
		if (quotedoc.TZip.value != '') {
			if(!valZip(quotedoc.TZip.value)) return (false);
		}
	} else {
		if(!valZip(quotedoc.FZip.value)) return (false);
		if(!valZip(quotedoc.TZip.value)) return (false);
	}
	
	// if date hasn't been set up yet, validate & set up
	if(!chkShipDate(quotedoc.SS_ShipDate.value)) return (false);

	return(true);
}

// validate ship date function
function chkShipDate(dtVal) {
  	var quotedoc = document.forms[0];
	var month = 0;
	var dayofWeek = 0;
	var dayofMonth = 0;
	var year = 0;

	var dteIn = new Date(dtVal);
	var dteTodayFull = new Date();
	var nHours = 24 * 7;
	
	// set up today as just date not time
	var month = dteTodayFull.getMonth();
	var dayofMonth = dteTodayFull.getDate();
	var year = dteTodayFull.getFullYear();
	var dteToday = new Date(year, month, dayofMonth);
	
	// isNaN means is not a number
	if(isNaN(dteIn)) {
		if (quotedoc.Language.value == 'E') {
			alert('Please enter a valid ship date in mm/dd/ccyy format');
		} else {
			alert('S.V.P. veuillez entrer une date valide numérique mois/date/année');
		}
		return (false);
	} else {
		// if requested ship date is earlier than today, error
		if (dteIn < dteToday) {
			if (quotedoc.Language.value == 'E') {
				alert('Ship date must be today or later');
			} else {
				alert('La date d\'expédition doit être aujourd\'hui ou plus tard');
			}
			return (false);
		}	
		// separate out month, day, year, and day of week
		month = (dteIn.getMonth()) + 1;
		dayofMonth = dteIn.getDate();
		dayofWeek = dteIn.getDay();
		year = dteIn.getFullYear();
		if (dayofWeek == 0 || dayofWeek == 6) {
			if (quotedoc.Language.value == 'E') {
				alert('Please enter a ship date that is Monday thru Friday');
			} else {
				alert('S.V.P. veuillez entrer une date du lundi au vendredi');
			}
			return (false);
		}
		// date entered less today's date answer is milliseconds - so convert to hours
		nWork = ((((dteIn - dteToday) / 1000) / 60) /60);
		if(nWork > nHours) {
			if (quotedoc.Language.value == 'E') {
				alert('Date must be less than 8 days from now');
			} else {
				alert('La date doit être à moins de 8 jours d\'aujourd\'hui');
			}
			return (false);
		}
		strYear = new String (year);
		strMonth = new String(month);
		strDayofMonth = new String(dayofMonth);
		
		// expand one digit month or day to 2 digit
		if(strMonth.length == 1) {
			strMonth = "0" + strMonth;
		}
		if(strDayofMonth.length == 1) {
			strDayofMonth = "0" + strDayofMonth;
		}
		quotedoc.CCYYMMDD.value = strYear + strMonth + strDayofMonth;
		return (true);
	}	
}

// remove unnecessary characters from pro numbers and trim to a max of 10 in length
function proTrim(strIn) {
	var strOut = '';
	var i = 0;
	for (i=0; i < strIn.length; i++) {
		if (strOut.length == 10) break;
		if (strIn.charAt(i) >= '0' && strIn.charAt(i) <= '9') {
			strOut = strOut + strIn.charAt(i);
		}
	}
	return strOut;
}

// validate an email address (returns true or false)
function checkEMail (strEMail) {
	var locAt;
	var locPeriod;
	var okEmail;
	
	locAt = strEMail.indexOf('@');
	okEmail = ((locAt != -1) &&
		(locAt != 0) &&
		(locAt != (strEMail.length - 1)) &&
		(strEMail.indexOf('@', locAt + 1 == -1)));
	if (okEmail) {
		locPeriod = strEMail.indexOf('.');
		okEmail = ((locPeriod != -1) && (locPeriod != (strEMail.length)));
	}
	return okEmail;	
}