
String.prototype.RTrim = function() {return this.replace(/\s+$/, "");}
String.prototype.LTrim = function() {return this.replace(/^\s+/, "");}
String.prototype.Trim  = function() {return this.RTrim().LTrim();}

function SubmitToBasket(strProdId, strProdCode, strProdPrice, strProdDesc, iProdQty, strProdWeight) {
	if ((strProdCode.Trim() == "") || (strProdPrice.Trim() == "") || (strProdDesc.Trim() == "")) {
		alert("Error: Could not Add Item To Basket");
	} else {
		document.AddToBasket.hfCurrentPage.value	= location.href
		
		document.AddToBasket.hfProdId.value			= strProdId
		document.AddToBasket.hfProdCode.value		= strProdCode.Trim();
		document.AddToBasket.hfProdPrice.value		= strProdPrice.Trim();
		document.AddToBasket.hfProdDesc.value		= strProdDesc.Trim();
		//document.AddToBasket.hfProdQty.value		= document.DataForm.elements["cboQty"+strProdId.Trim()].value;
		//document.AddToBasket.hfProdWeight.value	= strProdWeight.Trim();
		document.AddToBasket.submit();
	}
}

function SubmitToBasketAJAX(strProdId, strProdCode, strProdPrice, strProdDesc, iProdQty, strProdWeight) {
	//SubmitToBasket Function using AJAX to post data - to be used in conjunection with the quick basket
	if ((strProdCode.Trim() == "") || (strProdPrice.Trim() == "") || (strProdDesc.Trim() == "")) {
		alert("Error: Could not Add Item To Basket");
	} else {
		
		urlToPost = "../cart/addToBasketAJAX.asp";
		urlToPost += "?hfProdId=" + strProdId;
		urlToPost += "&hfProdCode=" + escape(strProdCode.Trim());
		urlToPost += "&hfProdPrice=" + escape(strProdPrice.Trim());
		urlToPost += "&hfProdDesc=" + escape(strProdDesc.Trim());
		//urlToPost += "&hfProdQty=" + escape(document.DataForm.elements["cboQty"+strProdId.Trim()].value);
		//urlToPost += "&hfProdWeight=" + escape(strProdWeight.Trim());

		msg = postAjax(urlToPost);
		
		if (Left(msg,7) == "Success") {
			document.getElementById("basket_summary").innerHTML = Right(msg,((msg.length)-8));
			document.getElementById("basket_item_desc").innerHTML = strProdDesc;
			document.getElementById("basket_item_price").innerHTML = "£" + strProdPrice;
			
			qb_show();
		} else {
			alert("There has been a problem adding this item to the basket, \nPlease wait a moment and try again.\nIf the problem persists please contact us.")
		}
	}
}

function buyNow(formId) {
	formName = 'AddToBasket' + formId
	document[formName].submit();
}

function ShoppingAction(PageName) {
	oEle = document.frmNavigation;
	if (PageName != undefined) {
		if (PageName == "Home") {
			oEle.action = oEle.Normal.value + "index.asp";
		} else if (PageName == "Shopping") {
			if (oEle.Continue.value != "") {
				//If we have a contiunue shopping url
				oEle.action = oEle.Continue.value;
			} else {
				oEle.action = oEle.Normal.value + "index.asp";
			}
		} else if (PageName == "Basket") {
			oEle.action = oEle.Normal.value + "cart/basket.asp";
		} else if (PageName == "LogIn") {
			oEle.action = oEle.Normal.value + "cart/login.asp";
		} else if (PageName == "GoogleCheckout") {
			oEle.action = oEle.Secure.value + "cart/include/google/googleprocess.asp";
		} else if (PageName == "Details") {
			oEle.action = oEle.Secure.value + "cart/personal.asp";
		} else if (PageName == "Payment") {
			oEle.action = oEle.Secure.value + "cart/payment.asp";
		} else if (PageName == "Confirm") {
			oEle.action = oEle.Secure.value + "cart/confirm.asp";
		}
		oEle.submit();
	} else {
		oEle.action = oEle.Home.value; 
		oEle.method = "";
		oEle.submit();
	}
}

function checkTerms(PageName) {
	if (document.frmBasket.Postage.value == "") {
		alert("Please Select a Postage Option");
		document.frmCheckTC.Postage.focus();
	} else if (!document.frmCheckTC.Terms.checked) {
		alert("Please Read and Accept our Terms and Conditions");
		document.frmCheckTC.Terms.focus();
	} else {
		ShoppingAction(PageName)
	}
}


function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
		alert("You can only enter a maximum of " + limitNum + " characters in this field");
	} 
}


function openPopUp(str_URL, int_width, int_height, str_resize, str_scroll) {
	if (int_width == 0) {
		int_width = 600
	} if (int_height == 0) {
		int_height = 400
	} if (str_resize != 'yes') {
		str_resize = 'no'
	} if (str_scroll != 'yes') {
		str_scroll = 'no'
	}
	newwindow = window.open(str_URL, '_blank', 'width='+int_width+',height='+int_height+',menubar=off,directories=off,toolbar=off,resizable='+str_resize+',scrollbars='+str_scroll);
	if (window.focus) {newwindow.focus()};
}



//Add more fields dynamically.
function updateAccessoryNew(id,price,isChecked) {
	if(!document.getElementById) return; //Prevent older browsers from getting any further.
	var accessories = document.getElementById("cart_accessories");

	var totalText = document.getElementById("total").innerHTML;
	var currentTotal = parseFloat(totalText);

	if (isChecked) {
		newTotal = currentTotal + price

		var input = document.createElement("input");
		input.id = 'id_' + id;
		input.name = "id_accessory";
		input.value = id;
		input.type = "hidden";
		accessories.appendChild(input);
	} else {
		if (document.getElementById("id_"+id) != null) {
		//Check if one of the fields exists - if so remove them all (prevents problems when using back button)
			newTotal = currentTotal - price;
			accessories.removeChild(document.getElementById("id_"+id));
		} else {
			newTotal = currentTotal;
		}
	}

	document.getElementById("total").innerHTML = newTotal.toFixed(2);
}



function checkLogin() {
	oEle = document.logForm;
	if (oEle.orderNo.value == "") {
		alert("Please enter the order number you wish to view");
		oEle.orderNo.focus();
	} else if (oEle.emailAddress.value == "") {
		alert("Please enter the email address you used when placing the order");
		oEle.emailAddress.focus();
	} else {
		oEle.submit();
	}
}


function setCurrency(currency) {
	var myURL = relPath + "cart/setCurrency.asp?currency=" + currency;
	msg = ajaxGet(myURL);
	//window.location = myURL;
	
	if (returnSuccess(msg,"There has been a problem setting your currency, \nPlease wait a moment and try again.\nIf the problem persists please contact us.")) {
		window.location.reload();
	}
}