function setImage(theImage, description)
{
     document.getElementById('pic').src = remove(theImage.src,"thumb_");
     document.getElementById('pic').focus();
     location.href = "#main-graphic";
     document.getElementById('desc').innerHTML = description;
     //document.getElementById('desc').innerHtml = theImage.alt;
}

function remove(s, t) 
{
	//Remove t from s (strings)
	i = s.indexOf(t);
	r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + remove(s.substring(i + t.length), t);
	return r;
}
   function hideDivs()
{
	Element.hide('MBBW');
	Element.hide('ERBS');
	Element.hide('GO');
	Element.hide('BCC');
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function check_clear(input_control,default_name)
{
  if (input_control.value == document.getElementById(default_name).value) input_control.value = '';
}

function check_fill(input_control,default_name)
{
  if (input_control.value == '') input_control.value = document.getElementById(default_name).value;
  input_control.value = trim(input_control.value);
}

function validateDate(newstring)
{
	if (!newstring)
	{
		return false;
	}
//valid characters for a Date
	var Chars = "0123456789/";
	for (var i = 0; i < newstring.length; i++)
	{
		if (Chars.indexOf(newstring.charAt(i)) == -1)
		{
			return false;
		}
	}
	if(countInstances(newstring,'/')!=2)
	{
		return false;
	}
	var substrings = newstring.split('/');
	if(!isDate(substrings[1],substrings[0],substrings[2]) )
	{
		return false;
	}
	if(substrings[0].length<1 || substrings[1].length<1  || substrings[2].length<1 )
	{
		return false;
	}
	return true;
}

function isDate (day,month,year) {
	// checks if date passed is valid
	// will accept dates in following format:
	// isDate(dd,mm,ccyy), or
	// isDate(dd,mm) - which defaults to the current year, or
	// isDate(dd) - which defaults to the current month and year.
	// Note, if passed the month must be between 1 and 12, and the
	// year in ccyy format.

	var today = new Date();
	year = ((!year) ? y2k(today.getYear()):year);
	month = ((!month) ? today.getMonth():month-1);
	if (!day) return false;
	if(year < 1900 ) return false;
	if(year> 9999) return false;
	var test = new Date(year,month,day);
	if ( (y2k(test.getYear()) == year) &&
		 (month == test.getMonth()) &&
		 (day == test.getDate()) )
		return true;
	else
		return false
}

function Message_window_popup(MessageText) {
	window.open('Window_message_popup.asp?'+MessageText,'MessageWindow','width=250,height=150,toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,left=200,top=200');
	}

function ConfirmMe(Prompt_displayed) {
	Responseval = window.showModalDialog('Window_confirm_popup.asp?'+Prompt_displayed,'WindowName',"dialogHeight:150px; dialogWidth:250px; dialogTop:200px; dialogLeft:200px; edge:Raised; center:Yes; help: No; resizable: No; status: No;");
	return Responseval;
}

function noenter() {
// disallows form submission if enter is pressed in a text box
	return !(window.event && window.event.keyCode == 13);
}

function validEmail(str) {
// validates email
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){return false}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false}
     	if (str.indexOf(at,(lat+1))!=-1){return false}
    	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false}
		if (str.indexOf(dot,(lat+2))==-1){return false}
		if (str.indexOf(" ")!=-1){return false}
	    return true
	}

function isValidEmail(str) {
// validates email ...in another way
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid E-mail ID")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("You Have Entered an Invalid E-mail ID")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(" ")!=-1){
	    alert("Invalid E-mail ID")
	    return false
	 }

		 return true
}

var newwindow;
function openNewWin(url)
// defines the parameters of a popup when a popup is utilized
{
	newwindow=window.open(url,'name','scrollbars=yes,menubar=yes,height=600,width=800,resizable=yes');
	if (window.focus) {newwindow.focus()}
}

function toggleElement(strParentId ,strId, strState) // 1 visible, 0 hidden
{
  	if(document.getElementById){
	var elementRef = document.getElementById(strId);
	var x =document.getElementById(strParentId);
	var tt =findPosX(x) ;
	var ww =findPosY(x)+20;
	if((elementRef.style)&& (elementRef.style.visibility!=null)){
	elementRef.style.visibility = strState;
	elementRef.style.left = tt + 'px';
	elementRef.style.top = ww + 'px';
	adjustiFrame(strId);
 	}
	}
}

function openWin4(URL,winName, intWidth, intHeight) {
	window.open(URL,"thewindow","toolbar=no,width=" + intWidth + ",height=" + intHeight + ",status=no,scrollbars=yes,resize=no,menubar=no");
}

function adjustiFrame(layerName)
// iframe code related to the javascript drop down menu
{
  if (!document.getElementById(layerName + "-iframe")) {
    try{
	var newNode = document.createElement("iFrame");
	//newNode.injectInside(layerName + "-iframe");

	newNode.setAttribute("id", layerName + "-iframe");
    //newNode.setAttribute("src", "javascript:false;");
    newNode.setAttribute("scrolling", "no");
    newNode.setAttribute("frameborder", "0");
    newNode.style.filter += "progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=4, Color='black', Positive='true')";
    document.body.appendChild(newNode);
	}
	catch(e){}
  }
  iFrameDiv = document.getElementById(layerName + "-iframe");
  var div = document.getElementById(layerName);

  try {
    iFrameDiv.style.position = "absolute";
    iFrameDiv.style.width = div.offsetWidth;
    iFrameDiv.style.height = div.offsetHeight;
    iFrameDiv.style.top = div.style.top;
    iFrameDiv.style.left = div.style.left;
    iFrameDiv.style.zIndex = div.style.zIndex - 1;
    iFrameDiv.style.visibility = div.style.visibility;
  } catch(e) {
  }
}

function findPosX(obj)
// code related to the javascript drop down menu that finds the position of a cell
{
	var curleft = 0;
	if (obj.offsetParent)
	{
	while (obj.offsetParent)
	{
	curleft += obj.offsetLeft
	obj = obj.offsetParent;
	}
	}
	else if (obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj)
// code related to the javascript drop down menu that finds the position of a cell
	{
	var curtop = 0;
	if (obj.offsetParent)
	{
	while (obj.offsetParent)
	{
	curtop += obj.offsetTop
	obj = obj.offsetParent;
	}
	}
	else if (obj.y)
	curtop += obj.y;
	return curtop;
}

function showThisId(str)
// shows a hidden object
	{
		document.getElementById(str).style.display = 'inline';
	}
function hideThisId(str)
// hides a hidden object
	{
		document.getElementById(str).style.display = 'none';
	}

function getCheckedValue( strRB ){
	var strTemp, i;
	i = 0;
	while (i < strRB.length ) {
	if ( strRB[i].checked )
	strTemp = strRB[i].value;
	i++;
	}
	return strTemp;
}

function roundNum(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function stripCharsInBag (s, bag)
{
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function countInstances(string, word) {
  var substrings = string.split(word);
  return substrings.length - 1;
}

var fNames = new Array('GetYourQuote','GetYourQuote2');

// Preload
if (document.graphics) {
  var fFlipped = new Array();
  for(i=0; i< fNames.length; i++) {
    fFlipped[i] = new Image();
    fFlipped[i].src = "graphics/"+ fNames[i]+".gif";
  }
}

function swap(s,n){
  var el;
  if( n && document.graphics && document.graphics[n] )
   el = document.graphics[n];
  else if( event.srcElement && event.srcElement.src )
   el = event.srcElement;
  if( el )
   el.src = s;
}


function showMsg(str)
{
	var strResponse;
	var strMsg = 'Please submit a custom quote request';
	if(str == '1'){strMsg = 'You <font color=red><b>WILL</b></font> qualify for a discount on multiple units, please call 1-800-665-5022 or submit an online quote request by clicking OK below.';}
	if(str == '2'){location.href='request-specialized-quote.php';}
	if(str == '3'){strMsg = 'Because of the many variables connected with shipping your selection, we must give you a custom quote. Please call us at 1-800-665-5022, or submit an online quote by clicking OK below.';}
	//alert(strMsg);
	//strResponse= ConfirmThis(strMsg);
//	return(ConfirmThis(strMsg));

	/*
 	if(strResponse=='submit'){
		return true;
		}
	else{
		return false;
		}
		*/
}

function checkVehicle(str)
{
//Certain vehicle types require custom quotes
	var vehicleIndex = document.getElementById('TypeOfVehicle').selectedIndex;
	var strVehicle = document.getElementById('TypeOfVehicle').options[vehicleIndex].value;
	if(Right(strVehicle,1) == 0)
	{
		customQuote('2');
	}
}

function ConfirmThis(Prompt_displayed) {
	  //alert(Prompt_displayed);
		// Responseval = window.showModalDialog('custom_popup.php?'+Prompt_displayed,'WindowName',"dialogHeight:160px; dialogWidth:350px; dialogTop:200px; dialogLeft:200px; edge:Raised; center:Yes; help: No; resizable: No; status: No;");
		//return Responseval;
		return(confirm(Prompt_displayed));
	}

function showThisId(str)
{
	document.getElementById(str).style.display = 'inline';
}

function hideThisId(str)
{
	document.getElementById(str).style.display = 'none';
}

function getUserSelections(str)
{
	//This function will not
	var eleLoaded = document.getElementById("txtIsRunning");
	//if form is not postback then these elements will exist
	if (eleLoaded != null){
		if(getCheckedValue(document.forms[0].elements['vehicleRunning']) == 'No')
		{showThisId('txtIsRunning')}
		if(getCheckedValue(document.forms[0].elements['trailerType']) == 'Enclosed')
		{showThisId('txtTrailerType')}
	}
}

function resetSelections()
{
	//will not run if form after form is submitted
	if(document.getElementById('TypeOfVehicle')){
		document.getElementById('TypeOfVehicle').selectedIndex = 0;
		document.getElementById('reqdFromLocationZip').value = document.getElementById('pickup_zip_default').value;
		document.getElementById('reqdToLocationZip').value = document.getElementById('delivery_zip_default').value;
		setCheckedValue(document.forms[0].elements['trailerType'],'Open');
		setCheckedValue(document.forms[0].elements['vehicleRunning'],'Yes');
	}
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function getCheckedValue( strRB ){
	var strTemp, i;
	i = 0;
	while (i < strRB.length ) {
	if ( strRB[i].checked )
	strTemp = strRB[i].value;
	i++;
	}
	return strTemp;
}

function specialRedirect()
{
	location.href='request-specialized-quote.php';
}

function internationalRedirect()
{
	location.href='international-shipping.php';
}

function customQuote(str)
// if the user selects a specialized quote vehicle from the short quote form, a msg will popup.
// clicking OK makes the message go away and load the request-specialized-quote.php page
// clicking CANCEL makes the message go away and resets the short quote form.
{
	if(showMsg(str) == true)
	{
		document.forms[0].action = 'request-specialized-quote.php';
		document.forms[0].submit();
	}
	else{
		hideThisId('txtIsrunning');
		hideThisId('txtTrailerType');
		resetSelections(str)}
}

function clearForm()
{
	for (var i = 0; i<document.forms[0].elements.length; i++)
    {

		var tempobj= document.forms[0].elements[i]
		if (tempobj.type=="text")
			{
			 	tempobj.value=""
			}
		else if (tempobj.type=="select-one")
			{
				tempobj.value=""
			}
	}

	document.forms[0].action = 'online-instant-quote.php';
	document.forms[0].submit();
}

function validateForm()
// checks if the required fields have input on the long form.
// if not, displays a message and puts the cursor in the missing field.
// if so, submits the information and redirects to the thank-you.php page
{

	var isValidForm = true;
	var emailID=document.forms[1].Email

	for (var i = 0; i <document.forms[1].elements.length; i++)
		{
			var strA = document.forms[1].elements[i];
			var strId = strA.id;
			var strValue = strA.value;
			var strName  = strA.name;
			var strA = document.forms[1].elements[i];
			var strId = strA.id;
			var strValue = strA.value;
			var strName  = strA.name;
			/*
			if ((strA.type == "select-one") && (strA.type.length != 0))
			{
				var b = document.getElementById(strName);
				strValue = b.options[b.selectedIndex].value;
			}
			*/
			if(strId.indexOf('reqd') >-1)
			{
					if( strValue.length == 0 )
					{
						alert("Please complete all required fields");
						document.forms[1].elements[strName].focus();
						isValidForm = false;
						return(isValidForm)
					}
			}
		}

	if (isValidEmail(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		isValidForm = false
	}

	if(isValidForm)
	{
		document.forms[1].submit();
	}
}


function validateInternationalForm()
{
  if (document.getElementById('TypeOfVehicle').value == '')
  {
    alert('Vehical Type Required');
    document.getElementById('TypeOfVehicle').focus();
    return(false);
  }
  if (document.getElementById('fromZip').value == '')
  {
    alert('From Zip Code Required');
    document.getElementById('fromZip').focus();
    return(false);
  }
  if (document.getElementById('toPort').value == '')
  {
    alert('To Country/Port Required');
    document.getElementById('toPort').focus();
    return(false);
  }
  if (document.getElementById('EmailAddress').value == '')
  {
    alert('Email Address Required');
    document.getElementById('EmailAddress').focus();
    return(false);
  }
  return(true);
}

function validateShortForm()
// checks if the required fields have input on the short quote form.
// if not, displays a message and puts the cursor in the missing field.
// if so, submits the information and redirects to the online-instant-quote.php page
{

	var customQuote = '';

  try
  {
	  var vehicleIndex = document.getElementById('reqdTypeOfVehicle').selectedIndex;
	  var strVehicle = document.getElementById('reqdTypeOfVehicle').options[vehicleIndex].value;
	  var toLocValue = trim(document.getElementById('reqdToLocationZip').value);
	  var fromLocValue = trim(document.getElementById('reqdFromLocationZip').value);
  }
  catch(e)
  {
     alert(e.message);
  }


	  if(vehicleIndex == 0)
	  {
	  	alert('Invalid vehicle type');
	  	document.getElementById('reqdTypeOfVehicle').focus();
	  	isValidForm = false;
	  	return(isValidForm)
	  }
	  /*
	  else if (toLocValue == fromLocValue)
	  {
        alert('Please call us at 1-800-665-5022 to get this rate quote.');
        return(false);
	  }
	  else if(fromLocValue == '' || fromLocValue == document.getElementById('pickup_zip_default').value)
	  {
	  	alert('Please enter pickup location zip code');
		  document.getElementById('reqdFromLocationZip').focus();
		  isValidForm = false;
		  return(isValidForm)
	  }
	  else if(toLocValue == '' || toLocValue == document.getElementById('delivery_zip_default').value)
	  {
	  	alert('Please enter delivery location zip code');
	  	document.getElementById('reqdToLocationZip').focus();
	  	isValidForm = false;
	  	return(isValidForm)
	  }
	  else if (!IsInt(fromLocValue) || fromLocValue.length != 5)
	  {
	  	alert('Please enter a valid zip code');
		  document.getElementById('reqdFromLocationZip').focus();
		  isValidForm = false;
		  return(isValidForm)
	  }
	  else if (!IsInt(toLocValue) || toLocValue.length != 5)
	  {
	  	alert('Please enter a valid zip code');
		  document.getElementById('reqdToLocationZip').focus();
		  isValidForm = false;
		  return(isValidForm)
	  }
	  */
	  else
	  {
		document.forms[0].action = 'online-instant-quote.php';
        document.forms[0].submit();
	  }

	  if(strVehicle.indexOf('All Other') == 0)
	  {
//	    if(showMsg('2') == false)
//	    {
//	  	  return(false)
//	    }
//	      else
//	      {
	   	    location.href='request-specialized-quote.php';
	   	    return(false)
//	      }
	  }
	  else if(toLocVehicle.indexOf('Region') == 0 || fromLocVehicle.indexOf('Region') == 0)
	  {
//	    if(showMsg('3') == false)
//	    {
//	  	  return(false)
//	    }
//	      else
//	      {
	   	    location.href='request-specialized-quote.php';
	   	    return(false)
//	      }
	  }
	  else
	  {
		document.forms[0].action = 'online-instant-quote.php';
        document.forms[0].submit();
	  }

}

function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

}


function IsInt(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


