﻿var totalamount = parseFloat('0.00').toFixed(2);
var ordcollection = new CreateCollection("OrderCollection");     
//================================================================
function ordernavigatestart(){
    var url = 'http://www.caribbeancuisine.co.uk/inc_ajaxscripts/ajaxwapper.aspx?mod=ordernavigatestart';
    ordernavigate(url);
}
//================================================================
function ordernavigatecategories(catid){
    var url = 'http://www.caribbeancuisine.co.uk/inc_ajaxscripts/ajaxwapper.aspx?mod=ordernavigatecategories&catid=' + catid;
    ordernavigate(url);
}
//================================================================
function ordernavigatemenuitem(itemId,catid){
    var url = 'http://www.caribbeancuisine.co.uk/inc_ajaxscripts/ajaxwapper.aspx?mod=ordernavigatemenuitem&itemid=' + itemId + '&catid=' + catid;
    ordernavigate(url);
}
//================================================================
function ordernavigate(strUrl){
    loadajaxloader();
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp==null){
    Unloadajaxloader();
    alert ("Your browser does not support AJAX!");
    return;}
    xmlHttp.open("POST",strUrl,true);
    xmlHttp.onreadystatechange = ordernavigateresponse;
    window.setTimeout("xmlHttp.send('');", 500);
}
//================================================================
function ordernavigateresponse(){ 
    if (xmlHttp.readyState==4){
    document.getElementById("ctl00_TitleSelection").innerHTML = xmlHttp.responseText;
    Unloadajaxloader();}
}
//================================================================
function searchpostalcode(){
    var pcode = document.getElementById("q_postalcode").value;
    if (pcode.length==4){document.getElementById("q_postalcode").value = pcode + ' ';}
    var pos=pcode.indexOf('  ');
    if (pos>=0){document.getElementById("q_postalcode").value = pcode.slice(0, -1);}
    document.getElementById("postcodedropsearch").innerHTML = '';
    if (pcode == '' || pcode.length > 4){
    document.getElementById("postcodedropsearch").style.display='none';
    return;}   
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp==null){
    alert ("Your browser does not support AJAX!");
    return;} 
    var Query = document.getElementById("q_postalcode").value;
    var url = 'http://www.caribbeancuisine.co.uk/inc_ajaxscripts/ajaxwapper.aspx?mod=dosearchpostcode&value=' + Query;
    xmlHttp.open("POST",url,true);
    xmlHttp.onreadystatechange = searchpostalcoderesponse;
    xmlHttp.send('');  
}
//================================================================
function searchpostalcoderesponse(){ 
    if (xmlHttp.readyState==4){
        var result = xmlHttp.responseText;
        if (result==''){
            document.getElementById("postcodedropsearch").style.display='none'
        }else{
            document.getElementById("postcodedropsearch").style.display=''
            document.getElementById("postcodedropsearch").innerHTML = result;
        }  
    }
}
//================================================================
function doselectpostcode(pcode){
    document.getElementById("q_postalcode").value = pcode;
    document.getElementById("postcodedropsearch").style.display='none';
    document.getElementById("postcodedropsearch").innerHTML = '';
}
//================================================================
function IsNumeric(strString){
    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;
    if (strString.length == 0) return false;
    for (i = 0; i < strString.length && blnResult == true; i++){
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1){
    blnResult = false;}}
    return blnResult;
}
//================================================================
function CreateCollection(ClassName){
    var obj=new Array();
    eval("var t=new "+ClassName+"()");
    for(_item in t){
        eval("obj."+_item+"=t."+_item);
    }
    return obj;
}
//================================================================  
function OrderCollection(){
    this.Add = function(obj){
        this.push(obj);
    }
    this.RemoveAll = function(){
        this.length = 0;
    }
    this.Remove = function(objindex){
          if (objindex < 0 || objindex > this.length - 1) return;
          this.splice(objindex, 1);
    } 
    this.Clone = function () {
        var c = new CCollection();
        for (var i = 0; i < this.length; i++)
            c.add(this[i]);
        return c;
    }
}
//================================================================
function OrderItem(itemName,itemId,lgsm,price,quantity){
    this.itemName = itemName
    this.itemId = itemId
    this.lgsm = lgsm
    this.price = price
    this.quantity = quantity
}
//================================================================
function addToOrder(itemName,itemId,pricelg,pricesm){
    if (document.getElementById("ord_lg").checked==true && document.getElementById("ord_sm").checked==true){
    Unloadajaxloader();
    alert ("You can not have large and small in the same order item");
    return;}
    if (document.getElementById("ord_lg").checked==false && document.getElementById("ord_sm").checked==false){
    Unloadajaxloader();
    alert ("You must select either a small or a large!");
    return;}    
    if (document.getElementById("ord_quantity").value=='' || document.getElementById("ord_quantity").value=='0'){
    Unloadajaxloader();
    alert ("You have to enter at least one item");
    return;}    
    if (IsNumeric(document.getElementById("ord_quantity").value)==false){
    Unloadajaxloader();
    alert ("Sorry this is an invalid quantity.");
    return;} 
    var xprice = parseFloat('0.00').toFixed(2);
    var lgsm = '';
    var quantity = document.getElementById("ord_quantity").value;
    if (document.getElementById("ord_lg").checked==true){
    xprice = parseFloat(pricelg).toFixed(2) * quantity;
    lgsm = 'Large';}
    if (document.getElementById("ord_sm").checked==true){
    xprice = parseFloat(pricesm).toFixed(2) * quantity;
    lgsm = 'Small';}
    ordcollection.Add(new OrderItem(itemName,itemId,lgsm,xprice.toFixed(2),quantity));
    totalamount = parseFloat(totalamount) + parseFloat(xprice.toFixed(2))
    document.getElementById("vieworderlnk").innerHTML = 'View Order ( &pound ' + totalamount.toFixed(2) + ' )';
    if (totalamount.toFixed(2) >= 15.00){
        document.getElementById("q_housestreet").style.display='';
        document.getElementById("q_housenum").style.display='';
        document.getElementById("q_postalcode").style.display='';
    }    
    ordernavigatestart();
}
//================================================================
function loadpreorderitems(type,dialodident){
    loaddialogajaxloader(type);
    document.getElementById("ViewOrderBox").innerHTML ='';
    var strHtml = '';
    var orItm = '';
    var dialodid = dialodident;
    for (var i = 0; i < ordcollection.length; i++){
        orItm = ordcollection[i];
        strHtml += '<table id="ViewOrderBoxItem" ><tr>';
        strHtml += '<td><span>' + orItm.itemName + ' ( ' + orItm.lgsm + ' )</span></td>';
        strHtml += '<td style="width: 110px">Total Price: &pound ' + orItm.price + '<label></label></td>';
        strHtml += '<td style="width: 80px">Quantity: <input id="ord_quantity' + i + '" type="text" value="' + orItm.quantity + '" /><input id="hord_quantity' + i + '" type="hidden" value="' + orItm.quantity + '" /></td>';
        strHtml += '<td style="width: 60px"><a href="javascript:orderremove(' + i + ',' + type + ',\'' + dialodid + '\');">Remove</a></td>';
        strHtml += '<td style="width: 60px"><a href="javascript:orderupdate(' + i + ',' + type + ',\'' + dialodid + '\');">Update</a></td>';
        strHtml += '</tr></table>';
    }
    document.getElementById("ViewOrderBox").innerHTML = strHtml;    
    Unloaddialogajaxloader(type);
}
//================================================================
function emptyorder(type,dialodident){
    loaddialogajaxloader(type);
    totalamount = parseFloat('0.00').toFixed(2);
    ordcollection.RemoveAll();
    document.getElementById("vieworderlnk").innerHTML = 'View Order ( &pound 0.00 )';
    document.getElementById("q_housestreet").style.display='none';
    document.getElementById("q_housenum").style.display='none';
    document.getElementById("q_postalcode").style.display='none';    
    document.getElementById('Dialogbg').style.display='none';
    document.getElementById(dialodident).style.display='none';
    Unloaddialogajaxloader(type);
}
//================================================================
function orderremove(index,type,dialodident){
    if (ordcollection.length <= 1){
    emptyorder(type,dialodident);
    return;}
    ordcollection.Remove(index);
    var orItm ='';
    totalamount = parseFloat('0.00').toFixed(2);
    for (var i = 0; i < ordcollection.length; i++){
    orItm = ordcollection[i];
    totalamount = parseFloat(totalamount) + parseFloat(orItm.price);}
    document.getElementById("vieworderlnk").innerHTML = 'View Order ( &pound ' + totalamount.toFixed(2) + ' )';
    if (totalamount.toFixed(2) >= 15.00){
    document.getElementById("q_housestreet").style.display='';
    document.getElementById("q_housenum").style.display='';
    document.getElementById("q_postalcode").style.display='';
    }else{
    document.getElementById("q_housestreet").style.display='none';
    document.getElementById("q_housenum").style.display='none';
    document.getElementById("q_postalcode").style.display='none';}    
    loadpreorderitems(type,dialodident);
}
//================================================================
function orderupdate(index,type,dialodident){
    if (document.getElementById("ord_quantity"+index).value=='' || document.getElementById("ord_quantity"+index).value=='0'){
    alert ("You have to enter at least one item.");
    return;}    
    if (IsNumeric(document.getElementById("ord_quantity"+index).value)==false){
    alert ("Sorry this is an invalid quantity.");
    return;} 
    var oldprice = parseFloat(ordcollection[index].price);
    var oldquantity = parseInt(document.getElementById("hord_quantity"+index).value); 
    var perprice = parseFloat(oldprice.toFixed(2) / oldquantity);
    var newquantity = parseInt(document.getElementById("ord_quantity"+index).value);
    var newprice = parseFloat(perprice * newquantity) 
    ordcollection[index].price = newprice.toFixed(2);
    ordcollection[index].quantity = newquantity;
    var orItm ='';
    totalamount = parseFloat('0.00').toFixed(2);
    for (var i = 0; i < ordcollection.length; i++){
    orItm = ordcollection[i];
    totalamount = parseFloat(totalamount) + parseFloat(orItm.price);}
    document.getElementById("vieworderlnk").innerHTML = 'View Order ( &pound ' + totalamount.toFixed(2) + ' )';
    if (totalamount.toFixed(2) >= 15.00){
    document.getElementById("q_housestreet").style.display='';
    document.getElementById("q_housenum").style.display='';
    document.getElementById("q_postalcode").style.display='';
    }else{
    document.getElementById("q_housestreet").style.display='none';
    document.getElementById("q_housenum").style.display='none';
    document.getElementById("q_postalcode").style.display='none';}    
    loadpreorderitems(type,dialodident);
}
//================================================================
var temptype;
var tempdialodident;
function loadorderitems(type,dialodident){
    if (totalamount == '0.00'){
    alert ("Sorry, your basket is empty, please place an order.");
    Unloaddialog(dialodident);
    return;}   
    if (document.getElementById('q_phonenum').value=='' || document.getElementById('q_name').value==''){
    alert ("You not completed the form, please complete the form to continue.");
    Unloaddialog(dialodident);
    return;}   
    temptype = type;
    tempdialodident = dialodident;
    if (totalamount.toFixed(2) >= 15.00){
        if (document.getElementById('q_housenum').value==''){
        alert ("You not completed the form, please complete the form to continue.");
        Unloaddialog(dialodident);
        return;}
        if (IsNumeric(document.getElementById("q_housenum").value)==false){
        alert ("Sorry this is an invalid number.");
        return;} 
        if (document.getElementById('q_housestreet').value==''){
        alert ("You not completed the form, please complete the form to continue.");
        Unloaddialog(dialodident);
        return;} 
        if (document.getElementById('q_postalcode').value==''){
        alert ("You not completed the form, please complete the form to continue.");
        Unloaddialog(dialodident);
        return;}
        xmlHttp = GetXmlHttpObject();
        if (xmlHttp==null){
        alert ("Your browser does not support AJAX!");
        return;} 
        var url = 'http://www.caribbeancuisine.co.uk/inc_ajaxscripts/ajaxwapper.aspx?mod=dovalidatepostalcode&value=' + document.getElementById('q_postalcode').value;
        xmlHttp.open("POST",url,true);
        xmlHttp.onreadystatechange = loadorderitemspt1;
        xmlHttp.send('');
    }else{  
        loadorderitemspt1(false);
    }   
}
//================================================================
function loadorderitemspt1(boolajax){ 
    var result;
    if (boolajax==false){
        result = 'false';
        loadorderitemspt2(result,temptype,tempdialodident);
    }else{
        if (xmlHttp.readyState==4){
            result = xmlHttp.responseText;
            loadorderitemspt2(result,temptype,tempdialodident);    
        }
    }  
}
//================================================================
function loadorderitemspt2(result,type,dialodident){    
    if (result=='true' || result=='false'){
        loaddialogajaxloader(type);
        document.getElementById("CompleteOrderBox").innerHTML ='';
        var strHtml = '';
        var orItm = '';
        var dialodid = dialodident;
        var subtotal = parseFloat('0.00').toFixed(2);
        strHtml += '<table id="ViewOrderBoxItem" ><tr>';
        strHtml += '<td><span><strong>' + document.getElementById("q_name").value + '</strong> ( ' + document.getElementById("q_phonenum").value + ' )</span>';
        strHtml += '<div style="float:right;"><div style="float:left;">  -:  Address: ' + document.getElementById("q_housenum").value + ' ' + document.getElementById("q_housestreet").value + ', </div><div style="margin-left: 5px;float:right; text-transform: uppercase;">' + document.getElementById("q_postalcode").value + '</div></div></td>';
        strHtml += '</tr></table><br />';
        if (result=='true'){
            strHtml += '<table id="ViewOrderBoxItem" ><tr>';
            strHtml += '<td><span><strong>To be delivered:</strong></span> <input style="vertical-align: middle;" onclick="confirmdelivery();" id="comp_chkdel" type="checkbox" />';
            strHtml += '<div id="confirmdelivery" style="display: none;"><br />Checking this box enables the delivery service <br />with the address you provided.</div></td>';
            strHtml += '</tr></table><br />';
        }
        for (var i = 0; i < ordcollection.length; i++){
            orItm = ordcollection[i];
            subtotal = parseFloat(subtotal) + parseFloat(orItm.price);
            strHtml += '<table id="ViewOrderBoxItem" ><tr>';
            strHtml += '<td style="width: 300px"><span>' + orItm.itemName + ' ( ' + orItm.lgsm + ' )</span></td>';
            strHtml += '<td style="width: 70px">Quantity: ' + orItm.quantity + '</td>';
            strHtml += '<td style="width: 130px">Total Price: &pound ' + orItm.price + '</td>';
            strHtml += '</tr></table>';
        }
        strHtml += '<br /><table id="ViewOrderBoxItem" ><tr>';    
        if (result=='true'){
            strHtml += '<td style="width: 300px; border-top: #FFFFFF 1px solid;"><span>Your order completed: </span></td>';
        }else{
            strHtml += '<td style="width: 300px; border-top: #FFFFFF 1px solid;"><span>Your order completed: <strong>To be picked up.</strong></span></td>';
        }      
        strHtml += '<td style="width: 70px; border-top: #FFFFFF 1px solid;">Inc VAT.</td>';
        strHtml += '<td style="width: 130px; border-top: #FFFFFF 1px solid;">Total Price: <strong>&pound ' + subtotal.toFixed(2) + '</strong></td>';
        strHtml += '</tr></table><br /><br />';               
        document.getElementById("CompleteOrderBox").innerHTML = strHtml;    
        Unloaddialogajaxloader(type);
    }else{
    alert (result);
    Unloaddialog(dialodident);}
}
//================================================================
function confirmdelivery(){
    document.getElementById("confirmdelivery").style.display = '';    
    window.setTimeout("document.getElementById(\"confirmdelivery\").style.display = 'none';", 2500);
}
//================================================================
function placeorder(type,dialodident){
    loaddialogajaxloader(type);
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp==null){
    alert ("Your browser does not support AJAX!");
    return;} 
    var qname = document.getElementById('q_name').value;
    var qphonenum = document.getElementById('q_phonenum').value;
    var qhousenun = document.getElementById('q_housenum').value;
    var qstreet = document.getElementById('q_housestreet').value;
    var qpostcode = document.getElementById('q_postalcode').value;
    var qdeliver = 'false';
    if (document.getElementById("comp_chkdel")!==null){
        if (document.getElementById("comp_chkdel").checked==true){qdeliver = 'true';}
    }
    var qcollection = '';
    var orItm = '';
    for (var i = 0; i < ordcollection.length; i++){
        orItm = ordcollection[i];
        qcollection += '{' + orItm.itemId + '|' + orItm.lgsm + '|' + orItm.quantity + '},';
    }
    qcollection = qcollection.slice(0, -1);
    var url = 'http://www.caribbeancuisine.co.uk/inc_ajaxscripts/ajaxwapper.aspx?mod=doplaceorder&qname=' + qname + '&qphonenum=' + qphonenum + '&qhousenun=' + qhousenun + '&qstreet=' + qstreet + '&qpostcode=' + qpostcode + '&qdeliver=' + qdeliver + '&qcollection=' + qcollection ;
    // alert (url);
    temptype = type;
    tempdialodident = dialodident;
    xmlHttp.open("POST",url,true);
    xmlHttp.onreadystatechange = placeorderresponse;
    xmlHttp.send('');
}
//================================================================
function placeorderresponse(){
    if (xmlHttp.readyState==4){
        var result = xmlHttp.responseText;
        if (result=='complete1'){
           completeordernotify(1,temptype,tempdialodident);
        }else if (result=='complete2'){
           completeordernotify(2,temptype,tempdialodident);
        }else if (result=='error2'){
            alert ("Error 2!, There was an error adding your ordered items, Please try again later.");
            Unloaddialogajaxloader(temptype);
            Unloaddialog(tempdialodident); 
        }else if (result=='error1'){
            alert ("Error 1!, There was an error adding your details to the order, Please try again later.");
            Unloaddialogajaxloader(temptype);
            Unloaddialog(tempdialodident);  
        }    
    }
}
//================================================================
function completeordernotify(notify,type,dialodident){
    var strResult;
    if(notify==1){
        strResult = 'Your order has been placed and will be delivered, if there is a problem please contact us.';
    }else if (notify==2){
        strResult = 'Your order has been placed, please give at least 30 minutes be for you pickup your order.';
    }
    totalamount = parseFloat('0.00').toFixed(2);
    ordcollection.RemoveAll();
    document.getElementById("vieworderlnk").innerHTML = 'View Order ( &pound 0.00 )';
    document.getElementById("q_name").value='';
    document.getElementById("q_phonenum").value='';
    document.getElementById("q_housestreet").value='';
    document.getElementById("q_housenum").value='';
    document.getElementById("q_postalcode").value='';
    document.getElementById("q_housestreet").style.display='none';
    document.getElementById("q_housenum").style.display='none';
    document.getElementById("q_postalcode").style.display='none';
    ordernavigatestart();
    alert (strResult);
    Unloaddialogajaxloader(type);
    Unloaddialog(dialodident); 
}
//================================================================
