window.onload = function()
{
  try
  {
    formInit();
  } catch(e) {
  	initError = "There were errors initializing the form:\n";
  	for (var i in e) {
  		initError += "\n error." + i + " : " + e[i];
  	}
  	alert (initError);
  }
}

var handsFreeSignUp;

function formInit()
{


  // create an instance of the form class
  handsFreeSignUp = new DraftfcbForm(document.forms["offerSignUp"]);

  // project-specific form properties
  handsFreeSignUp.ErrorPrefix = "There is a problem with the information you entered:";
  handsFreeSignUp.SixteenBefore = new Date("June 20, 1990");
  handsFreeSignUp.Rules.push(
    { Field: "txt_firstname",     Method: "Required",    Message: "'First Name' is a required field." },
    { Field: "txt_lastname",      Method: "Required",    Message: "'Last Name' is a required field." },
    { Field: "txt_email",         Method: "Required", Message: "'Your Email Address' is a required field." },
    { Field: "txt_email",         Method: "EmailFormat", Message: "'Your Email Address' doesn't seem to be valid." },
    { Field: "txt_postal",    Method: "Required",    Message: "'Zip Code' is a required field." },
    { Field: "txt_postal",    Method: "ZipFivePlusFour", Message: "You ZIP code must be in the format xxxxx or xxxxx-xxxx." },
    { Field: "txt_birthmonth",    Method: "Required",    Message: "'Birth month' is a required field." },
    { Field: "txt_birthday",      Method: "Required",    Message: "'Birth date' is a required field." },
    { Field: "txt_birthyear",     Method: "Required",    Message: "'Birth year' is a required field." },
	{ Field: "txt_dateofbirth",   Method: "ValidDateYMD", Message: "Your selected birthdate does not appear to be a valid date." },
    /* { Field: "txt_dateofbirth",   Method: "OverEighteenYMD", Message: "Sorry. You must be 18 years or older to enter." },*/
    { Field: "txt_country",      Method: "Required",    Message: "'Country' is a required field." },
    { Field: "txt_language",      Method: "Required",    Message: "'Language' is a required field." },
    { Field: "txt_moto_brand",      Method: "Required",    Message: "'Brand' is a required field." },
    { Field: "txt_mobile",		  Method: "Required",    Message: "'Primary Phone Number' is a required field." },
    { Field: "txt_mobile",		  Method: "PhoneTenDashes", Message: "Your primary phone number doesn't appear to be valid. It should be in the format xxx-xxx-xxxx." }
    /*{ Field: "txt_legal",         Method: "Required",    Message: "You must agree to the 'Legal' to register." }*/


  );

}


function formValidate()
{
  var goodToGo = false;
 
	if (handsFreeSignUp.Validate(handsFreeSignUp.Rules)) {	
		if(document.forms.offerSignUp.txt_moto_brand_option[0].checked||document.forms.offerSignUp.txt_moto_brand_option[1].checked){
						if (document.forms.offerSignUp.txt_legal[0].checked) {
						goodToGo = true; 
						} else {
						alert("Please review legal.");
						} 
				} else {
				alert("Please answer question : Do you own a Motorola Phone?");
		
		}
	}
					

  return goodToGo;
}

// init project-specific form event handlers
 


  /*handleOptIn(handsFreeSignUp.FormHandle.elements["txt_legal"]);
  handsFreeSignUp.FormHandle.elements["txt_legal"].onclick = function() {
    handleOptIn(this);
  }*/


//custom form event handler functions
//pre-validation submit event handler
function handleSubmit() {
}

//concatenate date of birth selections into hidden date field
function handleDateOfBirth() {
  var objSweepsYear = handsFreeSignUp.FormHandle.elements["txt_birthyear"];
  var objSweepsMonth = handsFreeSignUp.FormHandle.elements["txt_birthmonth"];
  var objSweepsDay = handsFreeSignUp.FormHandle.elements["txt_birthday"];
  handsFreeSignUp.FormHandle.elements["txt_dateofbirth"].value = objSweepsYear.options[objSweepsYear.selectedIndex].value + "/" + objSweepsMonth.options[objSweepsMonth.selectedIndex].value + "/" + objSweepsDay.options[objSweepsDay.selectedIndex].value;
}


//set txt_optin value based on related checkbox

function handleCountry(objElement) {
  handsFreeSignUp.FormHandle.elements["txt_country_selected"].value = (objElement.selectedIndex) ? "1" : "0";  
}

function handleBrand(objElement) {
  handsFreeSignUp.FormHandle.elements["txt_brand_selected"].value = (objElement.selectedIndex) ? "1" : "0";  
}

function handleLanguage(objElement) {
  handsFreeSignUp.FormHandle.elements["txt_language_selected"].value = (objElement.selectedIndex) ? "1" : "0";  
}
   