﻿// AJAX

//var siteroot ="/sbc/"




function sendAjaxRecommendSite(action, divResultsID, ControlsDivID) 
{
    http.open('POST', action, true);
    http.onreadystatechange = function() {
        var divResults = document.getElementById(divResultsID);
        var divControls = document.getElementById(ControlsDivID);
        
        if ((http.readyState == 4)||(http.readyState == 'complete'))
        {
            var response = http.responseText;
            if ((response.indexOf('</result>') != -1)&&(divResults != null)&&(divResults != 'undefined'))
            {
               divResults.innerHTML = response.substring(response.indexOf('<result>') + 8, response.indexOf('</result>'));
               if ((divControls != null)&&(divControls != 'undefined'))
               {
                    divControls.style.display = 'none';
               }
            }
        }
    };
    http.send(null);
}







// AJAX

    //global array for holding the return values from server ajax call coming back in msg    
    var retValues;
    //global for passing parameters to the javascript doAjaxRequest() function
    var params;



            //ctrlid        - the id of the control to write into
            //actionvalue   - inc | dec | set
            //idvalue       - id of the record
            //price         - price
            //crcatn        - crcatn
            function doAjaxRequest(sub,ctrlID,actionvalue,idvalue,price,crcatn,param7,param8,param9,param10,param11,param12,param13)
            {
                //debugger;
                switch (sub)
                {
                    case "doGetClubLookupDiv":
                        //Handles getting the innerHTML for the card order overview tree
                        //debugger;
                        $.ajax
                        ({
                            type: "POST",
                            url: siteroot + "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doGetClubLookupDiv&ctrlresultvalue=" + ctrlID + "&ctrlresultid=" + actionvalue ,
                            success: function(msg)
                            {
                                //debugger;
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                                                
                                var retvalue = getReturnValue("clublookupdiv");
                                if (retvalue != "")
                                {
                                    //debugger;
                                    //control('ctl00_ContentPlaceHolder1_ctl00_divAccountLookup').innerHTML = retvalue
                                    //$(".divAccountLookup").innerHTML = retvalue
                                    $(".divModalDialog").html(retvalue) ;
                                    
                                    setDivPosition();
                                   //Show the Hidden frame.
                                    $(".divModalDialog").css("display","block") ;
                                    
                                    //$(".divAccountLookup3").fadeTo("slow", 0.0);
                                    //$(".divAccountLookup3").fadeTo(0, 1.0);
                                    // $(".divAccountLookup").center(); 
                                    //debugger;
                                }                                
                                
                            },
                            error: function()
                            {
                                alert("error");
                            }
                        });
                        break; 
                        //                   
                    case "doGetClubList":
                        //Handles getting the innerHTML for the card order overview tree
                        //debugger;
                        $.ajax
                        ({
                            type: "POST",
                            url: siteroot + "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doGetClubList&keyword=" + ctrlID + "&postcode=" + actionvalue + "&ctrlresultvalue=" + idvalue + "&ctrlresultid=" + price + "&width=" + crcatn + "&height=" + param7,
                            success: function(msg)
                            {
                                //debugger;
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                                                
                                var retvalue = getReturnValue("clublookupdiv");
                                if (retvalue != "")
                                {
                                    //debugger;
                                    //control('ctl00_ContentPlaceHolder1_ctl00_divAccountLookup').innerHTML = retvalue
                                    //$(".divAccountLookup").innerHTML = retvalue
                                    $(".divModalDialog").html(retvalue) ;
                                    
                                    setDivPosition();
                                   //Show the Hidden frame.
                                    $(".divModalDialog").css("display","block") ;
                                    
                                    //$(".divAccountLookup3").fadeTo("slow", 0.0);
                                    //$(".divAccountLookup3").fadeTo(0, 1.0);
                                    // $(".divAccountLookup").center(); 
                                    //debugger;
                                }                                
                                
                            },
                            error: function()
                            {
                                alert("error");
                            }
                        });
                        break; 
                        
                        
                    case "doSetCurrentMainMenuId":
                        //parameters are..
                        //ctrlID= the id of the plan item
                        //actionvalue = mode =( edit | browse )
                        //idvalue = url to navigate to if any
                        //price = pmanno
                        //crcatn = product_code
                        //debugger;
                        $.ajax
                        ({
                            type: "POST",
                            url:  siteroot + "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doSetCurrentMainMenuId&mainmenuguid=" + ctrlID + "&link=" +  actionvalue ,
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                //debugger;
                                
                                var retvalue = getReturnValue("error");
                                if (retvalue != "")
                                {
                                   alert("Error " + retvalue);
                                }
                                else
                                {
                                    //CureentIplan has been set on the server
                                    //now if the url parameter is set, then navigate to it
                                    //debugger;
                                    var link = getReturnValue("link");
                                    //debugger;
                                    if (link != undefined)
                                    {
                                        if (link != "")
                                        {
                                            window.location =link;    
                                        }
                                    }
                                    
                                }
                                
                            },
                            error: function()
                            {    
                                alert("doSetCurrentMainMenuId() error");
                            }
                        });
                        break;                                        
                    case "doGetOrderTotalsOnly":
                        //parameters are..
                        //ctrlID= the id the control to put the value in
                        //actionvalue = the context we are ( currently only 'shoppingcart_a' )
                        //idvalue = the value we are interested in (totalpricegreen | totalpriceyellow)
                        //price = pmanno
                        //crcatn = product_code
                        //debugger;
                        $.ajax
                        ({
                            type: "POST",
                            url: "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doGetOrderTotalsOnly",
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                //debugger;
                                
                                var retvalue = getReturnValue("error");
                                if (retvalue != "")
                                {
                                   alert("Error " + retvalue);
                                }
                                else
                                {
                                    //debugger;
                                    //We have return params of
                                    //totalpricegreen | totalpriceyellow
                                    var pricegreen = getReturnValue("totalpricegreen");
                                    var priceyellow = getReturnValue("totalpriceyellow");
                                    if (idvalue=='totalpricegreen')
                                    {
                                       (control(ctrlID).value =pricegreen);    
                                    }
                                    if (idvalue=='totalpriceyellow')
                                    {
                                        (control(ctrlID).value =priceyellow); 
                                    }
                                    if (idvalue=='totalpriceall')
                                    {
                                        (control(ctrlID).value =(parseFloat(priceyellow)+parseFloat(pricegreen)).toFixed(2)); 
                                    }
                                }
                                
                            },
                            error: function()
                            {    
                                alert("doIplanDelete error");
                            }
                        });
                        break;                

                    //calls back to server to set CurrentIplanMode
                    case "doSetIplan":
                        //parameters are..
                        //ctrlID= the id of the plan item
                        //actionvalue = mode =( edit | browse )
                        //idvalue = url to navigate to if any
                        //price = pmanno
                        //crcatn = product_code
                        //debugger;
                        $.ajax
                        ({
                            type: "POST",
                            url: "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doSetIplan&PlanId=" + ctrlID + "&mode=" + actionvalue ,
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                //debugger;
                                
                                var retvalue = getReturnValue("error");
                                if (retvalue != "")
                                {
                                   alert("Error " + retvalue);
                                }
                                else
                                {
                                    //CureentIplan has been set on the server
                                    //now if the url parameter is set, then navigate to it
                                    if (idvalue != undefined)
                                    {
                                        if (idvalue != "")
                                        {
                                            window.location =idvalue;    
                                        }
                                    }
                                    
                                }
                                
                            },
                            error: function()
                            {    
                                alert("doIplanDelete error");
                            }
                        });
                        break;

                    //Covers both the DELETE AND ADD of iplanCards!
                    case "doAddIplanItem":
                        //parameters are..
                        //ctrlID= the id of the product item
                        //actionvalue = currentIplanId
                        //idvalue = catcode
                        //price = pmanno
                        //crcatn = product_code
                    
                        $.ajax
                        ({
                            type: "POST",
                            url: "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doAddIplanItem&context_id=" + ctrlID + "&PlanId=" + actionvalue + "&catcode=" + idvalue + "&pmanno=" + price + "&product_code=" + crcatn,
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                
                                var retvalue = getReturnValue("error");
                                
                                if (retvalue != "")
                                {
                                   alert("Error " + retvalue);
                                }
                                else
                                {
                                    //what action did the server take
                                    var retvalue = getReturnValue("action");
                                    if (retvalue== "add")
                                    {
                                        showPic('_images/iplan-tick.gif',param7);    
                                    }
                                    else
                                    {
                                        showPic('_images/iplan.gif',param7);
                                    }
                                    
                                    doAjaxRequest("getIplanData", "", param8, "", "", "");
                                    
                                }
                                
                            },
                            error: function()
                            {    
                                alert("doIplanDelete error");
                            }
                        });
                        break;
                        
                    case "doIplanDelete":
                        
                        // doAjaxRequest('doIplanDelete', iPlanId, '', '', '', 'Cards.aspx');
                    
                        $.ajax
                        ({
                            type: "POST",
                            url: "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doIplanDelete&PlanId=" + ctrlID + "&page=" + crcatn,
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                
                                
                                var retvalue = getReturnValue("error");
                                
                                if (retvalue != "")
                                {
                                   alert("Error " + retvalue);
                                }
                                else
                                {
                                    var retvalue = getReturnValue("redirect");
                                    if (retvalue != "")
                                    {
                                        window.location = retvalue;
                                    }
                                    else
                                    {
                                        doAjaxRequest("getIplanData", "", crcatn, "", "", "");
                                    }
                                }
                                

                                
                            },
                            error: function()
                            {    
                                alert("doIplanDelete error");
                            }
                        });
                        break;
                        
                    case "doIplanCreate":
                        //Handles creating a new iplan
                        //doAjaxRequest('doIplanCreate', planType, planName, pocketCount, '', '');

                        $.ajax
                        ({
                            type: "POST",
                            url: "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=doIplanCreate&PlanType=" + ctrlID + "&PlanName=" + actionvalue + "&description=" + idvalue,
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                
                                var retvalue = getReturnValue("error");
                                if (retvalue != "")
                                {
                                    alert("Error " + retvalue);
                                }
                                else
                                {
                                    //clear down the inputs
                                    control('RadioButtonIplanCard').checked = true;
                                    control('TextBoxIplanName').value = "";
                                    control('TextIplanDescription').value = "";
                                
                                    doAjaxRequest("getIplanData", "", crcatn, "", "", "");
                                }
                            },
                            error: function()
                            {    
                                alert("doIplanCreate error");
                            }
                        });
                        break;
                        
                    case "getIplanData":
                        //Handles getting the innerHTML for the card order overview tree
                        $.ajax
                        ({
                            type: "POST",
                            url: "Pages/Ajax/ajaxrequest.aspx",
                            data: "sub=getIplanData&getActivationPanel=1&getEditPanel=1&getCurrentPanel=1&page=" + actionvalue,
                            success: function(msg)
                            {
                                //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                                retValues = msg;
                                                                
                                var retvalue = getReturnValue("activationpanel");
                                if (retvalue != "")
                                {
                                    control('divIPlanActivate').innerHTML = retvalue
                                }                                
                                
                                var retvalue = getReturnValue("currentpanel");
                                if (retvalue != "")
                                {
                                    control("divIplanCurrent").innerHTML = retvalue;
                                }
                            },
                            error: function()
                            {
                                alert("error");
                            }
                        });
                        break;
                                        
                    case "clearShoppingCart":
                         //debugger;
                         $.ajax
                         ({
                           type: "POST",
                           url: "Pages/Ajax/ajaxrequest.aspx",
                           data: "sub=clearShoppingCart"  + "&id=" + idvalue ,
                           success: function(msg)
                           {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues=msg;
                            //debugger;
                            //Get the return status
                            var retvalue=getReturnValue("value");
                            if (retvalue !="")
                            {
                                if (retvalue=='ok')
                                {
                                    //debugger;
                                    doAjaxRequest("getorderoverview","divOrderTreeView","","","","");
                                    alert("Your order has been reset");
                                }
                                else
                                {
                                    alert("A problem occured: " + retvalue );
                                }                                
                            }
                            //alert("Current Value:" + retvalue);

                           },
                           error: function()
                           {
                            alert("error");
                           }
                         });
                         break;                   
                    case "updatecartcontent":
                         //debugger;
                         //Parameters
                         //
                         // sub         - the subroutine to run eg 'updatecartcontent'
                         // ctrlid      - the id of the control that contains the current quantity (see also
                         // actionvalue - the action 'inc'(rement) 'dec'rement 'set'(sets the value - uses ctrlid to pick up the value and send as param &newval )
                         // idvalue     - the id of the product 
                         // price       - the price to use
                         // crcatn      - crcatn 
                         // param7      - optional - currently used in shopping cart, identifies the context of where we are calling from
                         //               origianlly UpdateCartContent was only called from cards.aspx and on return we would
                         //               call ajax getOrderOverview for the TAB, in the shopping basket screen we dont want to do this
                         //               additional call, and this parameter identifies the context of the caller. 
                         //               If param7 = 'shoppingcart_a' then on return from this call we use the 
                         //               param7 / param8 / param9 in the call to ajax doGetOrderTotalsOnly as 
                         // param8      - identifies the ID of the control to put the value in 
                         // param9      - identifies the value we are interested in ( totalpricegreen | totalpriceyellow )
                         // param10     - identifies the stockID ( for getCost)
                         // param11     - identifies priceID for getcost()
                         // param12     - true | false ( isPacks ) identifies if we are in pack or single mode (for getCost() )
                         //             - NOTE : potential problem here if in singles mode and have 10 in basket then
                         // param13     - card or gift c | g
                         
                         //debugger;
                         $.ajax
                         ({
                           type: "POST",
                           url: "Pages/Ajax/ajaxrequest.aspx",
                           data: "sub=updatecartcontent" + "&action=" + actionvalue + "&id=" + idvalue + "&price=" + price +  "&crcatn=" + crcatn + "&newvalue=" + (control(ctrlID).value) + "&stockid=" + param10 + "&priceid=" + param11 + "&isquantityinpacks=" + param12 + "&type=" + param13,
                           success: function(msg)
                           {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues=msg;
                            //debugger;
                            var retvalue=getReturnValue("value");
                            //debugger;
                            if (retvalue !="")
                            {
                                (control(ctrlID).value =retvalue)
                                //Show dialog to user if quantity >=10
                                //debugger;
                                //if param7 is set to a value we dont want an update (eg it doesnt make
                                //sense to update if we are in the checkout)
                                
                                if ( (param7 == undefined) || (param7 ==''))
                                 {
                                    doAjaxRequest("getorderoverview","divOrderTreeView","","","","");
                                    if(retvalue >=10)
                                    {
                                        alert("You can view your order by selecting the 'Order' TAB");
                                    }                                
                                 }
                                 
                                //because we use th cotnrol in the checkout we state this context in param7
                                //and also need to tell
                                if (param7 == 'shoppingcart_a')
                                {
                                    //In this scenario checkout screen will give us the cotnrol id to put total in the 
                                    //param8 - The id of the control to put the value in
                                    //param 9 - identifies the value we are interested in totalpricegreen | totalpriceyellow
                                    doAjaxRequest("doGetOrderTotalsOnly",param8,param7,param9,"","");
                                 }                                 
                                 
                            }
                            //alert("Current Value:" + retvalue);

                           },
                           error: function()
                           {
                            alert("error");
                           }
                         });
                         break;
                      
                    case "getorderoverview":
                         //Handles getting the innerHTML for the card order overview tree
                         //debugger;
                         $.ajax
                         ({
                           type: "POST",
                           url: "Pages/Ajax/ajaxrequest.aspx",
                           data: "sub=getorderoverview" + "&action=" + actionvalue + "&id=" + idvalue + "&price=" + price + "&crcatn=" + crcatn + "&newvalue=" + (control(ctrlID).value),
                           success: function(msg)
                           {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues=msg;
                            //debugger;
                            var retvalue=getReturnValue("orderoverviewtotals");
                            if (retvalue >="")
                            {
                                (control('divOrderOverview').innerHTML =retvalue)
                            }
                            //alert("Current Value:" + retvalue);
                            var retvalue=getReturnValue("orderoverviewtree");
                            if (retvalue >="")
                            {
                                (control(ctrlID).innerHTML =retvalue);
                            }
                           },
                           error: function()
                           {
                            alert("error");
                           }
                         });
                         break;
                    case "getWebPageTree":
                         //debugger;
                         $.ajax
                         ({
                           type: "POST",
                           url: "Pages/Ajax/ajaxrequest.aspx",
                           data: "sub=getWebPageTree&help=1&iblad=1&welcome=1&firstid=" + param7,
                           success: function(msg)
                           {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues=msg;
                            //debugger;
                            var retvalue=getReturnValue("help");
                            if (retvalue !="")
                            {
                                (control('divHelpParent').innerHTML =retvalue);
                            }
                            var retvalue=getReturnValue("iblad");
                            if (retvalue !="")
                            {
                                (control('diviBladParent').innerHTML =retvalue);
                            }
                            //alert("Current Value:" + retvalue);
                            var retvalue=getReturnValue("welcome");
                            if (retvalue !="")
                            {
                                (control('divWelcomeParent').innerHTML =retvalue);
                                //look for the first id of the welcome page
                                //debugger;
                                var firstid = getReturnValue("firstid");
                                if (firstid != "")
                                {
                                    if ( param7 != undefined)
                                    {
                                        if (param7 =="yes")
                                        {
                                            doAjaxRequest('getWebPage','','','' + firstid + '','','');
                                        }
                                    }
                                }
                                
                                
                            }
                           },
                           error: function()
                           {
                            alert("error");
                           }
                         });
                         break;
                    case "getWebPage":
                         //debugger;
                         $.ajax
                         ({
                           type: "POST",
                           url: "Pages/Ajax/ajaxrequest.aspx",
                           data: "sub=getWebPage&id=" + idvalue,
                           success: function(msg)
                           {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues=msg;
                            //debugger;
                            var retvalue=getReturnValue("value");
                            (control('divProductContainer').innerHTML = retvalue)

                            var retvalue=getReturnValue("tabtexttopleft");
                            (control('tabtopleft').innerHTML =retvalue)

                            var retvalue=getReturnValue("tabtexttopright");
                            (control('tabtopright').innerHTML =retvalue)
  
                            var retvalue=getReturnValue("tabtextlefttop");
                            (control('tablefttabtop').innerHTML =retvalue)
  
                            var retvalue=getReturnValue("tabtextleftbottom");
                            (control('tablefttabbottom').innerHTML =retvalue)
                           },
                           error: function()
                           {
                            alert("error");
                           }
                         });
                         break;     
                    case "getWebPageEx":
                         //debugger;
                         // PARAMETERS
                         // ctrlid - wp_id of the web page
                         // actionvalue -  site_id of the site (guid) optional will default to the 
                         //                dbengine.currentsite_id 
                         $.ajax
                         ({
                           type: "POST",
                           url: "Pages/Ajax/ajaxrequest.aspx",
                           data: "sub=getWebPageEx&id=" + ctrlID + "&siteid=" + actionvalue,
                           success: function(msg)
                           {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues=msg;
                            //debugger;
                            var retvalue=getReturnValue("value");
                            (control('divProductContainer').innerHTML = retvalue)

                            var retvalue=getReturnValue("tabtexttopleft");
                            (control('tabtopleft').innerHTML =retvalue)

                            var retvalue=getReturnValue("tabtexttopright");
                            (control('tabtopright').innerHTML =retvalue)
  
                            var retvalue=getReturnValue("tabtextlefttop");
                            (control('tablefttabtop').innerHTML =retvalue)
  
                            var retvalue=getReturnValue("tabtextleftbottom");
                            (control('tablefttabbottom').innerHTML =retvalue)
                           },
                           error: function()
                           {
                            alert("error");
                           }
                         });
                         break;                                               
                 }

            }
            
            //Custom hyperlink for phase 2 -
            //links to web_page record to web_page with id (int) of pageid
            function linktopage(pageid,siteid)
            {
                //Siteid is optional - currentsiteid is used at server end if not sent
                //If no site id
                if (siteid == undefined)
                {
                    siteid='undefined';
                }  
                              
                 $.ajax
                 ({
                   type: "POST",
                   url: "Pages/Ajax/ajaxrequest.aspx",
                   data: "sub=getWebPageEx&id=" + pageid + "&siteid=" + siteid,
                   success: function(msg)
                   {
                    //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                    retValues=msg;
                    //debugger;
                    var retvalue=getReturnValue("value");
                    (control('divProductContainer').innerHTML = retvalue)

                    var retvalue=getReturnValue("tabtexttopleft");
                    (control('tabtopleft').innerHTML =retvalue)

                    var retvalue=getReturnValue("tabtexttopright");
                    (control('tabtopright').innerHTML =retvalue)

                    var retvalue=getReturnValue("tabtextlefttop");
                    (control('tablefttabtop').innerHTML =retvalue)

                    var retvalue=getReturnValue("tabtextleftbottom");
                    (control('tablefttabbottom').innerHTML =retvalue)
                   },
                   error: function()
                   {
                    alert("error");
                   }
                 });            
            }

            
            //Custom hyperlink for phase 2 -
            //links to iplan  
            function linktoiplan(setplanid,targetpage)
            {
                //Siteid is optional - currentsiteid is used at server end if not sent
                //If no site id
//                if (siteid == undefined)
//                {
//                    siteid='undefined';
//                }  
                              
                    $.ajax
                    ({
                        type: "POST",
                        url: "Pages/Ajax/ajaxrequest.aspx",
                        data: "sub=doSetIplanEx&PlanId=" + setplanid + "&mode=browse",
                        success: function(msg)
                        {
                            //We will get back a | separated list of variables "vara:1|varb:2|varc:3"
                            retValues = msg;
                            //debugger;
                            
                            var retvalue = getReturnValue("error");
                            if (retvalue != "")
                            {
                               alert("Error " + retvalue);
                            }
                            else
                            {
                                //CureentIplan has been set on the server
                                //now if the url parameter is set, then navigate to it
                                if ( targetpage != undefined )
                                {
                                    if (targetpage != "")
                                    {
                                        window.location =targetpage;    
                                    }
                                }
                                
                            }
                            
                        },
                        error: function()
                        {    
                            alert("doIplanDelete error");
                        }
                    });         
            }
                        
            //Ajax return values are stored in the global string params .
            //In the format 
            //  "variablenameA:1|variablenameB:2|variablenameC:3"
            //The above is treated as a variable name/value pair.
            //This function below returns the value of the named variable (param) from this string
            function getReturnValue(param)
            {
                var str = retValues.split("|");
                var n = 0;
                var idx = 0;
                var ret = "";
                while (n < str.length)
                {
                    var values = str[n].split("~");
                    var i = 0;
                    if (values[0] == param)
                    {
                        if (values.length >0)
                            {
                                ret = values[1];
                                n = str.length;
                            }
                    }
                    n += 1;
                }
                return ret;                
            }
            
            //The Javascript function doAjaxRequest() (above) takes 1 parameter
            //params - this contains multiple parameters in the format
            //  "varA~1|varB~2|varC~3"
            //The above is treated as a variable name/value pair.
            //This function adds a new parameter to the list.
            //This function is inteneded for use by the caller of the doAjaxRequest (clientside)
            //However if the code is being constructed server side then
            //there is a server side function
            function addParam(strParamName,strParamValue)
            {
            
                if (params.length >0)
                {
                    params += "|"; 
                }
                params += strParamName + "~" + strParamValue
                           
            }            
            
            function control(id)
            {
                var obj = document.getElementById(id);
                if (!obj )
                {
                    return undefined;
                } 
                else 
                {
                    return obj;
                }
            }
            
                        
function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();






function sendAjaxRequest(action) 
{
    http.open('POST', action, true);
    http.onreadystatechange = handleResponse;
    http.send(null);
}


function handleResponse()
{
    //debugger;
    var divMsg = document.getElementById('divMsgs');
    
    if ((http.readyState == 4)||(http.readyState == 'complete'))
    {
        var response = http.responseText;
        if (response.indexOf('</result>') != -1)
        {
           divMsg.innerHTML = response.substring(response.indexOf('<result>') + 8, response.indexOf('</result>')) + '<br /><br />';
        }
    }
}



function deleteIplan(iPlanId, iPlanName, page)
{    
    if (confirm('Delete ' + iPlanName + ' iPlan?'))
    {
        doAjaxRequest('doIplanDelete', iPlanId, '', '', '', page);
    }        
}


function showPic(imageSrc1,idx)
{
    var divImage = document.getElementById('productimage' + idx );
    divImage.src = imageSrc1;
}



///////////////////////////////////////////////////////////////////////////////////


//Account lookup suppoirt js functions
// # for id . for classname (JQUERY SELECTOR NOTE)

///////////////////////////////////////////////////////////////////////////////////

//CANCEL DIALOG
function CloseModalDialog(strElementClassName)
{ 
    //debugger;
    //Close the Dialog
    $(strElementClassName).css("display","none") ;
} 


// OK BUTTON SEARCH (account search dialog)
function btnSearchOk_Click(a,ctrlresultvalue,ctrlresultid)
{
    //debugger;
    doAjaxRequest("doGetClubList", $("#txtSearchAccountKeyword").val(),$("#txtSearchPostcode").val(),ctrlresultvalue, ctrlresultid,768,500);
}

// OK BUTTON SEARCH (contact search dialog)
function btnContactSearchOk_Click(a,ctrlresultvalue,ctrlresultid,optionalaccountid)
{
    //debugger;
    doAjaxRequest("doGetContactList", $("#txtSearchKeyword").val(),$("#txtSearchName").val(),ctrlresultvalue, ctrlresultid,optionalaccountid,640,500);
}

// OK BUTTON SEARCH (user list search dialog)
function btnUserSearchOk_Click(a,ctrlresultvalue,ctrlresultid,optionalaccountid)
{
    //debugger;
    doAjaxRequest("doGetUserList", $("#txtSearchFirstname").val(),$("#txtSearchLastname").val(),ctrlresultvalue, ctrlresultid,320,500);
}

function PickListItemSelected(id,value,ctrlresultvalue,ctrlresultid)
{
    //debugger;

    //$(".txtAccount").val(value) ;
    $("." + ctrlresultvalue).val(value) ;
    //alert($(".txtAccount").val());


    //$(".hidAccountID").val(id) ;
    $("." + ctrlresultid).val(id) ;
    //alert($(".hidAccountID").val());
    
    //Close the dialog
    CloseModalDialog(".divModalDialog") ;
}

