
$.fn.toggleCheckedState = function() {
	$(this).find(':checked').each(function() {
		$(this).attr('checked', false);
	});
	
	$(this).hide();
}

$.fn.productState = function() {

	// Reset shipping to 0 if no appliances are used
	if( $("input[name=Distribution]:checked").val() != 'Appliance' ) {
		$("input[name=Shipping]").val('0');
		$("#Shipping").hide();
	} else {
		$("#Shipping").show();
	}
	
	if( ($("input[name=Product]:checked").val() == 'Webmail' || $("input[name=Product]:checked").val() == 'ArchiveVault') && $("input[name=Distribution]:checked").val() == 'Cloud' ) {
		$("input[value=Software]").attr('checked', true);
		return
	}
	
	// Calculate the product price
	$(this).calculatePrice();
	
	$(".cloudDesc").hide();

	$("#cloud" + $("input[name=Product]:checked").val()).show();
	
	// Show/hide the Atmail userlicense
	if( $("input[name=Product]:checked").val() == 'Webmail' ) {
		$("#AtmailWebmailUserLicense").show();

		// Hide cloud as an option		
		$("#DistHc").fadeTo(500,0.5);
		$("#DistHc INPUT").attr('disabled', true);
		
		// Select the default user-license, otherwise the last cookie state
		if( $("input[value=" + $.cookie('UserLicense') + "]", "#AtmailWebmailUserLicense").length ) {
			var name = $.cookie('UserLicense');
			$("input[value=" + name + "]", "#AtmailWebmailUserLicense").productSelectionAppliance();
		} else {
			$("input[name=UserLicense]:first", "#AtmailWebmailUserLicense").attr('checked', true).productSelectionLicense();
		}
		
	}
	else if($("input[name=Product]:checked").val() != 'ArchiveVault') {
		$("#AtmailWebmailUserLicense").toggleCheckedState();
		$("#DistHc").fadeTo(500,1);		
		$("#DistHc INPUT").attr('disabled', false);
	}

	// Show cloud
	if( $("input[name=Product]:checked").val() == 'Server' && $("input[name=Distribution]:checked").val() == 'Cloud' ) {
		$("#ASLicense").hide();
		$("#CloudServerUserLicense").show();
		$("#AddOnsDiv").hide();
		
		// Select the default user-license, otherwise the last cookie state
		if( $("input[value=" + $.cookie('UserLicense') + "]", "#CloudServerUserLicense").length ) {
			var name = $.cookie('UserLicense');
			$("input[value=" + name + "]", "#CloudServerUserLicense").productSelectionAppliance();
		} else {
			$("input[name=UserLicense]:first", "#CloudServerUserLicense").attr('checked', true).productSelectionLicense();
		}
		
	}
	else {
		$("#CloudServerUserLicense").toggleCheckedState();
		$("#AddOnsDiv").show();
	}
	
	if( $("input[name=Product]:checked").val() == 'Server' && $("input[name=Distribution]:checked").val() != 'Cloud' ) {
		$("#AtmailServerUserLicense").show();
		
		// Select the default user-license, otherwise the last cookie state
		if( $("input[value=" + $.cookie('UserLicense') + "]", "#AtmailServerUserLicense").length ) {
			var name = $.cookie('UserLicense');
			$("input[value=" + name + "]", "#AtmailServerUserLicense").productSelectionAppliance();
		} else {
			$("input[name=UserLicense]:first", "#AtmailServerUserLicense").attr('checked', true).productSelectionLicense();
		}
		
	}
	else
	$("#AtmailServerUserLicense").toggleCheckedState();

	if( $("input[name=Product]:checked").val() == 'ArchiveVault' ) {
		$("#ArchiveVaultUserLicense").show();
		
		// Hide the Webmail user-license, kept appearing
		$("#AtmailWebmailUserLicense").hide();
		
		// Hide cloud as an option
		$("#DistHc").fadeTo(500,0.5);
		$("#DistHc INPUT").attr('disabled', true);
		
		// Select the default user-license, otherwise the last cookie state
		if( $("input[value=" + $.cookie('UserLicense') + "]", "#ArchiveVaultUserLicense").length ) {
			var name = $.cookie('UserLicense');
			$("input[value=" + name + "]", "#ArchiveVaultUserLicense").productSelectionAppliance();
		} else {
			$("input[name=UserLicense]:first", "#ArchiveVaultUserLicense").attr('checked', true).productSelectionLicense();		
		}
		
	}
	else
	$("#ArchiveVaultUserLicense").toggleCheckedState();

	// Show/hide the Atmail Appliance div
	if($("input[name=Distribution]:checked").val() == 'Appliance' && ( $("input[name=Product]:checked").val() == 'Webmail' || $("input[name=Product]:checked").val() == 'Server') ) {
		$("#AtmailAppliance").show();
		
		if( $("input[value=" + $.cookie('Appliance') + "]", "#AtmailAppliance").length ) {
			var name = $.cookie('Appliance');
			$("input[value=" + name + "]", "#AtmailAppliance").productSelectionAppliance();
		} else {
			$("input[name=Appliance]:first", "#AtmailAppliance").attr('checked', true).productSelectionAppliance();
		}		
		
	}
	else
	$("#AtmailAppliance").toggleCheckedState();

	// Show/hide the ArchiveVault Appliance div
	if( $("input[name=Distribution]:checked").val() == 'Appliance' && $("input[name=Product]:checked").val() == 'ArchiveVault' ) {
		$("#ArchiveVaultAppliance").show();		
		$("#ArchiveVaultUserLicenseAppliance").show();	
		$("#ArchiveVaultUserLicense").toggleCheckedState();	

		// Webmail product keeps appearing!
		$("#AtmailWebmailUserLicense").hide();

		// Preselect the default appliance, if no previous cookie/selection given
		if( $("input[value=" + $.cookie('Appliance') + "]", "#ArchiveVaultAppliance").length > 0 ) {
			var name = $.cookie('Appliance');
			$("input[value=" + name + "]", "#ArchiveVaultAppliance").productSelectionAppliance();
		} else {
			$("input[name=Appliance]:first", "#ArchiveVaultAppliance").attr('checked', true).productSelectionAppliance();
		}
		
		$("input[name=UserLicense]:first", "#ArchiveVaultUserLicenseAppliance").attr('checked', true).productSelectionLicense();
		
		
	} else {
		$("#ArchiveVaultAppliance").toggleCheckedState();
		$("#ArchiveVaultUserLicenseAppliance").hide();
	}
	
}

$.fn.productSelection = function() {

	$(this).attr('checked', true);
	
	// Store the cookie for selection state
	$(this).storeCookie();
	
	$(this).parent().parent().parent().parent().find('.SelectState').removeClass('SelectState');
	$(this).parent().parent().parent().addClass('SelectState');
	$(this).productState();
}

$.fn.productSelectionAppliance = function() {
	$(this).attr('checked', true);

	// Store the cookie for selection state
	$(this).storeCookie();
	
	$(this).parent().parent().find('.AppBox').removeClass('SelectState');
	$(this).parent().next().find('.AppBox').addClass('SelectState');
	if ($(this).attr('id').match(/^(AV|SC)/)) {
		$("#ASLicense").hide();
	} else if( $(this).attr('id') == 'AAC') {
		
		$("INPUT", "#AtmailServerUserLicense").each(function() { 
			$(this).parent().parent().hide();
			$(this).attr('disabled', true);	
		});

		$("INPUT", "#AtmailWebmailUserLicense").each(function() { 
			$(this).parent().parent().hide();
			$(this).attr('disabled', true);	
		});

		// Next, show whats just allowed
		$("INPUT[value=SLB25], INPUT[value=SLB50], INPUT[value=SLB150]", "#AtmailServerUserLicense").parent().parent().show();
		$("INPUT[value=SLB25], INPUT[value=SLB50], INPUT[value=SLB150]", "#AtmailServerUserLicense").attr('disabled', false);

		$("INPUT[value=WLB25], INPUT[value=WLB50], INPUT[value=WLB150]", "#AtmailWebmailUserLicense").parent().parent().show();
		$("INPUT[value=WLB25], INPUT[value=WLB50], INPUT[value=WLB150]", "#AtmailWebmailUserLicense").attr('disabled', false);
		
		$("#ASLicense").show();
		
	} else {
		
		$("#ASLicense").show();
		
		$("INPUT", "#AtmailServerUserLicense").each(function() { 
			$(this).parent().parent().show();
			$(this).attr('disabled', false);			
		});

		$("INPUT", "#AtmailWebmailUserLicense").each(function() { 
			$(this).parent().parent().show();
			$(this).attr('disabled', false);			
		});	
	}
	
	$(this).getShipping();
	
}

$.fn.productSelectionStorage = function() {
	
	if($(this).attr('checked') == true) {
		$(this).parent().parent().find('.AppBox').removeClass('SelectState');
		$(this).parent().next().find('.AppBox').addClass('SelectState');	
		
		// Store the cookie for selection state
		$(this).storeCookie();
	} else {
		$(this).parent().parent().find('.AppBox').removeClass('SelectState');
		$("#InternalStorage").attr('checked', true);	
	}

	// Calculate the new shipping price
	$(this).getShipping();
	
}

$.fn.productSelectionLicense = function() {
	// Store the cookie for selection state
	$(this).storeCookie();

	$("BODY").find('input[name=' + $(this).attr('name') + ']').parent().removeClass('SelectState');
	$("BODY").find('input[name=' + $(this).attr('name') + ']').parent().next('TD').removeClass('SelectState');
	
	$(this).parent().addClass('SelectState');
	$(this).parent().next('TD').addClass('SelectState')
	
	var prodCode = $(this).val()
	var activeSyncProductCode;
	
	if (prodCode.match(/^(W|S)LB/)) {
		activeSyncProductCode = 'AOASB';
	} else if (prodCode.match(/^(W|S)LI/)) {
		activeSyncProductCode = 'AOASISP';
	} else if (prodCode.match(/^(W|S)LC/)) {
		activeSyncProductCode = 'AOASC';
	}
	
	if (activeSyncProductCode) {
		var activeSyncUnitPrice = ActiveSyncUnitPrices[activeSyncProductCode];
		$("#ActiveSyncUnitPrice").val(activeSyncUnitPrice);
		$("#ActiveSyncUnitPriceLabel").html(activeSyncUnitPrice);
		$("#ActiveSyncUserLicenseCode").val(activeSyncProductCode);
	}
}

$.fn.productSelectionAddOn = function() {

	// Since a checkbox, need to use a different selection state for cookie
	if($(this).attr('checked') == true)	{
		$(this).storeCookie();
		$(this).parent().next('TD').addClass('SelectState')
		$(this).parent().addClass('SelectState')
		
	} else {
		$(this).storeCookie('AIONOFF');
		$(this).parent().removeClass('SelectState');
		$("BODY").find('input[name=' + $(this).attr('name') + ']').parent().next('TD').removeClass('SelectState');

	}
		
}

// Specify the shipping
$.fn.specifyShipping = function() {

	var country = $(this).val();
	
	if(country == 'US') {
		$("#City").hide();
		$("#Zip").hide();
		$("#shippingPrice").html("Free");
		
		$("input[name=Shipping]").val('0');
		
	} else {
		
		$("#City").show();		
		$("#Zip").show();
		$("#Zip").getShipping();
			
	}

}

// Get the shipping cost
$.fn.getShipping = function() {
	
	// Do not calculate the shipping if no zip specified, or we are not an appliance!
	if( !$("input[name=ShippingZip]").val() || $("input[name=Distribution]:checked").val() != 'Appliance')
		return;
		
	var args = "ShippingCountry=" + $("#CountrySelect").val() + "&City=" + "&ShippingZip=" + $("input[name=ShippingZip]").val() + "&Appliance=" + $("input[name=Appliance]:checked").val() + "&Quantity=" + $("#ApplianceQty").val() + "&StorageAppliance=" + $("input[name=StorageAppliance]:checked").val();
	
	//alert(args);
	$("#shippingHead").hide();
	$("#shippingPrice").html("<img src='images/loading.gif' height='16' width='16'/> Calculating shipping ...");

	$.php("/store/index.php/index/shipping/?" + args);
	
}

// Calculate the price of the shopping cart

$.fn.calculatePrice = function() {
	var total = 0;

	$(":checked").each(function() {
		productPrice = $(this).attr('title');
		
		// If we are an appliance, check the Quantity too
		if($(this).attr('name') == 'Appliance') {
			var qty = $("#ApplianceQty").val();
			productPrice = Number(productPrice) * Number(qty);
			//alert(qty + ":" + productPrice);
		}
		
		if(productPrice)
			total = Number(total) + Number(productPrice);
	});

	// Revise shipping
	if( !$("input[name=ShippingZip]").val() || $("input[name=Distribution]:checked").val() != 'Appliance')
		$("input[name=Shipping]").val(0);

	// Calculate shipping too
	var shipping = $("input[name=Shipping]").val();
	if( shipping > 0)
		total = Number(shipping) + Number(total);


	// If we are cloud, calculate the number of mailboxes as the Qty
	/*
	if( $("input[name=Distribution]:checked").val() == 'Cloud' ) {
		name = $("input[name=UserLicense]", "#CloudServerUserLicense").val();
		
		if(name == 'SCB')
			qtyName = "CloudBasicQty";
		else if(name == 'SCI')
			qtyName = "CloudISPQty";
		else if(name == 'SCC')
			qtyName = "CloudCorpQty";
			
		value = $("input[name=" + qtyName + "]").val();
		
		//alert(name + ":" + value);
		
		total = total * Number(value);
	}
	*/

	// If a discount is specified, calculate the final amount
	discountAmount = $("input[name=DiscountAmount]").val()
	
	if(discountAmount > 0) {
	discount = total * (discountAmount/100);
	discount = Math.round(discount);
	total = total - discount;
	}
	
	// Calculate partner discount
	var partnerMarkup = $("input[name=PartnerMarkup]").val()
	
	if(partnerMarkup > 0) {
	var markup = total * partnerMarkup;
	markup = Math.round(markup);
	total = total + markup;
	}
	
	var upgradePrice = 0;

	// User license upgrade
	if($("input[name=UserLicenseUpgradePrice]").val())
	upgradePrice = $("input[name=UserLicenseUpgradePrice]").val();

	// Support renewal
	if($("input[name=SupportLicenseUpgradePrice]").val())
	upgradePrice = Number(upgradePrice) + Number( $("input[name=SupportLicenseUpgradePrice]").val() );

	var activeSyncNumber = $("input[name=ActiveSyncUserLicense]").val();
	
	if( $("input[name=ActiveSyncUserLicense]").val() && activeSyncNumber < 10 && $("input[name=ActiveSyncUserLicense]").val() != 0)	{

		alert('Minimum of 10 users required for ActiveSync');
		$("input[name=ActiveSyncUserLicense]").focus();
		
	} else {

		upgradePrice = Number(upgradePrice) + Number(Number( $("input[name=ActiveSyncUserLicense]").val() ) * Number( $("input[name=ActiveSyncUnitPrice]").val()));
		
	}
	
	if(upgradePrice != 0)
	total = total + Number( upgradePrice );
	
	// Calculate the currency - In the PHP, required to evaluate currency.php pricing
	total = $("BODY").calculateCurrency(total);
	
	return;
	
}

// Store a cookie value
$.fn.storeCookie = function(v) {
    var options = { path: '/', expires: 90 };
	var name = $(this).attr('name');
	var value = $(this).attr('value');
	
	if(!v)
	$.cookie(name, value, options);	
	else
	$.cookie(name, v, options);	
	
}

// Get a cookie value, return
$.fn.getCookie = function() {
	var value = $.cookie($(this).attr('name'));	
	
	return value;
}

// Validate the checkout page is correct

$.fn.validateCheckout = function() {

	var distribution = $("input[name=Distribution]").val();
	
	if( distribution == 'Cloud' ) {
		
		if(!$("input[name=PrimaryDomain]").validateField("Please choose the Primary domain"))
			return 0;

		if(!$("input[name=Password]").val())
			return $("input[name=Password]").alertForm("Please specify a password");
			
		if($("input[name=Password]").val() != $("input[name=Password2]").val())
			return $("input[name=Password]").alertForm("Passwords do not match, please type password again");
		
	}
	
	if(!$("input[name=Name]").validateField("Please specify your Contact name"))
		return 0;

	if(!$("input[name=Company]").validateField("Please specify your Company name"))
		return 0;

	if(!$("input[name=Phone]").validateField("Please specify your Phone number"))
		return 0;

	if(!$("input[name=URL]").validateField("Please specify your URL"))
		return 0;

	if(!$("input[name=Email]").validateEmail())
		return 0;

	if(!$("input[name=Address]").validateField("Please specify your Address"))
		return 0;

	if(!$("input[name=City]").validateField("Please specify your City"))
		return 0;

	if(!$("input[name=Zip]").validateField("Please specify your Zip/Postal Code"))
		return 0;

	// If credit-card is selected, check the fields
	if($("select[name=PaymentMethod]").val() == 'creditcard')	{

		if(!$(this).validateCreditCard())
			return 0;
		
	}
	
	if($("input[name=license]").attr('checked') == false)	{
		return $("input[name=license]").alertForm("Please check the Atmail license agreement before proceeding with the order");			
	}
		
	return 1;
	
}

// Validate the credit-card information is correct
$.fn.validateCreditCard = function() {
	
	if(!$("input[name=CardName]").validateField("Please specify the Cardholder of the credit-card"))
		return 0;

	if(!$("input[name=CardNumber]").validateField("Please specify the credit-card number"))
		return 0;

	if(!$("input[name=CardCCV2]").validateField("Please specify the CCV2 number"))
		return 0;

	if($("input[name=CardCCV2]").val().length < 3)
		return $("input[name=CardCCV2]").alertForm("The CCV2 number must be at least 3 digits long");
	
	if(!$("input[name=CardNumber]").isCardNum())
		return $("input[name=CardNumber]").alertForm("Credit-card invalid. Please check the card-number and try again");

	if($("select[name=CardExpiryYear]").val() < $("input[name=CurrentYear]").val() )  {
		return $("select[name=CardExpiryYear]").alertForm("Credit-card date has expired. Please validate the Year provided");
	}
	
	if($("select[name=CardExpiryYear]").val() == $("input[name=CurrentYear]").val() && $("select[name=CardExpiryMonth]").val() < $("input[name=CurrentMonth]").val())  {
		return $("select[name=CardExpiryMonth]").alertForm("Credit-card date has expired. Please validate the month provided");
	}
	
	// Next, check the Card Address information is completed, if available on the page
	if($("input[name=CardAddress]").attr('name')) {
		if(!$("input[name=CardAddress]").validateField("Please specify the Card Address"))
			return 0;

		if(!$("input[name=CardCity]").validateField("Please specify the Card City"))
			return 0;

		if(!$("input[name=CardZip]").validateField("Please specify the Card Zip/Postal code"))
			return 0;		
	}
		
	return 1;
	
}

// Copy the contact details, to the CC payment field
$.fn.copyCompanyDetailsCC = function() {
	
	if($(this).attr('checked') == true) {
		$("input[name=CardAddress]").val( $("input[name=Address]").val() );
		$("input[name=CardCity]").val( $("input[name=City]").val() );
		$("input[name=CardState]").val( $("input[name=State]").val() );
		$("input[name=CardZip]").val( $("input[name=Zip]").val() );
		$("select[name=CardCountry]").val( $("select[name=Country]").val() );			
	} else {
		$("input[name=CardAddress]").val( '' );
		$("input[name=CardCity]").val( '' );
		$("input[name=CardState]").val( '' );
		$("input[name=CardZip]").val( '' );
		$("select[name=CardCountry]").val( '' );
	}
}

// Validate the quote form
$.fn.validateQuote = function() {

	if(!$("input[name=Name]").validateField("Please specify your Contact name"))
		return 0;

	if(!$("input[name=Company]").validateField("Please specify your Company name"))
		return 0;

	if(!$("input[name=Phone]").validateField("Please specify your Phone number"))
		return 0;

	if(!$("input[name=URL]").validateField("Please specify your URL"))
		return 0;

	if(!$("input[name=Email]").validateEmail())
		return 0;

	if(!$("input[name=Address]").validateField("Please specify your Address"))
		return 0;

	if(!$("input[name=City]").validateField("Please specify your City"))
		return 0;

	if(!$("input[name=Zip]").validateField("Please specify your Zip/Postal Code"))
		return 0;

	return 1;
	
}

// Validate the portal > update contact form
$.fn.validateContact = function() {

	if(!$("input[name=Name]").validateField("Please specify your Contact name"))
		return 0;

	if(!$("input[name=Company]").validateField("Please specify your Company name"))
		return 0;

	if(!$("input[name=Phone]").validateField("Please specify your Phone number"))
		return 0;

	if(!$("input[name=Email]").validateEmail())
		return 0;

	if(!$("input[name=URL]").validateField("Please specify your URL"))
		return 0;	

	if(!$("input[name=Address]").validateField("Please specify your Address"))
		return 0;

	if(!$("input[name=City]").validateField("Please specify your City"))
		return 0;

	if(!$("input[name=Zip]").validateField("Please specify your Zip/Postal Code"))
		return 0;

	if(!$("input[name=Country]").validateField("Please specify your Country"))
		return 0;

	return 1;
	
}

// Validate the portal > update contact form
$.fn.validatePassword = function() {

	if(!$("input[name=oldpassword]").validateField("Please specify the existing password"))
		return 0;

	if(!$("input[name=newpassword]").validateField("Please specify the new password field"))
		return 0;

	if(!$("input[name=newpassword2]").validateField("Please confirm the new password field"))
		return 0;

	if($("input[name=newpassword]").val() != $("input[name=newpassword2]").val()) {
		$("input[name=newpassword]").alertForm("New passwords do not match - Please verify");
		return 0;	
	}

	return 1;
}

// Validate the portal > submit remote install request
$.fn.validateRemoteRequest = function() {

	var RequestType = $("SELECT[name=RequestType]").val();
	var SetupType = $("SELECT[name=SetupType]").val();
	
	var PaymentRequired = 0;
	
	// Do we have payment required?
	if(RequestType == 'install' && $("input[name=RemoteInstall]").val() == 0 ) {
		PaymentRequired = 1;
	} else if(RequestType == 'upgrade' && $("input[name=RemoteUpgrade]").val() == 0 ) {
		PaymentRequired = 1;		
	} else if(RequestType == 'incident' && $("input[name=RemoteIncident]").val() == 0 ) {
		PaymentRequired = 1;
	}
	
	if(!$("SELECT[name=RequestType]").validateField("Please select the remote service request"))
		return 0;
	
	// Next, lets check the fields are specified that we neede to submit
	if(!$("input[name=RemoteServer]").validateField("Please specify the servers IP address"))
		return 0;
	
	if(!$("input[name=RemoteUser]").validateField("Please specify the username for SSH access"))
		return 0;

	if(!$("input[name=RemotePass]").validateField("Please specify the user SSH password"))
		return 0;

	if(!$("input[name=RemotePassRoot]").validateField("Please specify the root SSH password"))
		return 0;

	if(RequestType == 'install' || RequestType == 'upgrade') {
		if(!$("input[name=RemoteSQLUser]").validateField("Please specify the SQL username"))
			return 0;

		if(!$("input[name=RemoteSQLPassw]").validateField("Please specify the SQL password"))
			return 0;

		if(!$("input[name=RemoteSQLHost]").validateField("Please specify the SQL hostname"))
			return 0;
	} 

	if(RequestType == 'incident') {
		if(!$("input[name=RemoteAtmailUrl]").validateField("Please specify the Atmail URL"))
			return 0;

		if(!$("input[name=RemoteAtmailPass]").validateField("Please specify the Atmail Webadmin Password"))
			return 0;		
	}

	// Next, check the payment information
	if(PaymentRequired == 1 && (RequestType == 'install' || RequestType == 'upgrade')) {
		
		// Error if card is incorrect
		if(!$(this).validateCreditCard())
			return;
		
	}
	
	// Check the license checkbox is enabled
	if($("input[name=license]").attr('checked') == false)	{
		return $("input[name=license]").alertForm("Please check the license agreement before proceeding with the remote request");			
	}

	return 1;
}

// Validate the portal > update contact form
$.fn.validatePasswordReset = function() {

	if(!$("input[name=clientId]").validateField("Please specify the ClientID"))
		return 0;

	if(!$("input[name=Email]").validateEmail())
		return 0;

	return 1;
}

// Validate a field has a value
$.fn.validateField = function(message) {

	if(!$(this).val()) {
		alert(message);
		$(this).focus();
		
		// Add the yellow alert block, remove after onblur
		$(this).addClass('alertState');
		$(this).bind('blur', function() { $(this).removeClass('alertState'); });
		
		return 0
	} else {
		return 1;
	}
	
}

// Alert a form field is incorrect
$.fn.alertForm = function(message) {

	alert(message);
	$(this).focus();
	
	// Add the yellow alert block, remove after onblur		
	$(this).addClass('alertState');	
	$(this).bind('blur', function() { $(this).removeClass('alertState'); });
	return 0;
}

// Validate a credit-card number is correct
$.fn.isCardNum = function()
{
		var str = $(this).val();
		
        var total = 0;
        for (i=str.length-1; i >= 0; i-=2)
        {
                total += parseInt(str.charAt(i));
                if (i-1 < 0)
                {
                        break;
                }
        }
        for (i=str.length-2; i >= 0; i-=2)
        {
                temp = parseInt(str.charAt(i)) * 2;
                if (temp < 9)
                {
                        total += temp;
                }
                else
                {
                        tempstr = temp + "";
                        total += parseInt(tempstr.charAt(0));
                        total += parseInt(tempstr.charAt(1));
                }
                if (i-1 < 0)
                {
                        break;
                }
        }
        if (total % 10 != 0)
        {
                return false;
        }
        return true;
}

// Email validation
$.fn.validateEmail = function()
{
	if(!$(this).validateField("Please specify your Email address"))
		return 0;

	// Check just one email specified
	var re = new RegExp(/,|;/);
	var m = re.exec($(this).val());

	if (m)
	{
		return $(this).alertForm("Please enter a single email-address");
	}	

	// Basic email checks, has an @ and a .
	var re = new RegExp(/(.*)@(.*)\.(.*)/);
	var m = re.exec($(this).val());

	if (!m)
	{
		return $(this).alertForm("Please enter a correct email address");
	}

	return 1;
	
}

// Select the upgrade cost for users/support
$.fn.upgradeSelection = function(name) {
	var temp = new Array();	
	var name = $(this).attr('name');
	var selectionVar = $("select[name=" + name + "]").val();
	temp = selectionVar.split(":");

	$("input[name=" + name + "Price]").val(temp[1]);	
	$(this).calculatePrice();
};

