// Create a starting price for a custom plan
var totalPrice = 1;

$(function() {
	// Set the default value
	$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
	
	// Check the username is long enough
	$('.userName').bind("keyup", function() {
		if(isValidText($(".userName").val())) {
			$('tr #checkName').html("<span style='color: #1CA73A'>Name accepted!</span>");
		}else{
			$('tr #checkName').html("<span style='color: #FF0000'>Please enter a real name!</span>");
		}
	});
	
	// Check the phone number IS a number
	$('.userPhone').bind("keyup", function() {
		if(isValidDigit($(".userPhone").val())) {
			$('tr #checkPhone').html("<span style='color: #1CA73A'>Number accepted!</span>");
		}else{
			$('tr #checkPhone').html("<span style='color: #FF0000'>Please enter a valid phone number!</span>");
		}
	});
	
	// Check the email is valid
	$('.userEmail').keyup(function() {
		var email = $(".userEmail").val();
		if(isValidEmailAddress(email)) {
			$('tr #checkEmail').html("<span style='color: #1CA73A'>Your email is valid!</span>");
		}else{
			$('tr #checkEmail').html("<span style='color: #FF0000'>Please enter a valid email!</span>");
		}
	});
	
	// Quota Price
	var oldQPrice = 0;
	$('select[name=resQuota]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceQuota').html("&pound;" + newPrice);
		
		if (window.oldQPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldQPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldQPrice != null){
			if (window.oldQPrice > newPrice) {
				totalPrice = totalPrice - window.oldQPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldQPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldQPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	
	// Bandwidth Price
	var oldBPrice = 0;
	$('select[name=resBandwidth]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceBandwidth').html("&pound;" + newPrice);
		
		if (window.oldBPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldBPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldBPrice != null){
			if (window.oldBPrice > newPrice) {
				totalPrice = totalPrice - window.oldBPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldBPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldBPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});

	// FTP Price
	var oldFPrice = 0;
	$('select[name=resFTP]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceFTP').html("&pound;" + newPrice);
		
		if (window.oldFPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldFPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldFPrice != null){
			if (window.oldFPrice > newPrice) {
				totalPrice = totalPrice - window.oldFPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldFPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldFPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	// Email Account Price
	var oldEPrice = 0;
	$('select[name=resEmail]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceEmail').html("&pound;" + newPrice);
		
		if (window.oldEPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldEPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldEPrice != null){
			if (window.oldEPrice > newPrice) {
				totalPrice = totalPrice - window.oldEPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldEPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldEPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	// Email List Price
	var oldLPrice = 0;
	$('select[name=resEmailLists]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceEmailLists').html("&pound;" + newPrice);
		
		if (window.oldLPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldLPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldLPrice != null){
			if (window.oldLPrice > newPrice) {
				totalPrice = totalPrice - window.oldLPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldLPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldLPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	// Databases Price
	var oldDPrice = 0;
	$('select[name=resDatabases]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceDatabases').html("&pound;" + newPrice);
		
		if (window.oldDPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldDPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldDPrice != null){
			if (window.oldDPrice > newPrice) {
				totalPrice = totalPrice - window.oldDPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldDPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldDPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	// Subdomain Price
	var oldSPrice = 0;
	$('select[name=resSub]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceSub').html("&pound;" + newPrice);
		
		if (window.oldSPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldSPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldSPrice != null){
			if (window.oldSPrice > newPrice) {
				totalPrice = totalPrice - window.oldSPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldSPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldSPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	// Parked Domain Price
	var oldPPrice = 0;
	$('select[name=resParked]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;

			$('tr #priceParked').html("&pound;" + newPrice);

		
		if (window.oldPPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldPPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldPPrice != null){
			if (window.oldPPrice > newPrice) {
				totalPrice = totalPrice - window.oldPPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldPPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldPPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	// Addon Domains Price
	var oldAPrice = 0;
	$('select[name=resAddon]').change(function() {
		var actPrice;
		var newPrice = $(this).attr('value') / 1000;
		
			$('tr #priceAddon').html("&pound;" + newPrice);
		
		if (window.oldAPrice == null) {
			totalPrice = totalPrice + newPrice;
			window.oldAPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}else if (window.oldAPrice != null){
			if (window.oldAPrice > newPrice) {
				totalPrice = totalPrice - window.oldAPrice;
				totalPrice = totalPrice + newPrice;
			} else {
				actPrice = newPrice - window.oldAPrice;
				totalPrice = totalPrice + actPrice;					
			}
			window.oldAPrice = newPrice;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
		}
	});
	//################################################################################################
	//Dedicate IP
	var DediCheck = 0;
	$('input[name=extDedi]').click(function() {
		var Price = 4;
		if (window.DediCheck == null) {
			$('tr #e-ip').html("&pound;" + Price);
			totalPrice = totalPrice + Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.DediCheck = 1;
			$('input[name=extDedi]').val("1");
		} else if (window.DediCheck != null) {
			$('tr #e-ip').html("");
			totalPrice = totalPrice - Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.DediCheck = null;
			$('input[name=extDedi]').val("0");
		}
	});
	//Shell Access
	var ShellCheck = 0;
	$('input[name=extShell]').click(function() {
		var Price = 1;
		if (window.ShellCheck == null) {
			$('tr #e-shell').html("&pound;" + Price);
			totalPrice = totalPrice + Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.ShellCheck = 1;
			$('input[name=extShell]').val("1");
		} else if (window.ShellCheck != null) {
			$('tr #e-shell').html("");
			totalPrice = totalPrice - Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.ShellCheck = null;
			$('input[name=extShell]').val("0");
		}
	});
	//FrontPage
	var FrontCheck = 0;
	$('input[name=extFrontpage]').click(function() {
		var Price = 1;
		if (window.FrontCheck == null) {
			$('tr #e-frontpage').html("&pound;" + Price);
			totalPrice = totalPrice + Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.FrontCheck = 1;
			$('input[name=extFrontpage]').val("1");
		} else if (window.FrontCheck != null) {
			$('tr #e-frontpage').html("");
			totalPrice = totalPrice - Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.FrontCheck = null;
			$('input[name=extFrontpage]').val("0");
		}
	});
	//CGI Access
	var CGICheck = 0;
	$('input[name=extCGI]').click(function() {
		var Price = .75;
		if (window.CGICheck == null) {
			$('tr #e-cgi').html("&pound;" + Price);
			totalPrice = totalPrice + Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.CGICheck = 1;
			$('input[name=extCGI]').val("1");
		} else if (window.CGICheck != null) {
			$('tr #e-cgi').html("");
			totalPrice = totalPrice - Price;
			$('div .pacTotal').html("<h2>Total: &pound;" + Math.round(totalPrice*100)/100 + "</h2>");
			window.CGICheck = null;
			$('input[name=extCGI]').val("0");
		}
	});
	
	// Build the package using AJAX
	$('input[name=pacBuild]').click(function() {
		buildString = "userName=" + $('.userName').val() + "&userPhone=" + $('.userPhone').val() + "&userEmail=" + 
		$('.userEmail').val() + "&resQuota=" + $('select[name=resQuota]').val() + "&resBandwidth=" + $('select[name=resBandwidth]').val() + 
			"&resFTP=" + $('select[name=resFTP]').val() + "&resEmail=" + $('select[name=resEmail]').val() + "&resEmailLists=" + 
			$('select[name=resEmailLists]').val() + "&resDatabases=" + $('select[name=resDatabases]').val() + "&resSub=" + 
			$('select[name=resSub]').val() + "&resParked=" + $('select[name=resParked]').val() + "&resAddon=" + 
			$('select[name=resAddon]').val() + "&extDedi=" + $('input[name=extDedi]').val() + "&extShell=" + 
			$('input[name=extShell]').val() + "&extFrontpage=" + $('input[name=extFrontpage]').val() + "&extCGI=" + 
			$('input[name=extCGI]').val() + "&extLanguage=" + $('select[name=extLanguage]').val() + "&finCost=" + totalPrice;
		$.ajax({
			type: "POST",
			url: "includes/buildRequest.php",
			data: "buildString=&" + buildString,
			success: function() {
				$('div .finBox').html("<div class='success'>Your order has been sent and will be shortly processed!</div>");
				$('input[name=pacBuild]').attr("disabled", "true");
				
			}
		});
	});

});

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/i);
	return pattern.test(emailAddress);
}

function isValidText(stringText) {
	var alphaExp = new RegExp(/^[a-zA-Z (\')(\-)]+$/);		
	return alphaExp.test(stringText);
}

function isValidDigit(stringNumber) {
	var digitExp = new RegExp(/^(\+)?[(\-)?0-9 ]+$/i);
	return digitExp.test(stringNumber);
}