var browserType;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {
 browserType= "gecko"
}

function PrintThisPage() 
{ 
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
         sOption+="scrollbars=yes,width=750,height=600,left=100,top=25"; 
   
   var winprint=window.open("printreturn.asp","Print",sOption); 
 
    winprint.focus(); 
}


function ParseUSNumber(PhoneNumberInitialString)
  {
    var FmtStr="";
    var index = 0;
    var LimitCheck;

    LimitCheck = PhoneNumberInitialString.length;
    while (index != LimitCheck)
      {
        if (isNaN(parseInt(PhoneNumberInitialString.charAt(index))))
          { }
        else
          { FmtStr = FmtStr + PhoneNumberInitialString.charAt(index); }
        index = index + 1;
		//alert(FmtStr);
      }
	  
    if (FmtStr.length == 10)
      {
	  	//alert(FmtStr);
        FmtStr = "(" + FmtStr.substring(0,3) + ") " + FmtStr.substring(3,6) + "-" + FmtStr.substring(6,10);
		document.returns.phone.value=FmtStr;
		return true;
      }
    else
      {
	  	//alert("false " + FmtStr);
        return false;
     }
  }



function hide2(layerName) {
  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById(layerName)');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById(layerName)');
  else
     document.poppedLayer =   
        eval('document.layers[layerName]');
  document.poppedLayer.style.display = "none";
}

function show2(layerName) {
  if (browserType == "gecko" )
     document.poppedLayer = 
         eval('document.getElementById(layerName)');
  else if (browserType == "ie")
     document.poppedLayer = 
        eval('document.getElementById(layerName)');
  else
     document.poppedLayer = 
         eval('document.layers[layerName]');
  document.poppedLayer.style.display = "inline";
}

  
  // FORM 2 - general validation function starts here

	function verifyMe() {
		var form;
		form = document.returns;
		var error;
		error = false;
		var message;
		message = "Please correct the following fields:"	
		
		if (form.acct.value == ""){
			//must fill in company address city state sel zip
			if (form.company.value == ""){
				error = true;
				message += "\n\n   * Account or Company Information"
			}
		}
		
		if (form.req.value == ""){
			error = true;
			message += "\n\n   * Requisitioner"
		}
		
		if ((form.phone.value == "")||!(ParseUSNumber(form.phone.value))){
			error = true;
			message += "\n\n   * Phone Number (incl area code)"
		}
		
				
		if ((form.email.value == "")||!(checkEmail(form.email.value))) {
			error = true;
			message += "\n\n   * Email address\n     (Enter a valid email address)"
		}
		
		if ((form.inv1.value == "")||(form.stock1.value == "")||(form.qty1.value == "")||(form.reason1.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 1 Return"
		}
		
		//check if other items are entered.
		if (!(form.stock2.value == "")) {
			if ((form.inv2.value == "")||(form.stock2.value == "")||(form.qty2.value == "")||(form.reason2.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 2 Return"
			}
		}
		
		if (!(form.stock3.value == "")) {
			if ((form.inv3.value == "")||(form.stock3.value == "")||(form.qty3.value == "")||(form.reason3.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 3 Return"
			}
		}
		
		if (!(form.stock4.value == "")) {
			if ((form.inv4.value == "")||(form.stock4.value == "")||(form.qty4.value == "")||(form.reason4.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 4 Return"
			}
		}
		
		if (!(form.stock5.value == "")) {
			if ((form.inv5.value == "")||(form.stock5.value == "")||(form.qty5.value == "")||(form.reason5.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 5 Return"
			}
		}
		
		if (!(form.stock6.value == "")) {
			if ((form.inv6.value == "")||(form.stock6.value == "")||(form.qty6.value == "")||(form.reason6.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 6 Return"
			}
		}
		
		if (!(form.stock7.value == "")) {
			if ((form.inv7.value == "")||(form.stock7.value == "")||(form.qty7.value == "")||(form.reason7.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 7 Return"
			}
		}
		
		if (!(form.stock8.value == "")) {
			if ((form.inv8.value == "")||(form.stock8.value == "")||(form.qty8.value == "")||(form.reason8.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 8 Return"
			}
		}
		
		if (!(form.stock9.value == "")) {
			if ((form.inv9.value == "")||(form.stock9.value == "")||(form.qty9.value == "")||(form.reason9.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 9 Return"
			}
		}
		
		if (!(form.stock10.value == "")) {
			if ((form.inv10.value == "")||(form.stock10.value == "")||(form.qty10.value == "")||(form.reason10.selectedIndex == "")){
			error = true;
			message += "\n\n   * Invoice, Stock #, Qty & Reason for Item 10 Return"
			}
		}
		//end check
		
		if (error) {
			alert(message);
			return false;
		}
		else {
			var answer = window.confirm('Do you want to print a confirmation?');
			if (answer){
				PrintThisPage();
			}
			return true;
		}

}

	function checkEmail(email) {
        invalidChars = " /:,;*#^%&$!~`+=()'\"\\|{[]};:<>?"
        if (email == "") {
            return false
        }

        for (i=0; i<invalidChars.length; i++) {
            badChar = invalidChars.charAt(i)
            if (email.indexOf (badChar,0) != -1) {
                return false
            }
		} 

        atPos = email.indexOf ("@",1)

        if (atPos == -1) {
            return false
        }

        if ( email.indexOf ("@", atPos+1) != -1) {
            return false
        }
        periodPos = email.indexOf (".", atPos)

        if ( periodPos == -1) {
            return false
        }

        if ( periodPos+3 > email.length)    {
            return false
        }
        
        if ("." == email.charAt(email.length-1)) {
			return false;
		}

		return true;
	}
