// JavaScript Document

//Form Hints-----------------------------------------------------------------------------------------------------------------
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
      inputs[i].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      inputs[i].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  var selects = document.getElementsByTagName("select");
  for (var k=0; k<selects.length; k++){
    if (selects[k].parentNode.getElementsByTagName("span")[0]) {
      selects[k].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      selects[k].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
}
addLoadEvent(prepareInputsForHints);

//Form Validation------------------------------------------------------------------------------------------------------------
function validate_event_login_form(){
	if ( isempty( document.login_form.username)) {
		document.login_form.username.focus();
		alert("Please enter a username.");
		return false;
	}
	 if ( !checkemail( document.login_form.username )) {
		document.login_form.username.focus();
		return false;
	} 
	
	if ( isempty( document.login_form.password)) {
		document.login_form.password.focus();
		alert("Please enter a password.");
		return false;
	}
}
function validate_contact_form(){
	// From
	if ( isempty( document.contact_form.name)) {
		document.contact_form.name.focus();
		alert("Please enter a value in the NAME field before submiting your inquiry.");
		return false;
	}
	
	if ( isempty( document.contact_form.email)) {
		document.contact_form.email.focus();
		alert("Please enter a value in the EMAIL field before submiting your inquiry.");
		return false;
	}
	 if ( !checkemail( document.contact_form.email )) {
		document.contact_form.email.focus();
		return false;
	} 
}

function validate_mailing_list_form(){	
	if ( isempty( document.mailing_list_form.name)) {
		document.mailing_list_form.name.focus();
		alert("Please enter a valid name in the \"name\" to join the mailing list.");
		return false;
	}
	if ( isempty( document.mailing_list_form.email)) {
		document.mailing_list_form.email.focus();
		alert("Please enter a valid email address in the \"Email\" to join the mailing list.");
		return false;
	}
	if ( !checkemail( document.mailing_list_form.email )) {
		document.mailing_list_form.email.focus();
		return false;
	} 
}

//workshop registration-------------------------------------------------------------------------------------------------------------------------------
function validate_event_registration_form(){
	// From
	if ( isempty( document.event_registration_form.name)) {
		document.event_registration_form.name.focus();
		alert("Please enter a valid name in the \"NAME\" field before proceeding.");
		return false;
	}
	if ( isempty( document.event_registration_form.address)) {
		document.event_registration_form.address.focus();
		alert("Please enter a valid address in the \"ADDRESS\" field before proceeding.");
		return false;
	}
	if ( isempty( document.event_registration_form.city)) {
		document.event_registration_form.city.focus();
		alert("Please enter a valid city in the \"CITY\" field before proceeding.");
		return false;
	}
	if ( isempty( document.event_registration_form.zip)) {
		document.event_registration_form.zip.focus();
		alert("Please enter a valid zip code in the \"Zip Code\" field before proceeding.");
		return false;
	}
	/*
	if ( iszipcode( document.event_registration_form.zip)) {
		document.event_registration_form.zip.focus();
		alert("Please enter a valid zipcode in the \"ZIP CODE\" field before proceeding.");
		return false;
	}
	
	if ( isphonenumber( document.event_registration_form.phone)) {
		document.event_registration_form.phone.focus();
		alert("Please enter a valid phone number in the \"Phone\" field before proceeding to checkout.");
		return false;
	}
	*/
	if ( isempty( document.event_registration_form.email)) {
		document.event_registration_form.email.focus();
		alert("Please enter a valid name in the \"Email\" field before proceeding to checkout.");
		return false;
	}
	
	if ( isempty( document.event_registration_form.organization)) {
		document.event_registration_form.organization.focus();
		alert("Please enter a organization in the \"Organization\" field before proceeding to checkout.");
		return false;
	}
	
	if ( !checkemail( document.event_registration_form.email )) {
		document.event_registration_form.email.focus();
		return false;} 
	
	if ( ispassword( document.event_registration_form.password)) {
		document.event_registration_form.password.focus();
		alert("Please enter a password that is at least 6 digits long in the \"PASSWORD\" field before proceeding.");
		return false;
	}
	
	if ( isequal( document.event_registration_form.password,document.event_registration_form.confirm_password)) {
		document.event_registration_form.confirm_password.focus();
		alert("Your PASSWORD and CONFIRM PASSWORD do not match please renter before proceeding to checkout.");
		return false;
	}
}

//Order Form Validation-training_materials_detail.html--------------------------------------------------------------------
function validate_order_form(){
	// From
	if ( isempty( document.order_form.quantity)) {
		document.order_form.quantity.focus();
		alert("Please enter an integer value in the quantity field (i.e 1,2,3,etc ) before adding item to cart.");
		return false;
	}
	
}
