﻿function GetStatesByID(obj) {
    var CountryId;
    CountryId = obj.value;

    subOptions(document.getElementById('selstate'));
    addOption(document.getElementById('selstate'), "0", "Loading data...");

    subOptions(document.getElementById('selcounty'));
    addOption(document.getElementById('selcounty'), "0", "Choose a county");

    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose a area");



    Bloc.ObjectModel.State.GetStatesByCountryID(CountryId, OnPostGetStatesByIDCallBack);
    return false;
}

function OnPostGetStatesByIDCallBack(res) {

    subOptions(document.getElementById('selstate'));
    addOption(document.getElementById('selstate'), "0", "Choose a state");
    if (res.value != null & res.value != "") {
        var state = document.getElementById("selstate");
        subOptions(state);
        addOption(state, "0", "Choose a state");
        var num = res.value.length;

        for (i = 0; i < num; i++) {
            if (res.value[i].StateName != "") {
                addOption(state, res.value[i].StateID, res.value[i].StateName);
            }
        }
    }
}

function GetCountyByID(obj) {
    subOptions(document.getElementById('selcounty'));
    addOption(document.getElementById('selcounty'), "0", "Loading data...");

    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose a area");


    Bloc.ObjectModel.County.GetCountiesByStateID(obj.value, OnPostGetCountyByIDCallBack);

    return false;
}

function OnPostGetCountyByIDCallBack(res) {
    subOptions(document.getElementById('selcounty'));
    addOption(document.getElementById('selcounty'), "0", "Choose a county");

    if (res.value != null & res.value != "") {
        var county = document.getElementById("selcounty");
        var num = res.value.length;
        subOptions(document.getElementById('selcounty'));
        addOption(document.getElementById('selcounty'), "0", "Choose a county");
        for (i = 0; i < num; i++) {
            if (res.value[i].CountyName != "") {
                addOption(document.getElementById('selcounty'), res.value[i].CountyID, res.value[i].CountyName);
            }
        }
    }

}

function GetAreaByID(obj) {

    document.getElementById("ddlArea").options[0].text = "Loading data...";
    Bloc.ObjectModel.Area.GetAreasByCountyID(obj.value, OnPostGetAreaByIDCallBack);

    return false;
}

function OnPostGetAreaByIDCallBack(res) {
    subOptions(document.getElementById("ddlArea"));
    addOption(document.getElementById("ddlArea"), "0", "Choose a area");

    if (res.value != null & res.value != "") {
        var area = document.getElementById("ddlArea");
        var num = res.value.length;
        subOptions(document.getElementById("ddlArea"));
        addOption(document.getElementById("ddlArea"), "0", "Choose a area");

        areabuf = [];
        for (i = 0; i < num; i++) {
            if (res.value[i].AreaName != "") {
                areabuf.push(res.value[i].PostalCode);
                addOption(document.getElementById("ddlArea"), res.value[i].AreaID, res.value[i].AreaName + "( post Code: " + res.value[i].PostalCode + " )");
            }
        }
    }
}

function ClearSelectArea(obj) {
    subOptions($("selstate"));
    addOption($("selstate"), "0", "Choose state");

    subOptions($("selcounty"));
    addOption($("selcounty"), "0", "Choose county");

    subOptions($("ddlArea"));
    addOption($("ddlArea"), "0", "Choose area");

    GetStatesByID(obj);

}

function resetquantity(obj, productid, clientid) {
    if (obj.checked == 1) {
        var sel = "selquantity" + productid;
        $(sel).disabled = false;
        $(sel).value = 1;
    }
    else {
        var sel = "selquantity" + productid;
        $(sel).disabled = true;
        $(sel).value = 1;
    }
    resettotle(clientid);
}

function resettotle(clientid) {
    var total = "totle" + clientid;
    var table = "table" + clientid;
    var products = $(table).getElementsByClassName("product");
    var totle = 0;
    for (var i = 0, n = products.length; i < n; i++) {
        var productid = products[i].id;
        var chk = "chk" + productid;
        if ($(chk).checked == 1) {
            var sel = "selquantity" + productid;
            var spanproduct = "spanproduct" + productid;
            var price = $(sel).value * parseFloat($(spanproduct).innerHTML);
            totle += price;
        }
    }
    $(total).innerHTML = totle;
    //    var min = parseInt($(table).getAttribute("rel"),10);;
    //    if (min <= totle) {
    //        var a = "a" + clientid;
    //        $(a).disabled = false;
    //    }
    //    else {
    //        var a = "a" + clientid;
    //        $(a).disabled = true;
    //    }
}

function SaveOrder() {
    var clientid = $('clientid').value;
    resettotle(clientid);
    var totle = "totle" + clientid;
    var min = $('min').value;
    var total = parseInt($(totle).innerHTML, 10);
    if (total < min) {
        $('diverror').show();
        return false;
    }
    else {
        $('diverror').hide();
    }

    GetProductsQuantity(clientid);

    if ($F('ordername').trim() == "") {
        $('pmessage1').innerHTML = "Please type your name";

        $('diverror1').show();
        return false;

    }

    if ($F('address').trim() == "") {
        $('pmessage1').innerHTML = "Please type your address";

        $('diverror1').show();
        return false;
    }

    if ($F('phone').trim() == "") {
        $('pmessage1').innerHTML = "Please type your phone";

        $('diverror1').show();
        return false;
    }

    return true;
}

function GetProductsQuantity(clientid) {
    var table = "table" + clientid;
    var products = $(table).getElementsByClassName("product");
    var productids = "";
    var quantity = "";
    for (var i = 0, n = products.length; i < n; i++) {
        var productid = products[i].id;
        var chk = "chk" + productid;
        if ($(chk).checked == 1) {
            var sel = "selquantity" + productid;
            if (quantity == "")
                quantity = $(sel).value;
            else
                quantity += "|" + $(sel).value;

            if (productids == "")
                productids = productid;
            else
                productids += "|" + productid;
        }
    }
    $('products').value = productids;
    $('quantitys').value = quantity;
}

function searchhouseinselect() {
    countryid = $('ddlCountry').value;
    stateid = $("selstate").value;
    countyid = $("selcounty").value;
    areaid = $("ddlArea").value;


    $("alocation").innerHTML = $('ddlArea').options[$('ddlArea').selectedIndex].text + "," + $('selcounty').options[$('selcounty').selectedIndex].text;

    $("changelocation").href = "../popups/selectlocation.rails?Cid=" + countryid + "&Sid=" + stateid + "&Cyid=" + countyid + "&Aid=" + areaid;
    $("alocation").href = "../popups/selectlocation.rails?Cid=" + countryid + "&Sid=" + stateid + "&Cyid=" + countyid + "&Aid=" + areaid;
    Bloc.ObjectModel.Delivery.SearchRestaurant(countryid, stateid, countyid, areaid, OnPostBackSearchRestarant);
}

function OnPostBackSearchRestarant(res) {
    var buf = [];
    if (res.value != null) {
        for (var i = 0, n = res.value.length; i < n; i++) {
            var client = res.value[i];
            buf.push('<dd>');
            buf.push('<div class="wrap noimage">');
            buf.push('<h2><a href="menu.rails?CID='+client.ClientID+'">' + client.RestaurantName + '</a></h2>');
            buf.push('<p class="description">' + client.Description + '</p>');
            buf.push('<dl>');
            buf.push('<dt>Delivery times:</dt>');
            buf.push('<dd>');
            var d = new Date();

            if (client.OpenFrom < client.OpenTo) {
                if (d.getHours() >= client.OpenFrom && d.getHours() <= client.OpenTo)
                    buf.push(client.OpenFrom + ':00 to ' + client.OpenTo + ':59.');
                else
                    buf.push('We do not deliver now, try again between ' + client.OpenFrom + ':00 to ' + client.OpenTo + ':59.');
            }
            else {
                if (d.getHours() >= client.OpenFrom || d.getHours() <= client.OpenTo)
                    buf.push(client.OpenFrom + ':00 to ' + client.OpenTo + ':59.');
                else
                    buf.push('We do not deliver now, try again between ' + client.OpenFrom + ':00 to ' + client.OpenTo + ':59.');
            }

            buf.push('</dd>');
            buf.push('</dl>');

            buf.push('<div class="actions">');
            buf.push('<div class="button-green right">');
            buf.push('<div class="bwrap">');
            buf.push('<a href="menu.rails?CID='+client.ClientID+'">Order Online</a>');
            buf.push(' </div>');
            buf.push('</div>');
            buf.push('</div>');
            buf.push('</div>');
            buf.push('</dd>');
        }
    }
    

    var all = buf.join("");

    var dds = $('divrestaurant').childElements();
    for (var i = 1, n = dds.length; i < n; i++) {
        dds[i].remove();
    }

    new Insertion.Bottom($('divrestaurant'), buf.join(""));
    jQuery(document).trigger('close.facebox');
}

function viewmenu(clientid) {
    var menu = "limenu" + clientid;
    if ($(menu).style.display == "none") {
        $(menu).innerHTML = '<img class="loader" src="/Content/gfx/ajax-loader-small.gif" alt="" /> Loading...';
        $(menu).show();
        Bloc.ObjectModel.Delivery.GetClientInfoByClientID(clientid, OnPostGetMenuByClientID);
    }
}

function OnPostGetMenuByClientID(res) {
    var buf = [];
    var clientid = 0;
    if (res.value != null) {
        //for (var i = 0, n = res.value.length; i < n; i++) {
            var client = res.value;
            clientid = client.ClientID;
//            buf.push('<li class="client-sublist" id="limenu'+client.ClientID+'">');
            buf.push('<table id="table' + client.ClientID + '" rel="' + client.MinimumOrder + '" class="fullSiteWidth" border="0" cellspacing="2" cellpadding="0">');
            buf.push('<tr>');
            buf.push('<th scope="col">Order</th>');
            buf.push('<th scope="col">Quantity</th>');
            buf.push('<th scope="col">Item</th>');
            buf.push('<th scope="col">Description</th>');
            buf.push('<th scope="col">Price</th>');
            buf.push('</tr>');
            for (var j = 0, m = client.DeliveryMenu.length; j < m; j++) {
                var menu = client.DeliveryMenu[j];
                buf.push('<tr id="' + menu.ProductID + '" class="specalt hightlighted product">');
                buf.push('<td><input id="chk' + menu.ProductID + '" type="checkbox" onClick="resetquantity(this,' + menu.ProductID + ',' + client.ClientID + ')" /></td>');
                buf.push('<td><select id="selquantity' + menu.ProductID + '" name="Quantity" disabled onchange="resettotle(' + client.ClientID + ')">');
                for (var k = 1; k <= 10; k++) {
                    buf.push('<option value="' + k + '">' + k + '</option>');
                }
                buf.push('</select></td>');
                buf.push('<td>' + menu.ItemName + '</td>');
                buf.push('<td>' + menu.ItemDescription + '</td>');
                buf.push('<td><span id="spanproduct' + menu.ProductID + '">' + menu.Itemprice + '</a> RMB</td>');
                buf.push('</tr>');
            }
            buf.push('<tr class="specalt hightlighted">');
            buf.push('<td Colspan="3" >Total: <span id="totle' + client.ClientID + '">0</span>RMB</td>');
            buf.push('<td Colspan="2">');
            var d = new Date();
            if (client.OpenFrom < client.OpenTo) {
                if (d.getHours() >= client.OpenFrom && d.getHours() <= client.OpenTo)
                    buf.push('<a id="a' + client.ClientID + '" href="../popups/restaurant_order.rails?Cid=' + client.ClientID + '" class="submit-green left" rel="facebox" style="float:right;">Send your Order</a>');
                else
                    buf.push('This restaurant is closed now,it opens again from ' + client.OpenFrom + ':00 to ' + client.OpenTo + ':59</td>');
            }
            else {
                if (d.getHours() >= client.OpenFrom || d.getHours() <= client.OpenTo)
                    buf.push('<a id="a' + client.ClientID + '" href="../popups/restaurant_order.rails?Cid=' + client.ClientID + '" class="submit-green left" rel="facebox" style="float:right;">Send your Order</a>');
                else
                    buf.push('This restaurant is closed now,it opens again from ' + client.OpenFrom + ':00 to ' + client.OpenTo + ':59</td>');
            }
            buf.push('</tr>');

            buf.push('</table>');
        //}
            var all = buf.join("");
            var menu = "limenu" + clientid;
            $(menu).innerHTML = all;
//        var client = "liclient" + clientid;
//        new Insertion.After(client, all);
        jQuery('a[rel*=facebox]').facebox();

    }
}

