
var seenOnce = false;
var helpWin;

var x = 1800 //30 minute timeout
var y = 1
var z = x

var agt=navigator.userAgent.toLowerCase();
var isMajor = parseInt(navigator.appVersion);
var isMinor = parseFloat(navigator.appVersion);

var isNav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
            && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

var isNav4up = (isNav && (isMajor >= 4));
var isNav6up = (isNav && (isMajor >= 5));

function onLoadFunctions(){
    if (!document.pqEntryForm ){
    	formFocus();
    }
    if (document.categoryDetailForm ){
        setCategoryTypeOnLoad();
        popUpWindowOnLoad();
    }
    if (document.populateChildrenForm || document.alignmentForm  || document.productAlignmentForm  || document.categoryAlignmentForm || document.categoryImageSelectorForm || document.productImageSelectorForm){
        closeOnLoad();
    }
    if (document.productDetailForm){
        productDetailFormOnLoad();
        popUpWindowOnLoad();
    }
 }    

function formFocus(){
    if (document.loginForm){
        document.loginForm.login.focus(); 
    }
    else if (document.selfRegister){
        document.selfRegister.salutation.focus();
    }
    else if (document.newProductForm){
        document.newProductForm.productCode.focus();
    }
    else if (document.addUser){
        document.addUser.salutation.focus();
    }
    else if (document.orderTemplate){
        document.orderTemplate.cartName.focus();
    }
    else if (document.pqEntryForm){
        document.pqEntryForm.qty0.focus();
    }
    else if (document.orderEntryForm){
        if (document.orderEntryForm.dsAccount){
            document.orderEntryForm.dsAccount.focus();
        } else {
            document.orderEntryForm.poNumber.focus();
        }
    }
}

function stayAlert(){
    alert("are we alert yet?");
}

function cancelButton(){
    window.location = "javascript:history.go(-1)";
}

function DisplayWarning(pURL){
	if(confirm("This site may contain links or references to other Web sites.  Please be aware that we do not control other Web sites and that, in any case, this Privacy Policy does not apply to those Web sites.  We encourage you to read the privacy policy of every Web site you visit.\n\nThis link will take you to a Web site to which this Privacy Policy does not apply.  You are solely responsible for your interactions with that Web site. Press OK to continue.")){
	  window.open(pURL);
	}
}

function confirmLink(message, pURL){
    if(confirm(message)) location.href = pURL;
}

//Opens the Help file in a small window if not already open
function openHelpWindow(url) {
    if (helpWin && !helpWin.closed)
	   helpWin.focus();
	else
	   helpWin = window.open(url,"help",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=750,height=500');
}

//Closes the Help window if it is open
function closeHelpWindow() {
	if (helpWin && !helpWin.closed) {
	   helpWin.close()
	}
}

function ltrim(s)  {
   return s.replace( /^\s*/, "" )
}

function rtrim(s) {
   return s.replace( /\s*$/, "" );
}

function trim(s) {
   return rtrim(ltrim(s));
}

function CheckChangedAccount(oldAcct, newAcct){
    if (oldAcct == newAcct){
        return true;
    }
    var URI = top.location.href;
    NO_CHANGE_ACCT_PAGES = new Array(
        "/order/order_batch.jsp",
        "/order/order_dsacct_lookup.jsp",
        "/order/order_print.jsp",
        "/order/order_process.jsp",
        "/order/order_submit.jsp",
        "/order/splitorder_print.jsp",
        "/order/splitorder_submit.jsp",
        "/price_quote/pq_noCOACT.jsp",
        "/price_quote/pq_process.jsp",
		"/offer/customer_data.jsp",
		"/offer/purchase_analysis.jsp",
		"/offer/contract_details.jsp",
		"/offer/contract_terms.jsp",
		"/offer/contract_acknowledgement.jsp",
		"/offer/contract_submission_confirmation.jsp",
		"/offer_type_selection.jsp",
		"/offer_basics.jsp",
		"/product_category_selection.jsp",
		"/customer_price_tier_input.jsp",
		"/customer_data_requested.jsp",
		"/purchase_analysis_data_requested.jsp",
		"/terms_and_conditions.jsp",
		"/offer_preview.jsp",
		"/offer_act_confirmation.jsp");
    for (i = 0; i < NO_CHANGE_ACCT_PAGES.length; i++){
        if (URI.indexOf(NO_CHANGE_ACCT_PAGES[i]) >= 0){
            alert("Please complete the transaction on the current page before changing the Account Number.");
            return false;
        }
    }
    return true;
}


//This function will validate a date field
function validateDate(date) {
    // check that the field is not empty
    if(date == "" || date == null) {
	   //alert("Please enter the deliver date in the Deliver On field.")
	   return false
    }

    // check that the date is entered in the correct format (ie. MM/DD/YYYY)
  	if(!dateFormatOkay(date)) {
	      alert("The date must be in the format of MM/DD/YYYY.")
	      return false
	  }

    var dateArray = date.split("/")
	  var year = dateArray[2]
	  var month = dateArray[0]
	  var day = dateArray[1]

	  // check that numbers were entered for the year, month & day
	  if(isNaN(year) || isNaN(month) || isNaN(day)) {
	      alert("Please enter only numbers for the date.")
	      return false
	  }

	  // check that values entered for the date are acceptable
	  if(!isYearOkay(year) || !isMonthOkay(month) || !isDayOkay(year, month, day)) {
	      return false;
	  }
	
	  return true
}

//This function checks to see if the year is entered in the correct format
function dateFormatOkay(dateString) {
    var okay = true;
    
    if(dateString.length != 10) {
	      okay = false;
	  } else if((dateString.charAt(2) != '/') || (dateString.charAt(5) != '/')) {
	      okay = false;
    }  
    return okay;
}

//This function checks to see if the year is 4-digits long.
function isFourDigitYear(year) {
    if(year.length != 4) {
        alert ("Sorry, the year must be four-digits in length.")
	      return false;
    }
	  return true;
}

// This function checks to see if the year is acceptable.
function isYearOkay(year) {
    var currentDate = new Date()
	  var currentYear = currentDate.getYear()
    // getYear() in JavaScript 1.3 returns (year - 1900) for all years
    // getYear() in earlier versions of JavaScript returns 2-digit years 
	  // for years between 1900 & 1999 and 4-digit years otherwise
    if(currentYear < 1900)
        currentYear+=1900
    if(year < currentYear) {
	      alert("Sorry, the year cannot be less than " + currentYear + ".")
	      return false;
	  } else if (!isFourDigitYear(year)) {
	      return false;
    } else { 
	      return true;
    }
}

// This function checks to see if the month is acceptable.
function isMonthOkay(month) {
    if (month.length != 2) {
	   alert("The date must be in the format of MM/DD/YYYY.")
       //alert ("Sorry, the month must be two-digits in length.")
	   return false
	}
	else
	   month = month-0
    if (month < 1 || month > 12) {
	   alert("The date must be in the format of MM/DD/YYYY.")
	   //alert("Sorry, the month entered is invalid.")
	   return false
	}
	else
	   return true
}

// This function checks to see if the day is acceptable.
function isDayOkay(year, month, day) {
    if (day.length != 2) {
	   alert("The date must be in the format of MM/DD/YYYY.")
       //alert ("Sorry, the day must be two-digits in length.")
	   return false
	}
    if (day < "01" || day > "31") {
	   alert("The date must be in the format of MM/DD/YYYY.")
	   //alert("Sorry, the day entered is invalid.")
	   return false
	}
	else if (day == "31") {
	   // ensure that the month is allowed to have 31 days - yeesh!
	   if ((month == "02") || (month == "04") || (month == "06") || (month == "09") || (month == "11")) {
	      alert("The date must be in the format of MM/DD/YYYY.")
	      return false
	   }
	}
	else {
	   // perform check for leap year validation
	   if (month == "02"){
	      if (day == "29") {
	         if (!isLeapYear(year)) {
		        alert("The date entered is invalid -- not a leap year.")
			    return false
	         }
	      }
	      else if (day > "29") {
	     	 // greater 29 in Feb? I don't think so  [colin]
	         alert("The date must be in the format of MM/DD/YYYY.")
	     	 //alert("The date entered is invalid.")
	     	 return false
	      }
		  else
		     return true
	   }
	   else
	      return true
	}

	return true
}
//This function checks if the year entered is a leap year.
function isLeapYear(year) {
    if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0) {
	      return true
	  } else {
	      return false;
	  }
}

function displayDialog(msg, title, action) {
  var actionToTake = "window.close(); self.opener." + action + ";"
  errWindow=window.open('', 'errWindow', 'alwaysRaised=yes,dependent=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX=400,screenY=300,width=630,height=150');
  errWindow.document.open();
  errWindow.document.writeln('<html>');
  errWindow.document.writeln('<head>');

  errWindow.document.writeln('<meta http-equiv="Content-Type" content="text/html">');

  errWindow.document.writeln('<title>');
  errWindow.document.writeln(title);
  errWindow.document.writeln('</title>');
  errWindow.document.writeln('</head>');
  errWindow.document.writeln('<body onblur="self.focus();" bgcolor="#FFFDFD">');
  errWindow.document.writeln('<center>');
  errWindow.document.write('<font face="arial,helvetica,espy,sans-serif" size="2"><b>');
  errWindow.document.writeln(msg);
  errWindow.document.writeln('</b><br><br><br>');
  errWindow.document.writeln('<form>');
  errWindow.document.writeln('<input type=hidden name=result value="false">');
  errWindow.document.write("<input name='okButton' type=button value='OK' onclick='");
  errWindow.document.write(actionToTake);
  errWindow.document.writeln("'>&nbsp;&nbsp;&nbsp;");
  errWindow.document.write('<input name="closeButton" type=button value="Cancel" onclick="window.close();">');
  errWindow.document.writeln('</form>');
  errWindow.document.write('</font>');
  errWindow.document.writeln('</center>');
  errWindow.document.writeln('</body>');
  errWindow.document.writeln('</html>');
  errWindow.document.close();
}

function confirmEmailLink(mailto)
{
	displayDialog("By submitting the information in this e-mail message, you agree that the information you provide will be governed by our site's <a href='/commerce/privacy_policy.jsp' target='_new'>Privacy Policy</a>. Press OK to continue.","Sending emails",'open( "' + mailto + '" )')

}