// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var reg_form_empty_error = 0;

var promo_codes = [
"comp11",
"manual_entry",
"student",
"indiesac",
"web_test_3414"
];

var referral_codes = [];

function initList(listId, value) {
  var list = document.getElementById(listId);	
	
  for (i = 0; i < list.length; i++) {
    if (list.options[i].text == value) {
      list.options[i].selected = true;
      return;
    }
  }
}

function submitForm(theUrl, theForm, theId) {
	new Ajax.Request(theUrl, {
		parameters: theForm.serialize(true),
  	    onComplete: function(transport) {

//  DEV NOTE: I wanted to be explicit here and use 499 as a custom error code to indicate
//        that the form did not validate in the PHP server code.  This seems to work now
//		  since we moved to the GoDaddy hosting!
		  
		  if (transport.status == 400 || transport.status == 499 || transport.status == 500) {
	        $("error_content").innerHTML = transport.responseText;
	        if (theId != null) {
	        	$(theId).innerHTML = "";
	        }
		  }
		  else if (transport.status == 200) {
		    $("error_content").innerHTML = "";
		    
		    if (theId != null) {
		    	$(theId).innerHTML = transport.responseText;
		    }
	  	  }
		  else {
		    $("error_content").innerHTML = transport.statusText + ": status = " + transport.status;
		  }

	      // Force view to top of page so user sees the message
	      var href = location.href;

	      if (!href.endsWith("top")) {
	        location.href = location.href + '#top';
	      }
	      else {
     	    location.href = location.href;
		  }
	    }
    });
}

function submitEmailForm(theForm) {
	new Ajax.Request('email_process.php', {
		parameters: theForm.serialize(true)
	});
}


function validateConsumerRegForm(theForm) {
	var reason = "";

	reg_form_empty_error = 0;
	
	reason += validateEmpty(theForm.first);
	reason += validateEmpty(theForm.last);
	reason += validateEmail2(theForm.email, theForm.email2);
	reason += validatePhone(theForm.phone);
	reason += validateEmpty(theForm.address);
	reason += validateEmpty(theForm.city);
	reason += validateEmpty(theForm.state);
	reason += validateZip(theForm.zip);
//	reason += validateEmpty(theForm.source);
	reason += validatePromoCode(theForm.promo, theForm.referral);
	    
	// Validate the survey fields, if present
	if (theForm.survey.value == 1) {
		reason += validateEmpty(theForm.source);
		reason += validateEmpty(theForm.job);
		reason += validateEmpty(theForm.company);
		reason += validateSites(theForm);
		reason += validateEmpty(theForm.themes);
		reason += validateEmpty(theForm.business);
		reason += validateEmpty(theForm.themes2);
		reason += validateProgramFormats(theForm);
		reason += validateEmpty(theForm.others);
		reason += validateEmpty(theForm.magazines);
		reason += validateEmpty(theForm.sponsors);
	}
	
	
	if (reason != "") {
	  alert("Please correct the following entries and resubmit your form:\n" + reason);
	  return false;
	}

	submitForm('indiecon_confirm_reg.php', theForm, 'reg_content');
	return true;
}


function validateProgramFormats(theForm) {
	var reason = "";
	var checked = 0;
	var boxes = document.getElementsByName('formats[]');
	
	// OK if at least one box is checked.
	for (i = 0; i < boxes.length; i++) {
		if (boxes[i].checked == true) {
			checked = 1;
			break;
		}
	}
	
	if (!checked) {
		reason += "Please indicate your preference for IndieCon program formats.\n";
	}
	
	return reason;
}

function validateSites(theForm) {
	var reason = "";
	var checked = 0;
	var boxes = document.getElementsByName('sites[]');
	
	// OK if at least one box is checked.
	for (i = 0; i < boxes.length; i++) {
		if (boxes[i].checked == true) {
			checked = 1;
			break;
		}
	}
	
	if (!checked) {
		reason += "Please indicate the networking sites to which you belong.\n";
	}
	
	return reason;
}


function validateSpeakerRegForm(theForm) {
	var reason = "";

	reg_form_empty_error = 0;
	
	reason += validateEmpty(theForm.first);
	reason += validateEmpty(theForm.last);
	reason += validateEmail2(theForm.email, theForm.email2);
	reason += validateEmpty(theForm.title);
	reason += validatePhone(theForm.phone);
	reason += validateEmpty(theForm.city);
	reason += validateEmpty(theForm.state);
	      
	if (reason != "") {
	  alert("Please correct the following entries and resubmit your form:\n" + reason);
	  return false;
	}

	submitForm('speakers_confirm_reg.php', theForm, 'page-wrap');
	return true;
}


function validateOppsRegForm(theForm) {
	var reason = "";

	reg_form_empty_error = 0;
	
	reason += validateEmpty(theForm.first);
	reason += validateEmpty(theForm.last);
	reason += validateEmail2(theForm.email, theForm.email2);
	reason += validatePhone(theForm.phone);
	reason += validateEmpty(theForm.title);
	reason += validateEmpty(theForm.company);
	reason += validateEmpty(theForm.company_url);
	reason += validateEmpty(theForm.city);
	reason += validateEmpty(theForm.state);
	reason += validateZip(theForm.zip);
	      
	if (reason != "") {
	  alert("Please correct the following entries and resubmit your form:\n" + reason);
	  return false;
	}

	submitForm('opps_confirm_reg.php', theForm, 'page-wrap');
	return true;
}


function validateSubscribeForm(theForm) {
	var reason = "";

	reg_form_empty_error = 0;
	
	reason += validateEmpty(theForm.first);
	reason += validateEmpty(theForm.last);
	reason += validateEmail2(theForm.email, theForm.email2);
	      
	if (reason != "") {
	  alert("Please correct the following entries and resubmit your form:\n" + reason);
	  return false;
	}

	submitForm('subscribe_confirm.php', theForm, 'page-wrap');
	return true;
}


function validateEmailForm() {
	var reason = "";
	var theForm = document.getElementById('email_form');
	
	reg_form_empty_error = 0;
	reason += validateEmpty(theForm.email);
	reason += validateEmail(theForm.email);
	      
	if (reason != "") {
	  alert(reason);
	  return false;
	}
	
	return true;
}


function validatePromoCode(code, referral) {
	var reason = "";
	var my_code = code;     //String(code);
	var my_ref = referral;  //String(referral);
	
	if (!my_code.empty()) {
      reason = "Unknown promo code '" + my_code + "'.";
	  for (i = 0; i < promo_codes.length; i++) {
		if (my_code.toUpperCase() == promo_codes[i].toUpperCase()) {
		  reason = "";
		  
		  if (my_code.toUpperCase() == "vip") {
			  reason = "Unknown referral code '" + my_ref + "'.";
			  for (i = 0; i < referral_codes.length; i++) {
				  if (my_ref.toUpperCase() == referral_codes[i].toUpperCase()) {
					  reason = "";					  
					  break;
				  }
			  }
		  }

		  break;
		}
	  }
	}
	
	return reason;
}


function validateUnsubForm(theForm) {
	var reason = "";
	
	reg_form_empty_error = 0;
	reason += validateEmail(theForm.unsub_email);
	      
	if (reason != "") {
//	  document.getElementById('error_content').innerHTML = reason;
	  return false;
	}
	
	return true;
}



