// JavaScript Document

var x, y;
if (typeof self.innerHeight != "undefined") {
	x = self.innerWidth;
	y = self.innerHeight;
} else if (typeof document.compatMode != 'undefined' && document.compatMode == 'CSS1Compat') {
	x = document.documentElement.clientWidth;
	y = document.documentElement.clientHeight;
} else if (document.body) {
	x = document.body.clientWidth;
	y = document.body.clientHeight;
}

var userAgent=navigator.userAgent;
if ((userAgent.indexOf('Firefox') != -1) || (userAgent.indexOf('Netscape') != -1) || (userAgent.indexOf('Opera') != -1)) {
  	document.write('<link href="/TW_assets/styles/mozilla.css" rel="stylesheet" type="text/css">');
}

if ((userAgent.indexOf('MSIE 7') != -1) || (userAgent.indexOf('MSIE 8') != -1))  { //IE 7
	document.write('<link href="/TW_assets/styles/ie7up.css" rel="stylesheet" type="text/css">');
}

if ((userAgent.indexOf('Safari') != -1)) { // Safari
	document.write('<link href="/TW_assets/styles/safari.css" rel="stylesheet" type="text/css">');
}

function FDK_StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}

function FDK_AddToValidateArray(FormName,FormElement,Validation,SetFocus)
{
    var TheRoot=eval("document."+FormName);
 
    if (!TheRoot.ValidateForm) 
    {
        TheRoot.ValidateForm = true;
        eval(FormName+"NameArray = new Array()")
        eval(FormName+"ValidationArray = new Array()")
        eval(FormName+"FocusArray = new Array()")
    }
    var ArrayIndex = eval(FormName+"NameArray.length");
    eval(FormName+"NameArray[ArrayIndex] = FormElement");
    eval(FormName+"ValidationArray[ArrayIndex] = Validation");
    eval(FormName+"FocusArray[ArrayIndex] = SetFocus");
 
}

function FDK_ValidateNonBlank(FormElement,ErrorMsg)
{
  var msg = ErrorMsg;
  var val = FormElement.value;  

  if (!(FDK_StripChars(" \n\t\r",val).length == 0))
  {
     msg="";
  }

  return msg;
}

function FDK_AddNonBlankValidation(FormName,FormElementName,SetFocus,ErrorMsg)  {
  var ValString = "FDK_ValidateNonBlank("+FormElementName+","+ErrorMsg+")"
  FDK_AddToValidateArray(FormName,eval(FormElementName),ValString,SetFocus)
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function WA_setCookie(cookieobj, cookiename, cookievalue, cookiecheck, settype)     {
  var theValue = "";
  if (settype == 1)     {
    for (var n=0; n<cookievalue.length; n++)    {
      if (cookievalue[n].checked)     {
        theValue = cookievalue[n].value;
      }
    }
  }
  else if (settype == 2)     {
    for (var n=0; n<cookievalue.options.length; n++)    {
      if (cookievalue.options[n].selected)     {
        if (theValue != "")
          theValue += ", ";
        theValue += cookievalue.options[n].value;
      }
    }
  }
  else      {
    theValue = cookievalue
  }

  if (cookiecheck)     {
    cookieobj.setValue(cookiename, theValue);
  }
}

function WA_decodeCookieSelectVal(theVal)     {
  theVal = theVal.replace(/\|WACS\|/g, ", ");
  return theVal;
}

function WA_checkCookieSelectOption(theList, theOption, theVal)     {
  var testVal = "";
  if (theList.options[theOption].value)     {
    testVal = theList.options[theOption].value;
  }
  else      {
    testVal = theList.options[theOption].text;
  }
  if (testVal == WA_decodeCookieSelectVal(theVal))     {
    theList.options[theOption].selected = true;
    return true;
  }
  return false;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function WA_getCookie(cookieobj, cookiename, cookieset, settype)     {
  var theValue = cookieobj.getValue(cookiename);
  if (theValue != '') {
	  if (settype == 1)     {
		cookieset.value = theValue;
	  }
	  else if (settype == 2)     {
		cookieset.checked = (String(theValue)!="" && String(theValue).toUpperCase() !="FALSE");
	  }
	  else if (settype == 3)     {
		for (var n=0; n< cookieset.length; n++)     {
		  if (cookieset[n].value == theValue)     {
			cookieset[n].checked = true;
			break;
		  }
		}
	  }
	  else if (settype == 4)     {
		var setArr = theValue.split(", ");
		for (var m=0; m<setArr.length; m++)     {
		  for (var n=0; n<cookieset.options.length; n++)     {
			if (n+m < cookieset.options.length && cookieset.options[n+m])     {
			  if (WA_checkCookieSelectOption(cookieset, n+m, setArr[m]))     {
				break;
			  }
			}
			if (n!=0 && n-m >= 0 && cookieset.options[n-m])     {
			  if (WA_checkCookieSelectOption(cookieset, n-m, setArr[m]))     {
				break;
			  }
			}
			if (n-m < 0 && n+m >= cookieset.options.length)     {
			  break;
			}
		  }
		}
	  }
  }
}

function CookieDef(expires,path,domain,secure)
{
  this.secure = secure;
  this.path = path;
  this.domain = domain;
  this.getValue = getCookie;
  this.setValue = setCookie;
  this.expire = deleteCookie;
  if (expires == 0) {
    this.expires = "";
  } else {
    var today_date = new Date();
	 var expire_seconds = today_date.getTime() + (expires * 24 * 60 * 60  * 1000);
    today_date.setTime(expire_seconds);
    this.expires = today_date.toGMTString();
  }
}
function getCV(offset) {
  var endstr = document.cookie.indexOf(";", offset);
  if (endstr == -1) endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCV(j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return "";
}
function setCookie(name,value)
{
  document.cookie = name + "=" + escape(value) +
    ((this.expires == "") ? "" : ("; expires=" + this.expires)) +
    ((this.path == "") ? "" : ("; path=" + this.path)) +
    ((this.domain == "") ? "" : ("; domain=" + this.domain)) +
    ((this.secure == true) ? "; secure" : "");
}
function deleteCookie(name) {
  document.cookie = name + "=" + "" + "; expires=Thu,01-Jan-70 00:00:01 GMT";
}

function TW_ShowLayer(cookieobj, cookiename)     {
	var theValue = cookieobj.getValue(cookiename);

	if (theValue == "QuickOrder") {
		MM_showHideLayers('Search','','hide','QuickOrder','','show','OnSearch','','hide','OnQuickOrder','','show');
	
	} else {
		MM_showHideLayers('Search','','show','QuickOrder','','hide','OnSearch','','show','OnQuickOrder','','hide');
	}

}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function TW_processSearch(thisForm) {
	// v2.0
	// Read which search we are doing and populate the appopriate variable (hidden).
	var errors = '';
	var term = thisForm.SearchTerm.value;
	var length = term.length;
	var methods = thisForm.method;
	var x = methods.length;
	
	for (i=0; i<x; i++){
		if(methods[i].checked) var thisMethod = methods[i].value;
	}
	
	if (thisMethod == 'number') {
		if ((length > 23) || (length < 1) || (term == 'Search Catalog')) { errors = 'Please enter between 1 and 23 characters for the item number.\n'; } 
		else { thisForm.txtino.value = term; }
		}
		
	if (thisMethod == 'keyword') {
		if ((length > 20) || (length < 1) || (term == 'Search Catalog')) { errors = 'Please enter between 1 and 20 characters for the keyword.\n'; }
		else { thisForm.txtkey.value = term; }
		}

	if (errors) { alert('The following error(s) occurred:\n'+errors); }
	document.MM_returnValue = (errors == '');
	
}

function TW_CheckOrderUpdate(form) {
	// version 1.0
	// Checks data entered into order summary pane before allowing submission.
	var i, j;
	
	for (i = 0; i < form.elements.length; i++)
	{
		if (form.elements[i].id.substring(0,3) == "qty")
		{
			if (form.elements[i].value.length > 4)
			{
				alert("Field cannot be longer than 4 characters!");
				form.elements[i].select();
				form.elements[i].focus();
				return false;
				
			}
			
			for (j = 0; j < form.elements[i].value.length; j++)
			{
				if (form.elements[i].value.charAt(j) < "0" || form.elements[i].value.charAt(j) > "9")
				{
					alert("Field must contain only whole numbers 0-9!");
					form.elements[i].select();
					form.elements[i].focus();
					return false;
					
				}
			}
		}
	}
	
	return true;
}

function chkform(theForm)
{
	
	if (theForm.txtmbrid.value == "")
	{
		alert("You must enter a user ID");
		theForm.txtmbrid.focus();
		return (false);
	}
	
	if (theForm.txtmbrid.value.length < 1)
	{
		alert("You must enter a user ID.");
		theForm.txtmbrid.focus();
		return (false);
	}
	
	if (theForm.txtmbrid.value.length > 64)
	{
		alert("User ID cannot be more than 64 characters.");
		theForm.txtmbrid.focus();
		return (false);
	}
	if (theForm.txtmbrpassword.value == "")
	{
		alert("You must enter a password.");
		theForm.txtmbrpassword.focus();
		return (false);
	}
	
	if (theForm.txtmbrpassword.value.length < 1)
	{
		alert("You must enter a password.");
		theForm.txtmbrpassword.focus();
		return (false);
	}
	
	if (theForm.txtmbrpassword.value.length > 64)
	{
		alert("Password cannot be more than 64 characters.");
		theForm.txtmbrpassword.focus();
		return (false);
	}
	

	// Return clean
	return (true);
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function WA_deleteCookie(cookieobj, cookiename, cookiecheck)     {
  if (cookiecheck)     {
    cookieobj.setValue(cookiename, "");
    cookieobj.expire(cookiename);
  }
}

function TW_addFavorite(url,title) {
	if (document.all)
	window.external.AddFavorite(url,title)
}

function MM_setTextOfTextfield(objName,x,newText) { //v3.0
  var obj = MM_findObj(objName); if (obj) obj.value = newText;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = MM_findObj(selName); if (selObj) MM_jumpMenu(targ,selObj,restore);
}

function TW_ChangeTableWidth(thisTable,x)
{
  document.getElementById(thisTable).width=x
}

function TW_confirmChoice(msg) { 
	if (confirm(msg)) return true;
	else return false;
}

function TW_changeStyle(id, newClass) {

	identity=document.getElementById(id);
	identity.className=newClass;

}


function WAValidateAN(formElement,value,errorMsg,allowUpper,allowLower,allowNumbers,allowSpace,extraChars,focusIt,stopIt,required)  {
  var isValid = true;
  extraChars = extraChars.replace(/&quot;/g,'"');
  if ((!document.WAFV_Stop && !formElement.WAFV_Stop) || formElement.WAFV_Continue)  {
    for (var x=0; x<value.length; x++)  {
	  var charGood = false;
	  var nextChar = value.charAt(x);
	  var charCode = value.charCodeAt(x);
	  if (allowLower)  {
	    if (charCode >= 97 && charCode <= 122)  {
		  charGood = true;
		}
	  }
	  if (allowUpper)  {
	    if (charCode >= 65 && charCode <= 90)  {
		  charGood = true;
		}
	  }
	  if (allowNumbers)  {
	    if (charCode >= 48 && charCode <= 57)  {
		  charGood = true;
		}
	  }
	  if (allowSpace)  {
	    if (nextChar == " ")  {
		  charGood = true;
		}
	  }
	  if (extraChars)  {
	    if (unescape(extraChars).indexOf(nextChar) >= 0)  {
		  charGood = true;
		}
	  }
	  if (!charGood)  {
	    isValid = false;
		x = value.length;
	  }
	}
	if (required && value=="")
	  isValid = false;
  }
  if (!isValid)  {
    WAAddError(formElement,errorMsg,focusIt,stopIt);
  }
}
function WAAlertErrors(errorHead,errorFoot,setFocus,submitForm)  { 
  if (!document.WAFV_StopAlert)  { 
	  document.WAFV_StopAlert = true;
	  if (document.WAFV_InvalidArray)  {  
	    document.WAFV_Stop = true;
        var errorMsg = document.WAFV_Error;
	    if (errorHead!="")
		  errorMsg = errorHead + "\n" + errorMsg;
		if (errorFoot!="")
		  errorMsg += "\n" + errorFoot;
		document.MM_returnValue = false;
		if (document.WAFV_Error!="")
		  alert(errorMsg.replace(/&quot;/g,'"'));
		else if (submitForm)
		  submitForm.submit();
	    if (setFocus && document.WAFV_Focus)  {
		  document.tempFocus = document.WAFV_Focus;
          setTimeout("document.tempFocus.focus();setTimeout('document.WAFV_Stop = false;document.WAFV_StopAlert = false;',1)",1); 
        }
        else {
          document.WAFV_Stop = false;
          document.WAFV_StopAlert = false;
        }
        for (var x=0; x<document.WAFV_InvalidArray.length; x++)  {
	      document.WAFV_InvalidArray[x].WAFV_Stop = false;
	    }
	  }
	  else  {
        document.WAFV_Stop = false;
        document.WAFV_StopAlert = false;
	    if (submitForm)  {
	      submitForm.submit();
	    }
	    document.MM_returnValue = true;
	  }
      document.WAFV_Focus = false;
	  document.WAFV_Error = false;
	  document.WAFV_InvalidArray = false;
  }
}
function WAValidateNM(formElement,errorMsg,minLength,maxLength,allowDecimals,roundDecimals,reformatDecimals,punctuationMarks,focusIt,stopIt,required)  {
  var isValid = true;
  var theThousand = punctuationMarks.charAt(0);
  var theDecimal = punctuationMarks.charAt(1);
  var theCheck = 11/10;
  var trueDecimal = (String(theCheck).charAt(1));
  var value = formElement.value;
  var decimalIndex = value.length;
  if (punctuationMarks.indexOf(trueDecimal)<0 && value.indexOf(trueDecimal)>=0)  {
    isValid = false;
  }
  if (value.lastIndexOf(theDecimal)>=0) {
    decimalIndex = value.lastIndexOf(theDecimal);
  }
  while (value.indexOf(theThousand)>=0)  {
    decimalIndex = value.length;
    if (value.lastIndexOf(theDecimal)>=0) {
      decimalIndex = value.lastIndexOf(theDecimal);
    }
    if ((decimalIndex-(value.lastIndexOf(theThousand)+1))%3 != 0)
	  isValid = false;
    value = value.substring(0,value.lastIndexOf(theThousand)) + value.substring(value.lastIndexOf(theThousand)+1);
  }
  if (trueDecimal != theDecimal)  {
    while (value.indexOf(theDecimal)>=0)  {
      value = value.substring(0,value.indexOf(theDecimal)) + trueDecimal + value.substring(value.indexOf(theDecimal)+1);
    }
  }
  if ((!document.WAFV_Stop && !formElement.WAFV_Stop) && !(!required && value==""))  {
    for (var x=0; x<value.length; x++)  {
	  if  ((value.charAt(x)<0 || value.charAt(x) > 9) && (value.charAt(x) != " " && value.charAt(x) != "," && value.charAt(x) != "."))  {
        isValid = false;
      }
	}
    if (value == "")  {
	  isValid = false;
	}
	var oldVal = String(value);
	if (oldVal.indexOf(trueDecimal)>=0)  {
	  while (oldVal.charAt(oldVal.length-1)=="0" || oldVal.charAt(oldVal.length-1) == trueDecimal)
	    oldVal = oldVal.substring(0,oldVal.length-1);
	  if (oldVal.indexOf(trueDecimal)==0)
	    oldVal = "0" + oldVal;
	}
    if (String(allowDecimals) !="" )  {
	  if (String(value).indexOf(".") > 0 && ((String(value).indexOf(".") + allowDecimals + 2 <= String(value).length) || allowDecimals == 0))  {
	    isValid = false;
	  }
	}
	value = parseFloat(value);
	if (isNaN(value))  {
	  isValid = false;
	}
	else if (String(value).length!=String(oldVal).length) {
	  isValid = false;
	}
	else if ((minLength != "" && minLength > value) || (maxLength != "" && maxLength < value)) {
	  isValid = false;
	}
  }
  if (!isValid)  {
    WAAddError(formElement,errorMsg,focusIt,stopIt);
  }
  else  {
    if (value != "")  {
	  if (roundDecimals != "")  {
	    value = Math.round(value*roundDecimals)/roundDecimals;
	  }
	  if (reformatDecimals != "")  {
	    value = String(value);
		if (value.indexOf(trueDecimal)<0)
		  value += trueDecimal;
		if (value.indexOf(trueDecimal) < value.length - reformatDecimals)  {
		  value = value.substring(0,value.indexOf(trueDecimal) + reformatDecimals + 1);
		}
		else  {
		  while (value.indexOf(trueDecimal) > value.length - reformatDecimals - 1)  {
		     value += "0";
		  }
		}
	  }
	}
    if (trueDecimal != theDecimal)  {
	  value = String(value);
      while (value.indexOf(trueDecimal)>=0)  {
        value = value.substring(0,value.indexOf(trueDecimal)) + theDecimal + value.substring(value.indexOf(trueDecimal)+1);
      }
    }
	if (roundDecimals != "" || reformatDecimals != "")
	  formElement.value = value;
  }
}

function WAAddError(formElement,errorMsg,focusIt,stopIt)  {
  if (document.WAFV_Error)  {
	  document.WAFV_Error += "\n" + errorMsg;
  }
  else  {
    document.WAFV_Error = errorMsg;
  }
  if (!document.WAFV_InvalidArray)  {
    document.WAFV_InvalidArray = new Array();
  }
  document.WAFV_InvalidArray[document.WAFV_InvalidArray.length] = formElement;
  if (focusIt && !document.WAFV_Focus)  {
	document.WAFV_Focus = focusIt;
  }

  if (stopIt == 1)  {
	document.WAFV_Stop = true;
  }
  else if (stopIt == 2)  {
	formElement.WAFV_Continue = true;
  }
  else if (stopIt == 3)  {
	formElement.WAFV_Stop = true;
	formElement.WAFV_Continue = false;
  }
}
