function clearsearch(thisfield, defval, leaving) {
	if(thisfield.value==defval && !leaving) thisfield.value='';
	else { if(thisfield.value==''&&leaving) thisfield.value=defval; }
}
function searchcheck(thisform, defval, checkfield) {
	if(checkfield.value==defval) checkfield.value = '';
	return true; 
}
// 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+"");}
			}
		}
}

// Whereas this one allows the month to be a hidden or text field for year only choices
function setdateyr(frm,datefld) {
	var thisobj = ("document." + frm);
	var thisday = eval(thisobj + "." + datefld + "1.value");
	var thismon = eval(thisobj+"."+datefld+"2.value");
	if (thismon.length==1) thismon=("0" + thismon);
	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
}

// setzero is to turn a text string (which is a number) into a number with two padded decimal places
function setzero(sz_num) {
	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 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 showhide(id) { 
	obj = document.getElementById(id); 
	if (obj.style.display == "none") obj.style.display = ""; else obj.style.display = "none"; 
} 

function showhidediv(id, istate) { 
	obj = document.getElementById(id); 
	if (istate=="show" || (obj.style.display == "none" && istate=="swap")) obj.style.display = "block"; else obj.style.display = "none"; 
} 

function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
       if(document.layers[szDivID].visibility=="show"||iState==0) 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"||iState==0) obj.style.visibility = "hidden"; else obj.style.visibility = "visible";
    }
    else if(document.all)	// IE 4
        if(document.all[szDivID].style.visibility=="visible"||iState==0) 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;
}

function logincheck(thisform) {
	var msg = "";
	if((thisform.Password.value == "")&&(thisform.reminder.value=="")) { msg += "  - you have not entered a password\n" }
	// email validation
	var thise = thisform.Username ; var emsg = "  - Please ensure a valid email address is entered for your username\n"
	s = thise.value; s = s.replace(",","."); s = s.replace(" ",""); thise.value = s;
	if((s=="")||(s.indexOf('@')<=1)||(s.indexOf('.')<=0)) msg += emsg ;	
	else { if((s=="")||(s.length<6)) msg+=emsg ; else { var theStr=s; var index=theStr.indexOf("@");
	if (index>0) {var pindex=theStr.indexOf(".",index);	if (!((pindex>index+1) && (theStr.length > pindex+1))) msg += emsg ; }	else msg += emsg ; } }
	var success=(msg=="");
	if(success==false) alert("Sorry, but you have not yet filled in all of the required fields:\n\n"+msg);
	return success; 
}
function swapdivs(divstem, divno, total) { showhidediv(divstem+divno, "show"); for(i=1;i<=total;i++) { if(i!=divno) showhidediv(divstem+i, ""); } }

function switchdiv(block, imgstem, fp) {
	if(fp==undefined) fp="";
	thisimg = document.getElementById(block + 'img'); if(imgstem == undefined) imgstem = "";
	if(layervisibility(block)) { showhidediv(block, 'hide'); thisimg.src=fp + 'images/icons/plus'+imgstem+'.gif'; } 
	else { showhidediv(block, 'show'); thisimg.src=fp + 'images/icons/minus'+imgstem+'.gif'; }
}
function layervisibility(layerid) {
	 if(document.layers)	return (document.layers[layerid].visibility == "show")
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    { return (document.getElementById(layerid).style.visibility == "visible" || document.getElementById(layerid).style.display != "none") }
    else if(document.all)	// IE 4
        return (document.all[layerid].style.visibility=="visible")
}

function getotherevents(urn,cnt,gig,div,fullp,showhide) {
	document.getElementById(div).innerHTML='<h2 align="center">Loading ...</h2>';
	if(showhide==undefined) showhide=1;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } 
	xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4) { document.getElementById(div).innerHTML=xmlHttp.responseText } }
	if(div=="othergigs") ajaxpage = "more-gigs"; else ajaxpage = "more-events"
	xmlHttp.open("GET",fullp+"inc/" + ajaxpage + ".asp?x="+gig+"|"+urn+"|"+cnt+"|"+showhide+"|"+new Date().getTime(),true);
	xmlHttp.send(null);
} 
function getotherreviews(urn, cnt, rurn, div, fullp, showhide) {
	document.getElementById(div).innerHTML='<h2 align="center">Loading ...</h2>';
	if(showhide==undefined) showhide=1;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } 
	xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4) { document.getElementById(div).innerHTML=xmlHttp.responseText } }
	ajaxpage = "more-venue-reviews"
	xmlHttp.open("GET",fullp+"inc/" + ajaxpage + ".asp?x="+rurn+"|"+urn+"|"+cnt+"|"+showhide+"|"+new Date().getTime(),true);
	xmlHttp.send(null);
} 

function getajax(div, atype, fullp, thispage, srch) {
	document.getElementById(div).innerHTML='<img src="' + fullp + 'images/icons/loading.gif" />';
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } 
	xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4) { document.getElementById(div).innerHTML=xmlHttp.responseText; } }
	xmlHttp.open("GET", thispage + "?ajax="+div+"|"+atype+"|"+fullp+"|"+thispage+"|"+srch+"|"+new Date().getTime(),true);
	xmlHttp.send(null);
} 
