var preloadFlag = false;

function newImage(src, width, height) {
	if (document.images) {
		rslt = new Image();
		rslt.src = src;
		rslt.width = width;
		rslt.height = height;
		preloadFlag = true;
		return rslt;
	}
}

function changeImages(imgName, imgChgToName) {
	if (preloadFlag == true) {
		imgName.src = imgChgToName.src;
		imgName.width = imgChgToName.width;
		imgName.height = imgChgToName.height;
  }
}

function isNumeric(ctl) {
	if (isNaN(parseInt(ctl.value,10))) {
		return false;
	}
	else {
		return true;
	}
}

function isNumber(inputStr)
{
	oneDecimal = false
	for ( var i = 0; i < inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i)
		if ( i == 0 && oneChar == "-")
		{
			continue
		}
		if ( oneChar == "." && !oneDecimal)
		{
			continue
		}
		if ( oneChar < "0" || oneChar > "9")
		{
			return false
		}
	}
	return true
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function limitMaxLength(ctl, l)
{
  if (ctl.value.length > l) {
    ctl.value=ctl.value.substring(0,l);
  }
}

function UCase(ctl) {
  ctl.value = ctl.value.toUpperCase();
  return false;
}

function ltrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function rtrim(str)
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

function trim(str) {
   return rtrim(ltrim(str));
}

function tokCnt(value, token) {
  var cnt = 0;
  var i=0;

  if (value == null) {return -1;}
  for (i=0; i < value.length; i++) {
    if (value.charAt(i) == token) {cnt++;};
  }
  return cnt;
}

function checkValidDate(dateStr) {
  // dateStr must be of format month day year with either slashes
  // or dashes separating the parts. Some minor changes would have
  // to be made to use day month year or another format.
  // This function returns True if the date is valid.
  var slash1 = dateStr.indexOf("/");
  if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
  // if no slashes or dashes, invalid date
  if (slash1 == -1) { return false; }
  var dateMonth = dateStr.substring(0, slash1)
  var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
  var slash2 = dateMonthAndYear.indexOf("/");
  if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
  // if not a second slash or dash, invalid date
  if (slash2 == -1) { return false; }
  var dateDay = dateMonthAndYear.substring(0, slash2);
  var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
  if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
  // if any non-digits in the month, invalid date
  for (var x=0; x < dateMonth.length; x++) {
      var digit = dateMonth.substring(x, x+1);
      if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text month to a number
  var numMonth = 0;
  for (var x=0; x < dateMonth.length; x++) {
      digit = dateMonth.substring(x, x+1);
      numMonth *= 10;
      numMonth += parseInt(digit);
  }
  if ((numMonth <= 0) || (numMonth > 12)) { return false; }
  // if any non-digits in the day, invalid date
  for (var x=0; x < dateDay.length; x++) {
      digit = dateDay.substring(x, x+1);
      if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text day to a number
  var numDay = 0;
  for (var x=0; x < dateDay.length; x++) {
      digit = dateDay.substring(x, x+1);
      numDay *= 10;
      numDay += parseInt(digit);
  }
  if ((numDay <= 0) || (numDay > 31)) { return false; }
  // February can't be greater than 29 (leap year calculation comes later)
  if ((numMonth == 2) && (numDay > 29)) { return false; }
  // check for months with only 30 days
  if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) {
      if (numDay > 30) { return false; }
  }
  // if any non-digits in the year, invalid date
  for (var x=0; x < dateYear.length; x++) {
      digit = dateYear.substring(x, x+1);
      if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text year to a number
  var numYear = 0;
  for (var x=0; x < dateYear.length; x++) {
      digit = dateYear.substring(x, x+1);
      numYear *= 10;
      numYear += parseInt(digit);
  }
  // Year must be a 2-digit year or a 4-digit year
  if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
  // if 2-digit year, use 50 as a pivot date
  if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
  if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
  if ((numYear <= 0) || (numYear > 9999)) { return false; }
  // check for leap year if the month and day is Feb 29
  if ((numMonth == 2) && (numDay == 29)) {
      var div4 = numYear % 4;
      var div100 = numYear % 100;
      var div400 = numYear % 400;
      // if not divisible by 4, then not a leap year so Feb 29 is invalid
      if (div4 != 0) { return false; }
      // at this point, year is divisible by 4. So if year is divisible by
      // 100 and not 400, then it's not a leap year so Feb 29 is invalid
      if ((div100 == 0) && (div400 != 0)) { return false; }
  }
  // date is valid
  return true;
}

function checkValidDateYMD(dateStr) {
  // dateStr must be of format year month day with either slashes
  // or dashes separating the parts. Some minor changes would have
  // to be made to use day month year or another format.
  // This function returns True if the date is valid.
  var slash1 = dateStr.indexOf("/");
  if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
  // if no slashes or dashes, invalid date
  if (slash1 == -1) { return false; }
  var dateYear = dateStr.substring(0, slash1)
  var dateMonthAndDay = dateStr.substring(slash1+1, dateStr.length);
  var slash2 = dateMonthAndDay.indexOf("/");
  if (slash2 == -1) { slash2 = dateMonthAndDay.indexOf("-"); }
  // if not a second slash or dash, invalid date
  if (slash2 == -1) { return false; }
  var dateMonth = dateMonthAndDay.substring(0, slash2);
  var dateDay = dateMonthAndDay.substring(slash2+1, dateMonthAndDay.length);
  if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
  // if any non-digits in the month, invalid date
  for (var x=0; x < dateMonth.length; x++) {
      var digit = dateMonth.substring(x, x+1);
      if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text month to a number
  var numMonth = 0;
  for (var x=0; x < dateMonth.length; x++) {
      digit = dateMonth.substring(x, x+1);
      numMonth *= 10;
      numMonth += parseInt(digit);
  }
  if ((numMonth <= 0) || (numMonth > 12)) { return false; }
  // if any non-digits in the day, invalid date
  for (var x=0; x < dateDay.length; x++) {
      digit = dateDay.substring(x, x+1);
      if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text day to a number
  var numDay = 0;
  for (var x=0; x < dateDay.length; x++) {
      digit = dateDay.substring(x, x+1);
      numDay *= 10;
      numDay += parseInt(digit);
  }
  if ((numDay <= 0) || (numDay > 31)) { return false; }
  // February can't be greater than 29 (leap year calculation comes later)
  if ((numMonth == 2) && (numDay > 29)) { return false; }
  // check for months with only 30 days
  if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) {
      if (numDay > 30) { return false; }
  }
  // if any non-digits in the year, invalid date
  for (var x=0; x < dateYear.length; x++) {
      digit = dateYear.substring(x, x+1);
      if ((digit < "0") || (digit > "9")) { return false; }
  }
  // convert the text year to a number
  var numYear = 0;
  for (var x=0; x < dateYear.length; x++) {
      digit = dateYear.substring(x, x+1);
      numYear *= 10;
      numYear += parseInt(digit);
  }
  // Year must be a 2-digit year or a 4-digit year
  if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
  // if 2-digit year, use 50 as a pivot date
  if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
  if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
  if ((numYear <= 0) || (numYear > 9999)) { return false; }
  // check for leap year if the month and day is Feb 29
  if ((numMonth == 2) && (numDay == 29)) {
      var div4 = numYear % 4;
      var div100 = numYear % 100;
      var div400 = numYear % 400;
      // if not divisible by 4, then not a leap year so Feb 29 is invalid
      if (div4 != 0) { return false; }
      // at this point, year is divisible by 4. So if year is divisible by
      // 100 and not 400, then it's not a leap year so Feb 29 is invalid
      if ((div100 == 0) && (div400 != 0)) { return false; }
  }
  // date is valid
  return true;
}
function queryLogOff (loginPage) {
	var bLogOff = false;
	bLogOff = confirm("  Are you sure you want to log off \n(your current order will be deleted)?");
	if (bLogOff) {
		location=loginPage;
		return true;
	}

	return false;
}

function queryLO (loginPage) {
	var bLogOff = false;
	bLogOff = confirm("Are you sure you want to log off?");
	if (bLogOff) {
		location=loginPage;
		return true;
	}

	return false;
}
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;

		var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
		if (goodEmail)
			result = true;
  }
  return result;
}

function formElemtExist(f, name) {
	//parameters : f    = document.form
	//             name = element name to look for
	if (f.elements.length > 0) {
		for (var i=0; i<f.elements.length; i++) {
			if (f.elements[i].name == name) return true;
		}
	}

	return false;
}

function validateVIN(ctl, doConfirm) {
	var msg = "";
	var bCont = false;
	var chv = "";

	if (ctl.value.length == 18) {
		chv = ctl.value.charAt(0); //get 1st vin char.
		if (chv.toUpperCase() != "A")
			alert("VIN '" + ctl.value + "' invalid VIN entered.");
		else
			bCont = true;
	}

	if (ctl.value.length != 17 && !bCont) {
		ctl.focus();
	  alert("VIN '" + ctl.value + "' must be 17 characters long.");
	  bCont = false;
	}
	else {
		bCont = true;
	}

//	if (!validateChkSum(ctl)) {
//		msg = "                 Please check the VIN entered!\n\n      Warning: Failed checksum validation for VIN.";
//	}
//
//	if (!validateWMI(ctl)) {
//		if (msg == "")
//			msg = "                 Please check the VIN entered!\n\n      Warning: Failed country or make validation in VIN.";
//		else
//			msg = msg+"         \n\n      Warning: Failed country or make validation in VIN.";
//	}
//
//	if (!validateYr(ctl)) {
//		if (msg == "")
//			msg = "                 Please check the VIN entered!\n\n      Warning: Failed vehicle year validation in VIN.";
//		else
//			msg = msg+"         \n\n      Warning: Failed vehicle year validation in VIN.";
//	}
//	var bCont = true;
//	if (msg != "") {
//		var bCont = false;
//		if (doConfirm)
//			bCont = confirm(msg+"\n\nIgnore warning(s) if the vehicle was manufactured prior to 1981.\n\nPress OK to continue with registration or cancel to return to form.");
//		else
//			alert(msg+"\n\nIgnore warning(s) if the vehicle was manufactured prior to 1981.");
//	}
	return bCont;
}

function validateChkSum(ctl) {
	var evin = "1234567890ABCDEFGHJKLMNPRSTUVWXYZ";
	var cal = new Array(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,1,2,3,4,5,7,9,2,3,4,5,6,7,8,9);
	var weight = new Array(8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2);
	var vin = ctl.value.toUpperCase();
	var sm = 0;
	var w = 0;
	var chv;
	var che;
	var i, p;
	var okVIN = false;
	var corrCD = "";

	// VIN
	if (vin.length != 17) {
		ctl.focus();
	  alert("VIN '" + ctl.value + "' must be 17 characters long.");
	  return false;
	}

	for (var i = 0; i < vin.length; i++) {
		var found = false;

		chv = vin.charAt(i); //get vin char.

		for (p = 0; p < evin.length; p++) {
			che = evin.charAt(p);
			if (chv == che) {
				found = true;
			}

			if (found) {
				sm = sm + (cal[p] * weight[w]);
				//alert("pos=" + i + " value= " + chv + " cal=" + cal[p] + " w=" + weight[w] + " total=" + sm);
				w++;
				break;
			}
		}
	}

	chv = vin.charAt(8); //get checksum value in vin
	if (chv == "X")
		ck = 10;
	else
		ck = chv;

	chv = vin.charAt(16);

	if (((sm % 11) == ck) && (chv != " ")) {
		okVIN = true;
		corrCD = trim(ck);
	}
	else {
		for (i=0; i <=10; i++) {
			if (((sm % 11) == i) && (chv != " ")) {
				if (i == 10)
					corrCD = "X";
				else
					corrCD = trim(i);
				break;
			}
		}
	}
	//not returning value corrCD currently.

	return (okVIN);
}

function validateWMI(ctl) {
	var corg = "1234569JKLSVWYZ";
	var vin = ctl.value.toUpperCase();
	var chv;
	var che;
	var i, p;
	var found;

	// Validate country of manufacture/origin
	cofm = vin.charAt(0); //get country of mfg.

	for (p = 0; p < corg.length; p++) {
		che = corg.charAt(p);
		if (cofm == che) {
			found = true;
		}

		if (found) {
			break;
		}
	}

	if (!found) {
		return false;
	}

	//Validate manufacture, make and/or vehicle type
	if(	vin.charAt(1) == "N" && vin.charAt(2) == "I") {
		return false;
	}

	return true;
}

function validateYr(ctl) {

	var yrs = "123456789ABCDEFGHJKLMNPRSTVWXY";
	var vin = ctl.value.toUpperCase();
	var cyr;
	var che;
	var i, p;
	var found;

	// Validate Year of vehicle
	cyr = vin.charAt(9); //get year car.

	for (p = 0; p < yrs.length; p++) {
		che = yrs.charAt(p);
		if (cyr == che) {
			found = true;
		}

		if (found) {
			break;
		}
	}

	if (!found) {
		return false;
	}

	return true;
}

function isNumber(inputStr)
{
	oneDecimal = true;
	for ( var i = 0; i < inputStr.length; i++)
	{
		var oneChar = inputStr.charAt(i);
		if ( oneChar == "." && oneDecimal)
		{
			oneDecimal = false;
			continue;
		}
		else if ( oneChar == "." && !oneDecimal )
		{
			return false;
		}
		if ( oneChar < "0" || oneChar > "9")
		{
			return false;
		}
	}
	return true;
}
