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 toggleMe(f,disp_mode) { //dispay or hide with display mode
	if(!f)return false;
	if(f.style.display=='none') f.style.display=disp_mode;
	else f.style.display='none';
	return true;
}

function setFocus(f) {
	if(!f)return false;
	f.focus();
	return true;
}

function toggleValue(f){
	if(!f)return false;
	if('1'==f.value) f.value='0';
	else f.value='1';
	return true;
}

function alignVertical(f,vert,post,top,bottom){
	if(!f)return false;
	f.style.verticalAlign = vert;
	f.style.position = post;
	f.style.bottom = bottom;
	f.style.top = top;
    return true;   
}

String.prototype.trim = function () {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function setBackground(f,color) {
	if(!f)return false;
	f.style.background = color;
    return true;   
}

function isEmpty(f) {
	if(!f)return true;
	setBackground(f,'white');
	f.value = f.value.trim();
    if (0 == f.value.length)  return true;
    return false;
}

function isSelected(f) {
	if(!f)return false;
	setBackground(f,'white');
	if (0 == f.selectedIndex) return false;
    return true;   
}

function isSet(f) {
	if(!f)return false;
	if ('1'==f.value) return true;
    return false;   
}

function checkLength(f,len) {
	if(!f)return false;
	if (len==f.value.length) return true;
    return false;   
}


function validateSearchForm() {
	var bib_id=document.getElementById('bib');
	var div=document.getElementById('div');
	var name=document.getElementById('nm');
	var team_name=document.getElementById('tn');
	var comp_name=document.getElementById('cn');
	var	adv_mode=document.getElementById('adv');
	
	if(isSet(adv_mode)) {
	
		
		if(isEmpty(team_name)&&isEmpty(comp_name)&&isEmpty(name))
		{
			if (isEmpty(bib_id) || bib_id.value <1) {
				bib_id.focus();
				alert("Enter valid bib no.");
				return false;
			}
		}
		if (isEmpty(bib_id)&&isEmpty(team_name)&&isEmpty(comp_name)&&!isSelected(div)&&isEmpty(name)) {
			name.focus();
			alert("Enter a criteria or Select a Category");
			return false;
		}

		/*else if (isNaN(bib_id.value)) {
			setBackground(bib_id,'yellow');
			bib_id.focus();
			alert("Bib No. must be numeric");
			return false;
		}*/
	}
	else {

	if (isEmpty(bib_id) || bib_id.value <1 ) {
			bib_id.focus();
			alert("Enter valid bib no.");
			return false;
		}
		if (!isSelected(div)) {
			 /*if (isNaN(bib_id.value)) {
				setBackground(bib_id,'yellow');
				bib_id.focus();
				alert("Bib No. must be numeric");
				return false;
			}
			else*/ if(isEmpty(bib_id)) {
				bib_id.focus();
				alert("Enter Bib No. or Select a Category");
				return false;
			}
			
		}
		/*else if (isNaN(bib_id.value)) {
			setBackground(bib_id,'yellow');
			bib_id.focus();
			alert("Bib No. must be numeric");
			return false;
		}*/
	}

	return true;
}

function areBibsNumeric(f) {
    if (-1 != f.value.search(/[^\d,\s]/g,'')) return false;
    return true;
}

function validateCompareForm() {
	var my_bib=document.getElementById('mb');
	var friends_bib=document.getElementById('fb');
	var cat=document.getElementById('cat');
	var bError=0, errMsg1="", errMsg2="";
	
	if (isEmpty(my_bib)) {
		setBackground(my_bib,'yellow');
		errMsg1 = "Please enter: Your Bib No"; my_bib.focus();
		bError = 1;
	}
	/*else if (isNaN(my_bib.value)) {
		setBackground(my_bib,'yellow');
		errMsg1 = "Please enter: Your Bib No"; my_bib.focus();
		errMsg2 = "\nBib No. must be numeric";
		bError = 1;
	}*/

	if (isEmpty(friends_bib)) {
		setBackground(friends_bib,'yellow');
		if (bError) errMsg1 += ", Your Friends' Bib No";
		else { errMsg1 = "Please enter: Your Friends' Bib No"; friends_bib.focus(); }
		bError = 1;
	}
	/*else if (!areBibsNumeric(friends_bib)) {
		setBackground(friends_bib,'yellow');
		if (bError) errMsg1 += ", Your Friends' Bib No";
		else { errMsg1 = "Please enter: Your Friends' Bib No"; friends_bib.focus(); }
		errMsg2 = "\nBib No. must be numeric";
		bError = 1;
	}*/

	if(bError) {
		alert(errMsg1+errMsg2);
		return false;
	}

	return true;
}

function toggleAdvanceMode() {
	var adv_mode=document.getElementById('adv_mode');
	var send_btn=document.getElementById('send_btn');
	var mode_txt=document.getElementById('mode_txt');
	var adv_txt=document.getElementById('adv_txt');
	var quick_txt=document.getElementById('quick_txt');
	var adv=document.getElementById('adv');

	var bib_id=document.getElementById('bib');
	var name=document.getElementById('nm');

	toggleValue(adv);
	toggleMe(adv_mode,'block');
	toggleMe(adv_txt,'inline');
	toggleMe(quick_txt,'inline');
	
	if('1' == adv.value) {	
		alignVertical(send_btn,'bottom','relative','','6');
		alignVertical(mode_txt,'bottom','relative','','10');
		setFocus(name);
	}
	else {
		alignVertical(send_btn,'middle','','','');
		alignVertical(mode_txt,'middle','','','');
		setFocus(bib_id);
	}
	
	return true;
}
