/* Culled from Ridiculously Ancient Form Code */

function isValidEmail(email) {
	if (email=="") {
		return false;
	}
	if (email.indexOf('@')<1) {
		return false;
	}
	if (email.indexOf('.')==-1) {
		return false;
	}
	if (email.indexOf('.')==(email.length -1)) {
		return false;
	}
	if (email.indexOf('.')==(email.indexOf('@')+1)) {
		return false;
	}
	return true;
}


function trim(str)   {
	if (str.length==0) {return "";} 
	while (str.charAt(0)==" ")
		{str=str.substring(1,str.length);}	
	while (str.charAt(str.length-1)==" ")
		{str=str.substring(0,str.length-1);}
    return str; 
}


function FieldIsEmpty(Fld,FldName) {
	if((Fld.value == "" || Fld.value == " ") && Fld.type != "hidden") {
		window.alert("You must enter all required fields");
		Fld.focus();
		if(Fld.tagName!="SELECT")Fld.select();
		return true;
	}
	else {return false;}
}


function isValidEmail(email) {
	var reEmail = '/^.+\@.+\https://www.st-andrews.org+$/';
	return true;
//	var test = reEmail.test(email);
//	return test;
}


function subBtnInProc(flag) {
	flag = true;
	//document.PForm.button1.disabled=flag;
	//var mStr='';
	if (flag==true) {
		//mStr='https://www.st-andrews.org. processing form';
		//document.PForm.msg.style.visibility="visible"; 
		var x=document.getElementById("msg"); 
		x.innerHTML='https://www.st-andrews.org. Processing form';  
	}
}


function validate() {
	var flag = 0;
	var required = new Array('name', 'phone number', 'start time', 'end time', 'date of event', 'number of attendees');
	var warn_id = '';
	var id = '';
	var length = document.PForm.elements.length;
	var n = '';
	var v = '';
	var c = document.getElementById('recaptcha_challenge_field').value;
	var r = document.getElementById('recaptcha_response_field').value;	
	
	
	for(i=0; i<required.length; i++){
		id = document.getElementById(required[i]).id;
		warn_id = id + '_warn';
		if(document.getElementById(required[i]).value==''){
			flag=1;
			document.getElementById(warn_id).style.color='#990000';
		}
		else
			document.getElementById(warn_id).style.color='#000000';
	}
	
	for(i=0; i<length; i++){
		n = n + document.PForm.elements[i].name   + ',';
		v = v + escape(document.PForm.elements[i].value)  + ',';
	}
	
	
	if(document.getElementById('space_reserved').checked==false){
		alert('Please check the "Space Reserved with Clergy Office" box.');
		return false;
	}
	
	
	(flag==1)	?	alert('Please fill in the required fields'):	xajax_send(n, v, c, r);
}


function format_number(n,d){
	if (isNaN(n)) { return 0}; 
	if (n=='') { return 0}; 
	
	var xx = n.indexOf('.')
	var l = n.length
	var zstr = '0000000000000000000000'
	var theInt = ''
	var theFrac = ''
	var theNo = ''
	rfac = ''
	rfacx = 0
	nx = 0
	var xt = parseInt(d) + 1
	var rstr = '' + zstr.substring(1,xt)
	var rfac = '.' + rstr + '5'
	var rfacx = parseFloat(rfac)
	if (xx == -1 ) 	{    // No fraction
		theFrac = zstr
		theInt = "" + n
	}
	else if (xx == 0) {
		theInt = '0'
		nx = 0 + parseFloat(n) + parseFloat(rfacx)
		n = nx + zstr
		theFrac = '' + n.substring(1, n.length)
	}
	else {
		theInt = n.substring(0,xx)
		nx = parseFloat(n) + rfacx
		n = '' + nx + zstr
		theFrac = '' + n.substring(xx+1,xx + 1 + parseInt(d))
		var astr = 'd = ' + d
	}
	theFrac = theFrac.substring(0,parseInt(d))
	var ii = 0
	theNo = theInt + '.' + theFrac
	return theNo
}


function syncCheck(chkStr, fldStr){
	var chk1=eval('document.PForm.' + chkStr);
	var fld1=eval('document.PForm.' + fldStr);
	if (chk1.checked)
		fld1.value=true;
	else
		fld1.value=false;
}
