function highlight(cell) { cell.bgColor = '#000000'; }
function revert(cell)    { cell.bgColor = '#31416B'; }

// Checkjob fires when the focus is taken off the job title field
// It checks to see if this is a new job title, so resets the urn to 0
function checkjob(thisval,pre) {
  var checkval = eval("document.addemp." + pre + "name.value")
  if (checkval!=thisval)
	{
  	eval("document.addemp." + pre + "urn.value = '0'");
  	eval("document.addemp." + pre + "name.value = '" + thisval + "'");
	}
}

// Setjob splits the value of the job select box and sets the other fields
function setjob(thisobj,pre) {
  var thisstr = thisobj.options[thisobj.selectedIndex].value;
  var x = thisstr.split("~");
  eval("document.addemp." + pre + "urn.value = x[0]");
  eval("document.addemp." + pre + "name.value = x[1]");
  eval("document.addemp." + pre + "named.value = x[1]");
}

// setdate is to create a SQL server date from a series of 2 text boxes and a select menu
// All fields use the same stem for naming to facilitate the generic nature of the function
function setdate(frm,datefld) {
	var thisobj = ("document." + frm);
	var thisday = eval(thisobj + "." + datefld + "1.value");
	var thismon = eval(thisobj+"."+datefld+"2.options["+thisobj+"."+datefld+"2.selectedIndex].value");
	var thisyea = eval(thisobj + "." + datefld + "3.value");
	// irrespective of the rest of the date values, update the year to a 4 figure year
  	if ((thisyea<=100)&&(isNaN(parseInt(thisyea))==false))
		{if(thisyea>=31)
			{thisyea=(1900 + parseInt(thisyea));}
		else
			{thisyea=(2000 + parseInt(thisyea));}
		 eval(thisobj + "." + datefld + "3.value="+thisyea);
		}

	if (isNaN(parseInt(thisday))||thismon==0||isNaN(parseInt(thisyea)))
		{ eval(thisobj+"."+datefld+".value=''");}
	else
		{ 
		  if (thisday.length==1) thisday=("0" + thisday);
		  if (isDate(""+thisyea+thismon+thisday+"")==false) {
			alert(" "+thisday+"/"+thismon+"/"+thisyea+" is not a valid date");
			eval(thisobj+"."+datefld+".value=''");
			}
		  else  {var thisDate = new Date();
			if((datefld=="empDob")&&((thisDate.getFullYear()-thisyea)<15))
				{alert("Sorry, but that would make the age less than 16 - please try again");}
			else {thisfld = eval(thisobj+"."+datefld);thisfld.value=(""+thisyea+thismon+thisday+"");}
			}
		}
}

function isDate(dateStr) {
    month = dateStr.substring(4,6);
    day = dateStr.substring(6,8);
    year = dateStr.substring(0,4);
    if (day < 1 || day > 31) { return false;}
    if ((month==4 || month==6 || month==9 || month==11) && day>=31) {return false;}
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {return false;}
    }
    return true; // date is valid
}

function clearsearch(thisfield) {
	if(thisfield.value=='Enter keywords here') thisfield.value='';
	else {
		if(thisfield.value=='') thisfield.value='Enter keywords here';
		}
}

// Validate form
function searchform(thisform) {
	if ((thisform.searchtext.value=='')||(thisform.searchtext.value=='Enter keywords here')){
		alert("Sorry, but you haven't entered any search keywords!"); return false; } else return true;
}

// setzero is to turn a text string (which is a number) into a number with two padded decimal places
function setzero(sz_num) {
	if(sz_num=='') return "0.00"
	sz_num = "" + sz_num;
	sz_num = sz_num.replace(",","");
	if(isNaN(sz_num)) sz_num = "0" 
	sz_num = parseFloat(sz_num);
	sz_num = Math.round(sz_num*100)/100;
	sz_num = "" + sz_num;
	var sz_dotpos = sz_num.indexOf('.');
	if (sz_dotpos < 0) sz_num+=".00";
	if (sz_dotpos==(sz_num.length-1)) sz_num+="00";
	if (sz_dotpos==(sz_num.length-2)) sz_num+="0";
	return sz_num ;
}

function prod_details(a) {
	desc = ''; price = a[2]; perm_urn = 0; rrp = ''; partno = ''; del = '';
	if(a[3]=='F') {desc = 'Sorry, but this product is currently unavailable'; price='N/A';}
	else { 
		if(parseFloat(a[6])>0) del = currsign + setzero(parseFloat(a[6])); else del = 'Free';
		if(a[4]=='0') desc = 'Sorry, but this product is currently out of stock';
		else { // in stock and active
			if(a[5]=='T') desc+='Sale Price - with ' + a[8] + ' : was ' + currsign + a[7];
			if(parseFloat(a[9])>parseFloat(a[2])) rrp = 'Save ' + currsign + setzero(parseFloat(a[9])-parseFloat(a[2])) + ' on the RRP'
			if(!a[1]=='') partno='Part no: ' + a[1] 
			perm_urn = a[0]
		}
	}
	document.productform.status_price.value=price; document.productform.status_info.value=desc; document.productform.perm_urn.value=perm_urn; document.productform.status_rrp.value=rrp; document.productform.status_partno.value=partno; document.productform.delivery.value=del;
	//if (document.layers) { document.layers.product_info.document.write(desc); document.layers.product_info.document.close(); }
	//else { if (document.all) product_info.innerHTML = desc; }
}

function addtickets(cnt,available,add,current) {
	newtotal = parseInt(current) + parseInt(add)
	if(newtotal<0 || newtotal > available) return
	eval("document.productform.Quantity" + cnt + ".value=" + newtotal)
	setprice(cnt, eval("document.productform.ticketprice" + cnt +".value"), newtotal)
}
function checkquantity(cnt,available,current) {
	newtotal = parseInt(setzero(current))
	if(newtotal<0) newtotal = 0
	if(newtotal>available) newtotal = available
	eval("document.productform.Quantity" + cnt + ".value="+newtotal)
	setprice(cnt, eval("document.productform.ticketprice" + cnt +".value"), newtotal)
}
function setprice(cnt,price,qty) {
	if(qty>0) eval("document.productform.totalprice"+cnt+".value='£"+setzero(parseFloat(price)*parseInt(qty))+"'")
	else eval("document.productform.totalprice"+cnt+".value=''")
}
function getSelectedRadio(buttonGroup) { // returns the array number of the selected radio button or -1 if no button is selected
	if (buttonGroup[0]) { for (var i=0; i<buttonGroup.length; i++) if (buttonGroup[i].checked) return i } 
	else {if (buttonGroup.checked) return 0;} return -1; }

function getSelectedRadioValue(buttonGroup) { // returns the value of the selected radio button or "" if no button is selected
	var i = getSelectedRadio(buttonGroup);
	if (i == -1) return ""; else {if (buttonGroup[i]) return buttonGroup[i].value; else return buttonGroup.value; }}

function handlecheck(thisval) { if(thisval!="") window.open('forum/handlecheck.asp?h='+thisval,'','width=250,height=150,toolbar=no,location=no,menubar=no,scrollbars=no,resizable=no'); else alert("Sorry, but you have not entered a forum name to check")}
function testlink(thisval) { if(thisval!=""&&thisval!="http://") window.open(thisval,'linktest','toolbar=yes,location=yes,menubar=yes,scrollbars=yes,resizable=yes'); else alert("Sorry, but you have not entered an address to test")}

function showhide(id) { 
	obj = document.getElementById(id); 
	if (obj.style.display == "none") obj.style.display = ""; else obj.style.display = "none"; 
} 
function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
       if(document.layers[szDivID].visibility == "show") document.layers[szDivID].visibility = "hide"; else document.layers[szDivID].visibility = "show";
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        if(obj.style.visibility == "visible") obj.style.visibility = "hidden"; else obj.style.visibility = "visible";
    }
    else if(document.all)	// IE 4
        if(document.all[szDivID].style.visibility=="visible") document.all[szDivID].style.visibility = "hidden"; else document.all[szDivID].style.visibility = "visible";
		 showhide(szDivID)
}

// Ajax functions
function GetXmlHttpObject() {
	var xmlHttp=null;
	try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari
	catch (e) { // Internet Explorer
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	return xmlHttp;
}
