var form_field_validate=false; //used to check out if form fields are validated
var submitted = false;	//used to check out if submit button is clicked

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function IsNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (i=1; i < sText.length && IsNumber == true; i=i+2) { 
		Char = sText.charAt(i); 
		
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
function IsAlpha(sText) {
	var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var IsAlpha=true;
	var Char;
	for (i=0; i < sText.length && IsAlpha == true; i=i+2) { 
		Char = sText.charAt(i); 
		
		if (ValidChars.indexOf(Char) == -1) {
			IsAlpha = false;
		}
	}
	return IsAlpha;
}
function form_Validate(theForm)
{
  	// set var radio_choice to false
	var radio_choice = false;
	
	// Loop from zero to the one minus the number of radio button selections
	for (counter = 0; counter < theForm.title.length; counter++)
	{
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (theForm.title[counter].checked){
		radio_choice = true; 
		}
	}
	if (!radio_choice)
	{
		// If there were no selections made display an alert box 
		alert("Please select a Salutation.");
		//theForm.title.focus();
		return (false);
	}
  
  if (trim(theForm.firstname.value) == "")
  {
    alert("Please enter a value for the First Name field.");
    theForm.firstname.focus();
    return (false);
  }
   
  if (trim(theForm.lastname.value) == "")
  {
    alert("Please enter a value for the Last Name field.");
    theForm.lastname.focus();
    return (false);
  }
  
    if (trim(theForm.address.value) == "")
  {
    alert("Please enter a value for the Address field.");
    theForm.address.focus();
    return (false);
  }
   
  if (trim(theForm.city.value) == "")
  {
    alert("Please enter a value for the City field.");
    theForm.city.focus();
    return (false);
  }
  
  if (theForm.province.selectedIndex < 1){
	alert("Please enter a value for the Province field.");
	theForm.province.focus();
	return (false);
  }
  
  if (trim(theForm.postalcode.value) == "")
  {
    alert("Please enter a value for the Postal Code field.");
    theForm.postalcode.focus();
    return (false);
  }
	if (theForm.postalcode.value.length != 6){
		alert("Please enter the correct Postal Code without any space.");
		theForm.postalcode.focus();
		return (false);
	} else {
		if (!IsNumeric(theForm.postalcode.value) || !IsAlpha(theForm.postalcode.value)) {
			alert("Please enter the correct Postal Code without any space.");
			theForm.postalcode.focus();
			return (false);
		}
	}
  if (theForm.emailaddress.value == "")
  {
    alert("Please enter a value for the Email Address field.");
    theForm.emailaddress.focus();
    return (false);
  }

	if (theForm.emailaddress.value.length < 6 || !IsValidEmail(theForm.emailaddress.value))
	{
		alert("Please enter a valid value for the Email Address field.");
		theForm.emailaddress.focus();
		return (false);
	}
	
	radio_choice = false;
	for (counter = 0; counter < theForm.language.length; counter++)
	{
		if (theForm.language[counter].checked){
		radio_choice = true; 
		}
	}
	if (!radio_choice)
	{
		alert("Please select a Preferred Language.");
		return (false);
	}
	
	
	
	
	if (theForm.age.selectedIndex < 1){
	alert("Please enter a value for the Age field.");
	theForm.age.focus();
	return (false);
  }
	
	radio_choice = false;
	for (counter = 0; counter < theForm.included.length; counter++)
	{
		if (theForm.included[counter].checked){
		radio_choice = true; 
		}
	}
	if (!radio_choice)
	{
		alert("Please answer all the questions with * beside them.");
		return (false);
	}
	
		radio_choice = false;
	for (counter = 0; counter < theForm.receiving.length; counter++)
	{
		if (theForm.receiving[counter].checked){
		radio_choice = true; 
		}
	}
	if (!radio_choice)
	{
		alert("Please answer all the questions with * beside them.");
		return (false);
	}

  form_field_validate=true;		//this signifies that all fields have been validated properly
  return (true);
}


function IsValidEmail(email) {
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
	var pindex = theStr.indexOf(".",index);
	if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}



function submit_pressed() {
if(submitted == true && form_field_validate==true) { alert("submit has been pressed"); } //return; }
	if (form_field_validate==true) {
		submitted = true;
	}

}

String.prototype.toTitleCase = function()
{
	return this.replace(/\b([a-z])/g,function($0){return $0.toUpperCase()});
}