var popWin = false;
var submitform = false;
var showaddtocart, actualAction;
var expDays=10;
//var addToCartURL = "images/add-to-cart.gif";
var phpFileURL = "http://www.p.hostingprod.com/@simplycheap.com/mapprice/mapprice.php";
var expDate = new Date();
expDate.setTime(expDate.getTime()+(expDays*24*60*60*1000))

function findElement(item) {return document.getElementById ? document.getElementById(item) : document.all ? document.all[item] : null;}

function isEmail(str) {
    var supported = 0;
    if (window.RegExp) {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported)
        return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

    return (!r1.test(str) && r2.test(str));
}

function checkForm() {
    var orderForm = findElement("orderForm");
    orderForm.sendersemail.value=trim(orderForm.sendersemail.value);
    if(isEmail(orderForm.sendersemail.value) == "") {
        alert("Invalid E-mail\nPlease Re-enter");
        orderForm.sendersemail.focus();
        return false;
    } else{
        var newInput = document.createElement("INPUT");
        newInput.name = "page";
        newInput.type = "hidden";
        newInput.value = document.location.href;
        orderForm.appendChild(newInput);
        actualAction = "http://order.store.yahoo.com/cgi-bin/wg-order?simplycheap";
        orderForm.action = phpFileURL + "?price=" + findElement("mappriceval").value;
        orderForm.target = "newWin";
        return openWin("newWin", 300, 250, true);
    }
}

function trim(value) {
    startpos=0;
    while((value.charAt(startpos)==" ")&&(startpos<value.length))
        startpos++;
    if(startpos==value.length)
        value="";
    else{
        value=value.substring(startpos,value.length);
        endpos=(value.length)-1;
        while(value.charAt(endpos)==" ")
            endpos--;
        value=value.substring(0,endpos+1);
    }
 return(value);
}

function openWin(wName, wWidth, wHeight, wResize){
    features = 'left=0,top=0,scrollbars=0,status=1,resizable=' + ((wResize) ? 0 : 0) + ((wWidth) ? ',width=' + wWidth : '') + ((wHeight) ? ',height=' + wHeight : '');
    popWin = window.open('', wName, features);
    if(popWin.focus) { popWin.focus(); }
    submitform = true;
    //revealOrderBtn(true);
}

function revealOrderBtn(showaddtocart){
    var cururl = location.search;
    var orderForm = findElement("orderForm");
    var orderhtml = "<center><input type=image src="+addToCartURL+" border=0></center>";
    if(submitform)
        orderForm.submit();

    if(cururl == "?showaddtocart=true" || showaddtocart){
        /*findElement("orderbtn").innerHTML = orderhtml;
        findElement("mapsaleprice").innerHTML = findElement("mappricevaldiv").innerHTML;*/
        
        /**/
        var orderhtml = "<center><input type=image src="+addToCartURL+" border=0></center>";
        //findElement("orderbtn").innerHTML = orderhtml;
        var verifyDiv = findElement("mappriceverify");
            verifyDiv.innerHTML = "";
        var orderDiv = findElement("mappriceorderbutton");
            orderDiv.className = "on";
        var input1 = findElement("mappricebrand");
            input1.disabled = "disabled";
        var input2 = findElement("mappriceval");
            input2.disabled = "disabled";
        findElement("mapsaleprice").innerHTML = findElement("mappricevaldiv").innerHTML;        
        /**/
        theBrand = findElement("mappricebrand").value;
        theCookie = "show" + theBrand + "Price=showaddtocart:true";
        document.cookie = theCookie + "; expires=" + expDate.toGMTString() + "; path=/";
    }
    if(showaddtocart){
        orderForm.action = actualAction;
        orderForm.target = "_self";
        if(popWin.focus) { popWin.focus(); }
    }
}

function readMapPriceCookie(){
    var the_cookie = unescape(document.cookie);
    var theBrandInput = findElement("mappricebrand");
    if(theBrandInput)
        var theBrand = theBrandInput.value;
    if(the_cookie.length > 0){
        var cookieArray = the_cookie.split(";");
        for(i=0;i<cookieArray.length;i++){
            //alert(cookieArray[i]);
            if(cookieArray[i].indexOf("show" + theBrand + "Price") > -1)
                showaddtocart = true;
        }
    }
    if(showaddtocart){
        var orderhtml = "<center><input type=image src="+addToCartURL+" border=0></center>";
        //findElement("orderbtn").innerHTML = orderhtml;
        var verifyDiv = findElement("mappriceverify");
            verifyDiv.innerHTML = "";
        var orderDiv = findElement("mappriceorderbutton");
            orderDiv.className = "on";
        var input1 = findElement("mappricebrand");
            input1.disabled = "disabled";
        var input2 = findElement("mappriceval");
            input2.disabled = "disabled";
        findElement("mapsaleprice").innerHTML = findElement("mappricevaldiv").innerHTML;
    }
}

var isNS = (navigator.appName == "Netscape") ? 1 : 0;
var inEmailInput = false;

function keyhandler(e) {
    var theInput = findElement("sendersemail");
    if(e == null) e = window.event;

    var theReturnValue = true;

    if (e.keyCode==13 && inEmailInput && document.all)
        theReturnValue = false;

    return theReturnValue;
}