function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
function handleRequest(req) {	
	document.getElementById('mystore').innerHTML=req.responseText;
	//alert(req.responseText)
}

function setLocationCookie(loc){
sendRequest('setfromcookie.asp?b='+loc,handleRequest);

}  

function btnClick(b) {
    $.cookie( 'bmfmystore', b, { expires: 365, path: '/', domain: 'bobmillsfurniture.com', secure: false });					
    window.location = '/setlocation.asp?b=' + b;
}

//0 means disabled; 1 means enabled;
var popupStatus = 0;
function loadModal() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#modalWin").fadeIn("slow");
        popupStatus = 1;
    }
}

function disableModal() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#modalWin").fadeOut("slow");
        popupStatus = 0;
    }
}

function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

//centering popup
function centerPopup() {
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#modalWin").height();
    var popupWidth = $("#modalWin").width();
    $("#modalWin").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

function isValidZip(ZipCode) {
    var pattern = new RegExp(/^\d{5}([\-]\d{4})?$/);
    return pattern.test(ZipCode);
}

$(document).ready(function () {    
    var cookiename = 'bmfmystore';    
    var Currenturl = window.location.href;
    var OnHomePage;
    //var testHomePage = <%=Request.QueryString("Location")%>;
    if (Right(Currenturl, 22) == 'bobmillsfurniture.com/')
        OnHomePage = "true";
        
    if ($.cookie(cookiename) == null) {        
        if (OnHomePage) {
            centerPopup();
            loadModal();
        }
    } else {    	    
        var loc=$.cookie('bmfmystore');
        setLocationCookie(loc);
    }

    //click change store link
    $("#ChangeStore").click(function () {
        centerPopup();
        loadModal();
    });
        
    $("#modalClose").click(function () {
        disableModal();
    });
    //Click out event!
    $("#backgroundPopup").click(function () {
        disableModal();
    });
    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disableModal();
        }
    });
        
    //$(document).keypress(function (e) {
    //    if (e.keyCode == 13) {
    //        return false;
    //    }
    //});
});
