function useEnter (field, event, action) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13) {
        eval(action);
        return false;
    } 
    else
    return true;
}

/* SEARCH */
var search_cleared = 0;
function ClearSearchBox() {
    if (search_cleared == 0) {
        document.getElementById('find_spec').value = "";
        search_cleared = 1;
    }
}
function ValidateSearch() {
    var srchtxt = document.getElementById('find_spec').value;
    var isnum = 0;
    // See if the search is a number
    if (!isNaN(srchtxt)) {
        if (srchtxt.indexOf(".") >= 0) {
            isnum = 0;
        } else {
            isnum = 1;
        }
    }
    // If searching a number, force the search category to All Products (blank)
    if (isnum == 1) {
        document.getElementById('searchcategory').value = "";
    }
    return true;
}

/* COOKIES */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	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) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* MEMBER CHECKING
  Member checking refers to an attempt to migrate customer data from eOne to eController as the customer logs in
*/
function catchLogin() {
    var usr = document.shopper_lookup.strEmail.value;
    var pwd = document.shopper_lookup.strPassword.value;
    createCookie('mbrchk1',usr,1);
    createCookie('mbrchk2',pwd,2);
    //return CheckResubmit();
}
function loadMemberCheck() {
    var usr = readCookie('mbrchk1');
    var pwd = readCookie('mbrchk2');
    parent.document.getElementById('mbrcheckframe').style.display = "none";
    window.location = "https://personalize.kvvet.com/mbrcheck/mbr_login.php?usr="+usr+"&pwd="+pwd;
}
function loadLoginFailPage() {
    document.getElementById('mbrcheckframe').src = "/kvvet/assets/html/mbr_loginfail.html";
}
function refreshNewMember() {
    if (document.frmShopper.strBillFirstName.value != "") {
        eraseCookie('mbrchk1');
        eraseCookie('mbrchk2');
        window.top.location = "/KVVet/shopper_confirm.asp?message=shopper_update_msg&srcpg=shopupd";
    }
}

// ---- SHOPPING BASKET ---- //

// Default values and a list of non-vaccine items that are temp sensitive are defined in temp_sensitive_items.js

// NOTE: deleteItemFromBasket(ix) is defined in basket_item_ship_to_help.inc because it needs Sigma script tags

function autoAdd(service) {
	document.BasketBuy.txtBasketBuy.value = service;
	document.BasketBuy.submit();
}
function checkMinimum() {
    var li = LineItems;
    var charge = 0;
    var exempt = 0;
    var vax1 = 0;
    var vax2 = 0;
    var subtotal = 0;
    
    // Return array
    var rtn = new Array(0,0);
    
	for(i=0;i<li.length;i++) {
        if(li[i].SKU == 'MINIMUM') {
           charge += minchg_amt;
           rtn[0] = i;
        }
        if(li[i].SKU == 'ONA') {
           vax1 = nextday_amt;
        }
        if(li[i].SKU == 'BLA') {
           vax2 = secondday_amt;
        }
        subtotal += li[i].AdjPrice;
    }
    exempt = vax1 + vax2 + charge;
    var adjTotal = subtotal - exempt;
    if((charge < 1) && (adjTotal < order_minimum) && (adjTotal > 0)) {
        rtn[1] = 1; // Charge will be added
    }
    if((charge > 1) && (adjTotal >= order_minimum)) {
        rtn[1] = 2; // Charge will be removed with alert
    }
    if((charge > minchg_amt) || ((charge > 1) && (adjTotal == 0))) {
        rtn[1] = 3; // Charge will be silently removed
    }
    if((charge > 1) && (adjTotal < order_minimum) && (adjTotal > 0)) {
        rtn[1] = 4; // Charge remains
    }
    return rtn;
}
function checkVaccine() {
	var li = LineItems;
    var Loc = 0;
    var onaPosition = 0;
    var blaPosition = 0;
    var Pos = 0;
    var Last = 0;
    var Vax = 0;
    var Ship = 0;
	var TSByWeight = 0;
    var rtn = new Array(0,0,0);
    
	for (i=0; i<li.length; i++) {
	    Loc = li[i].SKU.charAt(0);
        if(Loc == '1') {
             Vax += 1;
        }
        for (j=0; j<TempSensItem.length; j++) {
            if (li[i].SKU == TempSensItem[j]) {
                Vax += 1;
            }
        }
		for (k=0; k<TempSensByWeight.length; k++) {
			if (li[i].SKU == TempSensByWeight[k]) {
				TSByWeight += 1;
			}
		}
        if(li[i].SKU == 'ONA') {
            Ship += 1;
            onaPosition = i;
            Pos = i;
        }
        if(li[i].SKU == 'BLA') {
            Ship += 1;
            blaPosition = i;
            Pos = i;
        }
        Last = i;
    }
    if (li.length > 0) {
        if(Vax > 0 && Ship < 1) {
            rtn[1] = 1; // Vaccine shipping will be added
        }
        if((li[Last].SKU == 'BLA') && (Ship > 1)) {
            rtn = Array(onaPosition,2); // Next Day charge will be removed after switching to 2nd Day
        }
        if(li[Last].SKU == 'ONA' && Ship > 1) {
            rtn = Array(blaPosition,3); // 2nd Day charge will be removed after switching to Next Day
        }
        if(Vax < 1 && Ship > 0) {
            rtn = Array(Pos,4); // Vaccine shipping will be removed
        }
        if(Vax > 0 && Ship == 1) {
            if (onaPosition > 0) {
                rtn[1] = 5;
            } else {
                rtn[1] = 6;
            }
        }
		if (TSByWeight > 0) { // Temp sensitive item exists, but it is one that is charged by weight not flat-rate
			rtn[1] = 7;
		}
    }
    return rtn;
}
function CustomPromo() {
    var li = LineItems;
    var subtotal = 0;
    var RequiredItemExists = 0;
    var RebateExists = 0;
    var RebateIndex = 0;
    var RebateStatus = 0;
    /*for (i=0; i<li.length; i++) {
        if ((li[i].SKU != "MINIMUM") && (li[i].SKU != "ONA") && (li[i].SKU != "BLA") && (li[i].SKU != "26505")) {
            subtotal += li[i].AdjPrice;
        } else if (li[i].SKU == "26505") {
            RequiredItemExists = 1;
        }
        if (li[i].SKU == "REBATE1") {
            RebateExists = 1;
            RebateIndex = i;
        }
    }
    if ((subtotal >= 5000) && (RequiredItemExists == 1) && (RebateExists == 0)) {
        document.getElementById('custom_promo_message').style.display = "block";
    }
    if (RebateExists == 1) {
        if (((subtotal >= 5000) && (RequiredItemExists == 0)) || (subtotal < 5000)) {
            deleteItemFromBasket(RebateIndex);
        }
    }*/
}
function ApplyCustomPromo() {
    autoAdd('REBATE1');
}
function SaveCartString() {
	var li = LineItems;
	var PriceGrabberString = "";
	for (i=0; i<li.length; i++) {
		var e = i + 1;
		PriceGrabberString += "item" + e + "=||" + (li[i].CurPrice / 100).toFixed(2) + "|" + li[i].SKU + "||" + li[i].Qty + "&";
	}
	createCookie('pgcart', PriceGrabberString, 7);
}
function BasketCheck() {
    var MinChg = checkMinimum();
    var Vax = checkVaccine();
	// save cart string
	SaveCartString();
    var RunCustomPromo = 0;
    switch(MinChg[1]) {
        case 1:
            // Add the minimum order charge
            alert("A $5.00 minimum order charge applies to all orders with merchandise totaling less than $50.00. Please wait a moment while this charge is added. It will be removed if your merchandise total reaches $50.00. Thank you!");
            autoAdd('MINIMUM');
            break;
        case 2:
            // Minimum value reached - remove charge with alert
            alert("Please wait a moment while the minimum order charge is removed. Thank you!");
        case 3:
            // Basket is empty, or the charge exists more than once - remove charge silently
            deleteItemFromBasket(MinChg[0]);
            break;
        default:
            // Min charge either not required or stays the same.
            // If charge remains, display message
            if (MinChg[1] == 4) document.getElementById('min_message').style.display = "block";
            
            // Determine if we can check for the custom promo yet (done after the vaccine activity)
            if (Vax[1] != 1) RunCustomPromo = 1;
            
            // VACCINE SHIPPING:
            switch(Vax[1]) {
                case 1:
                    // Vaccine charge needs to be added
                    alert("Please wait a moment while a Temperature Sensitive Shipping Charge is added. Vaccines and other designated temperature sensitive products require expedited shipping for a special flat rate. Thank you!");
                    autoAdd(default_tempsens);
                    break;
                case 2:
                    // Remove the Next Day charge because we've switched to 2nd Day
                case 3:
                    // Remove the 2nd Day charge because we've switched to Next Day
                case 4:
                    // No more vaccines, remove shipping charge
                    deleteItemFromBasket(Vax[0]);
                    break;
                case 5:
                    document.getElementById('ona_message').style.display = "block";
                    createCookie('TSShipping','1',1);
                    break;
                case 6:
                    document.getElementById('bla_message').style.display = "block";
                    createCookie('TSShipping','2',1);
                    break;
				case 7:
					createCookie('TSShipping','9',1);
					break;
                default:
                    createCookie('TSShipping','0',1);
                    break;
            }
            
            if (RunCustomPromo == 1) {
                CustomPromo();
            }
    }
}
function SubmitPromoCode() {
    var pcobj = document.getElementById('promocode');
    if (pcobj == null) return;
    var promocode = pcobj.value;
    if (promocode == "") return;
    var tmpHREF = new String(location.href.toLowerCase());
    var url = tmpHREF;
    if (tmpHREF.indexOf("refcode=") < 0) {
        url += "&refcode="+promocode;
    }
    window.location = url;
}

/* Legacy */
var ok = (document.images ? true : false);
var rootURL = '/KVVet/'

if (ok) {
	ln_tri = new Image;
        ln_tri.src = "/KVVet/assets/images/ln_tri.gif";
        ln_tri_on = new Image;
        ln_tri_on.src = "/KVVet/assets/images/ln_tri_on.gif";
        
		ln_fill = new Image;
		ln_fill.src = "/KVVet/assets/images/fill.gif";        	
}
function rollover(whichimg,overimg) {
   if (ok) {
       eval('document.images["' + whichimg + '"].src = ' + overimg + '.src');
   }
}
function photoEnlarge() {
	var prodID;
	var tmpHREF = new String(location.href.toLowerCase());
	var questionMark = tmpHREF.indexOf("?");
	if (questionMark == -1) {
		var prodID_loc = tmpHREF.split("/");
		prodID = prodID_loc[5];
	} else {
		var rszHREF = tmpHREF.split("?");
		var pageHREF = rszHREF[1].split("=");
		var idHREF = pageHREF[1].split("&");
		if (idHREF.length > 1) {
			prodID = idHREF[0];
		} else {
			prodID = pageHREF[1];
		}
	}	
	window.open('/KVVet/assets/product_images/large' + prodID + '.jpg','photoEnlarged','width=550,height=550,scrollbars=yes,resizable=yes');
}

var disable_ai = false;
function pageSpecificActions(myURL, myArgs) {
		
	//window.alert('URL = ' + myURL + '\n QString = ' + myArgs);
	if (myURL.indexOf("basket.asp") >= 0) {
        BasketCheck();
	}
    if (myURL.indexOf("shipping.asp") >= 0) {
        TSCheck();
	}
    if (myURL.indexOf("promotions") >= 0) {
        promoQty1();
	}
    if (myURL.indexOf("shopper_new.asp") >= 0) {
        refreshNewMember();
    }
    if (myURL.indexOf("catalog_request") >= 0) {
        // Default catalog request selection to Master Catalog
        var cc = document.getElementById('strCatalogCode');
        var ndx = 0;
        for (i=0; i<cc.options.length; i++) {
            if (cc.options[i].value.indexOf("INT-MSTR") >= 0) ndx = i;
        }
        document.getElementById('strCatalogCode').selectedIndex = ndx;
    }
    if (myURL.indexOf("shopper_lookup.asp") >= 0) {
        if (myArgs.indexOf("srcpg=lookup") >= 0) {
            if (readCookie('mbrchk1')) {
                loadLoginFailPage();
            }
        }
    }
    if (myURL.indexOf("productr.asp") >= 0) {
      window.setTimeout("disableAI()",1200);
    }
	if (myArgs.indexOf("dept_id=10000") >= 0 || myArgs.indexOf("dept%5fid%3d10000") >= 0 || myArgs.indexOf("dept%5fid=10000") >= 0 || myArgs.indexOf("dept_id%3d10000") >= 0
        || myArgs.indexOf("dept_id=20000") >= 0 || myArgs.indexOf("dept%5fid%3d20000") >= 0 || myArgs.indexOf("dept%5fid=20000") >= 0 || myArgs.indexOf("dept_id%3d20000") >= 0
        || myArgs.indexOf("dept_id=30000") >= 0 || myArgs.indexOf("dept%5fid%3d30000") >= 0 || myArgs.indexOf("dept%5fid=30000") >= 0 || myArgs.indexOf("dept_id%3d30000") >= 0) {
		document.getElementById('pageheader').style.backgroundImage = "url(/kvvet/assets/images/swoosh_pet.jpg)";
	}
    if (myArgs.indexOf("dept_id=40000") >= 0 || myArgs.indexOf("dept%5fid%3d40000") >= 0 || myArgs.indexOf("dept%5fid=40000") >= 0 || myArgs.indexOf("dept_id%3d40000") >= 0) {
        document.getElementById('pageheader').style.backgroundImage = "url(/kvvet/assets/images/swoosh_equine.jpg)";
    }
    if (myArgs.indexOf("dept_id=50000") >= 0 || myArgs.indexOf("dept%5fid%3d50000") >= 0 || myArgs.indexOf("dept%5fid=50000") >= 0 || myArgs.indexOf("dept_id%3d50000") >= 0) {
        document.getElementById('pageheader').style.backgroundImage = "url(/kvvet/assets/images/swoosh_livestock.jpg)";
    }
    if (myArgs.indexOf("dept_id=60000") >= 0 || myArgs.indexOf("dept%5fid%3d60000") >= 0 || myArgs.indexOf("dept%5fid=60000") >= 0 || myArgs.indexOf("dept_id%3d60000") >= 0) {
        document.getElementById('pageheader').style.backgroundImage = "url(/kvvet/assets/images/swoosh_healthlinks.jpg)";
    }
	return true;
}
function promoQty1() {
	if (!document.frmPromo.P3ListItemEditQty2) {
		var promoqty = 1;
		if (document.getElementById('kv_MaxPromoQty')) {
			promoqty = document.getElementById('kv_MaxPromoQty').value;
		}
		document.frmPromo.P3ListItemEditQty1.value = promoqty;
	}
}
function disableAI() {
  if (disable_ai == true) {
    document.form1.addl_info_text1.disabled = true;
  }
}
function goto(url) {
	window.location = url + "mscssid=<%=mscsShopperID%>&<%=URLGift%>";
}
function personalize(product, style) {
	var edit_id = document.form1.addl_info_text1.value;
    var tmpHREF = new String(location.href.toLowerCase());
    var e = "n"
    if (tmpHREF.indexOf("https:") >= 0) e = "y";
    var url = "http://personalize.kvvet.com/?product="+product+"&style="+style+"&e="+e;
	if (edit_id != "") {
		url += "&edit="+edit_id;
	}
	window.open(url, 'Personalize', 'width=580,height=650,scrollbars=yes,resizable=yes,location=no');
}
function searchfor(dept, terms) {
	document.getElementById('searchcategory').value = dept;
	document.getElementById('find_spec').value = terms;
	document.searchform.submit();
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* ----------
  
  Execute upon window load...
  
---------- */
window.onload = function() {
	var tmpHREF = new String(location.href.toLowerCase());  // Convert URL to Lower Case for comparison
	var rszHREF = tmpHREF.split("?");	//Split apart the URL and the query string
	var myURL = rszHREF[0];
	var myArgs;
	if (rszHREF.length > 1) {
		myArgs = rszHREF[1];
	} else {
		myArgs = "";
	}
	
	// Using the url and arguments, perform various JS tasks
	if (myURL.indexOf("_xtend.") >= 0) {
		/*
		  Run "extended" actions.
		  These include urls and arguments that utilize iframe'd pages from outside the base website
		*/
		xtendPageLoad(myURL, myArgs);
	} else {
		// run "normal" actions
		pageSpecificActions(myURL, myArgs);
	}
}