function radioCheck(ss) {
  for(var i = 0; i < ss.length; i++) {
	  if(ss[i].checked) { return ss[i].value; }
	  }
  return false;
}

function checkbox_empty(ss) {
  for(var i = 0; i < ss.length; i++) {
	   if(ss[i].checked) { return false; }
	 }
  return true;
}

function dropdown_empty(ss){
// Note: ss will need to be fetched via document.getElementsByName('ss[]')
// or something to that effect if [] is used for PHP array
  for(var i = 0; i < ss.length; i++) {
    if(ss[i].selected) {
      if(ss[i].value.length) { return false; }
    }
  }
  return true;
}

function stripChars(pstrSource){ 
  var m_strOut = new String(pstrSource); 
		// Non-alpha characters
  //m_strOut = m_strOut.replace(/[^0-9]/g, '');
		
		// remove spaces, ( ), -, and .
		m_strOut = m_strOut.replace(/[\-|(|)|\.| ]/g, ''); 

  return m_strOut; 
}
	
function disableForm(formID){
  var element = document.getElementById(formID);
  element.style.display = "none;";
}

function form_error_msg(msg){
  document.getElementById("form_error_msg").innerHTML = msg;
}
	
// ----------- validation functions ------------------------------------------

function validateDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
 
  //check to see if in correct format
  if(!objRegExp.test(strValue)){
    return false; //doesn't match pattern, bad date
	}
  else{
    var strSeparator = strValue.substring(2,3);
    var arrayDate = strValue.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31};
    var intDay = parseInt(arrayDate[1],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] !== null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay !== 0){
        return true; //found in lookup table, good date
			}
    }
    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 === 0) && (intYear % 100 !== 0) || 
             (intYear % 400 === 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}	

function  validateNumeric( strValue ) {
/*****************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
  True if valid, otherwise false.
******************************************************************/
var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}

	
function validateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
//var objRegExp = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
var objRegExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

  //check for valid email
  return objRegExp.test(strValue);
}

function validateUSPhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}

function validateZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

  //check for valid US Zipcode
  return objRegExp.test(strValue);
}

// function to see if date 1 occurs before date 2
function validFromDate(fromDate,toDate){

	// Assuming that the date was in the correct format, this is our separator
 var separator1 = fromDate.charAt(2);
	var arrDate1 = fromDate.split(separator1);

	var checkFromDate = new Date();
	checkFromDate.setFullYear(arrDate1[2],arrDate1[0]-1,arrDate1[1]);
	
 var separator2 = toDate.charAt(2);
	var arrDate2 = toDate.split(separator2);
	
	var checkToDate = new Date();
	checkToDate.setFullYear(arrDate2[2],arrDate2[0]-1,arrDate2[1]);
	
	if (checkToDate > checkFromDate){
	  return true;
	}
	else {
	  return false;
	}
}

function check_contact(){
  error = false;
 	var error_msg = "";
	
	 // required fields
  var first_name = document.contact.first_name.value;
  var last_name = document.contact.last_name.value;
  var email = document.contact.email.value;
  var confirm_email = document.contact.confirm_email.value;
		var professional = radioCheck(document.contact.professional);
		
  if(first_name === "" || last_name === "" || (!professional)){ 
    error_msg = error_msg + "<li>Please make sure all required fields are filled in.</li>";
    error = true;
  }
	
	 if ((email === "")||(!validateEmail(email))){
    error_msg = error_msg + "<li>Please enter a valid <b>Email Address</b>.</li>";
    error = true;
		}

	 if ((confirm_email === "")||(!validateEmail(confirm_email))||(email !== confirm_email)){
    error_msg = error_msg + "<li>Please <b>confirm your Email Address</b>.</li>";
    error = true;
		}
		
		var phone = "";
		if (professional === "yes"){
		  // required fields for professionals
		  var company = document.contact.company_yp.value;
				var address = document.contact.addr1_yp.value;
				var city = document.contact.city_yp.value;
				var state = document.contact.state_yp.value;
  	 var zip = document.contact.zip_yp.value;
				var country = document.contact.country_yp.value;
  		var mailing_list_yp = radioCheck(document.contact.mailing_list_yp);
				
				// non-required fields for professionals that require validation
  	 phone = document.contact.phone_yp.value;

    if(company === "" || address === "" || city === "" || state === "" || zip === "" || country === "" || (!mailing_list_yp)){ 
      error_msg = error_msg + "<li>Please make sure all required fields are filled in.</li>";
      error = true;
    }
		}
		
		if (professional === "no"){
		  // required fields for non-professionals
  		var mailing_list_np = radioCheck(document.contact.mailing_list_np);

		  // required fields for non-professionals wishing to receive brochure
				var address = document.contact.addr1_np.value;
				var city = document.contact.city_np.value;
				var state = document.contact.state_np.value;
  	 var zip = document.contact.zip_np.value;
				var country = document.contact.country_np.value;

				// non-required fields for non-professionals that require validation
  	 phone = document.contact.phone_np.value;		
  		var receive_brochure = radioCheck(document.contact.receive_brochure);

    if(!mailing_list_np){ 
      error_msg = error_msg + "<li>Please make sure all required fields are filled in.</li>";
      error = true;
    }
				
				if (receive_brochure === "yes"){
      if(address === "" || city === "" || state === "" || zip === "" || country === ""){ 
        error_msg = error_msg + "<li>Please include your address in order to receive our product brochure.</li>";
        error = true;
      }
				}
		}
		
		if (phone !== ""){
    phone = stripChars(phone);
    if ((phone.length < 10)||(!validateNumeric(phone))){
      error_msg = error_msg + "<li>Please enter a valid numerical <b>Phone Number</b>.</li>";
      error = true;
	   }
		}

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#form_errors').hide('fast');
		  return true;
  }
	 else{
		  $('#form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function check_register(){
  error = false;
	 var error_msg = "";
	
	 // required fields
  var first_name = document.register.first_name.value;
  var last_name = document.register.last_name.value;
  var email = document.register.email.value;
  var company = document.register.company.value;
  var register_type = document.register.register_type;
      //var info = document.register.info.value;
		
		// password
  var pw = document.register.password.value;
  var confirm_pw = document.register.confirm_password.value;
	
	 // non-required fields that needs validation
	 var zip = document.register.zip.value;
	 var phone = document.register.phone.value;
	 var fax = document.register.fax.value;

  if(first_name === "" || last_name === "" || company === "" ){ 
    error_msg = error_msg + "<li>Please make sure all required fields are filled in.</li>";
    error = true;
  }
		
		radio_check = false;
  for (var i=0; i < register_type.length; i++){
    if(register_type[i].checked){
      radio_check = true;
		  }
  }
 	if(!radio_check){
    error_msg = error_msg + "<li>Please tell us if you are a <b>Distributor</b>, a <b>Installer</b>, or a <b>Specifier</b>.</li>";
		  error = true;
		}
	
	 if ((email === "")||(!validateEmail(email))){
    error_msg = error_msg + "<li>Please enter a valid <b>Email Address</b>.</li>";
    error = true;
		}

	 if (pw === ""){
    error_msg = error_msg + "<li>Please enter a <b>Password</b>.</li>";
    error = true;
		}

	 if (confirm_pw === ""){
    error_msg = error_msg + "<li>Please <b>Confirm</b> your <b>Password</b>.</li>";
    error = true;
		}
		
		if ((pw.length < 4) || (pw.length > 12)){
    error_msg = error_msg + "<li>Please enter a <b>Password</b> between 4-12 characters.</li>";
    error = true;
		}
		
		if (pw !== confirm_pw){
    error_msg = error_msg + "<li><b>Password</b> and <b>Confirm Password</b> do not match.</li>";
    error = true;
		}
	
	 if (phone !== ""){
    phone = stripChars(phone);
	   if ((phone.length !== 10)||(!validateNumeric(phone))){
      error_msg = error_msg + "<li>Please enter a valid numerical <b>Phone Number</b>.</li>";
      error = true;
		  }
	 }
	
	 if (fax !== ""){
    fax = stripChars(fax);
	   if ((fax.length !== 10)||(!validateNumeric(fax))){
      error_msg = error_msg + "<li>Please enter a valid numerical <b>Fax Number</b>.</li>";
      error = true;
		  }
	 }
	
	 /*if (zip !== ""){
	   if (!validateZip(zip)){
      error_msg = error_msg + "<li>Please enter a valid <b>Zipcode.</b>.</li>";
      error = true;
		  }
	 }*/

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#form_errors').hide('fast');
		  return true;
  }
	 else{
		  $('#form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function check_forgot_pw(){
  error = false;
	 var error_msg = "";
	
	 // required fields
  var email = document.mailing_list.email.value;

	 if ((email === "")||(!validateEmail(email))){
    error_msg = error_msg + "<li>Please enter a valid <b>Email Address</b>.</li>";
    error = true;
	 }

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#form_errors').hide('fast');
		  return true;
  }
	 else{
		  $('#form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function check_mailing_list(){
  error = false;
	 var error_msg = "";
	
	 // required fields
  var first_name = document.mailing_list.first_name.value;
  var last_name = document.mailing_list.last_name.value;
  var email = document.mailing_list.email.value;

 	// non-required fields that needs validation
	 var phone = document.mailing_list.phone.value;

  if(first_name === "" || last_name === ""){ 
    error_msg = error_msg + "<li>Please make sure all required fields are filled in.</li>";
    error = true;
  }

	 if ((email === "")||(!validateEmail(email))){
    error_msg = error_msg + "<li>Please enter a valid <b>Email Address</b>.</li>";
    error = true;
	 }

	 if (phone !== ""){
    phone = stripChars(phone);
	   if ((phone.length !== 10)||(!validateNumeric(phone))){
      error_msg = error_msg + "<li>Please enter a valid numerical <b>Phone Number</b>.</li>";
      error = true;
		  }
	 }

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#form_errors').hide('fast');
		  return true;
  }
	 else{
		  $('#form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function check_date_seminiar_search(){
  error = false;
	 var error_msg = "";
	
	 // required fields
  var seminar_date_from = document.date_seminar_search.seminar_date_from.value;
  var seminar_date_to = document.date_seminar_search.seminar_date_to.value;
		
	 if (seminar_date_from === ""){
    error_msg = error_msg + "<li>Please enter a starting <b>From</b> date range.</li>";
    error = true;
	 }

	 if (seminar_date_to === ""){
    error_msg = error_msg + "<li>Please enter an ending <b>To</b> date range.</li>";
    error = true;
	 }
	
		if((validateDate(seminar_date_from))&&(validateDate(seminar_date_to))){
		  if(!validFromDate(seminar_date_from,seminar_date_to)){
		    error_msg = "<li><b>From</b> date must occur before <b>To</b> date.</li>";
			   error = true;
		  }
		}
		else{
      error_msg = error_msg + "<li>Please enter dates in the format of <b>MM/DD/YYYY</b>.</li>";
		  error = true;
		}

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#related_form_errors').hide('fast');
    $('#date_loading').fadeIn('fast');
    do_date_seminar_search(seminar_date_from,seminar_date_to,1)
    $('#date_loading').fadeOut('fast');
		  return true;
  }
	 else{
		  $('#related_form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function do_date_seminar_search(from,to,page){
				$.post("/jquery/seminar-search/",{ seminar_date_from: from, seminar_date_to: to, page: page, search_type: "date"},
      function(data){
        $('#seminars-left').html(data);
    });
}

function check_state_seminiar_search(){
  error = false;
	 var error_msg = "";
	
	 // required fields
  var state = document.state_seminar_search.state.value;
		
	 if (state === ""){
    error_msg = error_msg + "<li>Please select a <b>State</b>.</li>";
    error = true;
	 }

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#related_form_errors').hide('fast');
    $('#state_loading').fadeIn('fast');
    do_state_seminar_search(state,1);
    $('#state_loading').fadeOut('fast');
		  return true;
  }
	 else{
		  $('#related_form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function do_state_seminar_search(state,page){
				$.post("/jquery/seminar-search/",{ state: state, page: page, search_type: "state"},
      function(data){
        $('#seminars-left').html(data);
    });
}

function do_seminar_search(page){
				$.post("/jquery/seminar-search/",{ page: page},
      function(data){
        $('#seminars-left').html(data);
    });
}

function do_login(){
  $('#form_errors').fadeOut('fast');
  $('#loading-login').fadeIn('fast');
  var email = $('#email').val();
		var password = $('#password').val();
		var ref = $('#ref').val();
		for(i=0;i<3;i++){password = Base64.encode(password);password = password.split("").reverse().join("");}		
		$.post("/jquery/login/",{ email: email, password: password, ref: ref},
    function(data){
      var response = data.split('|');
      var response_type = response[0].toLowerCase();
      var response_msg = response[1];
						if (response_type == "success") {
        //alert(response_msg);
								window.location = response_msg;
      }
						else{
        $('#form_error_msg').html('<ul><li>'+response_msg+'</li></ul');
				    $('#form_errors').fadeIn('fast');
			  			$('input').removeAttr("disabled");
								$('#login').fadeTo('fast', 1.00);
						}
      $('#loading-login').fadeOut('fast');						
		});
}

function check_fields(){
  if (($('#email').val() !== "")&&($('#password').val() !== "")){
    $('#do-login').show('fast');
  }
  else{
	   $('#do-login').hide('fast');
  }
}

function check_simple_search(){
  if ($('#simple_keyword').val() !== ""){
    $('#do-simple-search').show('fast');
  }
  else{
	   $('#do-simple-search').hide('fast');
  }
}

function check_advanced_search(){
  if (($('#advanced_keyword').val() !== "")||($('#document_category').val() !== "")||($('#document_type').val() !== "")){
    $('#do-advanced-search').show('fast');
  }
  else{
	   $('#do-advanced-search').hide('fast');
  }
}

function check_distributor_search(){
  error = false;
	 var error_msg = "";
	
	 // required fields
	 var zip = document.distributor_search.zip_search.value;
	
  if (!validateZip(zip)){
    error_msg = error_msg + "<li>Please enter a valid <b>Zipcode.</b>.</li>";
    error = true;
	 }

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#form_errors').hide('fast');
		  return true;
  }
	 else{
		  $('#form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}

function check_applynow(){
  var error = false;
  var error_msg = "";
  
  var first_name = document.applynow.first_name.value;
  var last_name = document.applynow.last_name.value;
  var email = document.applynow.email.value;
  var phone = document.applynow.phone.value;
  var addr1 = document.applynow.addr1.value;
  var city = document.applynow.city.value;
  var country = document.applynow.country.value;
  var candidate_resume = document.applynow.candidate_resume.value;
  
  if(first_name === "" || last_name === "" ||  addr1 === "" || city === "" || country === ""){ 
    error_msg = error_msg + "<li>Please make sure all required fields are filled in.</li>";
    error = true;
  }

	if ((email === "")||(!validateEmail(email))){
    error_msg = error_msg + "<li>Please enter a valid <b>Email Address</b>.</li>";
    error = true;
	}

  phone = stripChars(phone);
  if ((phone.length !== 10)||(!validateNumeric(phone))){
    error_msg = error_msg + "<li>Please enter a valid numerical <b>Phone Number</b>.</li>";
    error = true;
  }
  
  if (candidate_resume === ""){
    error_msg = error_msg + "<li>Please upload a copy of your <b>R&#233;sum&#233;</b>.</li>";
  }

  error_msg = "<ul>" + error_msg + "</ul>";
	
	 if(!error){
    $('#form_errors').hide('fast');
		  return true;
  }
	 else{
		  $('#form_errors').show('slow');
		  form_error_msg(error_msg);
		  window.scrollTo(50,70);
    return false;
  }
}