
function goShopCategory(catId){
	$get("catId").value = catId;
	$get("prodId").value = "0";
	$get("pCount").value = "1";
	$get("sKey").value = "";
	$get("mwEvent").value = "VIEWCATEGORY";
	__doPostBack('viewCategory', 'View Category')
}

function showProductDetails(prodId) {
    if ($get("hiddenLyr" + prodId) != null) {
        if ($get("hiddenLyr" + prodId).style.visibility == 'visible') {
            $get("hiddenLyr" + prodId).style.display = 'none';
            $get("hiddenLyr" + prodId).style.visibility = 'hidden';
        }
        else {
            $get("hiddenLyr" + prodId).style.display = '';
            $get("hiddenLyr" + prodId).style.visibility = 'visible';
        }
    }
    else {
        $get("prodId").value = prodId;
        $get("mwEvent").value = "VIEWPRODUCT";
        __doPostBack('viewProduct', 'View Product')
    }
}

function purchaseProduct(prodId, prodPrice, prodName){

	var myString = "";
	myString = prodName.replace(/\|/g,"-");
	prodName = myString;

	myString = prodName.replace(/&/g,"and");
	prodName = myString;

	myString = prodName.replace(/=/g,"-");
	prodName = myString;

	var nameEQ = "MW_CART=";
	var currentStr = "";
	var idsStr = "";
	var priceStr = "";
	var nameStr = "";
	var idFound = 0;

	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' '){
			 c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			currentStr = c.substring(nameEQ.length,c.length);
		}
	}

	if (currentStr != ""){
		var cAry = currentStr.split("&");
		for (j = 0;j < cAry.length;j++){
			pAry = cAry[j].split("=");
			if (pAry[0] == "MW_IDS"){
			    idAry = pAry[1].split("|");
				for (x = 0; x < idAry.length; x++){
					if (idAry[x] == prodId){
						idFound = 1;
					}
	            }
				if (idFound == 0){
					idsStr = pAry[1] + "|" + prodId
				}
				else{
					break;
	            }
			}
			else if ((pAry[0] == "MW_PRICE")&&(idFound == 0)){
				priceStr = pAry[1] + "|" + prodPrice;
			}
			else if ((pAry[0] == "MW_NAME") && (idFound == 0)) {
			    var inputs = document.getElementsByTagName("select");
			    for (var n = 0; n < inputs.length; n++) {
			        if (inputs[n].name.substring(0, 3) == "opt") {
			            prodName = prodName + " - " + inputs[n].options[inputs[n].selectedIndex].value;
			        }
			    }
				nameStr = pAry[1] + "|" + prodName;
			}
		}
	}
	else{
		idsStr = prodId;
		priceStr = prodPrice;
		var inputs = document.getElementsByTagName("select");
		for (var n = 0; n < inputs.length; n++) {
		    if (inputs[n].name.substring(0,3) == "opt") {
		        prodName = prodName + " - " + inputs[n].options[inputs[n].selectedIndex].value;
		    }
		}
		nameStr = prodName;
	}
	
	if (idFound == 1) {
	    alert("This product is already in your shopping cart.");
    }
	else{
	    if (idsStr != ""){
		    var today = new Date();
		    var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000);

		    cookieStr = "MW_IDS=" + idsStr;
		    cookieStr = cookieStr + "&MW_PRICE=" + priceStr;
		    cookieStr = cookieStr + "&MW_NAME=" + nameStr;

		    document.cookie = "MW_CART=" + cookieStr + "; expires=" + expiry.toGMTString() + "; path=/";

		    alert(prodName + " has been added to your Shopping cart.\rClick CART / CHECKOUT at any time to view your cart and proceed to Checkout.")
	    }
	    else{
		    document.cookie = "MW_CART=; expires=" + expiry.toGMTString() + "; path=/";
	    }
	}
}

function cookieRemove(prodId){

	var nameEQ = "MW_CART=";
	var currentStr = "";
	var idsStr = "";
	var priceStr = "";
	var nameStr = "";
	var idFound = 0;
	var newidsStr = "";
	var newpriceStr = "";
	var newnameStr = "";

	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' '){
			 c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			currentStr = c.substring(nameEQ.length,c.length);
		}
	}

	if (currentStr != ""){
		var cAry = currentStr.split("&");
		for (j = 0;j < cAry.length;j++){
			pAry = cAry[j].split("=");
			if (pAry[0] == "MW_IDS"){
				idAry = pAry[1].split("|");
				for (x = 0; x < idAry.length; x++){
					if (idAry[x] == prodId){
						idFound = x;
					}
				}
				idsStr = pAry[1];
			}
			else if (pAry[0] == "MW_PRICE"){
				priceStr = pAry[1];
			}
			else if (pAry[0] == "MW_NAME"){
				nameStr = pAry[1];
			}
		}
	}

	if (idFound != 0){

		idAry = idsStr.split("|");
		for (x = 0; x < idAry.length; x++){
			if (x != idFound){
				if (newidsStr == ""){
					newidsStr = idAry[x]
				}
				else{
					newidsStr = newidsStr + "|" + idAry[x]
				}
			}
		}

		idAry = priceStr.split("|");
		for (x = 0; x < idAry.length; x++){
			if (x != idFound){
				if (newpriceStr == ""){
					newpriceStr = idAry[x]
				}
				else{
					newpriceStr = newpriceStr + "|" + idAry[x]
				}
			}
		}

		idAry = nameStr.split("|");
		for (x = 0; x < idAry.length; x++){
			if (x != idFound){
				if (newnameStr == ""){
					newnameStr = idAry[x]
				}
				else{
					newnameStr = newnameStr + "|" + idAry[x]
				}
			}
		}
	}

	var today = new Date();
	var expiry = new Date(today.getTime() + 28 * 24 * 60 * 60 * 1000);

	cookieStr = "MW_IDS=" + newidsStr;
	cookieStr = cookieStr + "&MW_PRICE=" + newpriceStr;
	cookieStr = cookieStr + "&MW_NAME=" + newnameStr;

	if (newidsStr != ""){
		document.cookie = "MW_CART=" + cookieStr + "; expires=" + expiry.toGMTString() + "; path=/";
		viewShoppingCart();
	}
	else{
	    document.cookie = "MW_CART=; expires=" + expiry.toGMTString() + "; path=/";
	    $get("mwEvent").value = "PRODUCTS";
	    __doPostBack('viewShoppingCart', 'View Shopping Cart')
	}
}

function viewShoppingCart(){

	var ca = document.cookie.split(';');
	var currentStr = "";
	var nameEQ = "MW_CART=";
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' '){
			 c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			currentStr = c.substring(nameEQ.length,c.length);
		}
	}

	if (currentStr != "") {
	    $get("mwEvent").value = "SHOPPINGCART";
	    __doPostBack('viewShoppingCart', 'View Shopping Cart')
	}
	else{
		alert("Your Shopping Cart is empty.")
	}
}

function shoppingCartAddQty(prodId){

    var inputs = document.getElementsByTagName("input");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].name == ("prodQty" + prodId)){
			inputs[n].value = parseInt(inputs[n].value) + 1;
			prodQty = inputs[n].value;
		}
		if (inputs[n].name == ("prodPrice" + prodId)){
			prodPrice = inputs[n].value;
		}
	}

	newPrice = parseFloat(prodPrice) * parseInt(prodQty);
	newPrice = "" + newPrice;
	
	if (newPrice.indexOf(".") > 0){
		pAry = newPrice.split(".");
		if (pAry[1].length > 2){
			newPrice = pAry[0] + "." + pAry[1].substring(0,2);
		}
		else if (pAry[1].length == 1){
			newPrice = newPrice + "0";
		}
	}
	else {
		newPrice = newPrice + ".00";
	}

    var inputs = document.getElementsByTagName("div");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].id == ("cPrice" + prodId)){
			inputs[n].innerHTML = newPrice;
		}
	}
	updateShopTotals();
}

function shoppingCartSubQty(prodId){

    var inputs = document.getElementsByTagName("input");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].name == ("prodQty" + prodId)){
			inputs[n].value = parseInt(inputs[n].value) - 1;
			prodQty = inputs[n].value;
			if (inputs[n].value == "0"){
				//UPDATE COOKIE - REFRESH PAGE
				cookieRemove(prodId);
			}
		}
		if (inputs[n].name == ("prodPrice" + prodId)){
			prodPrice = inputs[n].value;
		}
	}

	newPrice = parseFloat(prodPrice) * parseInt(prodQty);
	newPrice = "" + newPrice;

	if (newPrice.indexOf(".") > 0){
		pAry = newPrice.split(".");
		if (pAry[1].length > 2){
			newPrice = pAry[0] + "." + pAry[1].substring(0,2);
		}
		else if (pAry[1].length == 1){
			newPrice = newPrice + "0";
		}
	}
	else {
		newPrice = newPrice + ".00";
	}

    var inputs = document.getElementsByTagName("div");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].id == ("cPrice" + prodId)){
			inputs[n].innerHTML = newPrice;
		}
	}

	updateShopTotals();
}

function updateShipping(){
	updateShopTotals();
}

function updateShopTotals(){
	
	var nameEQ = "MW_CART=";
	var ca = document.cookie.split(';');
	var idsStr = "";
	var currentStr = "";
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' '){
			 c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			currentStr = c.substring(nameEQ.length,c.length);
		}
	}

	if (currentStr != ""){
		var cAry = currentStr.split("&");
		for (j = 0;j < cAry.length;j++){
			pAry = cAry[j].split("=");
			if (pAry[0] == "MW_IDS"){
				idAry = pAry[1].split("|");
				idsStr = pAry[1];
			}
		}
	}
	
	var shopTotal = 0;
    var inputs = document.getElementsByTagName("input");
    idAry = idsStr.split("|");
    for (var j = 0;j < idAry.length; j++){
		prodQty = 0;
		prodPrice = 0;
		for (var n=0; n < inputs.length; n++) {
			if (inputs[n].name.indexOf("prodQty" + idAry[j]) == 0){
				prodQty = inputs[n].value;
			}
			if (inputs[n].name.indexOf("prodPrice" + idAry[j]) == 0){
				prodPrice = inputs[n].value;
			}
		}
		shopTotal = shopTotal + (parseInt(prodQty) * parseFloat(prodPrice))
	}
	
	var idRef = 0;
    var inputs = document.getElementsByTagName("select");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].name == "shOptions"){
			for (var j = 0;j < inputs[n].options.length; j++){
				if (inputs[n].options[j].selected == true){
					idRef = inputs[n].options[j].value;
				}
			}
		}
	}

	var shippingTotal = 0;
	var expStr = "&nbsp;";
	if (idRef != 0){
		shipAmt = 0;
		for (k = 0; k < emsShipping.length;k++){
			if (emsShipping[k][0] == idRef){
				shipValue = emsShipping[k][2];
				shipMin = emsShipping[k][3];
				shipMod = emsShipping[k][4];
				shipInc = emsShipping[k][5];
				if (shipMin != "0"){
					expStr = "Shipping & Handling is $" + shipValue + " for purchase totals under $" + parseFloat(shipMin) + "<br />";
					expStr = expStr + " the amount increases $" + shipMod + " for each $" + shipInc + " (or portion there of) over $" + parseFloat(shipMin) + "";
					if (parseFloat(shopTotal) > parseFloat(shipMin)){
						shipAmt = parseFloat(shipMod);
						diffAmount = parseFloat(shopTotal) - parseFloat(shipMin);
						while (parseFloat(diffAmount) > parseFloat(shipInc)){
							shipAmt = shipAmt + parseFloat(shipMod);
							diffAmount = parseFloat(diffAmount) - parseFloat(shipInc)
						}
					}
					shippingTotal = parseFloat(shipAmt) + parseFloat(shipValue);
				}
				else{
					shippingTotal = emsShipping[k][2];
				}
			}
		}
	}

	shopTotal = parseFloat(shopTotal) + parseFloat(shippingTotal);
	
	var totalStr = "" + shopTotal;
	if (totalStr.indexOf(".") > 0){
		pAry = totalStr.split(".");
		if (pAry[1].length > 2){
			totalStr = pAry[0] + "." + pAry[1].substring(0,2);
		}
		else if (pAry[1].length == 1){
			totalStr = totalStr + "0";
		}
	}
	else {
		totalStr = totalStr + ".00";
	}

	var shippingTotal = "" + shippingTotal;
	if (shippingTotal.indexOf(".") > 0){
		pAry = shippingTotal.split(".");
		if (pAry[1].length > 2){
			shippingTotal = pAry[0] + "." + pAry[1].substring(0,2);
		}
		else if (pAry[1].length == 1){
			shippingTotal = shippingTotal + "0";
		}
	}
	else {
		shippingTotal = shippingTotal + ".00";
	}
	
        var inputs = document.getElementsByTagName("div");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].id == ("shopTotal")){
			inputs[n].innerHTML = totalStr;
		}
		if (inputs[n].id == ("shipTotal")){
			inputs[n].innerHTML = shippingTotal;
		}
		if (inputs[n].id == ("shipexp")){
			inputs[n].innerHTML = expStr;
		}
	}
        var inputs = document.getElementsByTagName("input");
	for (var n=0; n < inputs.length; n++) {
		if (inputs[n].name == ("cartTotal")){
			inputs[n].value = totalStr;
		}
		if (inputs[n].name == ("shippingTotal")){
			inputs[n].value = shippingTotal;
		}
	}

}

function showLargeImage(prodId, pWidth, pHeight, pPercent){
    if (pPercent != "0") {
        pWidth = (parseInt(pWidth) / 100) * parseInt(pPercent)
        pHeight = (parseInt(pHeight) / 100) * parseInt(pPercent)
        $get("lImages").width = parseInt(pWidth);
        $get("lImages").height = parseInt(pHeight);
        $get("largeImageClose").style.width = parseInt(pWidth);
	}
	getPageSizeWithScroll()
	$get("lImages").src = $get("mainPropImage" + prodId).src;
	eval("document" + myAll + "overlay" + myStyles + ".visibility='visible'")
	eval("document" + myAll + "iOverlay" + myStyles + ".visibility='visible'")
	document.location = "#top";
}

function hideLargeImage(){
	eval("document" + myAll + "overlay" + myStyles + ".height='100%'")
	eval("document" + myAll + "overlay" + myStyles + ".width='100%'")
	eval("document" + myAll + "overlay" + myStyles + ".visibility='hidden'")
	eval("document" + myAll + "iOverlay" + myStyles + ".visibility='hidden'")
}

function upDateImage(imgName, prodId){
	$get("mainPropImage" + prodId).src = "/Images/Products/" + imgName;
}

function shopPage(pNo){
    $get("pCount").value = pNo;
    $get("mwEvent").value = "NEXTPAGE";
    __doPostBack('nextShopPage', 'Next Shopping Page')
}

function shopCount(){
    $get("maxPage").value = $get("rCount").options[$get("rCount").options.selectedIndex].value;
    $get("mwEvent").value = "JUMPPAGE";
    __doPostBack('jumpShopPage', 'Jump Shopping Page')
}

function shopJump() {
    if ($get("sJump").options[$get("sJump").options.selectedIndex].value != "0") {
        $get("pCount").value = $get("sJump").options[$get("sJump").options.selectedIndex].value;
        $get("mwEvent").value = "JUMPPAGE";
        __doPostBack('jumpShopPage', 'Jump Shopping Page')
    }
}

function shopResults(){
    $get("prodId").value = "0";
    $get("mwEvent").value = "PRODUCTLIST";
    __doPostBack('seeCategoryList', 'See Category List')
}

function sendFriend(){
	if ($get("sfEmail").value != ""){
		if (!checkEmail($get("sfEmail"))){
			return;
		}
	}
	else{
		alert("To e-mail address may not be blank");
		$get("sfEmail").focus();
		$get("sfEmail").select();
		return;
    }
    $get("mwEvent").value = "SENDEMAIL";
    __doPostBack('sendFriendEmail', 'Send Friend E-mail')
}

function goSearch(whichSearch) {
    if (whichSearch == 1) {
        if ($get("pSearch").value == "") {
            alert("Please enter at least one word to search for.")
            return;
        }
    }
    else {
        if ($get("sOpt1").options[0].selected == true) {
            alert("Please select a keyword/phrase to search for.")
            return;
        }
        var sAry = $get("sOpt1").options[$get("sOpt1").selectedIndex].value.split("|");
        $get("pSearch").value = sAry[1]
        if ($get("sOpt2")) {
            if ($get("sOpt2").options[0].selected == false) {
                $get("pSearch").value = $get("pSearch").value + " " + $get("sOpt2").options[$get("sOpt2").selectedIndex].value
            }
        }
    }
    $get("prodId").value = "0"
    $get("sKey").value = $get("pSearch").value;
    $get("mwEvent").value = "PRODUCT SEARCH";
    __doPostBack('productSearch', 'Product Search')
}

function goGloablSearch(pageId, whichSearch) {
    if (whichSearch == 1) {
        if ($get("pSearch").value == "") {
            alert("Please enter at least one word to search for.")
            return;
        }
    }
    else {
        if ($get("sOpt1").options[0].selected == true) {
            alert("Please select a keyword/phrase to search for.")
            return;
        }
        var sAry = $get("sOpt1").options[$get("sOpt1").selectedIndex].value.split("|");
        $get("pSearch").value = sAry[1]
        if ($get("sOpt2")) {
            if ($get("sOpt2").options[0].selected == false) {
                $get("pSearch").value = $get("pSearch").value + " " + $get("sOpt2").options[$get("sOpt2").selectedIndex].value
            }
        }
    }
    $get("mId").value = pageId
    $get("prodId").value = "0"
    $get("sKey").value = $get("pSearch").value;
    $get("mwEvent").value = "PRODUCT SEARCH";
    __doPostBack('productSearch', 'Product Search')
}

function shoppingCheckout(ccId){
    if ($get("shOptions") != null) {
        if ($get("shOptions").options[0].selected == true) {
            alert("You must select Shipping Option.")
            return false;
        }
    }
	if ($get("shopName").value == ""){
		alert("Customer Name may not be left blank.");
		return false;
	}
	if ($get("shopEmail").value == ""){
		alert("Customer Email may not be left blank.");
		return false;
	}
	else{
		if (!checkEmail($get("shopEmail"))){
			return;
		}
	}
	if (!checkStandard($get("shopPhone"), 'Customer Phone',4)){
		return;
    }
    $get("CCID").value = ccId;
    $get("mwEvent").value = "SHOPPINGCARTCHECKOUT";
    __doPostBack('shoppingCheckout', 'Shopping Checkout')
}

function showPurchaseWindow() {
    obj = document.form1;
    obj.action = $get("purchaseUrl").value;
	obj.target = "PURCHASE";
	obj.submit();
	obj.target = "";
	obj.action = "/default.aspx";
}

function orderComplete() {

    obj = document.form1;
    obj.target = "";
    obj.action = "/default.aspx";
    
	document.cookie = "MW_CART=; expires=; path=/";
	$get("mwEvent").value = "PRODUCT";
	__doPostBack('productDisplay', 'Product Display')
}
