
//Global function for the search box in the top nav
function checkKeyword(keyword, defaultText) {
    var minKeywordLength = 0;
    var maxKeywordLength = 0;
    minKeywordLength += 3;
    maxKeywordLength += 30;
    var errMsg = '';
    //alert("minKeywordLength " + minKeywordLength);
    var searchTerms = '';
    searchTerms = keyword.value;
    //alert("searchTerms.length " + searchTerms.length);

	var invalidSearchTermMessage = 'Search term needed to be re-entered.'; // probably want to change this message! 
	var invalidChars = /[!?~`%^&*()+={}\[\]|\\:;\"<>/]/; // add all illegal characters here. 
	var matches_array = searchTerms.match(invalidChars);

    var noSearchTerm = 'Please enter a keyword or item number';
    var shortSearchTerm = 'Your keyword or item number must be at least 3 characters long';

    if (searchTerms == defaultText) {
        alert(noSearchTerm);
        return false;
    }
    if (searchTerms == '') {
        alert(noSearchTerm);
        return false;
    } else if ( searchTerms.length < minKeywordLength ) {
        alert(shortSearchTerm);
        return false;
    } else if (matches_array != null) { 
		// show the user a dialog if illegal characters are found in the keyword. 
		alert(invalidSearchTermMessage); 
		return false; 
    } else {
      return true;
    }
}

///Convenience function to modify the style of the specified element.  If element cannot be found,
// this function is a no-op.  Not all styles are supported: this is a convenience function just for
// our most commonly-accessed ones.
function setStyle(elementID, styleKey, styleValue) {
    var element = document.getElementById(elementID);
    if (element) {
        switch(styleKey) {
            case "visibility":
                element.style.visibility = styleValue;
                break;
            case "height":
                element.style.height = styleValue;
                break;
            case "display":
                element.style.display = styleValue;
                break;
        }
    }
}

//Convenience function to set the className of the specified element.  If element cannot be
// found, this function is a no-op.
function setClassName(elementID, newClass) {
    var element = document.getElementById(elementID);
    if (element) {
        element.className = newClass;
    }
}

//Ajax stuff

var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
//netscape, safari, mozilla behave the same??? 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 

function xmlGetData(url,clientProcessor){ 
    //alert("GetData: "+ url);
    if (url.length > 0){ 
        var objJSONresult;
        //create the state handler
        this.stateChanged = function() {
		    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') { 
                //Gather the results from the callback 
                objJSONresult = eval('(' + xmlHttp.responseText + ')');
                //Populate the innerHTML of the div with the results 
                clientProcessor(objJSONresult);
		    }
	    }
        var xmlHttp = GetXmlHttpObject(this.stateChanged); 
        xmlHttp.open('GET', url, true); 
	    xmlHttp.send(null); 
    } 
} 

function GetXmlHttpObject(handler) { 
    var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
    if (is_ie){ 
        //The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
         
        //Attempt to create the object 
        try{ 
            objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = handler; 
        } 
        catch(e){ 
        //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
        } 
    } 
    else if (is_opera){ 
        //Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
    } 
    else{ 
        // Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
    } 
     
    //Return the instantiated object 
    return objXmlHttp; 
} 

//Cart Stuff
function toggleCart(){
    var dvDropCart = document.getElementById("dvCartDrop");
    if (dvDropCart) {
	    if(dvDropCart.style.display == "block"){
    		dvDropCart.style.display = "none";	
    	}else{
    	    dvDropCart.style.display = "none";	
            xmlGetData("/ajx/getminicart.aspx",fcnDrawMiniCart);
	    }
	}
}
//hides the cart 5 seconds after user mouses out of cart
var alertTimerID = 0;
function hideCart(){
    var dvDropCart = document.getElementById("dvCartDrop");
    dvDropCart.style.display = "none";	
}
function autoHideCart(){
    //alert(dvDropCart.style.display);
    clearTimeout(alertTimerID);
    alertTimerID = setTimeout('hideCart()', 5000);
}

function fcnDrawMiniCart(objJSONresult) { 

    // get the div.  If we can't get it, no point in doing the work: short-circuit out.
    var dvDropCart = document.getElementById("dvCartDrop");
    if (!dvDropCart) {
        return;
    }
  
    //Handle SSL
    var maJsHost = "http://" + document.location.host + "/";
    
    var strOrderTotal = "0";
    
    //Check for nulls and empty arrays.
    var blnHaveResults = false;
    var blnHaveJustAdded = false;
    if (objJSONresult) {
        if (objJSONresult.results) {
            if (objJSONresult.results.length > 0) {
                blnHaveResults = true;
            }
        }
        if (objJSONresult.justAdded) {
            if (objJSONresult.justAdded.length > 0) {
                blnHaveJustAdded = true;
            }
        }
    }
    
    //Build the cart.
    var strHTML = "<div class=\"shoppingbagcontents\"><table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"250\">" +
        "<tr><td width=\"50\">&nbsp;</td><td width=\"177\">&nbsp;</td><td style=\"border-left: 2px solid #969696; border-top: 2px solid #969696; border-right: 2px solid #969696; background-color: #FFFFFF\" width=\"23\">" + 
        "<a href=\"#\" onClick=\"toggleCart();\"><img border=\"0\" align=\"middle\" alt=\"bag icon\" height=\"23\" src=\"/images/shoppingbag.gif\" width=\"19\"></a></td></tr>" +
        "<tr bgcolor=\"#FFFFFF\"><td colspan=\"2\" style=\"border-left: 2px solid #969696; border-top: 2px solid #969696;\"><img src=\"/images/pix.gif\" height=\"1\"></td><td style=\"border-right: 2px solid #969696;\"></td></tr>";
     //strHTML += "<tr><td colspan=\"5\" align=\"right\"><a href=\"#\" onclick=\"toggleDisplayDiv('cartcontainer');\"><img src=\"/images/btn_closewindow_sm.gif\" alt=\"close window\" border=\"0\" /></a></td></tr>";
        
    if (!objJSONresult || (!blnHaveResults && !blnHaveJustAdded)) {
        //If we have no json object, or both its items are empty, show the empty-cart message.
        strHTML += "<tr bgcolor=\"#FFFFFF\"><td colspan=\"3\" align=\"center\" class=\"text\" style=\"border-left: 2px solid #969696; border-right: 2px solid #969696;\"><br>Your cart is empty<br><br></td></tr>";
    } else {
        //OK, we have something.  Build the item table
        var i;
        if (blnHaveJustAdded) {
            //walk our justAdded items (usually there's only 1), and add each to the html.
            for (i=0;i<objJSONresult.justAdded.length;i++) {
                //Use the just-added graphic
                cartItem = objJSONresult.justAdded[i];
                //strHTML += getMiniCartItemRow(objJSONresult.justAdded[i], "/images/u_logo_mini_added.gif")
                  strHTML += "<tr bgcolor=\"#FFFFFF\"><td style=\"border-left: 2px solid #969696;\" width=\"50\">" + 
                    "<img alt=\"" + cartItem.NAME + "\" height=\"75\" src=\"" + cartItem.IMAGE + "\" width=\"50\"></td>" + 
                    "<td class=\"text\" colspan=\"2\" style=\"border-right: 2px solid #969696;\" width=\"200\">" + 
                    "<a href=\"#\">" + cartItem.NAME + "</a><br />" + 
                        "<a href=\"#\">" + cartItem.PRICE + "</a><br />" + 
                        "QTY: " + cartItem.QTY +  "<br />" + 
                        "SIZE:  " + cartItem.SIZE + "<br />" + 
                        "COLOR: " + cartItem.COLOR + "</td></tr>";
            }
        }
        if (blnHaveResults) {
            //walk our results, and add each to the html.
            for (i=0;i<objJSONresult.results.length;i++) {
                //Use the defined graphic
                cartItem = objJSONresult.results[i];
                strHTML += "<tr bgcolor=\"#FFFFFF\"><td style=\"border-left: 2px solid #969696;\" width=\"50\">" + 
                    "<img alt=\"" + cartItem.NAME + "\" height=\"75\" src=\"" + cartItem.IMAGE + "\" width=\"50\"></td>" + 
                    "<td class=\"text\" colspan=\"2\" style=\"border-right: 2px solid #969696;\" width=\"200\">" + 
                    "<a href=\"#\">" + cartItem.NAME + "</a><br />" + 
                        "<a href=\"#\">" + cartItem.PRICE + "</a><br />" + 
                        "QTY: " + cartItem.QTY +  "<br />" + 
                        "SIZE:  " + cartItem.SIZE + "<br />" + 
                        "COLOR: " + cartItem.COLOR + "</td></tr>";
                //strHTML += getMiniCartItemRow(objJSONresult.results[i], '');
            }
        }
        
        //Get and format the order total.
        strOrderTotal = objJSONresult.orderTotal;
        strHTML +="<tr bgcolor=\"#FFFFFF\"><td class=\"text\" colspan=\"3\" style=\"border-left: 2px solid #969696; border-right: 2px solid #969696;padding-top: 5px\">" +
        "SUBTOTAL: $" + strOrderTotal + "<a class=\"checkout\" href=\"/basket.aspx\"><img align=\"right\" border=\"0\"" +
        " height=\"18\" hspace=\"5\" src=\"/images/btn_checkout.gif\" width=\"66\"></a></td></tr>";
    }
    
 
    //close the cart table/html         
    strHTML +="<tr bgcolor=\"#FFFFFF\"><td align=\"center\" colspan=\"3\" style=\"border-left: 2px solid #969696; border-bottom: 2px solid #969696;border-right: 2px solid #969696; padding-bottom: 10px; padding-top: 10px;\">" +
        "<div class=\"closebag\"><a href=\"javascript:void(0);\" onclick=\"toggleCart();\">[ CLOSE SHOPPING BAG ]</a></div></td></tr>" +
        "</table></div>"
    
    //set it, show it. we're done.
    dvDropCart.innerHTML = strHTML;
    dvDropCart.style.display = "block";
}

function GetShippingTotal(val){
    var dvShipCost = document.getElementById("lblTotalShipping");
    if (dvShipCost) {
        var state = document.getElementById("selShipState");
        state = state.options[state.selectedIndex].value;
        var country = "US"; //document.getElementById("selShipCountry");
        //country = country.options[country.selectedIndex].value;
        xmlGetData("/ajx/getshipping.aspx?t=" + val + "&s=" + state + "&c=" + country,fcnSetShipTotal);
	}
}

function fcnSetShipTotal(objJSONresult) {
    var lblTotalShipping = document.getElementById("lblTotalShipping");
    var lblTotalShipping2 = document.getElementById("lblTotalShipping2");
    var lblTotal = document.getElementById("lblTotal");
    var lblTotal2 = document.getElementById("lblTotal2");
    if (!lblTotalShipping && !lblTotal) {
        return;
    }
    
    if (objJSONresult.TotalShipping) {
        lblTotalShipping.innerHTML = objJSONresult.TotalShipping;
        lblTotalShipping2.innerHTML = objJSONresult.TotalShipping;
        lblTotal.innerHTML= objJSONresult.Total;
        lblTotal2.innerHTML= objJSONresult.Total;
    }
    
}
