var isIE=false,isNS=false,openWin=null,x=0,y=0;

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 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_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 Encode(InString) {
	var key=Math.ceil(Math.random()*80);
	var newstr="";
	for (var idx=0;idx<InString.length;idx++) {
		var c=InString.charCodeAt(idx)*key;
		var c1=Math.floor(c/100);
		var c2=c-(c1*100);
		c1++;
		c2++;
		newstr=newstr+String.fromCharCode(c1)+String.fromCharCode(c2);
	}
	newstr=String.fromCharCode(key)+newstr;
	newstr=escape(newstr);
	while (newstr.indexOf('+')!=-1) {
		newstr=newstr.replace('+','%2B');
	}
	while (newstr.indexOf('-')!=-1) {
		newstr=newstr.replace('-','%2D');
	}
	while (newstr.indexOf('*')!=-1) {
		newstr=newstr.replace('*','%2A');
	}
	while (newstr.indexOf('/')!=-1) {
		newstr=newstr.replace('/','%2F');
	}
	while (newstr.indexOf('.')!=-1) {
		newstr=newstr.replace('.','%2E');
	}
	return newstr;
}

function Handle(ev) {
	if (openWin!=null) {
		openWin.focus();
		return false;
	}
	if (isNS)
		document.routeEvent(ev);
}

function NewLogin() {
	var currPage=document.location.pathname;
	currPage=currPage.substring(currPage.lastIndexOf("/")+1,currPage.indexOf("."));
	document.location="CHVCustLogin.csp?CSPCHD="+cspId+"&Page="+currPage;
}

function loginCheck() {
	var error=new Array();
	if (isIE||(isNS&&parseInt(navigator.appVersion)<5)) {
		if (isIE)
			var coll=document.all;
		else
			var coll=document.forms[0];
	}
	else if (isNS&&parseInt(navigator.appVersion)>4)
		var coll=document.getElementsByTagName("*");
	if (coll.UserName.value=="")
		error[error.length]="User Name";
	if (coll.Password.value=="")
		error[error.length]="Password";
	if (error.length>0) {
		var msg="The following fields are required";
		for (var idx=0;idx<error.length;idx++)
			msg+="\n"+error[idx];
		alert(msg);
	}
	else {
		var data=coll.UserName.value.toUpperCase()+"~"+coll.Password.value+"~";
		var page=document.location.pathname;
		page=page.substring(page.lastIndexOf("/")+1,page.indexOf("."));
		page=page.toLowerCase();
		if (page=="chvitem"||page=="chvcatalog")
			page="chvhome";
		document.location=page+".csp?CSPCHD="+cspId+"&DebtCust=1&Data="+Encode(data);
	}
}

function mouseOver(ev) {
	if (parseInt(navigator.appVersion)<5) {
		x=ev.x;
		y=ev.y;
	}
	else {
		x=ev.clientX;
		y=ev.clientY;
	}
	if (isNS)
		document.routeEvent(ev);
}

function navigate(Page,Category) {
	var currPage=document.location.pathname;
	currPage=currPage.substring(currPage.lastIndexOf("/")+1,currPage.indexOf("."));
	if (currPage=="process") {
		alert("Your information is still being processed.\nPlease wait until this processing has finished\nbefore continuing browsing.");
		return;
	}
	var openStr=server+app+Page+".csp?CSPCHD="+cspId;
	if (typeof(Category)!="undefined") {
		openStr+="&Category="+Category;
	}
	if (Page=="discount"&&secure=="1")
		openStr="https://"+openStr;
	else
		openStr="http://"+openStr;
	document.location=openStr;
}

function onLoad() {
	if (navigator.appName.indexOf("Netscape")>=0)
		isNS=true;
	else if (navigator.appName.indexOf("Microsoft")>=0)
		isIE=true;
	server=document.location.host;
	var page=document.location.pathname;
	page=page.substring(page.lastIndexOf("/")+1,page.indexOf("."));
	if (page=="item"||page=="CHVItem") {
		if (isIE||(isNS&&parseInt(navigator.appVersion)<5)) {
			if (isIE)
				var invImg=document.all.InvImg;
			else
				var invImg=document.images.InvImg;
		}
		else if (isNS&&parseInt(navigator.appVersion)>4)
			var invImg=document.getElementById("InvImg");
		if (newImg.src.indexOf(".jpg")>0) {
			var maxWidth=350,maxHeight=262;
			var actWidth=newImg.width;
			var actHeight=newImg.height;
			var scale=maxWidth/actWidth;
			if (scale*actHeight>maxHeight)
				scale=maxHeight/actHeight;
			var newWidth=Math.round(scale*actWidth),newHeight=Math.round(scale*actHeight);
			invImg.width=newWidth;
			invImg.height=newHeight;
			invImg.src=newImg.src;
		}
	}
	if (isNS) {
		window.captureEvents(Event.CLICK|Event.MOUSEOVER);
		document.captureEvents(Event.CLICK|Event.MOUSEOVER);
		window.onmouseover=mouseOver;
		document.onmouseover=mouseOver;
	}
	window.onclick=Handle;
	document.onclick=Handle;
}

function OpenCreditCard(Process) {
	if (secure=="1")
		var openStr="https://"+server+app+"creditcard.csp?CSPCHD="+cspId;
	else
		var openStr="creditcard.csp?CSPCHD="+cspId;
	if (typeof(Process)!="undefined"&&Process=="1")
		openStr+="&Process=1"
	openWin=window.open(openStr,"","help=no,status=no,top=5,left=150,width=550,height=520");
}

function OpenDebtor(Process) {
	if (secure=="1")
		var openStr="https://"+server+app+"debtor.csp?CSPCHD="+cspId;
	else
		var openStr="debtor.csp?CSPCHD="+cspId;
	if (typeof(Process)!="undefined"&&Process=="1")
		openStr+="&Process=1"
	openWin=window.open(openStr,"","help=no,status=no,top=50,left=200,width=500,height=375");
}

function Search() {
	var val="";
	if (isIE)
		val=document.all.SearchVal.value;
	else if (isNS)
		val=document.forms[0].SearchVal.value;
	if (val=="")
		alert("Please Enter a Search value first");
	else {
		var currPage=document.location.pathname;
		currPage=currPage.substring(currPage.lastIndexOf("/")+1,currPage.indexOf("."));
		if (currPage.indexOf("CHV")==0)
			document.location="CHVSearch.csp?CSPCHD="+cspId+"&SearchVal="+escape(val);
		else
			document.location="search.csp?CSPCHD="+cspId+"&SearchVal="+escape(val);
	}
}