function getObj(object){
    if (document.getElementById && document.getElementById(object) != null)
         return document.getElementById(object);
    else if (document.layers && document.layers[object] != null)
        return document.layers[object];
    else if (document.all)
        return document.all[object];
	else
		return false;
}
function switchDates(id,frm) {
	var tfrm 	= eval(frm);
 	var tstr 	= tfrm.ehrhash.value;
	var sArray 	= tstr.split("|");
 	for( i=0; i < sArray.length; i++){
 		tid=sArray[i]
 		ctable = getObj('tlevel_'+tid);
 		if( tid == id ){
 			ctable.style.display='inline';
 		}else{
 			ctable.style.display='none';
 		}
 	}
}
function switchDatesSelect(id,frm) {
	var tfrm 	= eval(frm);
 	var tstr 	= tfrm.ehrhash.value;
	var sArray 	= tstr.split("|");
 	for( i=0; i < sArray.length; i++){
 		tid=sArray[i]
 		in_select 	= getObj('checkin_level_'+tid);
 		out_select 	= getObj('checkout_level_'+tid);
 		if( tid == id ){
 			in_select.style.display='inline';
 			out_select.style.display='inline';
 		}else{
 			in_select.style.display='none';
 			out_select.style.display='none';
 		}
 	}
}

function switchPaymentType(id,index) {
	document.getElementById("payment").innerHTML = index;
	if( id != "CC" ) {
		document.getElementById("expdate").style.display = "none";
	} else {
		document.getElementById("expdate").style.display = "";
	}
}

function switchTeams(id,frm) {
	var tfrm 	= eval(frm);
 	var tstr 	= tfrm.sportidarray.value;
	var sArray 	= tstr.split("|");
 	for( i=0; i < sArray.length; i++){
 		tid=sArray[i]
 		ctable = getObj('tlevel_'+tid);
 		if( tid == id ){
 			ctable.style.display='inline';
 		}else{
 			ctable.style.display='none';
 		}
 	}
}
function fillCost(id)
{

	var init_element 	= document.all.item("date_init_id").value;
	var copyfrom 		= document.all.item("costid[" + id + "][" + init_element + "]");
	var dates			= document.all.item("date_array_id").value;
	var dArray 	= dates.split("|");
 	for( i=0; i < dArray.length; i++){
		tid=dArray[i]
		ctable = document.all.item("costid[" + id + "][" + tid + "]");
		ctable.value = copyfrom.value;
	}
}

function fillQuantity(id)
{
	var init_element 	= document.all.item("date_init_id").value;
	var copyfrom 		= document.all.item("quantityid[" + id + "][" + init_element + "]");
	var dates			= document.all.item("date_array_id").value;
	var dArray 	= dates.split("|");
 	for( i=0; i < dArray.length; i++){
		tid=dArray[i]
		ctable = document.all.item("quantityid[" + id + "][" + tid + "]");
		ctable.value = copyfrom.value;
	}
}
function formatAsMoney(obj) {
	var money = obj.value;
	obj.value = int2money(money)
}

function int2money(mnt)
{
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00'
              : ( (mnt*10 == Math.floor(mnt*10)) ?
                       mnt + '0' : mnt);
}
function checkForNumbers(id)
{

	var init_element 	= document.all.item("date_init_id").value;
	var dates			= document.all.item("date_array_id").value;
	var dArray 	= dates.split("|");

	thisCNum = document.all.item("costid[" + id + "][" + init_element + "]");
	if( isNaN(thisCNum.value) ){
		alert ('Field requires numbers only.');
		thisCNum.focus();
		thisCNum.select();
		return (false);
	}

	thisNum = document.all.item("quantityid[" + id + "][" + init_element + "]");
	if( isNaN(thisNum.value) ){
		alert ('Field requires numbers only.');
		thisNum.focus();
		thisNum.select();
		return (false);
	}
 	for( i=0; i < dArray.length; i++){
		tid=dArray[i]

		thisCNum = document.all.item("costid[" + id + "][" + tid + "]");
		if( isNaN(thisCNum.value) ){
			alert ('Field requires numbers only.');
			thisCNum.focus();
			thisCNum.select();
			return (false);
		}

		thisNum = document.all.item("quantityid[" + id + "][" + tid + "]");
		if( isNaN(thisNum.value) ){
			alert ('Field requires numbers only.');
			thisNum.focus();
			thisNum.select();
			return (false);
		}

	}
}
function getObjs(name)
{
	if( document.all.item(name) ){
		return document.all.item(name);
	}else{
		return false
	}
}

function updateRoomDescription(id)
{
	rtdesc = document.all.item("room_desc_" + id).value;
	document.all.item("room_description").value = rtdesc;
}
function popHelp(URL) {
	id = 'wsthelp'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=320');");
}
function popDay(URL) {
	id = 'wstadmin'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=300,height=320');");
}
function popUp(URL) {
	id = 'wstadmin'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=750,height=500');");
}
function pop(URL,W,H) {
	id = 'wstlog'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=" + W + ",height=" + H + "');");
}
function popLog(URL) {
	id = 'wstlog'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=300');");
}
function popHotel(URL) {
	id = 'wstlog'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=550,height=300');");
}

function popMail(URL) {
	id = 'wstMail'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=300');");
}
function popProgress(URL) {
	id = 'wstProgress'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=250');");
}
function popGrid(URL) {
	id = 'wstMail'
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=650,height=350');");
}
function goToSelected(url)
{
	window.location=url;
}
function checkAllSports()
{
	var tfrm 	= eval(document.thisForm);
 	var tstr 	= tfrm.sportidarray.value;
	var sArray 	= tstr.split("|");

 	for( i=0; i < sArray.length; i++){
 		tid=sArray[i]
 		ctable = eval( 'document.all.tlevel_'+tid);
 		csport = eval( 'document.all.wstspid_'+tid);

 		if( csport.checked ){
 			ctable.style.display='inline';
 		}else{
 			ctable.style.display='none';
 		}
 	}
}
function checkAllLevels(sportid)
{
 	var hashval 	= document.all.item("sport_level_hash_" + sportid).value;
	var is_all_chk  = document.all.item("is_all_checked_" + sportid);
	var is_ignored  = document.all.item("ignore_" + sportid);
	var hArray 		= hashval.split("|");
 	if( is_ignored.checked ){

	}else{
	 	if( is_all_chk.value == 3 ){
		 	for( i=0; i < hArray.length; i++){
		 		tid=hArray[i]
		 		ccheck = eval( 'document.all.splid_'+tid);
		 		if( ccheck ){
					ccheck.checked = false;
		 		}
		 	}
			is_all_chk.value = 2;
	 	} else {
		 	for( i=0; i < hArray.length; i++){
		 		tid=hArray[i]
		 		ccheck = eval( 'document.all.splid_'+tid);
		 		if( ccheck ){
					ccheck.checked = true;
		 		}
		 	}
			is_all_chk.value = 3;
		}
	}
}
function checkAllEvents(sportid)
{
	var hashval 	= document.all.item("events_hash_" + sportid).value;
	var is_all_chk  = document.all.item("events_checked_" + sportid);
	var hArray 		= hashval.split("|");

 	if( is_all_chk.value == 3 ){
	 	for( i=0; i < hArray.length; i++){
	 		tid=hArray[i]
	 		ccheck = eval( 'document.all.events_'+tid);
	 		if( ccheck ){
				ccheck.checked = false;
	 		}
	 	}
		is_all_chk.value = 2;
 	} else {
	 	for( i=0; i < hArray.length; i++){
	 		tid=hArray[i]
	 		ccheck = eval( 'document.all.events_'+tid);

	 		if( ccheck ){
				ccheck.checked = true;
	 		}
	 	}
		is_all_chk.value = 3;
	}

}
function ignoreAllLevels(sportid)
{
	var hashval 	= document.all.item("sport_level_hash_" + sportid).value;
	var is_ignored  = document.all.item("ignore_" + sportid);
	var hArray 		= hashval.split("|");

	if( is_ignored.checked ){
	 	for( i=0; i < hArray.length; i++){
	 		tid=hArray[i]
	 		ccheck = eval( 'document.all.splid_'+tid);
			if( ccheck ){
				ccheck.checked = false;
				ccheck.disabled = true;
	 		}
	 	}
	} else {
	 	for( i=0; i < hArray.length; i++){
	 		tid=hArray[i]
			ccheck = eval( 'document.all.splid_'+tid);
			if( ccheck ){
				ccheck.disabled = false;
	 		}
	 	}
	}

}
function autofill_username()
{
	if( document.thisForm.username.value == '' ){
		if( document.thisForm.email.value != ''){
			document.thisForm.username.value = document.thisForm.email.value;
		}else if(  document.thisForm.firstname.value != ''){
			nstr = 	document.thisForm.firstname.value + '.' + document.thisForm.lastname.value
			document.thisForm.username.value = nstr;
		}
	}
}
function isEmail(eaddress) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(eaddress)){
		return (true)
	}else{
		return (false)
	}
}
function validBookingEmailRequest()
{
	for(i=1;i<=10;i++){
		nfirst	= getObj('firstname_' + i);
		nlast	= getObj('lastname_' + i);
		nemail 	= getObj('email_' + i);
		if( nfirst.value != "" || nlast.value != "" || nemail.value != "" ){
			if( nfirst.value == ""){
				alert('First name is required');
				nfirst.focus();
				return false;
			}else if(nlast.value == ""){
				alert('Last name is required');
				nlast.focus();
				return false;
			}else if( isEmail(nemail.value) == false ){
				alert('All email addresses must be valid.');
				nemail.focus();
				return false;
			}
		}
	}
	return true;
}
function trim(str){
   return RTrim(LTrim(str));
}
function clearSelectOptions(sbox)
{
	while (sbox.options.length > 0) sbox.options[0] = null;
}
function buildSelectBox(sbox,optArray)
{
	i=0;
	for( key in optArray ){
		sbox.options[i] = new Option(key,optArray[key]);
	}
}
function uhash(obj)
{
	hashval = getObj(obj)
	return hashval.value.split("|");
}
function hidebtn(objname)
{
	o = getObj(objname);
	o.style.visibility = 'hidden';
}
function archivebooking()
{
	var doyou = confirm("Are you sure you want to archive this booking? (OK = Yes   Cancel = No)");
	if (doyou == true)
		return true;
	else if (doyou == false)
		return false;
}
function checkSubmitEC(ecvar)
{
	org_id = document.thisForm.org_id.value;

	if( org_id == "" ){
		alert("Organization is required");
		document.thisForm.org_id.focus();
		return false;
	}
	
	return checkSubmitECOnly(ecvar);

}

function checkSubmitECOnly(ecvar)
{
	ec = eval("document.thisForm."+ecvar+".value");

	validchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
	var strlen = ec.length;


	if( ec == "" ){
		alert("Event Code is required");
		eval( "document.thisForm."+ecvar+".focus()");
		return false;
	}

	for (var i = 0; i < strlen; i++ ) {
    	if (validchars.indexOf(ec.charAt(i)) < 0) {
       		alert("Event Code must contain only letters and numbers and no spaces");
			eval( "document.thisForm."+ecvar+".focus()");
       		return false;
       	}
    }

	return true;

}