// JavaScript Document
var G_IamExplorer=(navigator.appName == "Microsoft Internet Explorer");	//sadly, there are one or two minor browser differences we may need to check for.
var selbox_align=new Array();

if(!window.G_imgdir) {var G_imgdir='';}

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_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_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 clearDefault(el){
  if (el.defaultValue==el.value) el.value = ""
}

MM_preloadImages(G_imgdir+'images/logout.gif');
function HdrLogin(){
	//SDC, 30-Jan-2006: Trigger login/logout popup form.
	var wi=350;
	var hi=250;
	var base = document.baseURI || document.URL;
	var spliturl=base.split(/\//);
	var popupurl='loginpopup.php';
// 	if(spliturl[0]!='https:') { popupurl='https://'+spliturl[2]+'/'+popupurl; } //the login form must open in https, whether we're already on an https page or not.

	pagecc_custform = window.open(popupurl+'?hdrmode=1'+(G_loggedin?'&logout=1':''), 'cc_login', 'width='+wi+',height='+hi+',left='+((screen.width-wi)/2)+',top='+((screen.height-(hi*2))/2)+',toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=0');
	G_LoginOffered=true;
	if(document.getElementById('leftBoxSignup')) {
		document.getElementById('leftBoxSignup').style.display = 'block';
	}

}

function HdrLoginFlip(loggedin,userstring) {
	G_loggedin=loggedin;
	//Change the login button to a logout button and vice-versa.
	if (document.getElementById("header_login_button")){document.getElementById("header_login_button").src=(loggedin?G_imgdir+"images/logout.gif":G_imgdir+"images/login.gif");}
	if (document.getElementById("header_login_link"))  {document.getElementById("header_login_link").innerHTML=loggedin?"Logout":"Login"; }

	//also change the text under it to show login info if applicable.
	if (document.getElementById('lpd_showuser')) { document.getElementById('lpd_showuser').innerHTML="Logged in"+(userstring?(" as "+userstring):""); }
	if (document.getElementById('lpd_showuser')) { document.getElementById('lpd_showuser').style.visibility=loggedin?"":"hidden"; }
	if (document.getElementById('lpd_showuser')) { document.getElementById('lpd_showuser').style.display=loggedin?"":"none"; }
	if (document.getElementById('lpd_lostlogin')){ document.getElementById('lpd_lostlogin').style.visibility=loggedin?"hidden":""; }
	if (document.getElementById('lpd_lostlogin')){ document.getElementById('lpd_lostlogin').style.display=loggedin?"none":""; }
	
	//-DCK:Refresh prices after login -->Czech Republic
	try{ 
        if (currpage=="1" || currpage=="2" || currpage=="3"){ReloadCurrentNums(); }
    }
    catch(e){}
	//+
	
}

var G_LoginFormAvail=false;	//this is used when you click the login/logout button to determine whether to pop up a login box, or whether the current page already has a login form on it to display.

function hideSignupButton() {
	if(document.getElementById('leftBoxSignup')){
		document.getElementById('leftBoxSignup').style.display = 'none';
	}
}

function SubmitPromoCode(promo) {
	if(!promo || promo=="Promo Code") {return false;}
	var apicall="promocode.php?promo="+promo;
	apilite_call(apicall, 'ShowPromo', promo, 'ShowPromo', '');
}

function ShowPromo(response,promo) {
//console.log(response,promo);
	var pf=document.getElementById('promo_form');
	var ps=document.getElementById('promo_show');
	var pc=document.getElementById('promo_code');

	if(pf) {
		pf.style.display=(promo?'none':'');
		ps.style.display=(promo?'':'none');
		pc.innerHTML=response;
	}
	if(promo && ReloadCurrentNums) {ReloadCurrentNums();}

	if(promo) {
		alert("Thank you. Your promo code is now active.");
	} else {
		alert("Sorry, the promo code you specified is invalid.");
	}
}

//-----------------------------------------------------------------------

function HighestInArray(arr) {
	var max=arr[0];
	for(var ctr in arr) { if(arr[ctr]>max) {max=arr[ctr];} }
	return max;
}

function LowestInArray(arr) {
	var min=arr[0];
	for(var ctr in arr) { if(arr[ctr]<min) {min=arr[ctr];} }
	return min;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function pausecomp(millis) {
	//courtesy of sean.co.uk. it's a bit naughty, but setTimer() wasn't what I needed here.
	var date = new Date();
	var curDate = null;
 
	do { curDate = new Date(); }
	while(curDate-date < millis);
}

function LeadingZeros(num,len) {
	num=num.toString();
	while(num.length<len) { num="0"+num; }
	return num;
}

function SecsToMins(secs) {
	secs=parseInt(secs);
	if(isNaN(secs)) {secs=0;}
	return Math.floor(secs/60)+':'+((secs%60)<10?'0':'')+(secs%60);
}

function GetStyle(stylename,key) {
	var styles = document.styleSheets;
	var thisstyle=new Object;
	var stycount=0;
	var rulcount=0;
	for(stycount=0;stycount<styles.length;stycount++) {
		for(rulcount=0;rulcount<styles[stycount].cssRules.length;rulcount++) {
			thisstyle=styles[stycount].cssRules[rulcount];
			if(thisstyle.selectorText==stylename) {
				return thisstyle.style[key];
			}
		}
	}
}

function SetStyle(stylename,key,value) {
	var styles = document.styleSheets;
	var thisstyle=new Object;
	var thisrules=new Object;
	var stycount=0;
	var rulcount=0;
	var rlen=0;
	for(stycount=0;stycount<styles.length;stycount++) {
		try {
			thisrules=styles[stycount].cssRules;
			rlen=thisrules.length;
		} catch(e) {
			thisrules=styles[stycount].rules;
			rlen=thisrules.length;
		}
		for(rulcount=0;rulcount<rlen;rulcount++) {
			thisstyle=thisrules[rulcount];
			if(thisstyle.selectorText==stylename) {
				thisstyle.style[key]=value;
				return;
			}
		}
	}
}

function MSIE_MsgBox(text) {
	//MSIE-specific hack for alert()s and confirm()s. If we don't do this, MSIE will interpret the ampersand as a hotkey underline for the next character. Other browsers don't do this.
	if(G_IamExplorer) {text=text.replace(/&/g,"&&");}
	return text;
}

function ShowPounds(pence,mode) {
	var symbol='';
	if(mode=='html') {symbol='&#163;';}
	if(mode=='jsunicode') {symbol='\u00A3';}
	return symbol+' '+(pence/100).toFixed(2);
}

function valid_email(fieldValue) {
	regexp = /^([-A-Z0-9_]+(\.[-A-Z0-9_]+)?)\@([-A-Z0-9\.]+\.[A-Z]+(\.[A-Z]+)?)$/i
	return(fieldValue.match(regexp));
}

function AutoLogout() {
	//Called when a management function returns a login error.
	document.body.style.cursor = "default";
	LoginLogout(1);
	document.getElementById("loginmsg").innerHTML='Sorry, but your login has timed out. You will need to log in again to continue.';
	document.getElementById("loggedininfo").innerHTML="";
	return;
}

