// JScript File

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";
}

function init()
{
    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 Yard Sale...");
	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);
    
    //Create the "wait" image
    globject.waitImg = document.createElement("IMG");
    globject.waitImg.src = "../images/ajax-loader.gif";
    globject.waitImg.alt = "Please Wait...";
    
    //var globject = {};

    globject.tzo = new Date().getTimezoneOffset();
    
    document.getElementById("ys_start_time").selectedIndex = -1;
    document.getElementById("ys_end_time").selectedIndex = -1;
        
    //make a calendar
    globject.calE1 = new YAHOO.widget.Calendar("calE1", "calendar", 
    {"MULTI_SELECT":true, "mindate":new Date(), "title":"<div>Yardsale Dates</div>"});
    //globject.calE1.selectEvent.subscribe(showDates);
    //calE1.deselectEvent.subscribe(remDates);
    globject.calE1.render();
    
    globject.img_row = document.getElementById("img_row");
    globject.del_row = document.getElementById("del_row");
    
    globject.flyout = document.getElementById("flyout");
    globject.flyoutimg = document.getElementById("flyoutImg");
    var dd0 = new YAHOO.util.DD(globject.flyout);
    
    //Get a reference to the current step dom element
    globject.currStep = document.getElementById("curr_step");
    
    //The row of image links in the image table
    globject.img_row;
    
    //The step number the user is currently on
    globject.onstep = 1;
    
    //the ordered list that shows the wizard steps
    globject.wizList = document.getElementById("step_list");
    
    //bool is true if logged in user has some saved yardsales
    globject.hasYs = false;
    
    //id of the yardsale being edited. Zero if is a new yardsale
    globject.cur_ys = 0;
    
    //the array of yardsale data for the logged in user
    globject.ysData;
    //globject.listYardSales();
    
    //Build the category list
   //globject.getCategories();
    
    //Array of category checkboxes
    globject.catListItems = [];
    
    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.showStep1();
    
    document.getElementById("ys_name").focus(); 
    
    //initialize form for testing
    
    //document.getElementById("ys_name").value = "Test Yard Sale";
    //document.getElementById("txtAddress1").value = "123 Test Rd.";
   // document.getElementById("txtCity").value = "Some City";
    //document.getElementById("txtZip").value = "90210";
    //document.getElementById("kywords").value = "This is the description this is the description this is the description";
    
}

globject.SetCategory = function(p_catid, p_descr)
{
    var elem = document.getElementById(p_catid);
    
    if(elem.checked)
    {
        globject.CatArr.push({"catid" : p_catid, "catdescr" : p_descr});
    }
    else
    {
        for(var i = 0; i < globject.CatArr.length; i++)
        {
            if(globject.CatArr[i].catid == p_catid)
            {
                 globject.CatArr.splice(i,1);
            }           
        }
    }
}

function showDates()
{
    alert(globject.calE1.getSelectedDates()[0].getTime());
}
//step one of wizard is to add a drop down of yardsales that the user
//has created
//STEP ONE
globject.listYardSales = function(){
    //get list of yardsales for current user
    //will return a 'none' if there are none
    //globject.currStep.appendChild(globject.waitImg);
    
    var xhrYsList = new CROWCODER.ajax.YuiConxn();
    xhrYsList.sUrl = "../services/services.aspx";
    xhrYsList.postdata = "?method=ysByUser";
	                  
    xhrYsList.callback.success = buildYsSelect;        
    xhrYsList.callback.failure = showFail;
    xhrYsList.makeCall();
    
    function buildYsSelect(o)
    {
        globject.ysData = eval("(" + o.responseText + ")");

        if(globject.ysData[0].ys_id == "none")
        {
            globject.hasYs = false;
            document.getElementById("sel_ys").className = "step_hidden";
            globject.cur_ys = 0;
        }
        else
        {
            globject.hasYs = true;
            document.getElementById("sel_ys").className = "step_visible";
            
            
            //Add each yardsale returned to a select drop down
            globject.yslist = document.getElementById("selYardSale");        
            for(var i = 1; i < globject.ysData.length; i++)
            {
                var opt = document.createElement("OPTION");
                opt.text = globject.ysData[i].ys_desc;
                opt.value = globject.ysData[i].ys_id;
                globject.yslist.options[i] = opt;
            }
            
        }
        globject.stephinttxt.data = "Edit a yard sale, or create a new one.";
        globject.stepnbrtxt.data = "Step 1 of 6";
        document.getElementById("ys_name").focus();
        
    }
    
    function showFail(o)
    {
        alert("unable to retrieve list\n" + o.responseText);
    }
}

//on change handler for the yard sale select list
globject.selYardSale_onChange = function()
{
    var sel = document.getElementById("selYardSale");
    globject.cur_ys = sel.options[sel.selectedIndex].value;
    if(globject.cur_ys > 0)
    {
        CROWCODER.ajax.showWait(document.getElementById("curr_step"));
        globject.loadData();    
        
    }   
    else
    {
        globject.resetWiz();
    }         
}

globject.buildImgTbl = function(p_arr)
{      
    //remove the row of images if a user chooses a new yardsale
    //from the dropdown and it has no images assoc. w/ it      
    var tmp_cnt = globject.img_row.cells.length;
    for(var i = 0; i < tmp_cnt ; i++)
    {
        globject.img_row.deleteCell(0);
        globject.del_row.deleteCell(0); 
    }       
    //alert(o.responseText);
    if(p_arr.length)
    {
        if(p_arr[0].image_name == "none")
        {
            return;
        }
        
        for(var i = 0; i < p_arr.length; i++)
        {
            globject.insertImg(p_arr[i]);                     
        }         
    }    
}

/***
 * @method Unchecks all categories, and re-checks the ones that
 * are assigned to the current yardsale
 * @param {Object} p_arr An array of integers that are category ids assigned
 *     to the current yardsale
 */
globject.fillCats = function(p_arr)
{
    for(var i = 0; i < globject.catListItems.length; i++)
    {
        globject.catListItems[i].checked = false;        
    }    
    for(var j = 0; j < p_arr.length; j++)
    {
        document.getElementById(p_arr[j]).checked = true;
    }    
}

/***
 * @method Clears dates off the calendar and selects dates assigned to the
 *     current 
 * @param {Object} p_arr
 */
globject.selectDates = function(p_arr)
{
    var dt = new Date();
    var hrs;
    var mins;
    var tod;
    var offset = dt.getTimezoneOffset() * 60000;
    var selstart = document.getElementById("ys_start_time");
    
    globject.calE1.clear();
    for(var i = 0; i < p_arr.length; i++)
    {
        globject.calE1.select(new Date(p_arr[i] + offset));
    }
    globject.calE1.render();
    
}

/***
 * @method fills the wizard controls with data from the
 * chosen yardsale
 */
globject.loadData = function()
{   
    var xhr = new CROWCODER.ajax.YuiConxn();
    xhr.sUrl = "../services/services.aspx";
    xhr.postdata = "?method=getData&ysid=" + globject.cur_ys;
	                  
    xhr.callback.success = getData;        
    xhr.callback.failure = showFail;
    xhr.makeCall();
    
    function getData(o)
    {        
        var d = eval("(" + o.responseText + ")");
        if(o.responseText == "invalid")
        {
            alert("Invalid Yard Sale Data Detected. Try again.")
            return;
        }
        
        document.getElementById("ys_name").value = d.name;
        document.getElementById("txtAddress1").value = d.street;
        document.getElementById("txtCity").value = d.city;
        document.getElementById("txtZip").value = d.zip;
        document.getElementById("kywords").value = d.kywords;
        
        globject.buildImgTbl(d.imgarr);
        globject.fillCats(d.catarr);
        globject.selectDates(d.datearr);
        globject.selectTimes(d.starttime, d.endtime);
        
        CROWCODER.ajax.hideWait(document.getElementById("curr_step"));
        
    }
    function showFail(o)
    {
        alert(o.statusText);
    }
}

/***
 * @method Selects the proper time from the time drop downs when loading
 * yard sale data into the wizard
 * @param {Object} p_start the time in milliseconds when the yardsale starts
 * @param {Object} p_end the time in ms when the yard sale ends
 */
globject.selectTimes = function(p_start, p_end)
{
    var localStartTime = p_start - (globject.tzo * 60000);
    var localEndTime = p_end - (globject.tzo * 60000);
    var selStart = document.getElementById("ys_start_time");
    var selEnd = document.getElementById("ys_end_time");
    
    //alert(localStartTime + " : " + localEndTime);
    
    for(var i = 0; i < selStart.options.length; i++)
    {
        if(selStart.options[i].value == localStartTime.toString())
        {
            selStart.selectedIndex = i;
        }
    }
    for(var i = 0; i < selEnd.options.length; i++)
    {
        if(selEnd.options[i].value == localEndTime.toString())
        {
            selEnd.selectedIndex = i;
        }
    }
}

/***
 * @method Empties the wizard fields of data when
 * user chooses "Create New" in step one
 */
globject.resetWiz = function()
{
    //clear out wizard fields for new yardsale entry
    document.getElementById("ys_name").value = "";
    document.getElementById("txtAddress1").value = "";
    document.getElementById("txtCity").value = "";
    document.getElementById("txtZip").value = "";
    document.getElementById("kywords").value = "";
    globject.calE1.clear();
    for(var i = 0; i < globject.catListItems.length; i++)
    {
        globject.catListItems[i].checked = false;        
    }  
    ///document.getElementById("ys_start_time").selectedIndex = -1;
    //document.getElementById("ys_end_time").selectedIndex = -1;
    
    
}

//Adds an image to the list    
globject.addImage = function()
{
    //verify the url is actually a link to an image
    var xhr = new CROWCODER.ajax.YuiConxn();
    xhr.sUrl = "../services/services.aspx";
    xhr.postdata = "?method=checkImg&url=" +
        document.getElementById("img_name").value;
	                  
    xhr.callback.success = verify_url;        
    xhr.callback.failure = showFail;
    xhr.makeCall();
    
    function verify_url(o)
    {
        if(o.responseText == "true")
        {
           globject.insertImg(document.getElementById("img_name").value);
           //remove the contents of the text box
           document.getElementById("img_name").value = "";
        }        
        else
        {
            alert("There is problem with the URL you entered.");
            return;
        }
    }
    
    function showFail(o)
    {
        alert("Unable to link your image.");
        return;
    }
}  

globject.insertImg = function(img_url)
{
try
{
   var td = document.createElement("td");
   td.className = "imgTd";
   var img = document.createElement("img");
   img.alt = "photo";
   img.className = "imgTn";
   img.src = img_url;
   img.onmouseover = globject.showFlyout;
   //img.onmouseout = globject.hideFlyout;
   
   //scale img
   var scale_factor = 60.0 / img.height;
   img.height = scale_factor * img.height;
   img.width = scale_factor * img.width;
   
   td.appendChild(img);
   globject.img_row.appendChild(td);
   
   var tdDel = document.createElement("td");
   tdDel.className = "imgTd";
   var delBtn = document.createElement("INPUT");
   delBtn.type = "button";
   delBtn.value = "Remove";
   delBtn.onclick = globject.removeImg;
   tdDel.appendChild(delBtn);
   globject.del_row.appendChild(tdDel);
   }
   catch(ex)
   {
       alert(ex);
   }
}

globject.removeImg = function()
{
    //alert(this.id);
    
    globject.img_row.removeChild(globject.img_row.cells[this.parentNode.cellIndex]);
    globject.del_row.removeChild(globject.del_row.cells[this.parentNode.cellIndex]);
}

globject.showFlyout = function()
{
    var pos = YAHOO.util.Dom.getXY(this);
    pos[1] = pos[1] - 60;
    YAHOO.util.Dom.setXY(globject.flyout, pos);
    globject.flyoutimg.src = this.src;
    globject.flyout.className = "flyoutShow";        
}

globject.hideFlyout = function()
{
    globject.flyout.className = "flyoutHid";    
}

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 a title for your yard sale. " +
        "ie: Moving Sale or Attic Clean Out!";
    globject.stepnbrtxt.data = "Step 1 of 7";
    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 the address of the Yard Sale.";
    globject.stepnbrtxt.data = "Step 2 of 7";
    document.getElementById("txtAddress1").focus();     
    globject.hideNextPrev();

}

//Enter kywords
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 = "Describe your yard sale. " +
        "Use specific words like Lawnmower or bicycles. Also mention " +
        "Moving Sale or Multiple Family Sale if appropriate.";
    globject.stepnbrtxt.data = "Step 3 of 7";
    document.getElementById("kywords").focus();
    globject.hideNextPrev();
}

//Address
globject.showStep4 = function()
{
    globject.hideSteps();
    globject.onstep = 4;
    document.getElementById("step4").className = "cur_step";
    document.getElementById("step_four").className = "step_visible";
    document.getElementById("s4").className = "current";
    globject.stephinttxt.data = "Enter Dates and times.";
    globject.stepnbrtxt.data = "Step 4 of 7";
    
    globject.hideNextPrev();
}

//Dates and times
globject.showStep5 = function(){
    globject.hideSteps();
    globject.onstep = 5; 
    document.getElementById("step5").className = "cur_step";
    document.getElementById("step_five").className = "step_visible";
    document.getElementById("s5").className = "current";
    globject.stephinttxt.data = "Select each category that applies to the " +
    "items you are selling.";
    globject.stepnbrtxt.data = "Step 5 of 7";
    if(globject.firstCat)
    {
        globject.firstCat.focus();    
    }
    globject.hideNextPrev();
}

//Add photo links
globject.showStep6 = function(){
    globject.hideSteps();
    globject.onstep = 6; 
    document.getElementById("step6").className = "cur_step";
    document.getElementById("step_six").className = "step_visible";
    document.getElementById("s6").className = "current";
    globject.stephinttxt.data = "Add or remove links to photos." +
        "This is not an image upload. Select the help link for more information.";
    globject.stepnbrtxt.data = "Step 6 of 7";
    document.getElementById("img_name").focus();
    globject.hideNextPrev();   
}

//Submit yardsale data
globject.showStep7 = function()
{
    globject.hideSteps();
    globject.onstep = 7;
    document.getElementById("step7").className = "cur_step";
    document.getElementById("step_seven").className = "step_visible";
    document.getElementById("s7").className = "current";
    globject.stephinttxt.data = "Submit Yardsale.";
    globject.stepnbrtxt.data = "Step 7 of 7";
    globject.previewYs();
    globject.hideNextPrev();
        
}

globject.hideNextPrev = function()
{
    var nxt = document.getElementById("btnNext");
    var prv = document.getElementById("btnPrev");
    var fin = document.getElementById("btnFin");
    
    switch(globject.onstep)
    {
        case 1:
            nxt.value = "Next >>";
            nxt.className = "step_visible";
            fin.className = "step_hidden";
            prv.className = "step_hidden";
            break;
        case 2:
            nxt.value = "Next >>";
            nxt.className = "step_visible";
            fin.className = "step_hidden";
            prv.className = "step_visible";
            break;
        case 3:
            nxt.value = "Next >>";
            nxt.className = "step_visible";
            fin.className = "step_hidden";
            prv.className = "step_visible";
            break;
        case 4:
            nxt.value = "Enter Optional Info >>";
            nxt.className = "step_visible";
            fin.className = "step_visible";
            prv.className = "step_visible";
            break;
        case 5:
            nxt.value = "Enter Optional Info >>";
            nxt.className = "step_visible";
            fin.className = "step_visible";
            prv.className = "step_visible";
            break;
        case 6:
            nxt.value = "Finish";
            nxt.className = "step_visible";
            fin.className = "step_visible";
            prv.className = "step_visible";
            break;
        case 7:
            nxt.className = "step_hidden";
            fin.className = "step_hidden";
            prv.className = "step_visible";
            break;
    }
}

globject.getCategories = function()
{    
    //if list is already built just show it
    if(globject.catList)
    {
        document.getElementById("curr_step").appendChild(globject.catList);
        globject.stephinttxt.data = "Select each category that applies " + 
            "to the items you will have for sale";
        globject.stepnbrtxt.data = "Step 4 of 7";
        return;
    }
   
    var ajaxTest = new CROWCODER.ajax.YuiConxn();
    ajaxTest.sUrl = "../services/services.aspx";
    ajaxTest.postdata = "?method=getAllCategories";
	                  
    ajaxTest.callback.success = showTest;        
    ajaxTest.callback.failure = showFail;
    ajaxTest.makeCall();
    document.getElementById("cats_wi").className = "step_visible";
    //CROWCODER.ajax.showWait(document.getElementById("curr_step"));
    
    function showTest(o)
    {
        document.getElementById("cats_wi").className = "step_hidden";
        //CROWCODER.ajax.hideWait(document.getElementById("curr_step"));
        //globject.waitImg = globject.currStep.removeChild(globject.waitImg);
        //globject.showCategories(o.responseText);
        var lst = document.getElementById("cat_list");
        var lst2 = document.getElementById("cat_list2");
        lst.id = "catlist";
        var arr = eval("(" + o.responseText + ")");
        
        for(var i = 0; i < arr.categories.length; i++)
        { 
            //alert(arr.categories[i].cat_nam);
            var li = document.createElement("LI");        
            var cbx = document.createElement("INPUT");
            cbx.type = "checkbox";
            cbx.id = arr.categories[i].cat_id;
            
            //Save reference to first cb so we can set focus to it
            if(i==0)
            {
                globject.firstCat = cbx;    
            }
            
            cbx.dispVal = arr.categories[i].cat_nam;
            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);} 
            globject.catListItems[i] = cbx;
        }
        globject.catList = lst;
        //For DEBUG
        ////globject.catListItems[0].checked = true;
        //globject.catListItems[1].checked = true;
        //globject.catListItems[3].checked = true;
       // var tmpdate = new Date();
       // globject.calE1.select(tmpdate);
        //tmpdate.setDate(tmpdate.getDate() + 2)
        //globject.calE1.select(tmpdate);
        ///tmpdate.setDate(tmpdate.getDate() + 3)
       // globject.calE1.select(tmpdate);

        //alert(tmpdate);
        //globject.calE1.select(new Date(tmpdate.getTime() + 86400000));
        //globject.calE1.render();
        //document.getElementById("ys_start_time").selectedIndex = 17;
        //document.getElementById("ys_end_time").selectedIndex = 31;
        //globject.showStep7();
    }
    function showFail(o)
    {
        alert(o.statusText + " major failure");
    }
}

globject.moveBack = function()
{
    switch(globject.onstep)
    {
        case 2:
            globject.showStep1();
            break;
        case 3:
            globject.showStep2();
            break;
        case 4:
            globject.showStep3();
            break;
        case 5:
            globject.showStep4();
            break;
        case 6:
            globject.showStep5();
            break;
        case 7:
            globject.showStep6();
            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;
        case 4:
            globject.showStep5();
            globject.onstep = 5;
            break;
        case 5:
            globject.showStep6();
            globject.onstep = 6;
            break;        
        case 6:
            globject.showStep7();
            globject.onstep = 7;
            break;
    }    
}

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";
    document.getElementById("step_five").className = "step_hidden";
    document.getElementById("step5").className = "step"
    document.getElementById("s5").className = "idle";
    document.getElementById("step_six").className = "step_hidden";
    document.getElementById("step6").className = "step"
    document.getElementById("s6").className = "idle";
    document.getElementById("step_seven").className = "step_hidden";
    document.getElementById("step7").className = "step"
    document.getElementById("s7").className = "idle";
    
}

/**
 * Gathers all data from wizard and wraps into an object literal
 */
globject.getData = function()
{
    globject.ys = {};
    globject.ys.ys_id = globject.cur_ys;
    globject.ys.ys_name = document.getElementById("ys_name").value;
    globject.ys.cats = globject.CatArr; //CROWCODER.JsUtils.getCheckedBoxes(globject.catListItems);
    globject.ys.kywords = document.getElementById("kywords").value;
    globject.ys.address1 = document.getElementById("txtAddress1").value;
    globject.ys.city = document.getElementById("txtCity").value;
    globject.ys.zip = document.getElementById("txtZip").value;
    globject.ys.dates = globject.getDates();
    
}

globject.getDates = function()
{
    var selStart = document.getElementById("ys_start_time");
    var selEnd = document.getElementById("ys_end_time")
    var arrDates = globject.calE1.getSelectedDates();
    try
    {
        var startTime = parseInt(selStart.options[selStart.selectedIndex].value);
        var endTime = parseInt(selEnd.options[selEnd.selectedIndex].value);    
    }
    catch(e)
    {
        //do nothing here
    }
    
    var tmpDate;
    
    //the array of ysdates
    var arrYsDates = [];
    
    //set the start and end time for each date
    for(var i = 0; i < arrDates.length; i++)
    {        
        tmpDate = arrDates[i].getTime();   
        arrYsDates[i] = {"startDt" : tmpDate + startTime, 
                         "endDt" : tmpDate + endTime};   
    }
    return arrYsDates;
}

globject.PreviewCategories = function()
{
    var strCatList = "";
    if(globject.CatArr.length == 0)
    {
        strCatList = " ";
    }
    else
    {
         for(var i = 0; i < globject.CatArr.length; i++)
         {
             strCatList += globject.CatArr[i].catdescr + "<br />";
         }   
    }
    
    return strCatList;
}

globject.previewYs = function()
{
    globject.getData();
    document.getElementById("prevName").innerHTML =
            globject.ys.ys_name;
    document.getElementById("prevCats").innerHTML =
        globject.PreviewCategories(); 
        //CROWCODER.JsUtils.getCheckedNames(globject.CatArr); //globject.catListItems);
    document.getElementById("prevKeywrds").innerHTML = 
        document.getElementById("kywords").value; // globject.ys.kywords;
    document.getElementById("prevSt").innerHTML = 
        globject.ys.address1;
    document.getElementById("prevCity").innerHTML = 
        globject.ys.city;
    document.getElementById("prevZip").innerHTML = 
        globject.ys.zip;
    document.getElementById("prevDates").innerHTML =
        CROWCODER.JsUtils.getCalDates(globject.calE1);
}

globject.submitYs = function()
{
    //CROWCODER.ajax.showWait(document.getElementById("curr_step"));
    var dataok = globject.verifyData();
    
    if(dataok)
    {
         var ys = {};
         
         ys.id = globject.cur_ys;
         ys.tzo = globject.tzo.toString();
         ys.name = document.getElementById("ys_name").value;
         ys.addr1 = document.getElementById("txtAddress1").value;
         ys.city = document.getElementById("txtCity").value;
         ys.zip = document.getElementById("txtZip").value;
         ys.kywords = document.getElementById("kywords").value;
         ys.cats = [];
         for(var i = 0; i < globject.CatArr.length; i++)
         {
             ys.cats.push(globject.CatArr[i].catid);   
         }
         ys.photos = [];
         for(var i = 0; i < globject.img_row.cells.length; i++)
         {
             ys.photos.push(globject.img_row.cells[i].childNodes[0].src);
         }
         ys.dates = globject.getDates(); 
          
        var xhr = new CROWCODER.ajax.YuiConxn();
        xhr.sUrl = "../services/services.aspx";
        xhr.postdata = "?method=submitYs&ysjson=" + 
            encodeURIComponent(ys.toJSONString()); 
                    
        xhr.callback.success = verifyYs;        
        xhr.callback.failure = showFail;
        xhr.makeCall();   
        globject.waiter.show();    
    }   
    //CROWCODER.ajax.hideWait(document.getElementById("curr_step"));
    function verifyYs(o)
    {
        globject.waiter.hide();
        if(o.responseText == "error")
        {
            window.location.href = "Message.htm?qs=5";
        }
        else
        {
            //alert(o.responseText);
            globject.SendAlerts(o.responseText);
            window.location.href = "Message.htm?qs=4";   
            //call to create alerts 
        }               
    }
    function showFail(o)
    {
        globject.waiter.hide();
        //alert(o.responseText);
        window.location.href = "Message.htm?qs=5";
    }
}

globject.SendAlerts = function(p_newid)
{
    var xhr = new CROWCODER.ajax.YuiConxn();
    xhr.sUrl = "../services/services.aspx";
    xhr.postdata = "?method=SendAlerts&ysid=" + 
        encodeURIComponent(p_newid) + "&tzo=" + globject.tzo; 
                
    xhr.callback.success = verifyYs;        
    xhr.callback.failure = showFail;
    xhr.makeCall();
    
    function verifyYs(o)
    {
        return;
    }
    function showFail(o)
    {
        alert(o.responseText);
    }
}

globject.delYs = function()
{
    if(globject.cur_ys > 0)
    {
        var ans = confirm("Are you sure you want to delete " + 
            document.getElementById("ys_name").value + "?");
        if(ans)
        {
            var idx = document.getElementById("selYardSale").selectedIndex;
            var xhr = new CROWCODER.ajax.YuiConxn();
            xhr.sUrl = "../services/services.aspx";
            xhr.postdata = "?method=deleteYs&ysid=" + 
                globject.cur_ys; 
                        
            xhr.callback.success = verifyYs;        
            xhr.callback.failure = showFail;
            xhr.makeCall();
            
            function verifyYs(o)
            {
                globject.cur_ys = 0;
                document.getElementById("selYardSale").remove(idx);
                document.getElementById("selYardSale").selectedIndex = 0;
                globject.resetWiz();
                alert(o.responseText);
            }
            function showFail(o)
            {
                alert(o.responseText);
            }       
        }    
    }    
}

globject.verifyData = function()
{
    var ysname = document.getElementById("ys_name");
    if(ysname.value == "")
    {
        alert("Please enter a name for your yard sale before continuing");
        globject.showStep1();        
        ysname.focus();
        return false;        
    }    
    
    var address1 = document.getElementById("txtAddress1");
    if(address1.value == "")
    {
        alert("Please enter the yard sale address before continuing");
        globject.showStep2();        
        address1.focus();
        return false;
    }
    
    var city = document.getElementById("txtCity");
    if(city.value == "")
    {
        alert("Please enter the city before continuing");
        globject.showStep2();        
        city.focus();
        return false;
    }
    
    var zip = document.getElementById("txtZip");    
    if(zip.value == "")
    {
        alert("Please enter the zip code before continuing");
        globject.showStep2();        
        zip.focus();
        return false;
    }
    
    //Needs dates
    var datearr = globject.calE1.getSelectedDates();
    if(datearr.length < 1)
    {
        alert("Please choose at least one date before continuing");
        globject.showStep4();        
        globject.calE1.cells[0].focus();
        return false;
    }
    
    var start = document.getElementById("ys_start_time");
    if(start.selectedIndex < 0)
    {
        alert("Please enter the start time before continuing");
        globject.showStep4();        
        start.focus();
        return false;
    }
    
    var end = document.getElementById("ys_end_time");
    if(end.selectedIndex < 0)
    {
        alert("Please enter the end time before continuing");
        globject.showStep4();        
        end.focus();
        return false;
    }
    
    //Verify start time is less than end time
    if(start.options[start.selectedIndex].value >= 
        end.options[end.selectedIndex].value)
    {
        alert("Start time must be less than end time.");
        globject.showStep4();        
        end.focus();
        return false;
    }
    
    return true;
}

globject.ShowNameHelp = function()
{
    var msg = "Enter a name for your yard sale. For example, Multiple " +
    "Family Yard Sale would be a good quick description to attract attention";
    var hdr = "Yard Sale Name";
    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowCatHelp = function()
{
    var msg = "Choose the categories that apply to the items you are" +
    " selling. This step is optional, but categorizing your yard " +
    "sale may assist searching.";
    var hdr = "Yard Sale Categories";
    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowDescHelp = function()
{
    var msg = "Describe your yardsale to attract interest. Keep it " +
    "short, but include specific items that may be in high demand " +
    "like kayak or Hummels.";
    var hdr = "Yard Sale Description";
    
    CROWCODER.JsUtils.showHelp(msg, hdr);    
}

globject.ShowCalHelp = function()
{
    var msg = "Click to select the date(s) of your yard sale. ";
    var hdr = "Yard Sale Dates";
    
    CROWCODER.JsUtils.showHelp(msg, hdr); 
}

globject.ShowStartHelp = function()
{
    var msg = "Choose the time of day your yard sale will begin.";
    var hdr = "Yard Sale Start Time";
    
    CROWCODER.JsUtils.showHelp(msg, hdr); 
}

globject.ShowEndHelp = function()
{
    var msg = "Choose the time of day your yard sale will end.";
    var hdr = "Yard Sale End Time";
    
    CROWCODER.JsUtils.showHelp(msg, hdr); 
}

globject.ShowSubmitHelp = function()
{
    var msg = "Review your yard sale and submit for posting, or, " +
     "go back to any step to make changes.";
    var hdr = "Post Yard Sale";
    
    CROWCODER.JsUtils.showHelp(msg, hdr); 
}

globject.ShowImgHelp = function()
{
    var msg = "Type or paste a hyper link to a photo here and " +
        "click Add. PLEASE NOTE that this is not an image upload. " +
        "You must host your photos elsewhere, and tell yardsaleseason.com " +
        "where to find them by adding a link like this one: <br />" +
        "http://www.somesite.com/photos/stuff.jpg <br />" +
        "Yardsaleseason.com recommends www.photobucket.com or " +
        " picasaweb.google.com for image hosting because both " +
        "services are free and provide easy linking.";
    var hdr = "Linking to a photo";
    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}

globject.ShowImgHelp2 = function()
{
    var msg = "Yardsaleseason.com does not accept photos for upload. " +
        "You must host your photos elsewhere, and tell yardsaleseason.com " +
        "where to find them by adding a link like this one in step 6: <br />" +
        "http://www.somesite.com/photos/stuff.jpg <br /><br />" +
        "Yardsaleseason.com recommends www.photobucket.com " +
        " because the service is free and provides easy linking.";
    var hdr = "Linking to a photo";
    
    CROWCODER.JsUtils.showHelp(msg, hdr);
}
window.onload = function(){init();}

