var DEBUG = false;
var oxml = null;
var sURL = null;
var cal_id = null;
var temp_id = null;
var sched_id = null;
var slot_id = null;
var appt_dt = null;
var start_tm = null;
var end_tm = null;

function appt_init()
{
  if(! navigator.cookieEnabled)
  {
    alert("This application requires that first party cookies are enabled to function properly!");
  }
}
function emptyCart()
{
    var frm = document.forms["frmSchedule"];
    if(frm["cboAppts"].options.length <= 0)
    {
        alert("You're cart is already empty!");
        return false;
    }
    var calDate = frm['__caldate'].value;
    var MSG = "Are you sure you want to empty you're cart?";
    if(confirm(MSG))
    {
        if(calDate.trim() == "")
            top.location.href='index.php?cmdSmt=empty-cart';
        else
            top.location.href = 'index.php?cmdSmt=empty-cart&__caldate=' + calDate;
    }
}
function swapImg(oImg)
{
    if("object" != typeof(oImg)) return false;
    var sName = new String(oImg.src);
    var sPath = sName.substring(0,sName.lastIndexOf("/")+1);
    sName = sName.substr(sName.lastIndexOf("/")+1);
    var sExt = sName.substring(sName.indexOf(".")+1,sName.length);
    if(-1 == sName.indexOf("_over"))
    {
        sName = sName.substring(0,sName.indexOf("."));
        sName = sName + "_over." + sExt;
        if(oImg.alt != null) window.status = oImg.alt;
        if(document.all) window.event.returnValue = true;
    } else {
        sName = sName.substring(0,sName.lastIndexOf("_"));
        sName = sName + "." + sExt;
        window.status="";
        if(document.all) window.event.returnValue = true;
    }
    oImg.src = sPath + sName;
    return true;
}
function addAppt()
{
    if(addAppt.arguments.length < 7) return false;
    cal_id = addAppt.arguments[0].toString();
    temp_id = addAppt.arguments[1].toString();
    sched_id = addAppt.arguments[2].toString();
    slot_id = addAppt.arguments[3].toString();
    appt_dt = addAppt.arguments[4].toString();
    start_tm = addAppt.arguments[5].toString().replace("%3A",":").replace("%20"," ");
    end_tm = addAppt.arguments[6].toString().replace("%3A",":").replace("%20"," ");
    
    // create a url
    sURL = basename() + "?cmdSmt=addappt&template_id=" + escape(temp_id) + "&calendar_id=" + escape(cal_id) + 
            "&schedule_id=" + escape(sched_id) + "&slot_id=" + escape(slot_id) +
            "&appt_date=" + escape(appt_dt) + "&start_time=" + 
            escape(start_tm) + "&end_time=" + escape(end_tm);
            
    
    try {
        oxml = new XMLHTTP();
    } catch(e) {
        alert("An error has occurred: " + e.message + "\n" + sURL);
        sURL += "&XHE=0";
        goToURL();
        return false;
    }
    if("object" != typeof oxml)
    {
        sURL += "&XHE=0";
        goToURL();
        return false;
    }
    // else the client browser supports ajax scripting
    sURL += "&XHE=1&ajx=1";
    //prompt("",sURL);
    oxml.open("GET",sURL);
    oxml.onreadystatechange(load_combo);
	oxml.send();
}
function validateAppts(frm)
{
    if("object" != typeof frm) return false;
    if(frm["cboAppts"].options.length <= 0)
    {
        alert("You must select one or more appointments!");
        return false;
    }
    
    if((validate_field(frm["txtUser"],"text"))&&(! validate_field(frm["txtUser"],"email"))) 
    {
        alert("Please enter a valid e-mail address to login!");
        frm["txtUser"].value="";
        frm["txtUser"].focus();
        return false;
    } else if((! validate_field(frm["txtUser"],"text"))||(! validate_field(frm["txtPwd"],"text"))) {
        var sMemMsg = "If you are an existing customer, please click Cancel and enter your e-mail address and password!\n" +
        "Otherwise, please click ok to continue and book the selected appointments!\n";
        if(! confirm(sMemMsg)) 
        {
            frm["txtUser"].value="";
            frm["txtPwd"].value="";
            frm["txtUser"].focus();
            return false;
        }
    }
    if(confirm("Click OK, to continue and book the selected appointments?"))
        return true;
    else
        return false;
}

function load_combo()
{
    //alert("HERE" + oxml.readyState() + " " + oxml.COMPLETE);
    try {
        if(oxml.readyState() != oxml.COMPLETE) return;
        
        var xstatus = oxml.responseText();
        var frm = document.forms['frmSchedule'];
        if("object" != typeof frm) { goToURL(basename() + "?XHE=0"); return; }
        var cbo = frm["cboAppts"];
        if("object" != typeof cbo) { goToURL(basename() + "?XHE=0"); return; }
        
        if(xstatus.toString() == "1") // success
        {
            var opt = new Object();
            opt["text"] = new String(appt_dt + " (" + start_tm + "-" + end_tm + ")");
            opt["value"] = new String(temp_id + "~" + sched_id + "~" + slot_id); 
            if(addOptionToCombo(cbo,opt))
            {
                var el = document.getElementById("itm_count");
                if("object" != typeof el) return true;
                var sLength = new String(cbo.options.length.toString());
                el.innerHTML = "You have " + sLength + " item(s) in your cart!";
                return true;
            }
            return false;
        } else {
            alert(xstatus.toString());
        }
        return false;    
    } catch(e) {
        if(document.all) alert(e.message);
        else alert(e);
    }
}

function goToURL()
{
    if(goToURL.arguments.lenght == 1) top.location.href = goToURL.arguments[0].toString();
    else top.location.href=sURL;
}
function basename()
{
	baseName = window.location.pathname.substring(window.location.pathname.lastIndexOf("/")+1);
	return baseName;
}