
/**
This is a JavaScript library for the goldpartying public-facing scheduling form
*/

function fSubmit() {
	clean=false;
	if (document.frm.name.value == "") 
		alert ("Please enter the name of the host before submitting");
	else if (document.frm.phone.value == "") 
		alert ("Please enter a phone number to contact the host at");
 	else if (!validatePhone(document.frm.phone))
		document.frm.phone.focus();
	else if ((document.frm.email.value == "") ||  (!isEmail(document.frm.email.value))) 
		alert ("Please enter a valid Email Address");
	else if (document.frm.street.value == "") 
		alert ("Please enter the Street Address at which the party will be held");
	else if (document.frm.city.value == "") 
		alert ("Please enter the City in which the party will be held");
	else if (document.frm.state.value == "")
		alert ("Please enter the State in which the party will be held");
	else if (document.frm.zip.value == "") 
		alert ("Please enter the ZipCode at which the party will be held");
 	else if (!validateZip(document.frm.zip))
		document.frm.zip.focus();
	else if (document.frm.ADate.value == "") 
		alert ("Please enter the Date on which the party will be held");
	else if (document.frm.stime.value == "") 
		alert ("Please enter the Time at which the party will be held"); 
	else if (document.frm.tzone.value == "") 
		alert ("Please enter the local timezone where the party is to be held");
	else 
		clean = true;
	if (clean) {
		scroll(0,0);
		document.getElementsByName('skrim')[0].style.display='block'; document.getElementById('pleasewait').style.display='block'; 
		document.frm.action = "fContacts.php";
		document.frm.submit();
	}
}

function GiveFocus(name) { 
    document.forms[0].elements[name].focus(); 
} 

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  
  return (!r1.test(str) && r2.test(str));
}

function dayfill() {
	var myDays= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
	theday=new Date(document.frm.ADate.value);
	thisDay=theday.getDay();
	thisDay=myDays[thisDay];
	document.frm.theday.value=thisDay;
}

function validateZip(field){
	var thevalue = "";
	thevalue = field.value;
    var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
    if (!zipCodePattern.test(thevalue)) {
        alert('Please enter a valid 5-digit zipcode');
		field.focus();
		return false;
	} else {
		return true;
	}
} 
function validatePhone(field) {
	var thevalue = "";
	thevalue = field.value;
    var num = thevalue.replace(/[^\d]/g,'');
    if(num.length != 10) {
        alert('Please enter a valid 10-digit phone number (ie. including area code)');
		field.focus();
		field.select();
		return false;
    } else {
        field.value = "(" + num.substring(0,3) + ") " + num.substring(3, 6) + "-" + num.substring(6);
		return true;
    }
}

function echeck(field) {
	var thefield = field;
	var thevalue = "";
	thevalue = thefield.value;
	if (!isEmail(thevalue))	{
		alert ("The email address you provided is invalid, please re-check it. Email\naddresses cannot contain spaces or other non-alphanumeric characters.");
		thefield.focus();
		thefield.select();
		return false;
	} else {
		return true;
	}
}
