function faqSetup () {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("faq")) return false;
	
	var myFAQ = document.getElementById("faq");
	
	var answers = myFAQ.getElementsByTagName("span");
	var questions = myFAQ.getElementsByTagName("div");

	for (var i=0; i<answers.length; i++) {
		if (answers[i].className != "company-name"){
			answers[i].className ="hide";
		}
	}
	
	for (var j=0; j<questions.length; j++) {
			questions[j].onclick = function () {
				return showAns (this);
			}
			questions[j].onmouseover=function() {
				this.className+=" over";
	  		}
			questions[j].onmouseout=function() {
				this.className="";
			}
	}
}

function showAns (question) {
	var parent = question.parentNode;
	var answer = parent.getElementsByTagName("span");
	for (var i=0; i<answer.length; i++) {
		if (answer[i].className != "show" && answer[i].className != "company-name") {
			answer[i].className ="show";
		} else if (answer[i].className != "company-name") {
			answer[i].className ="hide";
		}
	}
	return false;
}

function quoteformSetup () {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("custom-details")) return false;		

	var myTable = document.getElementById("custom-details");
	var addRowLink = document.getElementById("addrow");
	var removeRowLink = document.getElementById("removerow");

	addRowLink.className ="show";
	removeRowLink.className ="show";

	addRowLink.onclick = function () {
		addRow();
		return false;
	}
	removeRowLink.onclick = function () {
		removeRow();
		return false;
	}
}

function addRow () {
	var myTable = document.getElementById("custom-details");
	var rowNumber = 1;
	
	for (var i=0; document.getElementById("row" + rowNumber); i++) {
		rowNumber++;	
	}
	if (rowNumber < 11) {
		myTable.appendChild(row(rowNumber));
	}
	
}

function removeRow () {
	var myTable = document.getElementById("custom-details");
	var rowNumber = 1;
	var removedRow;
	
	for (var i=0; document.getElementById("row" + (rowNumber+1)); i++) {
		rowNumber++;	
	}
	if (rowNumber > 3) {
		removedRow = document.getElementById("row" + rowNumber);
		myTable.removeChild(removedRow);
	}
	
}


function row (num) {
	var myRow = document.createElement ("tr");
	myRow.setAttribute("id", "row" + num);
	
	var myTH = document.createElement ("th");
	var rowNum = document.createTextNode(num + ".");
	
	myTH.appendChild(rowNum);
	myTH.setAttribute("scope", "row");
	
	var myT1 = document.createElement ("td");
	var myI1 = createInput("material-type-", num, "50");
	myT1.appendChild(myI1);
	
					  
	var myT2 = document.createElement ("td");
	var myI2 = createInput("hazard-class-", num, "50");
	myT2.appendChild(myI2);
	
	var myT3 = document.createElement ("td");
	var myI3 = createInput("container-type-", num, "50");
	myT3.appendChild(myI3);
	
	var myT4 = document.createElement ("td");
	var myI4 = createInput("quantity-", num, "4");
	myT4.appendChild(myI4);
	
	myRow.appendChild(myTH);
	myRow.appendChild(myT1);
	myRow.appendChild(myT2);
	myRow.appendChild(myT3);
	myRow.appendChild(myT4);
	
	return myRow;

	
}

function createInput (naming, num, mlength) {
	var theInput = document.createElement ("input");
	theInput.setAttribute("type","text");
	theInput.setAttribute("name", naming + num);
	theInput.setAttribute("id", naming + num);
	theInput.setAttribute("maxlength", mlength);
	
	return theInput;
}

function quoteformSetupAMS () {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("tbl-parts")) return false;		

	var myTableAMS = document.getElementById("tbl-parts");
	var addRowLinkAMS = document.getElementById("addrow");
	var removeRowLinkAMS = document.getElementById("removerow");

	addRowLinkAMS.className ="show";
	removeRowLinkAMS.className ="show";

	addRowLinkAMS.onclick = function () {
		addRowAMS();
		return false;
	}
	removeRowLinkAMS.onclick = function () {
		removeRowAMS();
		return false;
	}
}

function addRowAMS () {
	var myTableAMS = document.getElementById("tbl-parts");
	var rowNumberAMS = 1;
	
	for (var i=0; document.getElementById("row" + rowNumberAMS); i++) {
		rowNumberAMS++;	
	}
	if (rowNumberAMS < 15) {
		myTableAMS.appendChild(rowAMS(rowNumberAMS));
	}
	
}

function removeRowAMS () {
	var myTableAMS = document.getElementById("tbl-parts");
	var rowNumberAMS = 1;
	var removedRowAMS;
	
	for (var i=0; document.getElementById("row" + (rowNumberAMS+1)); i++) {
		rowNumberAMS++;	
	}
	if (rowNumberAMS > 3) {
		removedRowAMS = document.getElementById("row" + rowNumberAMS);
		myTableAMS.removeChild(removedRowAMS);
	}
	
}


function rowAMS (num) {
	var myRowAMS = document.createElement ("tr");
	myRowAMS.setAttribute("id", "row" + num);
	
	var myT1 = document.createElement ("td");
	var myI1 = createInputAMS("quantity-", num, "4");
	myT1.className = "quantity";
	myT1.appendChild(myI1);
	
					  
	var myT2 = document.createElement ("td");
	var myI2 = createInputAMS("part-num-", num, "25");
	myT2.className = "partnum";
	myT2.appendChild(myI2);
	
	var myT3 = document.createElement ("td");
	var myI3 = createInputAMS("description-", num, "50");
	myT3.className = "desc";
	myT3.appendChild(myI3);
	
	myRowAMS.appendChild(myT1);
	myRowAMS.appendChild(myT2);
	myRowAMS.appendChild(myT3);
	
	return myRowAMS;

	
}

function createInputAMS (naming, num, mlength) {
	var theInput = document.createElement ("input");
	theInput.setAttribute("type","text");
	theInput.setAttribute("name", naming + num);
	theInput.setAttribute("id", naming + num);
	theInput.setAttribute("maxlength", mlength);
	
	return theInput;
}

startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("secnav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
					tertiarynav = document.getElementById("container");
					tertiarynav.className=tertiarynav.className.replace(" over", "");
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
		navRoot3 = document.getElementById("tnav");
		for (i=0; i<navRoot3.childNodes.length; i++) {
			node = navRoot3.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over2";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over2", "");
				}
			}
		}
	}
}

var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,scrollbar=0,toolbar=0';
var popImages = new Array();

function popupProd(url) {

	var newImg = new Image();
	newImg.src = url;
	
	var popupURL = "popup.asp?large-img=" + url;
	
	//var height = newImg.height + 100;
	//var width = newImg.width + 30;
   // window.alert("height:"+height+" width:" +width);

  var theWindow = window.open(popupURL,'productPict',_POPUP_FEATURES); 
  theWindow.focus();
 // theWindow.resizeTo(width, height);
  return theWindow;
 
}

function popupSetup () {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("oem-product")) return false;
	
	var productTBL = document.getElementById("oem-product");
	var productLINK = productTBL.getElementsByTagName("a");

	for (var j=0; j<productLINK.length; j++) {
			//alert(productLINK[j].href);
			if (productLINK[j].href.indexOf('#pg-sec') <= 0){ //makes sure back to top is not a popup
					productLINK[j].onclick = function () {
					popupProd(this.href); 
				return false;			
				}
				popImages[j] = new Image();
				popImages[j].src = productLINK[j].href;
			}
	}
}

function resizeWin() {

	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("main-img")) return false;
	
	var newImg = document.getElementById("main-img");

	var height = newImg.height;
	var width = newImg.width ;
   // window.alert("height:"+height+" width:" +width);

  var theWindow = this.window; 
  theWindow.focus();
  theWindow.resizeTo(width, height);
  theWindow.innerHeight = height;
  theWindow.innerWidth = width;


  //document.body.clientHeight = height;
  //.body.clientWidth = width;

	return theWindow;
 
}


function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultValue) {
		return false;	
	} else {
			return true;
	}
}

function isEmail(field) {
	if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
		return false;	
	} else {
		return true;	
	}
}

function validateForm(whichform){
	for (var i=0; i<whichform.elements.length; i++){
		var element = whichform.elements[i];
		if (element.className.indexOf("required") != -1) {
			if (!isFilled(element)){
				//alert("Please fill in the "+element.name+" filed.");
				alert("Please fill in all fields.");
				return false;
			}
		}
		if (element.className.indexOf("email") != -1) {
			if (!isEmail(element)) {
				alert("Please enter a valid email address.");
				return false;
			}
		}
		if (element.className.indexOf("blankFLD") != -1) {
			if (isFilled(element)) {
				return false;
			}
		}
	}
	return true;
}

function prepareForms(){
	for (var i=0; i<document.forms.length; i++) {
		var thisform = document.forms[i];
		if (thisform != document.getElementById("cse-search-box")){
		//resetFields(thisform);

			thisform.onsubmit = function() {

				return validateForm(this);	
			}
		}
	}
}

// Striping
 // this function is needed to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripe(id) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }

window.onload = function () {
	faqSetup ();
	quoteformSetup ();
	quoteformSetupAMS ();
	startList ();
	popupSetup ();
	prepareForms();
	stripe("state-codes")
	
}