/**
 * @author Bob
 */
/**
 * @author Bob
 */
var globject = {};

globject.CatArr = [];

globject.navmouseover = function(p)
{
    var elem = document.getElementById(p);
            elem.className = "navtblhover";
}

globject.navmouseout = function(p)
{
    var elem = document.getElementById(p);
            elem.className = "navtbl";
}
 
globject.init = function()
{
    globject.waiter = new YAHOO.widget.Panel("waiter",  
				{ width:"240px", 
					fixedcenter:true, 
					close:false, 
					draggable:false, 
					modal:true,
					visible:false,
					effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.1} 
				}) ;
                
    globject.waiter.setHeader("Creating Alert...");
	globject.waiter.setBody("<img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
	globject.waiter.render(document.body);
       
    //Track which step the user is on
    globject.onstep = 0;
        
    //An array of category ids that are selected from the
    //list of categories
    globject.catarr = [];
    
    globject.lastDist = 0;
    globject.lastZip = 0;
    
    //Setup wizard description rows
    globject.stephint = document.getElementById("stephint");
    globject.stephinttxt = document.createTextNode("");
    globject.stephint.appendChild(globject.stephinttxt);
    
    globject.stepnbr = document.getElementById("stepnbr");
    globject.stepnbrtxt = document.createTextNode("");
    globject.stepnbr.appendChild(globject.stepnbrtxt); 
        
    globject.tzo = new Date().getTimezoneOffset() * 60000;  
    
    //Begin Wizard
    globject.showStep1();
    
    document.getElementById("txtZip").focus();

}// end init

globject.getCategories = function()
{
    document.getElementById("cats_wi").className = "step_visible";
    var xhr = new CROWCODER.ajax.YuiConxn();
    xhr.postdata = "?method=getAllCategories";
	xhr.sUrl = "../services/services.aspx";                 
    xhr.callback.success = buildCats;        
    xhr.callback.failure = showFail;
    xhr.makeCall();
    
    function buildCats(o)
    {
        var lst = document.createElement("UL");
        var lst2 = document.createElement("UL");
        lst.id = "catlist";
        var arr = eval("(" + o.responseText + ")");
        
        for(var i = 0; i < arr.categories.length; i++)
        { 
            var li = document.createElement("LI");        
            var cbx = document.createElement("INPUT");
            cbx.type = "checkbox";
            cbx.value = arr.categories[i].cat_id;
            cbx.onclick = globject.category_On_Select;
            li.appendChild(cbx);
            li.appendChild(document.createTextNode(arr.categories[i].cat_nam));
            if(i < arr.categories.length/2)
            {lst.appendChild(li);}
            else{lst2.appendChild(li);}
        }
        document.getElementById("cats_wi").className = "step_hidden";        
        document.getElementById("cats").appendChild(lst);
        document.getElementById("cats2").appendChild(lst2);
    }
    function showFail(o)
    {
        throw new Error("Failed to Return Category List");
    }
}

globject.category_On_Select = function()
{
    if(this.checked)
    {
        globject.catarr.push(this.value);
    }
    else
    {
        for(var i = 0; i < globject.catarr.length; i++)
        {
            if(globject.catarr[i] == this.value)
            {
                globject.catarr.splice(i, 1);
            }
        }
    }
}

globject.validate = function()
{
    var zip = document.getElementById("txtZip");
    var within = document.getElementById("txtWithin");
    var eml1 = document.getElementById("txtEmail").value;
    var eml2 = document.getElementById("email2").value;
    
    if(isNaN(zip.value) || zip.value == "")
    {
        zip.value = "";
        globject.showStep1();
        zip.focus();
        alert("Zip code must be numeric");        
        return false;
    }    
    
    if(isNaN(within.value) || within.value == "")
    {
        within.value = "";
        globject.showStep1();
        within.focus();
        alert("Within distance must be numeric");
        return false;
    }
    
    if((eml1.indexOf(".") > 2) && (eml1.indexOf("@") > 0))
    {
        if(!eml1.valueOf() == eml2.valueOf())
        {
            alert("Email addresses do not match. Please re-enter.");
            globject.showStep2();
            document.getElementById("txtEmail").focus();
            return false;
        }
    }
    else
    {
        alert("Email address appears to be an invalid format");
        globject.showStep2();
        document.getElementById("txtEmail").focus();        
        return false;
    }
    return true;
}

/***
 * Shows the search wizard and hide the search results
 */
globject.showWiz = function ()
{
    document.getElementById("wiztable").className = "step_visible";
    globject.tdResults.className = "step_hidden";
    document.getElementById("NoDataMsg").className = "step_hidden";
    document.getElementById("resultBtns").className = "step_hidden";
}

globject.showStep1 = function()
{
    globject.hideSteps();    
    globject.onstep = 1
    document.getElementById("step1").className = "cur_step";
    document.getElementById("step_one").className = "step_visible";
    document.getElementById("s1").className = "current";
    globject.stephinttxt.data = "Enter your zip code and the distance you are willing to travel.";
    globject.stepnbrtxt.data = "Step 1 of 3";
    globject.hideNextPrev();
}


globject.showStep2 = function()
{
    globject.hideSteps();    
    globject.onstep = 2
    document.getElementById("step2").className = "cur_step";
    document.getElementById("step_two").className = "step_visible";
    document.getElementById("s2").className = "current";
    globject.stephinttxt.data = "Enter your email address and create Alert.";
    globject.stepnbrtxt.data = "Step 2 of 3";    
    document.getElementById("txtEmail").focus();
    globject.hideNextPrev();
}

globject.showStep3 = function()
{
    globject.hideSteps();    
    globject.onstep = 3
    document.getElementById("step3").className = "cur_step";
    document.getElementById("step_three").className = "step_visible";
    document.getElementById("s3").className = "current";
    globject.stephinttxt.data = "Choose the categories you want to be alerted for. " +
        "Skip this step to receive alerts from all categories.";
    globject.stepnbrtxt.data = "Step 3 of 3";
    globject.hideNextPrev();
}

/*
globject.showStep3 = function()
{
    globject.hideSteps();    
    globject.onstep = 3
    document.getElementById("step3").className = "cur_step";
    document.getElementById("step_three").className = "step_visible";
    document.getElementById("s3").className = "current";
    globject.stephinttxt.data = "Enter keywords to search yard sale descriptions. " +
        "IMPORTANT - Seperate words with commas only. Skip this step to see yard " +
        "sales with any description.";
    globject.stepnbrtxt.data = "Step 3 of 3";
    globject.hideNextPrev();
}
*/

globject.hideSteps = function()
{
    document.getElementById("step_one").className = "step_hidden";
    document.getElementById("step1").className = "step";
    document.getElementById("s1").className = "idle";
    document.getElementById("step_two").className = "step_hidden";
    document.getElementById("step2").className = "step"
    document.getElementById("s2").className = "idle";
    document.getElementById("step_three").className = "step_hidden";
    document.getElementById("step3").className = "step"
    document.getElementById("s3").className = "idle";
    //document.getElementById("step_four").className = "step_hidden";
    //document.getElementById("step4").className = "step"
    //document.getElementById("s4").className = "idle";  
}

globject.moveBack = function()
{
    switch(globject.onstep)
    {
        case 2:
            globject.showStep1();
            break;
        case 3:
            globject.showStep2();
            break;
        case 4:
            globject.showStep3();
            break;        
    }
}

globject.moveNext = function()
{
    switch(globject.onstep)
    {
        case 1:
            globject.showStep2();
            globject.onstep = 2;
            break;
        case 2:
            globject.showStep3();
            globject.onstep = 3;
            break;
        //case 3:
           // globject.showStep4();
           // globject.onstep = 4;
           // break;
    }    
}

globject.hideNextPrev = function()
{
    var nxt = document.getElementById("btnNext");
    var prv = document.getElementById("btnPrev");
    var end = document.getElementById("btnEnd");
    
    switch(globject.onstep)
    {
        case 1:
            nxt.value = "Next >>";
            nxt.className = "step_visible";
            end.className = "step_hidden";
            prv.className = "step_hidden";
            break;
        case 2:
            nxt.value = "Enter Optional Criteria >>";
            nxt.className = "step_visible";
            end.className = "step_visible";
            prv.className = "step_visible";
            break;
        case 3:
            nxt.value = "Enter Optional Criteria >>";
            nxt.className = "step_hidden";
            end.className = "step_visible";
            prv.className = "step_visible";
            break;
        case 4:
            nxt.className = "step_hidden";
            end.className = "step_hidden";
            prv.className = "step_visible";
            break;
    }
}


globject.SetCategory = function(p_catid)
{
    var elem = document.getElementById(p_catid);
    
    if(elem.checked)
    {
        globject.CatArr.push(p_catid);
    }
    else
    {
        for(var i = 0; i < globject.CatArr.length; i++)
        {
            if(globject.CatArr[i] == p_catid)
            {
                 globject.CatArr.splice(i,1);
            }           
        }
    }
}

globject.submitAlert = function()
{
    if(! globject.validate())
    {
        return;
    }
    
    var ysalert = {};
    ysalert.catarr = globject.CatArr.join(",");
    ysalert.email = document.getElementById("txtEmail").value;
    ysalert.within = document.getElementById("txtWithin").value;
    ysalert.zip = document.getElementById("txtZip").value;
    //ysalert.kywrds = document.getElementById("keywords").value;
    
    var xhr = new CROWCODER.ajax.YuiConxn();
    xhr.sUrl = "../services/services.aspx";
    xhr.postdata = "?method=createAlert&alert=" + 
        ysalert.toJSONString();
	                  
    xhr.callback.success = doSuccess;        
    xhr.callback.failure = showFail;    
    xhr.makeCall();    
    globject.waiter.show();
    
    function doSuccess(o)
    {
        if(o.responseText == "ok")
        {
            globject.waiter.hide();
            window.location.href = "Message.htm?qs=1";    
        }
        else
        {
            globject.waiter.hide();
            window.location.href = "Message.htm?qs=2"; 
        }
        
    }
    function showFail()
    {
        alert("Unknown error creating Alert. Please try again");
    }
}

globject.delAlert = function()
{
    var deleml = document.getElementById("deleml").value;
    if(deleml == "")
    {return;}
    
    var xhr = new CROWCODER.ajax.YuiConxn();
    xhr.sUrl = "../services/services.aspx";
    xhr.postdata = "?method=deleteAlert&email=" + 
        deleml;
	                  
    xhr.callback.success = doSuccess;        
    xhr.callback.failure = showFail;    
    xhr.makeCall();    
    window.location.href = "Message.htm?qs=3";
    
    function doSuccess(o)
    {
        return;
    }
    
    function showFail()
    {
       return;
    }
}

globject.kywrdFocus = function()
{
    var ta = document.getElementById("keywords");
    ta.className = "";
    ta.value = "";
}

globject.ShowDelHelp = function()
{
    var msg = "Enter your email address and click Delete to stop " +
        "receiving yard sale alerts. You will receive a confirmation " +
        "email, and you may create other alerts at anytime";
    var hdr = "Delete an Alert";
    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowZipHelp = function()
{
    var msg = "Enter your zip code. You will receive alerts " +
        "based on distances from this zip code. Distances are " +
        "approximate distances beween the centers of the zip codes, " +
        "not distances to the street addresses of the yard sales.";
    var hdr = "Zip Code Help";    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowDistHelp = function()
{
    var msg = "Enter the distance you are willing to travel. Alerts " +
        "sent to you will provide yard sale details for yard sales " +
        "that fall within this mile radius of your zip code. Distances " +
        "are approximate, and are based on the center of your zip code " +
        "and the center of the yard sale's zip code, not its street address.";
    var hdr = "Distance Help";    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowCatHelp = function()
{
    var msg = "If your yard sale search is for something fairly specific, " +
    "you can choose categories that will filter your alert results. " +
    "Be aware that many yard sale postings may not have chosen any categories " +
    "to associate with. Skip this step to receive alerts regardless of cateogry.";
    var hdr = "Category Help";    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowDescHelp = function()
{
    var msg = "If your search is for a specific item, like lawnmower or " +
        "bicycle, you can enter those words here to only receive alerts for " +
        "yard sales that have been submitted with those keywords. Be aware " +
        "that many yard sales may have been posted with little or no description. " +
        "Skip this step to receive alerts for yard sales with any description.";
    var hdr = "Description Help";    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowEmailHelp = function()
{
    var msg = "Enter and confirm your email address. This is the email address " +
        "yard sale alerts will be sent to. Your yard sale alerts will contain all "+ 
        "of the yard sale information, ready for you to print.";
    var hdr = "Email Help";    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}



window.onload = function(){globject.init();}

