var FloralDecor = {
	OrchidClub : {
		ValidateOrchidClubForm:function() {
			if($("ocRNG").value.blank()) {
				FloralDecor.HighlightAndFocus("ocRNG");
				return false;
			}
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:FloralDecor.OrchidClub.ValidateOrchidClubForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "orchidClubForm" ) )});		
			return false;
		},
		ValidateOrchidClubForm_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.result) {
				document.location = z.redirectURL;
			} else {
				alert(z.error);
				$("ocRNG").value="";
				FloralDecor.HighlightAndFocus("ocRNG");
			}
		}
	},
	Products : {
		ShowLargeImage:function(ppId) {
			var url = "index.cfm?action=Products_ShowLargeImage&ppId="+ppId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:FloralDecor.Products.ShowLargeImage_onSuccess, onFailure:errFunc});
		},
		ShowLargeImage_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			ShoppingCart.InitialiseLightboxHolder();
			$("LightBoxHolder").update(z.imageHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 535, 520, false );
		},
		HideLargeImage:function() {
			ShoppingCart.CloseLightBox();
		}
	},
	ValidateProductEnquiryForm:function() {
		if($("yourName").value.blank()) {
			FloralDecor.HighlightAndFocus("yourName");
			return false;
		}
		if($("yourTelephone").value.blank()) {
			FloralDecor.HighlightAndFocus("yourTelephone");
			return false;
		}
		if($("yourEmail").value.blank()) {
			FloralDecor.HighlightAndFocus("yourEmail");
			return false;
		}
		if($("yourEnquiry").value.blank()) {
			FloralDecor.HighlightAndFocus("yourEnquiry");
			return false;
		}
		return true;
	},
	HighlightAndFocus:function(id) {
		new Effect.Highlight(id);
		$(id).focus();
	},
	ChangeProductView:function(id) {
		var ppId = $("productChanger").value;
		if(ppId!=0) {			
			var url = "index.cfm?action=Products_GetProductDetails&id="+id+"&ppId="+ppId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:FloralDecor.ChangeProductView_onSuccess, onFailure:errFunc});
		}
	}	
	,ChangeProductView_onSuccess:function(t) {
		z = parseJSON( t.responseText );
		$("productImage").update(z.imageHTML);
		$("productPriceEuro").innerHTML = z.price;
		$("ppId").value = z.ppId;
	}
	,AddToCart:function() {
		$("addToCartButton").disabled=true;
		document.addToCartForm.submit();								
	}
	,SerializeFormValues:function() {
		var f = "";
		var objArray = new Array('pId','ppId','pageNo');		
		for(i=0;i<objArray.length;i++) {
			f+=objArray[i] + " = " + $(objArray[i]).value + "\n";
		}
		return f;
	}
}

var Account = {
	ViewOrder:function(orderId) {
		var url = "index.cfm?action=MyAccount_ViewOrder&orderId="+orderId;
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:Account.ViewOrder_onSuccess, onFailure:errFunc});
	},
	ViewOrder_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.viewOrderHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 600, 600, false );
	}
}

var ShoppingCart = {
	ShowSearch:function() {
		var url = "index.cfm?action=Search_ShowBox";
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ShowSearch_onSuccess, onFailure:errFunc});
		return false;
	},
	ShowSearch_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.searchBoxHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 400, 400, false );
		$("searchTerm").focus();
		return false;
	},
	ValidateSearchForm:function() {
		if($("searchTerm").value.blank()) {
			ShoppingCart.HighlightAndFocus("searchTerm");
			return false;
		} else {
			return true;
		} 	
	},
	AddGiftWrap:function(origPrice,shipPrice,gwPrice) {				
		if($("shippingCountryId")) {
			shipPrice = $("shippingCountryId").options[$("shippingCountryId").selectedIndex].getAttribute("key1");
		}
		var gw = $("giftWrap").checked;
		var s = parseFloat(origPrice)+parseFloat(shipPrice)+parseFloat(gwPrice);
		var ns = parseFloat(parseFloat(origPrice)+parseFloat(shipPrice));
		if(gw) {
			$("orderTotal").value = s.toFixed(2);
			$("grandTotal").update(s.toFixed(2));
			$("finalTotal").update(s.toFixed(2));
		} else {
			$("orderTotal").value = ns.toFixed(2);
			$("grandTotal").update(ns.toFixed(2));
			$("finalTotal").update(ns.toFixed(2));
		}
		new Effect.Highlight($("grandTotal"),{startcolor:'#C6EC6A'});
		new Effect.Highlight($("finalTotal"),{startcolor:'#C6EC6A'});
	},
	UpdateShipping:function(obj,origValue) {
		var shipping = obj.value;
		var shippingId = $("shippingId").options[$("shippingId").selectedIndex].getAttribute("key");
		var url = "index.cfm?action=ShoppingCart_UpdateShippingFromCart&shippingId="+shippingId;
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.UpdateShipping_onSuccess, onFailure:errFunc});
		
		if(shipping==-1) {
			$("shippingCost").update("0.00");
			$("grandTotal").update(origValue);
		} else {
			var grandTotalValue = parseFloat(origValue)+parseFloat(shipping);
			$("shippingCost").update(parseFloat(shipping).toFixed(2));
			$("grandTotal").update(grandTotalValue.toFixed(2));
		}
	},
	UpdateShipping_onSuccess:function(t) {
		z = parseJSON(t.responseText);
	},
	RecalculateShipping:function() {
		var oldShippingGroup = cShippingGroup;
		var oldShippingCost = cShippingGroupPrice;		
		if($("shippingCountryId").value!=-1) {	
			var newShippingGroupCost = $("shippingCountryId").options[$("shippingCountryId").selectedIndex].getAttribute("key1");
			var newShippingGroup = $("shippingCountryId").options[$("shippingCountryId").selectedIndex].getAttribute("key2");			
			var newShippingCountry = $("shippingCountryId").options[$("shippingCountryId").selectedIndex].getAttribute("key3");
			$("pp1").innerHTML=newShippingGroup;
			$("pp2").update(newShippingCountry);
			$("shippingCost").update(newShippingGroupCost);
			ShoppingCart.AddGiftWrap(cTotal,parseFloat(newShippingGroupCost),gwPrice);
		}
	},
	ShowEditAccountDetails:function() {
		var url = "index.cfm?action=ShoppingCart_ShowEditAccountDetailsForm";
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ShowEditAccountDetails_onSuccess, onFailure:errFunc});
	},
	ShowEditAccountDetails_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.editAccountFormHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 500, 400, false );
		$("firstname").focus();
		return false;
	},
	ValidateEditAccountForm:function() {
		if($("firstname").value.blank()) {
			ShoppingCart.HighlightAndFocus("firstname");
			return false;
		} else if($("lastname").value.blank()) {
			ShoppingCart.HighlightAndFocus("lastname");
			return false;
		} else if($("customerEmail").value.blank() || !$("customerEmail").value.isEmail()) {
			ShoppingCart.HighlightAndFocus("customerEmail");
			return false;
		} else if($("customerPassword1").value.blank()) {
			ShoppingCart.HighlightAndFocus("customerPassword1");
			return false;
		} else if($("customerPassword2").value.blank()) {
			ShoppingCart.HighlightAndFocus("customerPassword2");
			return false;
		} else if($("customerPassword1").value!=$("customerPassword2").value) {
			ShoppingCart.HighlightAndFocus("customerPassword1");
			ShoppingCart.HighlightAndFocus("customerPassword2");
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ValidateEditAccountForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "editAccountForm" ) )});		
		return false;
	},
	ValidateEditAccountForm_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.CloseLightBox();
	},
	ShowEditShippingDetails:function() {
		var url = "index.cfm?action=ShoppingCart_ShowEditShippingDetailsForm";
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ShowEditShippingDetails_onSuccess, onFailure:errFunc});
	},
	ShowEditShippingDetails_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.editShippingDetailsFormHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 500, 400, false );
		$("address1").focus();
		return false;
	}, 
	ValidateEditShippingDetailsForm:function() {
		
		if($("address1").value.blank()) {
			ShoppingCart.HighlightAndFocus("address1");
			return false;
		} else if($("city").value.blank()) {
			ShoppingCart.HighlightAndFocus("city");
			return false;
		} else if($("telephone").value.blank()) {
			ShoppingCart.HighlightAndFocus("telephone");
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ValidateEditShippingDetailsForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "editShippingDetailsForm" ) )});		
		return false;
	},
	ValidateEditShippingDetailsForm_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.CloseLightBox();
	},
	ChangeShippingAddress:function() {
		if($("shippingChooser").checked) {
			var url = "index.cfm?action=ShoppingCart_ChangeShippingAddress";
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ChangeShippingAddress_onSuccess, onFailure:errFunc});
		} else {
			$("shippingAddressHolder").update("&nbsp;");
			$("pp1").innerHTML=cShippingGroup;
			$("pp2").update(cShippingCountry);
			$("shippingCost").update(cShippingGroupPrice);
			ShoppingCart.AddGiftWrap(cTotal,cShippingGroupPrice,gwPrice);
		}
	},
	ChangeShippingAddress_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		$("shippingAddressHolder").update(z.shippingAddressHTML);
	},
	RemoveItem:function(key) {
		var shippingId = $("shippingId").options[$("shippingId").selectedIndex].getAttribute("key");
		var url = "index.cfm?action=ShoppingCart_RemoveItem&key="+key+"&shippingId="+shippingId;
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.RemoveItem_onSuccess, onFailure:errFunc});
		return false;
	}
	,RemoveItem_onSuccess:function(t) {
		z = parseJSON( t.responseText );
		ShoppingCart.RenderShoppingCart(z.shoppingCartHTML);
	}
	,ValidateCCNumber:function(event) {
		
	}
	,UpdateQTY:function(qty,key) {
		var obj = $("sp_qty_"+key);
		var cVal = parseInt(obj.value);
		var shippingId = $("shippingId").options[$("shippingId").selectedIndex].getAttribute("key");
		if(cVal!=0 && cVal!=qty && isNumeric(cVal)) {
			var url = "index.cfm?action=ShoppingCart_UpdateQTY&key="+key+"&qty="+cVal+"&shippingId="+shippingId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.UpdateQTY_onSuccess, onFailure:errFunc});
		} else {
			obj.value = qty;
		}
	}
	,UpdateQTY_onSuccess:function(t) {
		z = parseJSON( t.responseText );				
		ShoppingCart.RenderShoppingCart(z.shoppingCartHTML);
	}
	,EmptyCart:function() {
		var confirmation = confirm("Are you sure you wish to remove all items from your shopping basket?");
		if(confirmation) {
			var url = "index.cfm?action=ShoppingCart_Empty";
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.EmptyCart_onSuccess, onFailure:errFunc});
		}
		return false;
	}
	,EmptyCart_onSuccess:function(t) {
		z = parseJSON( t.responseText );
		ShoppingCart.RenderShoppingCart(z.shoppingCartHTML);
	}
	,RenderShoppingCart:function(cHTML) {
		$("shoppingCartHolder").update(cHTML);	
		myLightbox = new Lightbox();		
	}
	,Checkout:function(baseHref) {
		if($("shippingId").value==-1) {
			alert("Please choose a delivery option first");
		} else {
			document.location = baseHref + "index.cfm?page=checkout";
		}
	}
	,ValidateLogin:function() {
		if($("email").value.blank() || !$("email").value.isEmail()) {
			ShoppingCart.HighlightAndFocus("email");
			return false;
		} else if($("password").value.blank()) {
			ShoppingCart.HighlightAndFocus("password");
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ValidateLogin_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "loginForm" ) )});		
		return false;
	},
	ValidateLogin_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		if(!z.error) {
			document.location="index.cfm?page="+z.targetURL;
		} else {
			alert(z.errorMessage);
			$("email").focus();
		}	
	},
	HighlightAndFocus:function(id) {		
		new Effect.Highlight($(id),{startcolor:'#C6EC6A'});
		$(id).focus();
	},
	ShowCreateAccountForm:function(targetURL) {
		var url = "index.cfm?action=ShoppingCart_ShowCreateAccountForm&targetURL="+targetURL;			
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ShowCreateAccountForm_onSuccess, onFailure:errFunc});		
	},
	ShowCreateAccountForm_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.createAccountFormHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 500, 400, false );
		$("firstname").focus();
		return false;
	},
	ViewShippingCosts:function() {
		var url = "index.cfm?action=ShoppingCart_ViewShippingCosts";			
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ViewShippingCosts_onSuccess, onFailure:errFunc});
	},
	ViewShippingCosts_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.popupHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 550, 500, false );
	},
	MoreInfo:function(key) {
		var url = "index.cfm?action=ShoppingCart_ViewMoreInfo&key="+key;			
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.MoreInfo_onSuccess, onFailure:errFunc});
	},
	MoreInfo_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.popupHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 400, 300, false );
	},
	ValidateCreateAccountForm:function() {
		if($("firstname").value.blank()) {
			ShoppingCart.HighlightAndFocus("firstname");
			return false;
		} else if($("lastname").value.blank()) {
			ShoppingCart.HighlightAndFocus("lastname");
			return false;
		} else if($("customerEmail").value.blank() || !$("customerEmail").value.isEmail()) {
			ShoppingCart.HighlightAndFocus("customerEmail");
			return false;
		} else if($("customerPassword1").value.blank()) {
			ShoppingCart.HighlightAndFocus("customerPassword1");
			return false;
		} else if($("customerPassword2").value.blank()) {
			ShoppingCart.HighlightAndFocus("customerPassword2");
			return false;
		} else if($("customerPassword1").value!=$("customerPassword2").value) {
			ShoppingCart.HighlightAndFocus("customerPassword1");
			ShoppingCart.HighlightAndFocus("customerPassword2");
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ValidateCreateAccountForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "createAccountForm" ) )});		
		return false;
	},
	ValidateCreateAccountForm_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		if(!z.error) {
			$("LightBoxHolder").update(z.createAccountFormStep2HTML);
			$("address1").focus();
		} else {
			alert(z.errorMessage);
		}
	},
	ValidateCreateAccountStep2Form:function() {
		if($("address1").value.blank()) {
			ShoppingCart.HighlightAndFocus("address1");
			return false;
		} else if($("city").value.blank()) {
			ShoppingCart.HighlightAndFocus("city");
			return false;
		} else if($("telephone").value.blank()) {
			ShoppingCart.HighlightAndFocus("telephone");
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ValidateCreateAccountStep2Form_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "createAccountStep2Form" ) )});		
		return false;
	},
	ValidateCreateAccountStep2Form_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		document.location = "index.cfm?page="+z.targetURL;
	},
	ShowForgotPasswordForm:function() {
		var url = "index.cfm?action=ShoppingCart_ShowForgotPasswordForm";			
		new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ShowForgotPasswordForm_onSuccess, onFailure:errFunc});	
	},
	ShowForgotPasswordForm_onSuccess:function(t) {
		z = parseJSON(t.responseText);	
		ShoppingCart.InitialiseLightboxHolder();
		$("LightBoxHolder").update(z.forgotPasswordFormHTML);	
		MFLightbox.showBoxByID( "LightBoxHolder", 400, 400, false );
		$("emailAddress").focus();
		return false;
	},
	ValidateForgottenPasswordForm:function() {
		if($("emailAddress").value.blank() || !$("emailAddress").value.isEmail()) {
			ShoppingCart.HighlightAndFocus("emailAddress");
			return false;
		}
		new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:ShoppingCart.ValidateForgottenPasswordForm_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "forgottenPasswordForm" ) )});
		return false;
	},
	ValidateForgottenPasswordForm_onSuccess:function(t) {
		z = parseJSON(t.responseText);
		if(z.error) {
			alert(z.errorMessage);
			ShoppingCart.HighlightAndFocus("emailAddress");
		} else {
			ShoppingCart.GenerateMessage(z.messageHTML);
			ShoppingCart.CloseLightBox();
		}
	},
	GenerateMessage:function(m) {
		if(!$("message")) {
			$$("div.copy")[0].insert({top:"<div onclick='ShoppingCart.HideMessage()' id='message' style='display:none'>&nbsp;</div>"});
		}
		$("message").update(m);
		$("message").show();
	},
	HideMessage:function() {
		if($("message")) {
			new Effect.Puff($("message"));
		}
	},	
	TogglePostCode:function(obj,list) {
		var cRequired = true;
		var nArray = list.split(",");
		var val = obj.options[obj.selectedIndex].value;
		for(i=0;i<nArray.length;i++) {
			if(nArray[i]==val) {
				cRequired = false;
				break;
			}
		}
		if(!cRequired) {
			$("postCodeHolder").hide();
		} else {
			$("postCodeHolder").show();
		}
	},
	InitialiseLightboxHolder:function() {
		if(!$("LightBoxHolder")) {
			$$("body")[0].insert("<div id='LightBoxHolder' style='display:none'>Dan</div>");
		}
	},
	CloseLightBox:function() {
		Element.hide('box');
		Element.hide('overlay');
	},
	ChangeCC:function(obj) {
		var cc = obj.value;
		$("ccImage").src="images/creditcards/" + cc + ".png";
		switch(cc) {
			case "laser":
				$("CVVNumberHolder").hide();
			break;
			case "visa":
				$("CVVNumberHolder").show();
			break;
			case "mastercard":
				$("CVVNumberHolder").show();
			break;
			case "amex":
				$("CVVNumberHolder").show();
			break;
		}
		$("ccError").update("&nbsp;");
		$("ccNumber").focus();
	},
	ValidateCheckoutForm:function() {		
		var validated = true;				
		var cCardExpired = false;
		var cCardValidated = true;
		

		if($("shippingFirstName").value.blank()) {
			ShoppingCart.HighlightAndFocus("shippingFirstName");
			return false;
		} else if($("shippingLastName").value.blank()) {
			ShoppingCart.HighlightAndFocus("shippingLastName");
			return false;
		} else if($("shippingAddress1").value.blank()) {
			ShoppingCart.HighlightAndFocus("shippingAddress1");
			return false;
		} else if($("shippingAddress2").value.blank()) {
			ShoppingCart.HighlightAndFocus("shippingAddress2");
			return false;
		} else if($("shippingCity").value.blank()) {
			ShoppingCart.HighlightAndFocus("shippingCity");
			return false;
		}
		
		if( $("rb2") && !$("rb2").checked ) {
			//This is an account checkout
			return true;
		} else {
			// This is a credit card checkout
			cCardValidated = ShoppingCart.CheckCardNumber(true);
			cCardExpired = ShoppingCart.CheckCardExpiry();
	
			if(cCardValidated && !cCardExpired) {
				return true;
			} else {
				return false;
			}			
		}
				
	},
	ChangePaymentType:function(obj) {
		if( obj.value == "CREDITCARD" ) {
			$("accountPayment").hide();
			$("creditcardPayment").show();
		} else {
			$("accountPayment").show();
			$("creditcardPayment").hide();
		}
	},
	CheckCardNumber:function(focusElement) {
		var validated = true;
		var ccNumber = $("ccNumber").value;
		var ccType = $("cardType").value;
		
		switch(ccType) {
			case "laser":
				if(!CreditCard.IsLaser(ccNumber)) {
					if(focusElement) {
						ShoppingCart.HighlightAndFocus("ccNumber");
					} else {
						new Effect.Highlight("ccNumber",{startcolor:'#C6EC6A'});
					}
					$("ccError").update("Not a valid Laser Card number");
					validated=false;
				} else {
					$("ccError").update("&nbsp;");
				}
			break;
			case "visa":
				if(!CreditCard.IsVisa(ccNumber)) {
					if(focusElement) {
						ShoppingCart.HighlightAndFocus("ccNumber");
					} else {
						new Effect.Highlight("ccNumber",{startcolor:'#C6EC6A'});
					}
					$("ccError").update("Not a valid Visa Card number");
					validated=false;
				} else {
					$("ccError").update("&nbsp;");
				}
			break;
			case "mastercard":
				if(!CreditCard.IsMasterCard(ccNumber)) {
					if(focusElement) {
						ShoppingCart.HighlightAndFocus("ccNumber");
					} else {
						new Effect.Highlight("ccNumber",{startcolor:'#C6EC6A'});
					}
					$("ccError").update("Not a valid Mastercard number");
					validated=false;
				} else {
					$("ccError").update("&nbsp;");
				}
			break;
			case "amex":
				if(!CreditCard.IsAmericanExpress(ccNumber)) {
					if(focusElement) {
						ShoppingCart.HighlightAndFocus("ccNumber");
					} else {
						new Effect.Highlight("ccNumber",{startcolor:'#C6EC6A'});
					}
					$("ccError").update("Invalid American Express number");
					validated=false;
				} else {
					$("ccError").update("&nbsp;");
				}
			break;
		}
		return validated;
	},
	CheckCardExpiry:function() {
		var cCardExpired = false;
		var now = new Date();
		var expiryMonth = $("expirationMonth").value;
		var expiryYear = $("expirationYear").value;
		
		if(expiryYear<now.getFullYear()) {
			cCardExpired = true;
		} else {
			if(expiryYear==now.getFullYear() && expiryMonth<now.getMonth()) {
				cCardExpired = true;
			} else {
				cCardExpired = false;
			}
		}
		
		if(cCardExpired) {
			$("edError").update("Your card has expired");
			validated=false;
		} else {
			validated=true;
			$("edError").update("&nbsp;");
		}
		return cCardExpired;
	}
}

var CreditCard = {
	IsVisa:function(cc) {
		if(((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4)) {
			return CreditCard.IsCreditCard(cc);
		} else {
			 return false;
		}
	},
	IsMasterCard:function(cc) {
		var firstdig = cc.substring(0,1);
		var seconddig = cc.substring(1,2);
		if((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) {
			return CreditCard.IsCreditCard(cc);
		} else {
			return false;
		}
	},
	IsAmericanExpress:function(cc) {
		var firstdig = cc.substring(0,1);
		var seconddig = cc.substring(1,2);
		if((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))) {
			return CreditCard.IsCreditCard(cc);
		} else {
			return false;
		}
	},
	IsLaser:function(cc) {
		if((cc.length == 19) && isNumeric(cc)) {
			return true;
		} else {
			return false;
		}
	},
	IsCreditCard:function(st) {
		if(st.length > 19) {
			return false;
		}		
		var sum = 0; 
		var mul = 1; 
		var l = st.length;
		for(i = 0;i<l;i++) {
			digit = st.substring(l-i-1,l-i);
		    tproduct = parseInt(digit ,10)*mul;
		    if (tproduct >= 10) {
		      sum += (tproduct % 10) + 1;
		    } else {
		      sum += tproduct;
			}
		    if(mul == 1) {
		      mul++;
		    } else {
		      mul--;
			}
		}
		
		if((sum % 10) == 0) {
			return true;
		} else {
			return false;
		}
	}
}

String.prototype.isEmail = function () { 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this); 
	return (matches != null && this == matches[0]); 
}

function isNumeric(x) { 
	var y=parseInt(x); 
	if (isNaN(y)) {
	   return false; 
	} else {
		return true;
	}
} 

function LoadingAjax(txt) {
	if(typeof(txt)=="undefined") {
		txt = "Loading...";
	}
	return "<div class='ajaxMsg' id='widgetAjaxMsg'> "+txt+"</div>";
}

function parseJSON( json ){
	var o = eval('(' + json + ')');
	if( o.redirectURL )
	{
		document.location = o.redirectURL;
		return;
	}
	if( $("message") != null ) new Element.remove( "message" );
	if( o.errMsg )
	{
		if( o.errMsgTitle == null ) o.errMsgTitle = "Error";
		if( o.errMsgTimer == null ) o.errMsgTimer = 5000;
		tw.ShowMessage( o.errMsgTitle, o.errMsg, "error", o.errMsgTimer, o.errMsgPosition );
		return o;
	}
	if( o.msgTitle || o.msg )
	{
		if( o.msgTimer == null ) o.msgTimer = 5000;
		tw.ShowMessage( o.msgTitle, o.msg, o.msgClass, o.msgTimer, o.msgPosition );
	}
	return o;
}

var errFunc = function(t) {
	var win = window.open("", "win", "width=1024,height=700,resizable=yes,scrollbars=yes,status=no"); // a window object
	win.document.open("text/html", "replace");
	win.document.write( "<html><body style='margin:0'><div style='border-bottom:1px solid #222;background:#666;padding:10px;'><h1 style='color:#FFF;margin:0;padding:0;'>Teamwork Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}