d=document;

if (d.images) {
	
	// topnav roll
	sections = new Array("index","residential","building");
	for (x=0;x<=sections.length;x++){
	var setup = sections[x]+'on = new Image();'+sections[x]+'on.src = "images/global/nav/'+sections[x]+'_on.gif";'+sections[x]+'off = new Image();'+sections[x]+'off.src = "images/global/nav/'+sections[x]+'_off.gif";'
	eval(setup)
	}
	
	// index roll
	sections = new Array("Safety__Care","Careers","About_Us","Contact_Us");
	for (x=0;x<=sections.length;x++){
	var setup = sections[x]+'on = new Image();'+sections[x]+'on.src = "images/index/nav/'+sections[x]+'_on.gif";'+sections[x]+'off = new Image();'+sections[x]+'off.src = "images/index/nav/'+sections[x]+'_off.gif";'
	eval(setup)
	}
	
}

function on(which){if (d.images){document[which].src = eval(which + "on.src");}}
function off(which){if (d.images){document[which].src = eval(which + "off.src");}}

function popwin(url,name,width,height){
var t=(screen.height-height)/2;
var l=(screen.width-width)/2;
window.open(url,name,'width='+width+',height='+height+',top='+t+',left='+l+',location=0,status=0,scrollbars=0,toolbar=0,resizeable=no,noresize');
}

function popmap(which){
popwin('map.asp?map='+which,which,764,602)
}

//  order form checker

function checkform(which){	
	var errmsg = "";	
	if (which.name.value==""){errmsg = errmsg + "· The Name field does not appear to be filled out correctly.\n";}
	if ((which.email.value=="")||(!isEmailAddr(which.email.value))||(which.email.value.length < 3)){
    errmsg = errmsg + "· The Email field does not appear to be filled out correctly.\n";
  	}
	if (which.phone.value==""){errmsg = errmsg + "· The Phone field does not appear to be filled out correctly.\n";}
	if (which.address.value==""){errmsg = errmsg + "· The Address field does not appear to be filled out correctly.\n";}
	if (which.city.value==""){errmsg = errmsg + "· The City field does not appear to be filled out correctly.\n";}
	if (which.state.value==""){errmsg = errmsg + "· The State field does not appear to be filled out correctly.\n";}
	if (which.zip.value==""){errmsg = errmsg + "· The Zip or Postal Code field does not appear to be filled out correctly.\n";}
	if (which.comments.value==""){errmsg = errmsg + "· The Comments field does not appear to be filled out correctly.\n";}
	if (errmsg!=""){
		errmsg="The following errors were found:\n\n"+errmsg+"\nPlease fix these errors and try again.";
		alert(errmsg);
		return false;
	}else{
		return true;
	}
}

// email format checker

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;
  }
  return result;
}

// credit card number fomat checker

function checkcard(cardNumber, cardType){

  var isValid = false;
  var ccCheckRegExp = /[^\d -]/;
  isValid = !ccCheckRegExp.test(cardNumber);

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ -/g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

    switch(cardType)
    {
      case "mastercard":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;

      case "visa":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;

      case "amex":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;

      default:
        prefixRegExp = /^$/;
        alert("Card type not found");
    }

    prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    isValid = prefixIsValid && lengthIsValid;
  }

  if (isValid)
  {
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }

    isValid = (checkSumTotal % 10 == 0);
  }

  return isValid;
}