var selected_domain_option = 'own';
var selected_template = '';
var selected_package = '';
var promo_discount = '';
var promo_template = '';
var promo_ws_type = '';
var template_discount_applied = 0;
var ws_type_discount_applied = 0;

function updateTotals(form){
		var formElement = document.getElementById(form);

		formElement.domainOpt[0].checked = true;
		formElement.domainOpt[1].checked = false;
	
		updateDomain('own');
		updatePackage();
		updateTemplate();
}
function submitForm(form) {	

	//var validEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|tv|us|ph|me|rs|cc|al|dz|ar|au|at|be|bm|bo|br|vg|bf|kh|ca|cy|dk|do|fr|de|gr|gu|gt|hk|ie|il|it|jp|mx|nl|an|nz|no|pt|pr|qa|sa|sg|kr|kp|es|ch|tw|th|gm|uk|vu|vi|ve))$/;
	
	var formElement = document.getElementById(form);
	var count = document.getElementById(form).elements.length;
	var validForm = true;
	
	var agreement = document.getElementById('licenseAgreement');
	
	if ( agreement.checked == 0 ){
			alert("Please read and check off that you have read and agree to the terms of the license agreement.");
			validForm = false;
	}
		
	for(i=0; i<count; i++){
		element = document.getElementById(form).elements[i];
		title = element.title;
		if ( title.substr(0,9) == "Required:" ){			
			value = element.value;
			value = value.trim();
			title = title.substr(10);
			if ( ( value == title || value == "") ){
					alert("Please enter '" + title + "' before submitting the signup form");
					var validForm = false;
					break;
			}
		}
	}
	
	if (validForm){
		/*
		var businessType = document.getElementById('businessType').value;
		var websiteType = document.getElementById('websiteType').value;
		alert("BT: " + businessType);
		alert("WT: " + websiteType);
		*/
		formElement.submit();
	}
	
}
function updateDomain(option){
	var subDisplay = document.getElementById('subTotal');
	oldtotal = subDisplay.innerHTML;
	
	newtotal = oldtotal;					
			
	if (option == 'buy' && selected_domain_option == 'own'){
		newtotal = parseFloat(oldtotal) + 50;
	}else if (option == 'own' && selected_domain_option == 'buy'){
		newtotal = parseFloat(oldtotal) - 50;
	}
	
	newtotal = formatCurrency(newtotal);
	
	subDisplay.innerHTML = newtotal;
	subBox = newtotal;
	
	selected_domain_option = option;
}
function updatePackage(){
	var package = document.getElementById('package');
	var subDisplay = document.getElementById('subTotal');
	var oiPackage = document.getElementById('oiPackage');
	var websiteType = document.getElementById('websiteType');
	
	var discount = 0;
	
	oldtotal = subDisplay.innerHTML;
	
	newtotal = oldtotal;					

	switch (package.value) {
		case 'Starter Package':	newValue = 199.95; type = '09'; break;
		case 'Pro Package':		newValue = 399.95; type = '10'; break;
		case 'Premium Package':	newValue = 599.95; type = '11'; break;
		default:				newValue = 0; type = '00';
	}
	switch (selected_package) {
		case 'Starter Package':	oldValue = 199.95; old_type = '09'; break;
		case 'Pro Package':		oldValue = 399.95; old_type = '10';break;
		case 'Premium Package':	oldValue = 599.95; old_type = '11'; break;
		default:				oldValue = 0; old_type = '00';
	}
	
	if ( promo_ws_type != "" && promo_ws_type.toUpperCase() == type.toUpperCase() ){
		discount = parseFloat(promo_discount);
		ws_type_discount_applied = discount;
	}else if ( promo_ws_type != "" ){
		ws_type_discount_applied = discount -= ws_type_discount_applied;
		if ( ws_type_discount_applied < 0 ){
			ws_type_discount_applied = 0;
		}
	}
	
	newtotal = parseFloat(oldtotal) + (newValue - oldValue);

	newtotal -= discount;

	newtotal = formatCurrency(newtotal);

	subDisplay.innerHTML = newtotal;
	subBox = newtotal;
	
	if ( package.value != "Package:" ){
		oiPackage.innerHTML = package.value;
	}else{
		oiPackage.innerHTML = "Not Selected";
	}
	
	websiteType.value = type;
	selected_package = package.value;
}
function updateTemplate(){
	var template = document.getElementById('template');
	var subDisplay = document.getElementById('subTotal');
	var oiTemplate = document.getElementById('oiTemplate');
	oldtotal = subDisplay.innerHTML;
		
	newtotal = oldtotal;					
		
	var discount = 0;
		
	// if there is a promo_template required and we switch to it, make sure the discount is applied
	if ( promo_template != "" && promo_template.toUpperCase() == template.value.toUpperCase() ){
		discount = parseFloat(promo_discount);
		template_discount_applied = discount;
	}else if ( promo_template != "" ){
		template_discount_applied = discount -= template_discount_applied;
		if ( template_discount_applied < 0 ){
			template_discount_applied = 0;
		}
	}
			
	if (template.value == 'Haylee' && selected_template != 'Haylee'){
		newtotal = parseFloat(oldtotal) + 99;
	}else if (template.value != 'Haylee' && selected_template == 'Haylee'){
		newtotal = parseFloat(oldtotal) - 99;
	}
	
	newtotal -= discount;
	
	newtotal = formatCurrency(newtotal);
	
	subDisplay.innerHTML = newtotal;
	subBox = newtotal;
	
	if ( template.value != "Template:" ){
		oiTemplate.innerHTML = template.value;
	}else{
		oiTemplate.innerHTML = "Not Selected";
	}
	
	selected_template = template.value;
}
function checkPromoCode(){
	
	var promoBox = document.getElementById('promocode');
	var pc = promoBox.value;
	
	var subDisplay = document.getElementById('subTotal');
	oldtotal = subDisplay.innerHTML;
	
	var discountBox = document.getElementById('discount');
	var discount = discountBox.value;

	var promoCodeElement = document.getElementById('PromoCodeArea');
	var promoCodeHTML = promoCodeElement.innerHTML;
	promoCodeElement.innerHTML = "<span class=\"sideTitle\">Checking Promotion Code ...</span>";

	var request = getRequest();

	var oiPromo = document.getElementById('oiPromo');
	var oiDiscount = document.getElementById('oiDiscount');

	request.onreadystatechange = function(){
		if(request.readyState == 4){
			var promoResult = JSON.decode(request.responseText);

			var description = promoResult.description;
			var discount = promoResult.discount;
			var requiredTemplate = promoResult.requiredTemplate;
			var requiredWSType = promoResult.requiredWSType;

			if ( ws_type_discount_applied > 0 ){
				oldtotal = parseFloat(ws_type_discount_applied) + parseFloat(oldtotal);
				ws_type_discount_applied = 0;
			}
			
			if ( template_discount_applied > 0 ){
				oldtotal = parseFloat(template_discount_applied) + parseFloat(oldtotal);
				template_discount_applied = 0;
			}

			promo_discount = String(discount);
			promo_template = String(requiredTemplate);
			promo_ws_type = String(requiredWSType);
		
			var package = document.getElementById('package');
		
			var ws_type = null;
		
			switch (package.value) {
				case 'Starter Package':	ws_type = '09'; break;
				case 'Pro Package':		ws_type = '10'; break;
				case 'Premium Package':	ws_type = '11'; break;
			}

			var template = document.getElementById('template');

			if ( request.responseText != 0 ){
								
				// if a template is required and the proper template is not selected no discount is available.
				if ( promo_template != "" && promo_template.toUpperCase() !=  template.value.toUpperCase() ){
					discount = 0;
					template_discount_applied = 0;
				}else if ( promo_template != "" ){
					template_discount_applied = parseFloat(promo_discount);
				}
				
				// if a ws type is required and the proper type is not selected no discount is available.
				if( promo_ws_type != "" && ( ws_type == null || ( promo_ws_type.toUpperCase() !=  ws_type.toUpperCase() ) ) ){
					discount = 0;
					ws_type_discount_applied = 0;
				}else if (promo_ws_type != "" ) {
					ws_type_discount_applied = parseFloat(promo_discount);
				}
								
				var newtotal = ( parseFloat(discount) > 0 ) ? ( parseFloat(oldtotal) - parseFloat(discount) ) : parseFloat(oldtotal);

				oiPromo.innerHTML = pc;
				oiDiscount.innerHTML = description;
				
			//	promoCodeHTML = 	"		<h5><span>Promo Code Entered:</span> " + pc + "<br/>";
			//	promoCodeHTML +=	"		<span>Discount:</span> " + description + "</h5><br/><br/><br/>";
			//	promoCodeHTML +=	"		<input type=\"hidden\" name=\"promocode\" id=\"promocode\">";
			//	promoCodeHTML +=	"		<input type=\"hidden\" name=\"discount\" id=\"discount\">";
			//	promoCodeHTML +=	"		<input type=\"hidden\" name=\"sub\" id=\"sub\">";
				
			}else{
				alert("Sorry, " + pc + " is an invaild promo code.");
				var newtotal = parseFloat(oldtotal);
				oiPromo.innerHTML = "";
				oiDiscount.innerHTML = "";

			}
			
			
			promoCodeElement.innerHTML = promoCodeHTML;
			
			promoBox = document.getElementById('promocode');
			discountBox = document.getElementById('discount');
			subBox = document.getElementById('sub');
			promoEnteredField = document.getElementById('promoEntered');
			
			promoBox.value = pc;
			promoEnteredField.value = pc;
			
			discount = formatCurrency(discount);
			newtotal = formatCurrency(newtotal);
			
			discountBox.value = discount;
			subBox.value = newtotal;
		
			subDisplay.innerHTML = newtotal;
			
		}
	}
	parameters = "pc=" + pc;
	sendRequest(request, "POST", "include/php/checkPromoCode.php", parameters);
}
