/**
function centerIt(name) {
  var el = document.getElementById(name);
  var w = window.innerWidth ? window.innerWidth : document.body.clientWidth;
  var h = window.innerHeight ? window.innerHeight : document.body.clientHeight;
  var w0 = Math.floor((w - el.offsetWidth) / 2);
  var h0 = Math.floor((h - el.offsetHeight) / 2);
  var lstyle = document.all ? el.style.pixelLeft : el.style.left;
  var tstyle = document.all ? el.style.pixelTop : el.style.top;
  if(document.all) {
	if (document.readyState == 'complete') {
		el.style.pixelLeft = w0;
		//el.style.pixelTop = h0;
	}
  } else {
		el.style.left = w0;
		//el.style.top = h0;
  }
  el.style.visibility='visible';
}
*/
function doRegion(r) {
	var exp = new Date();
	var nowPlusAyear = exp.getTime()+(365*24*60*60*1000);
	exp.setTime(nowPlusAyear);
	var val = "UK";
	if(r=="US") val="US";
	setCookie("The Wren Press region", val, exp);
}
function toggleHomeInfo(name) {
	var tStyle = document.getElementById(name).style;
	var aStyle = document.getElementById("about_us").style;
	
	if(tStyle.display == "none" || tStyle.display == "") { 
		tStyle.display="block";
		aStyle.display = "none";
	} else { 
		tStyle.display = "none";	
		aStyle.display = "block";
	}
} 
function optsPopup(poid) {
		var wurl = "optionsPopup.html?poid="+poid;
		var wfeatures = "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width=800,height=600";
 		var newwin = window.open(wurl, '', wfeatures);
		window.setTimeout(newwin.focus,2*1000); 
		
}
var img = null;
var t = null;
var el = null;
var cl = "";
function imgPopup() {
		var wurl = "zoom.html?img="+img;
		var wfeatures = "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width="+el.width+",height="+el.height;
 		var newwin = window.open(wurl, '', wfeatures);
		window.setTimeout(newwin.focus,2*1000); 
		
}
function linkImgPopup(img) {
		var wurl = "zoom.html?img="+img;
		var wfeatures = "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=350,height=450";
 		var newwin = window.open(wurl, '', wfeatures);
		window.setTimeout(newwin.focus,2*1000); 
}

function imgPopup2(ev) {
    var ev  = (ev) ? ev : ((event) ? event : null);
    if(!ev) return;
    var node = (ev.target) ? ev.target : ((ev.srcElement) ? ev.srcElement : null);
    if(!node || node.tagName != 'IMG') return;
    var nsrc  = node.src;
    if(nsrc.indexOf('edit.gif') != -1) return;
    if(!nsrc || nsrc=="") return; 
    var Lsrc = nsrc.substring(0,nsrc.length-5)+"L"+nsrc.substring(nsrc.length-4);
    document.getElementById("hid").src=Lsrc; 
    img = Lsrc;
    el = document.getElementById('hid');
    if(!el || !img) return;
    window.setTimeout(imgPopup,2*1000);
}
function setCookie(name, value, expires) {
	document.cookie = name + "=" + escape(value) + ";path=/" + ((expires == null ? "" : ";expires=" + expires.toGMTString()));
}
function delCookie(name) {
	document.cookie = name + "=;expires=Thu, 01-Jan-00 00:00:01 GMT" + "; path=/";
}
function getCookie(name) {
	var cname = name + "=";
	var dc = document.cookie;
	if(dc.length > 0) {
		begin = dc.indexOf(cname);
		if(begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if(end == -1) end = dc.length;
			return unescape(dc.substring(begin, end));
			
		}
	}
	return null;
}
function manStorePopup() {
		var wurl = "man_store.html";
		var wfeatures = "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";
 		var newwin = window.open(wurl, 'store', wfeatures);
		newwin.focus(); 
}
function userSitePopup() {
		var wurl = "default.html";
		var wfeatures = "";
 		var newwin = window.open(wurl, 'usersite', wfeatures);
		newwin.focus(); 
}
function manProdOptsPopup(pid) {
		var wurl = "man_options.html?pid="+pid;
		var wfeatures = "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=yes,copyhistory=no";
 		var newwin = window.open(wurl, 'opts', wfeatures);
		newwin.focus(); 
}
function cookiePopup() {
		var wurl = "cookies.html";
		var wfeatures = "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=400,Height=400";
 		var newwin = window.open(wurl, 'cookies', wfeatures);
		newwin.focus(); 
}

function hideBasketAlert() {
	document.getElementById("ba").className="basketAlert";
}
function hideInfoAlert() {
	document.getElementById("ia").className="basketAlert";
}
function hideCookieAlert() {
	document.getElementById("nc").className="basketAlert";
}
function checkCookie() {
	var test = getCookie("TESTCOOKIE");
	//delete cookie used for test purposes
	//delCookie("info request");
	if(test.length < 1) {
		document.getElementById("nc").className="basketAlertShow";
     		setTimeout(cookiePopup,3*1000);
     		setTimeout(hideCookieAlert,3*1000);
     		return false;
       } else return true;
}
function validateOpts() {
    var ops=0;
    var err = "";
    var fl = document.forms["prod"].elements.length;
    var fname="";
    var isfull="";
    for(var f = 0; f < fl; f++) {
		var field = document.forms["prod"].elements[f]; 
		if(field.name.toUpperCase() == "PID" || field.name.toUpperCase() == "CPID") continue;
		ops+= 1;
		if(fname != field.name) {
			if(isfull !="*" && fname != "") err +="Please choose: "+fname+"\n";
			fname=field.name;
			isfull="";
		}
		if(field.type == 'radio' || field.type == 'checkbox') {
			if(field.checked) 
				isfull = "*";
		} else 
			if(field.value.length > 0) isfull = "*";
				
		if(fname.indexOf("optional extra : ") > -1) { 
			if(!field.checked){
				var newname = document.forms["prod"].elements[f+1].name;
				checkOEopts(field,newname);
				fname =  newname
			}
			isfull="*";
		}
		
    }
    if(isfull !="*") err += "Please choose: "+fname+"\n";
    
	if(err != "" && ops > 0)  
		alert(err); 
	else {
		if(ops<1 || (err == "" && ops > 0)) 
    		addBasket(); 
	}
}

function getProdOptions() {
    var ops="";
    var fl = document.forms["prod"].elements.length;
    for(var f = 0; f < fl; f++) {
		var field = document.forms["prod"].elements[f]; 
		if(field.name.toUpperCase() == "PID" || field.name.toUpperCase() == "CPID") continue;

		if(field.type == 'radio' || field.type == 'checkbox') {
			if(field.checked) 
				ops+= field.name.toUpperCase()+"-"+field.value+"++";
		} else {
			if(field.value.length > 0)
				ops+=field.name.toUpperCase()+"-"+field.value+"++";	
		}
    }
    return ops;
}

function addBasket() {
     if(checkCookie()) {
	var numberRegExp = new RegExp("^[1-9][0-9]*$");
	var exp = new Date();
	var nowPlusAyear = exp.getTime()+(365*24*60*60*1000);
	exp.setTime(nowPlusAyear);
	var pid = document.forms["basket"].pid.value;
	var cpid = document.forms["basket"].cpid.value;
	var q = document.forms["basket"].quant.value;
	if(!numberRegExp.test(q)) {
		alert('Please enter a number. The minimum ammount is 1.');
		document.forms["basket"].quant.value = 1;
		return false;
	}
	var name = document.forms["basket"].sname.value+" basket";
	var val = getCookie(name);
	var bIsDone="N";
	var nval="";
	if(val != null) {
		var a = val.split('$$'); 
		for(var i = 0; i < a.length; i++) {
			if(!a[i]) continue;
			
			var npid = a[i].substring(0,a[i].indexOf("Q"));
			npid = npid.substring(npid.indexOf(":")+1);
			
			if(npid != pid) { 
				nval += a[i]+"$$";
			} else if(npid == pid) { 
				var nq = a[i].substring(a[i].indexOf("Q")+2);
				nq = nq.substring(0,nq.indexOf("CPID"));
				var opts = a[i].substring(a[i].indexOf("OPTIONS")+8);
				var re = /(\s+)/g; 
				var cops = opts.replace(re,""); 
				var  nops = getProdOptions().replace(re,"");
				if(nops==cops) {
					nq = nq*1;
					q = q*1;
					var xtra = nq+q;
					nval += "PID:"+pid+"Q:"+xtra+"CPID:"+cpid+"OPTIONS:"+opts+"$$";
					bIsDone="Y";
				} else
					nval += a[i]+"$$";	
				
			}
		} 	
	}
	if(bIsDone == "N") nval += "PID:"+pid+"Q:"+q+"CPID:"+cpid+"OPTIONS:"+getProdOptions()+"$$";
	//alert(nval);
	setCookie(name, nval, exp);
	document.forms["balert"].aquant.value = q;
	document.getElementById("ba").className="basketAlertShow";
	var btval = document.forms["btalert"].bquant.value;
	btval = btval*1;
	var addval = document.forms["basket"].quant.value;	
	addval = addval*1
	btval = btval+addval;
	document.forms["btalert"].bquant.value = btval;
	setTimeout(hideBasketAlert,2*1000);
    }
}

function round (n) {
	//give numbe 2 decimal places
  	n = n - 0;
  	var d = 2;
  	var f = Math.pow(10, d);
  	n = Math.round(n * f) / f;
  	n += Math.pow(10, - (d + 1));
  	n += '';
  	return d == 0 ? n.substring(0, n.indexOf('.')) :
      			n.substring(0, n.indexOf('.') + d + 1);
}

function redoTotal(form, end) {
	var numberRegExp = new RegExp("^[1-9][0-9]*$");
	var t = 0;
	var qPrefix = "Q";
	var pPrefix = "P";
	var tPrefix = "T";
    	for (var i = 0; i <= end-1; i++) {
      		var v = parseFloat(form[qPrefix + i].value);
      		if(isNaN(v) || !numberRegExp.test(v)) {
			v=0;
			form[qPrefix + i].value=v;
		}
      		v = v * parseFloat(form[pPrefix + i].value);
      		form[tPrefix + i].value =round(v);
      		t += v ? v : 0;
    	}
  	form["tot"].value = round(t);
} 
function resetCookie(form,end,item) {
	var numberRegExp = new RegExp("^[1-9][0-9]*$");
	var pidPrefix = "PID";
	var qPrefix = "Q";
	var cpidPrefix = "CPID";
	var opPrefix = "OPTIONS";
	var nval="";
	for (var i = 0; i <= end-1; i++) {
		if(i==parseFloat(item)) continue;
		var v = parseFloat(form[qPrefix + i].value);
		if(isNaN(v) || !numberRegExp.test(v)) {
			v=0;
			form[qPrefix + i].value=v;
		}
		nval += "PID:"+form[pidPrefix + i].value+"Q:"+v+"CPID:"+form[cpidPrefix + i].value+"OPTIONS:"+form[opPrefix + i].value+"$$";
	}
	var name = document.forms[0].sname.value+" basket";
	var exp = new Date();
	var nowPlusAyear = exp.getTime()+(365*24*60*60*1000);
	exp.setTime(nowPlusAyear);
	//alert(nval);
	setCookie(name, nval, exp);
	document.getElementById("ba").className="basketAlertShow";
	setTimeout(hideBasketAlert,2*1000);

}
function doVAT(val,f) {
	f.value = val;
} 
function resetInfoCookie(form,end,item) {
	var pidPrefix = "PID";
	var nval="";
	for (var i = 0; i <= end-1; i++) {
		if(i==parseFloat(item)) continue;
		nval += form[pidPrefix + i].value;
	}
	var name = "info request";
	var exp = new Date();
	var nowPlusAyear = exp.getTime()+(365*24*60*60*1000);
	exp.setTime(nowPlusAyear);
	//alert(nval);
	setCookie(name, nval, exp);
}
function removeItem(form,end,item) {
	resetCookie(form,end,item);
	window.location="basket.html";	
}
function removeInfo(form,end,item) {
	resetInfoCookie(form,end,item);
	window.location="info.html";	
}
function addInfo() {
     if(checkCookie()) {
     	document.forms["info"].popts.value = getProdOptions();
     	document.forms["info"].submit();
	/**var exp = new Date();
	var nowPlusAyear = exp.getTime()+(365*24*60*60*1000);
	exp.setTime(nowPlusAyear);
	var pid = document.forms["info"].pid.value;
	var cpid = document.forms["info"].cpid.value;
	var name = "info request";
	var val = getCookie(name);
	
	var nval="";
	if(val != null) nval+=val;
	nval += "PID:"+pid+"CPID:"+cpid+"OPTIONS:"+getProdOptions()+"$$";
	//alert(nval);
	setCookie(name, nval, exp);
	document.getElementById("ia").className="basketAlertShow";
	var ival = document.forms["ialert"].iquant.value;
	ival = ival*1;
	ival = ival+1;
	document.forms["ialert"].iquant.value = ival;
	setTimeout(hideInfoAlert,2*1000);*/
    }
}
function checkOEopts(e,el) {
	var fl = document.forms["prod"].elements.length;
	if(e.checked == true) { 
		alert("Please ensure you choose a "+ el);
		for(var f = 0; f < fl; f++) {
			var field = document.forms["prod"].elements[f];
			if(field.name==el)
			field.style.visibility = "visible";
		}
		return;
	}else {
		
		for(var f = 0; f < fl; f++) {
			var field = document.forms["prod"].elements[f]; 
			if(field.checked && field.name==el)  
				field.checked = false;
			if(field.name==el)
			field.style.visibility = "hidden";
		}
	}
}