// JavaScript Document

//Overlays

function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}
// global to close active overlays
var timer = 0;
function overlay(curobj, subobjstr, liftright, wide, timeon, liftup){
if (document.getElementById){
	clearTimeout(timer);
	closealloverlays();
	var ypos;
	var subobj=document.getElementById(subobjstr)
	subobj.style.display=(subobj.style.display!="block")? "block" : "none";
	var xpos=getposOffset(curobj, "left") + liftright;
	if (!liftup) {
		ypos=getposOffset(curobj, "top") +2;
	}
	else {
		ypos=getposOffset(curobj, "top") +liftup;
	}
	
	subobj.style.left=xpos+"px"
	subobj.style.top=ypos+"px"
	subobj.style.width=wide+"px"
	if (timer !== 0) {
		timer = setTimeout("closealloverlays()",timeon*1000);
	}
	return false
}
else
	return true
}

function closealloverlays() {
	for (i=0; i<50; i++) {
		if (document.getElementById("overlay"+i)) {
			overlayclose("overlay"+i);
		}
	}
}


function overlayclose(subobj){
	if (document.getElementById(subobj)) {
		document.getElementById(subobj).style.display="none"
	}
}


function showawindow(wimname,action) {
	if (document.getElementById(wimname)) {
		if (action == 'login') {
			document.getElementById("couponbox").style.display = 'none';
			document.getElementById("taglinebox").style.display = 'none';
			document.getElementById(wimname).innerHTML='<table border="0" cellspacing="0" cellpadding="2" class="loginbox"><tr><td align="right">Account</td><td><input name="login" type="text" id="login" maxlength="100" style="width:150px;"></td></tr><tr><td align="right">Password</td><td><input name="password" type="password" id="password" maxlength="20" style="width:150px;"></td></tr><tr><td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="left"><div id="loginerrmess" style="color:#FF0;">Enter your details</div></td><td align="right"><input name="submit" id="submit" type="button" value="Login" style="width:70px;" onClick="chklogin();"></td></tr></table></td></tr></table>';
		}
		if (action == 'coupon') {
			document.getElementById("loginbox").style.display = 'none';
			document.getElementById("taglinebox").style.display = 'none';
			document.getElementById(wimname).innerHTML='<table border="0" cellspacing="0" cellpadding="2" class="loginbox"><tr><td align="right">Coupon</td><td><input name="coupon" type="password" id="coupon" maxlength="20" style="width:150px;"></td></tr><tr><td align="right">&nbsp;</td><td>&nbsp;</td></tr><tr><td colspan="2"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="left"><div id="loginerrmess" style="color:#FF0;">Enter coupon code</div></td><td align="right"><input name="submit" id="submit" type="button" value="Enter" style="width:70px;" onClick="chkcoupon();"></td></tr></table></td></tr></table>';
		}
		document.getElementById(wimname).style.display=(document.getElementById(wimname).style.display!="block")? "block" : "none";
	}

if (document.getElementById("loginbox").style.display == 'none' && document.getElementById("couponbox").style.display == 'none') {
		document.getElementById("taglinebox").style.display=(document.getElementById("taglinebox").style.display!="block")? "block" : "none";
	}

}

function chklogin(){
	if (document.getElementById("login").value == "") {
		document.getElementById("loginerrmess").innerHTML="Enter account";
		return;
	}
	if (document.getElementById("password").value == "") {
		document.getElementById("loginerrmess").innerHTML="Enter password";
		return;
	}
	document.getElementById("loginerrmess").innerHTML='<img src="images/wait.gif" width="10" height="10">&nbsp;Please wait';
	document.getElementById("submit").value="Checking";
	document.getElementById("submit").disabled=true;

	var url="chklogin.asp";
	var chkReturnStatus = "";
	var mypostrequest=new ajaxRequest();
	var login = encodeURIComponent(document.getElementById("login").value);
	var password = encodeURIComponent(document.getElementById("password").value);
	var parameters="login="+login+"&password="+password;
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
		if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
			chkReturnStatus = mypostrequest.responseText;
			if (chkReturnStatus == "err1") {
				document.getElementById("loginerrmess").innerHTML="Login details incorrect";
				document.getElementById("submit").value="Try again";
				document.getElementById("submit").disabled=false;
				document.getElementById("password").value = "";
			}
			if (chkReturnStatus == "err2") {
				document.getElementById("loginerrmess").innerHTML="Account not active";
				document.getElementById("password").value = "";
			}
			if (chkReturnStatus == "err3") {
				parent.location='admin/admin.asp';
			}


		}
		else{
		 alert("An error has occured trying to login. Please call your account manager at Write-The-Web")
		}
	 }
	}
	mypostrequest.open("POST", url, true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
}

function chkcoupon(){
	if (document.getElementById("coupon").value == "") {
		document.getElementById("loginerrmess").innerHTML="Enter coupon code";
		return;
	}
	document.getElementById("loginerrmess").innerHTML='<img src="images/wait.gif" width="10" height="10">&nbsp;Please wait';
	document.getElementById("submit").value="Checking";
	document.getElementById("submit").disabled=true;

	var url="coupon-check.asp";
	var chkReturnStatus = "";
	var mypostrequest=new ajaxRequest();
	var coupon = encodeURIComponent(document.getElementById("coupon").value);
	var parameters="couponCode="+coupon;
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
		if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
			chkReturnStatus = mypostrequest.responseText;
			if (chkReturnStatus == "err1" || chkReturnStatus == "err4") {
				document.getElementById("loginerrmess").innerHTML="Coupon code not valid";
				document.getElementById("submit").disabled=false;
				document.getElementById("submit").value="Try again";
				document.getElementById("coupon").value = "";
			}
			if (chkReturnStatus == "err2") {
				document.getElementById("loginerrmess").innerHTML="Coupon has expired";
				document.getElementById("submit").value="Enter";
				document.getElementById("submit").disabled=false;
				document.getElementById("coupon").value = "";
			}
			if (chkReturnStatus.indexOf("err3") > -1) {
				var displaytext = chkReturnStatus.split("|");
				document.getElementById("couponbox").innerHTML='<div class="loginbox">'+displaytext[1]+'<div>';
				if (document.getElementById("couponbutton")) {
					document.getElementById("couponbutton").innerHTML = '<a href="products.asp?cou=xy" class="nav" onFocus="if(this.blur)this.blur();">Show coupon products</a>';
					updcartcoupons();
				}
			}
		}
		else{
		 alert("An error has occured trying to register your coupon code. Please call your account manager at Write-The-Web")
		}
	 }
	}
	mypostrequest.open("POST", url, true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
}

function prodPanelFill(prodid, memofield) {
	var url="prod-panel-fill.asp";
	var mypostrequest=new ajaxRequest();
	prodid = encodeURIComponent(prodid);
	memofield = encodeURIComponent(memofield);
	var parameters="prodid="+prodid+"&memofield="+memofield;
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
		if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
			document.getElementById("prodinfopanel").innerHTML = mypostrequest.responseText;
			document.getElementById("prodinfopanel").scrollTop = 0;
			if (document.getElementById("memo1")) {
				document.getElementById("memo1").className="winnav";
			}
			if (document.getElementById("memo2")) {
				document.getElementById("memo2").className="winnav";
			}
			if (document.getElementById("memo3")) {
				document.getElementById("memo3").className="winnav";
			}
			if (document.getElementById("memo4")) {
				document.getElementById("memo4").className="winnav";
			}
			if (document.getElementById(memofield)) {
				document.getElementById(memofield).className="winnavbg";
			}
		}
		else{
		 alert("We cannot find the product information you need.")
		}
	 }
	}
	mypostrequest.open("POST", url, true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
}


//recommend a friend
function chkrafriend(){
	var errormess = "";
	var errormess2 = "";
	var theValue = "";
	var oldmess = document.getElementById("rafcartTime").innerHTML
	document.getElementById("rafcartTime").innerHTML='<img src="images/wait.gif" width="10" height="10">&nbsp;Please wait';
	document.getElementById("send").value="Checking";
	document.getElementById("send").disabled=true;
	
	if (document.getElementById("liame").value == "") {
		errormess += "E:Mail, ";
	}
	
	if (document.getElementById("name").value == "") {
		errormess += "name, ";
	}
	
	if (document.getElementById("surname").value == "") {
		errormess += "surname, ";
	}
	
	theValue = document.getElementById("liame").value;
	if (alltrim(theValue) !== "") {
		if (!isValidEmail(alltrim(theValue))) {
			errormess2 += " <b>Your E:mail address is not correct.</b>";
		}
	}
	
	if (errormess || errormess2) {
		document.getElementById("raferrormess").innerHTML="Please complete your " + errormess + "to enable us to send your message" + errormess2;
		document.getElementById("rafcartTime").innerHTML=oldmess;
		document.getElementById("send").value="Send";
		document.getElementById("send").disabled=false;
		return;
	}
	if (document.getElementById("fliame").value == "") {
		errormess += "friend's E:Mail, ";
	}
	
	if (document.getElementById("fname").value == "") {
		errormess += "friend's name, ";
	}
	
	if (document.getElementById("fsurname").value == "") {
		errormess += "friend's surname, ";
	}
	
	theValue = document.getElementById("fliame").value;
	if (alltrim(theValue) !== "") {
		if (!isValidEmail(alltrim(theValue))) {
			errormess2 += " <b>Your friend's E:mail address is not correct.</b>";
		}
	}
	
	if (errormess || errormess2) {
		document.getElementById("raferrormess").innerHTML="Please complete your " + errormess + "to enable us to send your message. " + errormess2;
		document.getElementById("rafcartTime").innerHTML=oldmess;
		document.getElementById("send").value="Send";
		document.getElementById("send").disabled=false;
		return;
	}
	document.getElementById("send").value="Sending";
	document.getElementById("raferrormess").innerHTML="Please wait while we send your message";

	var url="rafsend.asp";
	var chkReturnStatus = "";
	var rafrequest=new ajaxRequest();
	var liame = encodeURIComponent(document.getElementById("liame").value);
	var name = encodeURIComponent(document.getElementById("name").value);
	var surname = encodeURIComponent(document.getElementById("surname").value);
	var fliame = encodeURIComponent(document.getElementById("fliame").value);
	var fname = encodeURIComponent(document.getElementById("fname").value);
	var fsurname = encodeURIComponent(document.getElementById("fsurname").value);
	var message = encodeURIComponent(document.getElementById("message").value);
	var raftext = encodeURIComponent(document.getElementById("raftext").value);
	var raftitletext = encodeURIComponent(document.getElementById("raftitletext").value);
	var raflink = encodeURIComponent(document.getElementById("raflink").value);
	var xy = encodeURIComponent(document.getElementById("xy").value);


	var parameters="liame="+liame+"&name="+name+"&surname="+surname+"&fliame="+fliame+"&fname="+fname+"&fsurname="+fsurname+"&message="+message;
	parameters+="&raftext="+raftext+"&raftitletext="+raftitletext+"&raflink="+raflink+"&xy="+xy;
	rafrequest.onreadystatechange=function(){
	 if (rafrequest.readyState==4){
		if (rafrequest.status==200 || window.location.href.indexOf("http")==-1){
			document.getElementById("raferrormess").innerHTML=rafrequest.responseText;
			document.getElementById("rafcartTime").innerHTML=oldmess;
			document.getElementById("send").value="Send";
			document.getElementById("send").disabled=false;
			document.getElementById("fliame").value = "";
			document.getElementById("fname").value = "";
			document.getElementById("fsurname").value = "";
			document.getElementById("message").value = "";

		}
		else{
		 alert("An error has occured trying to send your request. Please call your account manager at Write-The-Web")
		}
	 }
	}
	rafrequest.open("POST", url, true)
	rafrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	rafrequest.send(parameters)
}

//request a product
function chkrap(){
	var errormess = "";
	var errormess2 = "";
	var theValue = "";
	var oldmess = document.getElementById("rapcartTime").innerHTML
	document.getElementById("rapcartTime").innerHTML='<img src="images/wait.gif" width="10" height="10">&nbsp;Please wait';
	document.getElementById("rapsend").value="Checking";
	document.getElementById("rapsend").disabled=true;
	
	if (document.getElementById("rapliame").value == "") {
		errormess += "E:Mail, ";
	}
	
	if (document.getElementById("rapname").value == "") {
		errormess += "name, ";
	}
	
	if (document.getElementById("rapsurname").value == "") {
		errormess += "surname, ";
	}
	
	theValue = document.getElementById("rapliame").value;
	if (alltrim(theValue) !== "") {
		if (!isValidEmail(alltrim(theValue))) {
			errormess2 += " <b>Your E:mail address is not correct.</b>";
		}
	}
	
	if (errormess || errormess2) {
		document.getElementById("raperrormess").innerHTML="Please complete your " + errormess + "to enable us to send your message" + errormess2;
		document.getElementById("rapcartTime").innerHTML=oldmess;
		document.getElementById("rapsend").value="Send";
		document.getElementById("rapsend").disabled=false;
		return;
	}

	document.getElementById("rapsend").value="Sending";
	document.getElementById("raperrormess").innerHTML="Please wait while we send your request.";

	var url="rapsend.asp";
	var chkReturnStatus = "";
	var raprequest=new ajaxRequest();
	var liame = encodeURIComponent(document.getElementById("rapliame").value);
	var name = encodeURIComponent(document.getElementById("rapname").value);
	var surname = encodeURIComponent(document.getElementById("rapsurname").value);
	var message = encodeURIComponent(document.getElementById("rapmessage").value);
	var xy = encodeURIComponent(document.getElementById("rapxy").value);


	var parameters="liame="+liame+"&name="+name+"&surname="+surname+"&message="+message+"&xy="+xy;
	raprequest.onreadystatechange=function(){
	 if (raprequest.readyState==4){
		if (raprequest.status==200 || window.location.href.indexOf("http")==-1){
			document.getElementById("raperrormess").innerHTML=raprequest.responseText;
			document.getElementById("rapcartTime").innerHTML=oldmess;
			document.getElementById("rapsend").value="Send";
			document.getElementById("rapsend").disabled=false;
			document.getElementById("rapliame").value = "";
			document.getElementById("rapname").value = "";
			document.getElementById("rapsurname").value = "";
			document.getElementById("rapmessage").value = "";

		}
		else{
		 alert("An error has occured trying to send your request. Please call your account manager at Write-The-Web")
		}
	 }
	}
	raprequest.open("POST", url, true)
	raprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	raprequest.send(parameters)
}



//Restrict to whole numbers on input	
function isWholeNumber(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8 && unicode!=45){ 
		if (unicode<48||unicode>57) 
		return false 
	}
}

//Restrict to decimal numbers on input	
function isDeclNumber(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){ 
		if (unicode<46||unicode>57) 
		return false 
	}
}

//format to d decimal places
function toDecl(obj,a,d) {
		if (!isNaN(a)) {
		a = a / 1;
		a = a.toFixed(d); 
		document.getElementById(obj).value = a;
	}
}

function alltrim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

var alwayscombo={
location: ["bottom", "right"], 
addoffset: [0, 0], 
comboid: "snlogo", 
navigate:function(){
var selectobj=this.comboref.getElementsByTagName("select")[0]
if (selectobj.options[selectobj.selectedIndex].value!="default")
window.location=selectobj.options[selectobj.selectedIndex].value
},
floatcombo:function(){
var docElement=(document.compatMode=='CSS1Compat')? document.documentElement: document.body
if (this.location[0]=="top")
this.comboref.style.top=0+this.addoffset[0]+"px"
else if (this.location[0]=="bottom")
this.comboref.style.bottom=0+this.addoffset[0]+"px"
if (this.location[1]=="left")
this.comboref.style.left=0+this.addoffset[1]+"px"
else if (this.location[1]=="right")
this.comboref.style.right=0+this.addoffset[1]+"px"
},
init:function(){
this.comboref=document.getElementById(this.comboid)
this.comboref.style.visibility="visible"
this.comboref.style.zIndex="99"
this.floatcombo()
}
}
if (window.addEventListener)
window.addEventListener("load", function(){alwayscombo.init()}, false)
else if (window.attachEvent)
window.attachEvent("onload", function(){alwayscombo.init()})

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
 if (window.ActiveXObject){ 
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
   }
  }
 }
 else if (window.XMLHttpRequest) 
  return new XMLHttpRequest()
 else
  return false
}

function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

