﻿function ShowCatalogue(ComponentID, FormName, Goal)
{
    //alert (ComponentID);
   //var ObjHotelType = document.getElementById("selHotelType");
   //var ClassID = ObjHotelType.options[ObjHotelType.selectedIndex].value;
    if (Goal) {
        document.getElementById("txtGoal").value = Goal;
    } 
    else {
        document.getElementById("txtGoal").value = "";
    }
    
    var data = extractFormData(FormName);

    getAction("Load Catalogue",ComponentID,data);
}

function gotonextpage (curpage, totalpages, goal, ComponentID){
    if (curpage < totalpages){
        curpage = curpage+1;
        
        //alert(curpage);
        document.getElementById("currentPage").value = curpage;
        var data = extractFormData("frmProds");
        getAction("Load Catalogue",ComponentID,data);
    }
}

function gotoprevpage (curpage, totalpages, goal, ComponentID){
    if (curpage <= totalpages && curpage > 1){
        curpage = curpage-1;
        
        //alert(curpage);
        document.getElementById("currentPage").value = curpage;
        var data = extractFormData("frmProds");
        getAction("Load Catalogue",ComponentID,data);
    }
}

function ChangePageSize (pagesize, goal, ComponentID){

    //alert(pagesize);
    document.getElementById("selPagesize").value = pagesize;
    document.getElementById("currentPage").value = 1;
    var data = extractFormData("frmProds");
    //alert (data);
    getAction("Load Catalogue",ComponentID,data);
}

function GotoCheckout(ComponentID, FormName, userloggedin){

    try {
    
    if (userloggedin){
        var data = extractFormData(FormName);        
        getAction("Go to Checkout",ComponentID,data);
    }
    else{
    
        alert ('Please register or log in before you complete your order');

        $("fieldset#signin_menu").toggle();
		$(".signin").toggleClass("menu-open");
		window.scrollTo(0,0);
		document.getElementById("gotoafterwards").value = "gotocheckout";
		document.getElementById("txtUsername").focus();

    }
        //$.modaldialog.prompt();
    
    /*
        if (userloggedin == 1){
            //alert (ComponentID);
            var data = extractFormData(FormName);
            getAction("Go to Checkout",ComponentID,data);
        }
        else{
                alert ('Please register or log in before proceedng to the checkout section');
          
                $("fieldset#signin_menu").toggle();
				$(".signin").toggleClass("menu-open");
				window.scrollTo(0,0);
				document.getElementById("txtUsername").focus();
            //var data = "action=backtocart";
            //getAction("Register",ComponentID,data);
        }*/
    }
    catch (ex){
        alert("Error occured:\n" + ex.message);
    }
}

function UpdatePrice(counter, measurementid, forcatalogue, deliveryleadtime)
{    
    //alert (getElementText(document.getElementById("txtPrice")));
    var stockid = document.getElementById("txtStockID" + counter).value;
    var pricecurrent = document.getElementById("txtItemPriceDisplay" + counter);
    var priceold = document.getElementById("txtOldPrice" + counter);
    var flavour = document.getElementById("selFlavour" + counter).value;
    var oldmeasurement = document.getElementById("txtPrevMeasurementid" + counter).value;
    
    //alert(getElementText(field));
    //setElementText(document.getElementById("txtPrice"), "change");

   var AfterPriceCheck = function(responseText,loadIn,postLoad,extendedData)
   {
        var result = responseText;
        //alert (result);
        if (result.substr(0,5).toLowerCase() == "error")
        {
            document.getElementById("selMeasurement"+counter).value = oldmeasurement;
            //alert(result);
            return;
        }
        else
            {
                var arr = result.split('^^');
                //alert (arr[0])
                //alert (pricecurrent + "              " + arr[0])
                if (forcatalogue == "true"){
                    setElementText(pricecurrent, arr[0]);
                }
                else{
                    setElementText(pricecurrent, arr[0] + " each");
                }
                //alert (pricecurrent.value)
                //alert ("after set1 and before set2")
                if (forcatalogue == "true"){
                    setElementText(priceold, ("Normal Price: " + arr[1]));
                }

                //alert ("set measurement : " + measurementid);
                document.getElementById("txtPrevMeasurementid" + counter).value = measurementid;

                if(forcatalogue == "false"){
                    var flavourmeasure = "";
                    if(arr[5] == 'N/A'){
                        arr[5] = '';
                    }
                    if(arr[6] == 'N/A'){
                        arr[6] = '';
                    }
                    flavourmeasure = arr[6] + " " + arr[5];
                    setElementText(document.getElementById("txtMeasureFlavour"+counter), flavourmeasure);
                    document.getElementById("txtItemPriceVal"+counter).value = arr[7];
                }
                
                //alert ("after set2")
                
                //alert(document.getElementById("selFlavour" + stockid).innerHTML);
                //alert("Arr[2] : " + arr[2]);
                //alert("second split");
                var selection = arr[4].split('<>');
                //alert (selection);
                // 1|choc-mint_^1<>2|chocolate_^0
                //alert("selection : " + selection[0]);
                
                //document.getElementById("selFlavour" + stockid).innerHTML = arr[2];
                /*
                var newdiv = document.createElement("select");
                newdiv.innerHTML = arr[2];
                var container = document.getElementById("selFlavour" + stockid);
                container.appendChild(newdiv);
                */
                
                var selectbox = document.getElementById("selFlavour" + counter);
                //alert (selectbox.id);
                //return;
                selectbox.options.length = 0;
                //alert("cleared");
                //return;
                //var selval = selection.split('|');


                // Update the flavour
                for(var i=0; i < (selection.length-1); i++) {
		           
		            //alert("third split");
                    var newarr = selection[i].split('|');
                    // [0] = 1(ID) 
                    // [1] = 1/0(QTY Avail) 
                    // [2] = chocolate_^1
                    //alert("forth split");
                    var selectsplit = newarr[1].split('_^');
                    // [0] = chocolate ; [1] = 1

                    var value = newarr[0];
                    var text = selectsplit[0];
                    
// defaultSelected
//                    var oOption = document.createElement("OPTION");
//                    oOption.text = text;
//                    oOption.value = value;
                    if (selectsplit){
                        if(selectsplit[1] == 1){
//                            oOption.defaultSelected = true;
                            //alert("selected")
                            selectbox.options[i]=new Option(text, value, true, true);
                            document.getElementById("txtPrevFlavourID" + counter).value = value; // set the flavour
                            
                            if (arr[3] == 0){
                                // no stock available
                                //alert("voor update")
                                setElementText(document.getElementById("txtStockAvail"+counter), ("* " + arr[2]))
                                document.getElementById("txtStockAvail"+counter).className = "content font_size_8pt font_color_rage_red vertical_align_top";
                            }
                            else {
                                //alert ('stock available')
                                // stock available
                                //document.getElementById("txtStockAvail" + counter).value = '* Stock available';
                                setElementText(document.getElementById("txtStockAvail"+counter), '* Stock available')
                                document.getElementById("txtStockAvail"+counter).className = "content font_size_8pt font_color_grey_1 vertical_align_top";
                            }
                        }
                        else{
                            selectbox.options[i]=new Option(text, value, false, false);
                        }
                    }
//                    selectbox.options[i] = new Option(text,value);
//                    selectbox.options[i].defaultSelected = true;
                    
                    //alert ("done")
		        }
		        
		        if (forcatalogue == 'false'){
		            UpdateCartTotal(forcatalogue);  
		        }
		        
                $('#imgCartAddGeneral').fadeIn(1000, function() {
                // Animation complete
                    });
                $('#imgCartAddGeneral').fadeOut(1000, function() {
                    // Animation complete
                });
            
		        ///return;
             }
    }

    if (forcatalogue == 1){
        forcatalogue = 'true';
    }
    else{
        forcatalogue = 'false';
    }
    var data = "action=updateprice&StockID=" + stockid + "&MeasurementID=" + measurementid + "&FlavourID=" + flavour + "&PrevMeasurementID=" + oldmeasurement + "&ForCatalogue=" + forcatalogue;
    var page = "/modules/Catalogue/CatalogueModule.aspx";
    //alert(data);

    ajaxRequest(page,data,null,null,AfterPriceCheck,null,null);

}


function UpdateAvailability(counter, flavourid, delleadtime, forcatalogue)
{
    //alert (getElementText(document.getElementById("txtPrice")));
    var stockid = document.getElementById("txtStockID" + counter).value
    var measurementid = document.getElementById("selMeasurement" + counter).value;
    var availableelement = document.getElementById("txtStockAvail" + counter);
    //var prevmeasurementid = document.getElementById("txtPrevMeasurementid" + counter).value;
    var prevflavourid = document.getElementById("txtPrevFlavourID" + counter).value;
    
/*    if(forcatalogue == "false"){
        alert ("hier")
        var measureflavour = document.getElementById("txtMeasureFlavour" + counter);
    }*/
    // debug
    var tdQty = document.getElementById("tdQty" + counter);
    // debug
    
    //alert(getElementText(field));
   //setElementText(document.getElementById("txtPrice"), "change");
   
   var AfterAvailCheck = function(responseText,loadIn,postLoad,extendedData)
   {
        var result = responseText;
        //alert (result)
        if (result.substr(0,5).toLowerCase() == "error")
        {
            alert('Response Error : ' + result);
            return;
        }
        else
        {
            var arr = result.split('^^');
            //arr[0,1,2] QTY, FLAVOUR, MEASUREMENT

            if(forcatalogue == "false"){
                var flavourmeasure = "";
                if(arr[1] == 'N/A'){
                    arr[1] = '';
                }
                if(arr[2] == 'N/A'){
                    arr[2] = '';
                }
                flavourmeasure = arr[2] + " " + arr[1];
                setElementText(document.getElementById("txtMeasureFlavour"+counter), flavourmeasure);
            }
            
            if (parseInt(arr[0]) > 0)
            {
                setElementText(availableelement, "* Stock available");
                
                /*if(forcatalogue == "true"){
                    setElementText(tdQty, "Qty["+arr[0]+"]");
                }*/

                availableelement.className = "content font_size_8pt font_color_grey_1 vertical_align_top";
            }
            else
            {   
                setElementText(availableelement, "* " + delleadtime);
                /*if(forcatalogue == "true"){
                    setElementText(tdQty, "Qty[0]");
                }*/
                availableelement.className = "content font_size_8pt font_color_rage_red vertical_align_top";
            }

            document.getElementById("txtPrevFlavourID" + counter).value = flavourid;

            $('#imgCartAddGeneral').fadeIn(1000);
            $('#imgCartAddGeneral').fadeOut(1000);
        }
    }

    if(forcatalogue==0)
    {
        forcatalogue = 'false'
    }else{
        forcatalogue = 'true'
    }
    var data = "action=updateavailability&StockID=" + stockid + "&FlavourID=" + flavourid + "&MeasurementID=" + measurementid + "&PrevFlavourID=" + prevflavourid + "&ForCatalogue=" + forcatalogue;  
    var page = "/modules/Catalogue/CatalogueModule.aspx"
    //alert(data);
    ajaxRequest(page,data,null,null,AfterAvailCheck,null,null);
}

function UpdateCartTotal(forcatalogue)
{
    
    var itemprice;
    var itemqty;
    var i;
    var subtotal = 0.00;
    
    if (forcatalogue == "true"){
        var itemcount = document.getElementById("fieldnumbermini").value;
    }
    else{
        var itemcount = document.getElementById("fieldnumber").value;
    }
    //alert (itemcount);
    if (itemcount > 0) {
        for (i=1; i<=itemcount; i++)
        {
            //alert ("voor");
            if (document.getElementById("txtItemPriceVal"+i) != null)
            {
                //alert (i);
                itemprice = document.getElementById("txtItemPriceVal"+i).value;
                itemqty = document.getElementById("txtQty"+i).value;
                
                //alert ("Price : " + itemprice + "\nqty : " + itemqty);
                if (isNaN(itemprice)){
                    alert("There has been a problem calculating your total. Please refresh your cart and try again");
                    return false;
                }
                if (isNaN(itemqty)){
                    itemqty = 1;
                    document.getElementById("txtQty"+i).value = 1;
                }
                
                subtotal = subtotal + (itemprice * itemqty);
                document.getElementById("txtSubTotal"+i).value = (itemprice * itemqty);
                setElementText(document.getElementById("txtItemLineTotal"+i), currencyFormatter((itemprice * itemqty), false, true));
                //alert (subtotal);
            }
        }
        //alert (subtotal);
        document.getElementById("txtSTDisplayValue").value = subtotal;
        setElementText(document.getElementById("txtSTDisplayText"), currencyFormatter(subtotal, true, false));
        setElementText(document.getElementById("txtSTDisplayValue"), subtotal);
        
        
        
    }
}



// Add item to cart
function Cat_AddItemToCart(counter, showminicart, forcatalogue)
{
    //alert (getElementText(document.getElementById("txtPrice")));
    var stockid = document.getElementById("txtStockID" + counter).value;
    var flavour = document.getElementById("selFlavour" + counter).value;
    var measurement = document.getElementById("selMeasurement" + counter).value;
    var qty = document.getElementById("txtQty" + counter).value;
    var imgCartAdd = "imgCartAdd" + counter;

    var prevmeasurementid = document.getElementById("txtPrevMeasurementid" + counter).value;
    var prevflavourid = document.getElementById("txtPrevFlavourID" + counter).value;

	if (isNaN(qty) || qty == '' || qty == 0)
	{
		document.getElementById("txtQty" + counter).value = "";
		return;
	}
	
    //alert(stockid + "\n" + flavour + "\n" + measurement + "\n" + qty);
    //return;
    //setElementText(document.getElementById("txtPrice"), "change");

    var loadRightContent = function(responseText,loadIn,postLoad,extendedData)
    {
        var result = responseText;

        if (result.substr(0,5).toLowerCase() == "error"){
                alert('Response Error : ' + result);
                return;
        }
        else{
                document.getElementById("rightcontent").innerHTML = result;
            }
    }

    var AfterItemAdd = function(responseText,loadIn,postLoad,extendedData)
    {
        var result = responseText;
//alert (result);
        if (result.substr(0,5).toLowerCase() == "error")
            {
                alert(result);
                return;
            }
        else
            {
                if (!showminicart){
                    UpdateCartTotal();
                }
                if (document.getElementById(imgCartAdd))
                {
                    $('#'+imgCartAdd).fadeIn(1000, function() {
                        // Animation complete
                    });
                    
                    $('#'+imgCartAdd).fadeOut(1000, function() {
                        // Animation complete
                    });
                } 
                else
                {
                    if (document.getElementById("imgCartAddGeneral")){
                    
                        $('#imgCartAddGeneral').fadeIn(1000, function() {
                            // Animation complete
                        });
                        
                        $('#imgCartAddGeneral').fadeOut(1000, function() {
                            // Animation complete
                        });
                    }
                }

                if (showminicart){
                    var data = "";
                    var page = "/content/rightcontent.aspx"
                    ajaxRequest(page,data,null,null,loadRightContent,null,null);
                }
             }
    }

    var data = "action=addtocart&StockID=" + stockid + "&MeasurementID=" + measurement + "&FlavourID=" + flavour + "&QTY=" + qty + "&PrevMeasurementID=" + prevmeasurementid + "&PrevFlavourID=" + prevflavourid + "&ForCatalogue=" + forcatalogue;
    var page = "/modules/Catalogue/CatalogueModule.aspx"
    //alert (data);
    ajaxRequest(page,data,null,null,AfterItemAdd,null,null);
}

// Removes the entry from a table
function removeFromCart(varRownumber, miniCart, stockid, flavourid, measurementid)
{
    var AfterItemRemove = function(responseText,loadIn,postLoad,extendedData)
    {
        var result = responseText;

        if (result.substr(0,5).toLowerCase() == "error"){
            alert('Response Error : ' + result);
            return;
        }
        else{

            var deleteRecord = varRownumber.substr(12, varRownumber.length - 12);
            
            var linetotal = document.getElementById("txtSubTotal"+deleteRecord).value; // line being deleted
            if (document.getElementById("txtSTDisplayValue")){
                var subtotal = document.getElementById("txtSTDisplayValue").value; // cart sub-total
            }
            
            var theRow = "minicartrow"+deleteRecord;
            var theSpacer = "minicartrowspacer"+deleteRecord;
            var theLineTotal = "minicartrowtotal"+deleteRecord;
            
            if (miniCart){
                var row1LTotal = document.getElementById(theLineTotal);
                row1LTotal.parentNode.removeChild(row1LTotal); // FireFox fix
                var row1 = document.getElementById(theRow);
                row1.parentNode.removeChild(row1); // FireFox fix
                var row1Spacer = document.getElementById(theSpacer);
                row1Spacer.parentNode.removeChild(row1Spacer); // FireFox fix
                
                document.getElementById('fieldnumberminiCur').value -= 1; // The next display number
                //alert(document.getElementById('fieldnumbermini').value);
            }
            else {
                var d1=document.getElementById("minicart");                
                var d2=document.getElementById(theRow);
                var d3=document.getElementById(theSpacer);
                d1.removeChild(d2);
                d1.removeChild(d3);
                
                document.getElementById('fieldnumberCur').value -= 1; // The next display number
                //alert(document.getElementById('fieldnumber').value);
            }

            //alert ("before : " + subtotal);
            subtotal = (subtotal - linetotal);
            //alert ("After : " + subtotal);
            
            if (document.getElementById("txtSTDisplayValue")){
                document.getElementById("txtSTDisplayValue").value = subtotal;
            }
            if (document.getElementById("txtSTDisplayText")){
                setElementText(document.getElementById("txtSTDisplayText"), currencyFormatter(subtotal,"",false));
            }

            var fieldcount;
            
            if (miniCart){
                fieldcount = document.getElementById('fieldnumberminiCur').value;
            }
            else{
                fieldcount = document.getElementById('fieldnumberCur').value;
            }
            if (fieldcount == 0){
                
                var thetable = document.getElementById("minicart");
                if (miniCart){
                    
                    while(thetable.hasChildNodes())
                    {
                        thetable.removeChild(thetable.firstChild);
                    }
                    
                    var tr = document.createElement("tr");
                    var td = document.createElement("td");
                    var img = document.createElement("img");

                    img.setAttribute("src", "/images/your_cart_is_empty.gif");
                    //alert ("add image");
                    td.appendChild(img);
                    tr.appendChild(td);
                    thetable.appendChild(tr);
                }
                else{
                
                    var totaldisplay = document.getElementById("totaldisplay");
                    var theform = document.getElementById("frmCartview");
                    var noitemsdiv = document.getElementById("noitems");
                    
                    while(thetable.hasChildNodes())
                    {
                        thetable.removeChild(thetable.firstChild);
                    }

                    totaldisplay.parentNode.removeChild(totaldisplay); // FireFox fix

                    var spacerdiv1 = document.createElement("div");
                    var img2 = document.createElement("img");

                    img2.setAttribute("src", "/images/specials_spacer_6.gif");
                    spacerdiv1.appendChild(img2);
                    thetable.appendChild(spacerdiv1);

                    var cartimagedivspacer = document.createElement("div");
                    var cartimagediv = document.createElement("div");
                    var cartimage = document.createElement("img");
                    
                    cartimagedivspacer.setAttribute("height", "60");
                    
                    //cartimagedivspacer.setAttribute("height","20px");
                    
                    cartimage.setAttribute("src", "/images/your_cart_is_empty.gif");
                    cartimagediv.appendChild(cartimage);
                    noitemsdiv.appendChild(cartimagedivspacer);
                    noitemsdiv.appendChild(cartimagediv);
                }
            }
        }
    }

    var data = "action=removefromcart&StockID=" + stockid + "&MeasurementID=" + measurementid + "&FlavourID=" + flavourid;
    //alert (data)
    var page = "/modules/Catalogue/CatalogueModule.aspx"
    //alert(data); alert(page);
    ajaxRequest(page,data,null,null,AfterItemRemove,null,null);
}

/*
function InitFaceBox(){
	$('a[rel*=facebox]').facebox();
}
*/

//////////////////////////////////////////////////////////////////////////////////////////////
//TAB SCROLLING FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////
    var menuheight = 500;
    var menuspeed = 5;
    var actualwidth = -1;
    var actualwidthsub = -1;
    var lefttime = null,righttime = null;
    var downtime = null, uptime = null;

    function moveleft(divisionToMove,widthToUse){
    
    //alert (widthToUse);
        var tabs = document.getElementById(divisionToMove);
        try {
            var left = tabs.style.left;
            //alert("Left : " + left +"\nIntLeft :" + parseInt(left) + "\nDifference : " + (menuwidth - actualwidth) + "\nActual Width : " + actualwidth + "\nMenu Width : " + menuwidth);
           //alert("Left : " + left +"\nmenuwiyydth :" + parseInt(menuwidth) + "\nwidthToUse : " + widthToUse);
            
           // return;
            if (parseInt(left) > (menuwidth - widthToUse)){
                //alert("before : " + tabs.style.left)
	            tabs.style.left = parseInt(left) - menuspeed + "px";
	            //alert("after : " + tabs.style.left)
            }
            lefttime = window.setTimeout("moveleft('" + divisionToMove + "'," + widthToUse + ")",100);
        }
        catch (ex){
            alert("Error scrolling left :\n" + ex.message);
        }
        finally {
            left = null;
        }
        tabs = null;
    }
    
    function moveright(divisionToMove){
        var tabs = document.getElementById(divisionToMove);
        try {
            var left = tabs.style.left;
            alert("Left : " + left +"\nIntLeft :" + parseInt(left));
            //return;
            
            
            if (parseInt(left) != ((menuwidth - widthToUse))){
            //alert ((-1*parseInt(top)))
	             tabs.style.left = (parseInt(left) - menuspeed) + "px";
            }
            else{
                //alert('equal');
                //return;
            }
            
            
            
            /*if (parseInt(left) < 0){
	            tabs.style.left = (parseInt(left) + menuspeed) + "px";
            }
            righttime = window.setTimeout("moveright('" + divisionToMove + "')",100);*/
        }
        catch (ex){
            alert("Error scrolling right :\n" + ex.message);
        }
        finally {
            left = null;
        }
        tabs = null;
    }

    function stopleft(){
        try {
            clearTimeout(lefttime);
        }
        catch (ex){
            alert("Error clearing left timeout:\n" + ex.message);
        }
    }

    function stopright(){
        try {
            clearTimeout(righttime);
        }
        catch (ex){
            alert("Error clearing right timeout:\n" + ex.message);
        }
    }
    
    function movedown(divisionToMove,widthToUse){
    
        var tabs = document.getElementById(divisionToMove);
        //alert(tabs.offsetHeight);

        try {
            var top = tabs.style.top;
            //alert("Left : " + left +"\nIntLeft :" + parseInt(left) + "\nDifference : " + (menuwidth - actualwidth) + "\nActual Width : " + actualwidth + "\nMenu Width : " + menuwidth);
           //alert("Top : " + top +"\nmenuwidth :" + parseInt(menuwidth) + "\nwidthToUse : " + widthToUse);
            
           // return;
            //if (parseInt(top) > (menuwidth - widthToUse)){
            if (parseInt(top) != ((menuheight - widthToUse))){
            //alert ((-1*parseInt(top)))
	            tabs.style.top = (parseInt(top) - menuspeed) + "px";
            }
            else{
                //alert('equal');
                //return;
            }
            //document.getElementById("offset").innerHTML = tabs.style.top;
            
            downtime = window.setTimeout("movedown('" + divisionToMove + "'," + widthToUse + ")",20);
        }
        catch (ex){
            alert("Error scrolling down :\n" + ex.message);
        }
        finally {
            top = null;
        }
        tabs = null;
    }
            
   function moveup(divisionToMove){
        var tabs = document.getElementById(divisionToMove);
        try {
            var top = tabs.style.top;
            //alert("Left : " + left +"\nIntLeft :" + parseInt(left));
            //return;
            if (parseInt(top) < 0){
	            tabs.style.top = (parseInt(top) + menuspeed) + "px";
            }
            //document.getElementById("offset").innerHTML = tabs.style.top;
            uptime = window.setTimeout("moveup('" + divisionToMove + "')",20);
        }
        catch (ex){
            alert("Error scrolling up :\n" + ex.message);
        }
        finally {
            top = null;
        }
        tabs = null;
    }         
            
    function stopup(){
        try {
            clearTimeout(uptime);
        }
        catch (ex){
            alert("Error clearing left timeout:\n" + ex.message);
        }
    }
    
    function stopdown(){
        try {
            clearTimeout(downtime);
        }
        catch (ex){
            alert("Error clearing left timeout:\n" + ex.message);
        }
    }

function cart_SaveLoad(userloggedin, ComponentID, saveload, event){
    try {
        if (userloggedin == 1){
            if (saveload == 'save'){
                /*
                var cartname = prompt("Please give your cart a name");
 
                var AfterSave = function(responseText,loadIn,postLoad,extendedData)
                {
                    var result = responseText;
                    if (result.substr(0,5).toLowerCase() == "error")
                    {
                            var arr = result.split("|");
                            alert(arr[1]);
                            return;
                    }
                    else if (result.substr(0,7).toLowerCase() == "overide")
                    {
                        var arr = result.split("|");
                        if (confirm(arr[1])){
                        
                            var data = "action=savecart&cartname=" + encodeURI(cartname)+ "&overide=true"; 
                            var page = "/modules/Catalogue/CatalogueModule.aspx"

                            ajaxRequest(page,data,null,null,AfterSave,null,null);
                        }
                    }
                    else{
                        alert (result);
                    }
                }

                if (cartname != null && cartname != "") {
                    var data = "action=savecart&cartname=" + encodeURI(cartname) + "&overide=false"; 
                    var page = "/modules/Catalogue/CatalogueModule.aspx"
                    ajaxRequest(page,data,null,null,AfterSave,null,null);
                }*/
                
                $('#dialog_cartsave').jqmShow(); return;
                
            }
            else{
                //alert("Please note that by loading a previous cart, your current items will be replaced")
                //alert ('lightbox display with different carts');
            }
        }
        else{
            if (saveload == 'save'){
                alert ("Please register or log in to save your cart");
            }
            else{
                alert ('Please register or log in to load a previously saved cart');
            }
            
                $("fieldset#signin_menu").toggle();
				$(".signin").toggleClass("menu-open");
				window.scrollTo(0,0);
				document.getElementById("txtUsername").focus();
            //var data = "action=backtocart";
            //getAction("Register",ComponentID,data);
        }
    }
    catch (ex){
        alert("Error occured:\n" + ex.message);
    }
}    

function cart_Clear(ComponentID, Override){
    try {
        var AfterClear = function(responseText,loadIn,postLoad,extendedData)
        {
            var result = responseText;
            //alert (result);
            if (result.substr(0,5).toLowerCase() == "error"){
                alert('Response Error : ' + result);
                return;
            }
            else{
                getAction("Load Catalogue",ComponentID,data);
            }
        }

    var data = "action=clearcart";
    var page = "/modules/Catalogue/CatalogueModule.aspx"

    var answer = "";

    if (Override){
        answer = true;
    }
    else{
        answer = confirm ("You are about to empty your cart. Click OK to continue");
    }
    
    if (answer)
        ajaxRequest(page,data,null,null,AfterClear,null,null);
}
    catch (ex){
        alert("Error occured:\n" + ex.message);
    }
}  

function cart_Load(cartid, ComponentID){

    var AfterLoad = function(responseText,loadIn,postLoad,extendedData)
    {
        var result = responseText;
        
        if (result.substr(0,5).toLowerCase() == "error"){
            alert('Response Error : ' + result);
            return;
        }
        else{
            //document.getElementById("loadbasketsclose").click();
            //$('#mask').fadeOut(1000);
		    //$('.window').fadeOut(1000);
		    window.setTimeout("getAction('View Cart',"+ComponentID+",{})",300);
		    $.fn.colorbox.close();
            //$.fn.colorbox.close();
            //alert (result);
        }
    }

    var data = "action=loadcart&cartstorageid="+ cartid;
    var page = "/modules/Catalogue/CatalogueModule.aspx"

    ajaxRequest(page,data,null,null,AfterLoad,null,null);
}

function cart_Save(userid){

    var CartName = document.getElementById("txtCartName").value;
    
    var AfterSave = function(responseText,loadIn,postLoad,extendedData)
    {
        var result = responseText;
        if (result.substr(0,5).toLowerCase() == "error")
        {
            var arr = result.split("|");
            alert(arr[1]);
            return;
        }
        else if (result.substr(0,7).toLowerCase() == "overide")
        {
            var arr = result.split("|");
            if (confirm(arr[1])){
            
                var data = "action=savecart&cartname=" + encodeURI(CartName)+ "&overide=true"; 
                var page = "/modules/Catalogue/CatalogueModule.aspx"

                ajaxRequest(page,data,null,null,AfterSave,null,null);
            }
        }
        else{
            alert (result);
            $('#dialog_cartsave').jqmHide();
        }
    }

    if (CartName != null && CartName != "") {
        var data = "action=savecart&cartname=" + encodeURI(CartName) + "&overide=false"; 
        var page = "/modules/Catalogue/CatalogueModule.aspx"
        ajaxRequest(page,data,null,null,AfterSave,null,null);
    }
    else{
        alert("Please enter a name for your cart");
        return;
    }
}

function PlaceOrderCheck(ComponentID, formname, usertype)
{

    var DestinationUser = $.trim(document.getElementById("txtDestinationUser").value);
    if(DestinationUser==""){alert("Please enter a valid name who this order should be addressed to"); window.scrollTo(0,(findPosY(document.getElementById("txtDestinationUser")) / 2)); return;}
    
    var DestinationTelNo = $.trim(document.getElementById("txtDestinationTelNo").value);
    if(DestinationTelNo==""){alert("Please enter a valid contact number for the person who this order should be addressed to"); window.scrollTo(0,(findPosY(document.getElementById("txtDestinationTelNo")) / 2)); return;}

    var DeliveryID = ReturnSelectedOption(document.getElementsByName("optdelivery"));
    if(DeliveryID == -1){alert("Please select a delivery option before you continue"); window.scrollTo(0,(findPosY(document.getElementById("del1")) / 2)); return;}

    var AddressID = ReturnSelectedOption(document.getElementsByName("optAddress"+DeliveryID));
    
    if(AddressID == -1){alert("Please select a valid address before you continue"); window.scrollTo(0,(findPosY(document.getElementById("optAddress"+DeliveryID)) / 2 )); return;}

    if (AddressID == -2){ // alternative address (should be saved)
        // validation

        if (document.getElementById("txtNewAddress1"+DeliveryID).value != "")
        {
            if (document.getElementById("txtSuburb"+DeliveryID).value != "")
            {
                //if (document.getElementById("txtCity"+DeliveryID).value != "")
                //{
                    if (document.getElementById("txtCode"+DeliveryID).value != "")
                    {
                        // do nothing
                    }
                    else
                    {
                        alert ("Please enter a valid postal code before submitting your order");
                        window.scrollTo(0,(findPosY(document.getElementById("txtCode"+DeliveryID))));
                        document.getElementById("txtCode"+DeliveryID).focus();
                        return;
                    }
                /*}
                else
                {
                    alert ("Please enter a valid city before submitting your order");
                    window.scrollTo(0,(findPosY(document.getElementById("txtCity"+DeliveryID))));
                    document.getElementById("txtCity"+DeliveryID).focus();
                    return;
                }*/
            }
            else
            {
                alert ("Please enter a valid suburb before submitting your order");
                window.scrollTo(0,(findPosY(document.getElementById("txtSuburb"+DeliveryID))));
                document.getElementById("txtSuburb"+DeliveryID).focus();
                return;
            }
        }
        else
        {
            alert ("Please enter a valid delivery address before submitting your order");
            window.scrollTo(0,(findPosY(document.getElementById("txtNewAddress1"+DeliveryID))));
            document.getElementById("txtNewAddress1"+DeliveryID).focus();
            return;
        }
    }
    
    // Check if order is more than 2500 if it's a wholesale user :
    
    if (usertype == 4){ // Wholesaler
        var minamount = document.getElementById("txtSTDisplayValueTotal").value;
        /*if (minamount < 2500){
            alert("Please make sure your order amount is above R2 500.00 before continuing.");
            return;
        }*/
    }
    
    var PaymentID = ReturnSelectedOption(document.getElementsByName("optPayment"));
    if (PaymentID == -1){alert("Please select a payment method before you continue"); window.scrollTo(0,(findPosY(document.getElementById("optPayment1")))); return;}

    document.getElementById("txtAddressID").value = AddressID;
    document.getElementById("txtPaymentID").value = PaymentID;
    document.getElementById("txtComponentID").value = ComponentID;
    document.getElementById("txtDeliveryID").value = DeliveryID;

    var ooscounter = parseInt(document.getElementById("ooscounter").value);
    var records = parseInt(document.getElementById("fieldnumber").value);

/* -- Taken out 11-Jul-2010 to simplify the order placement process

    if (ooscounter != 0 && ooscounter < records){
        $('#dialog_confirm1').jqmShow(); return;
        //alert ("keep order and ship when can OR split order, ship and place backorder OR remove items and ship what can immediately");
    }
    else if(ooscounter == records) {
        $('#dialog_confirm2').jqmShow(); return;
        //alert ("Keep order and ship when can OR cancel order completely");
    }
    else{
        //alert("place order");
        //return; 
        PlaceOrder(AddressID, PaymentID, ComponentID, 1, DeliveryID);
    }
*/


    PlaceOrder(AddressID, PaymentID, ComponentID, 1, DeliveryID, DestinationUser, DestinationTelNo);

  
}

function PlaceOrder(AddressID, PaymentID, ComponentID, Option, DeliveryID, DestinationUser, DestinationTelNo){

    var AddLine1 = "";
    var AddLine2 = "";
    var AddSuburb = "";
    var AddCity = "";
    var AddCode = "";
    var AddProvinceID = 0;
    var AddProvinceText = "";
    var counter = document.getElementById("fieldnumber").value;
    var AddProvinceIDIndex = 0;
    var DeliveryCharge = 0;
    
    if (AddressID == -2){ // new address to be added
        AddLine1 = document.getElementById("txtNewAddress1"+DeliveryID).value;
        AddLine2 = document.getElementById("txtNewAddress2"+DeliveryID).value;
        AddSuburb = document.getElementById("txtSuburb"+DeliveryID).value;
        AddCity = document.getElementById("txtCity"+DeliveryID).value;
        AddCode = document.getElementById("txtCode"+DeliveryID).value;
        AddProvinceID = document.getElementById("selProvince"+DeliveryID).value;
        AddProvinceIDIndex = document.getElementById("selProvince"+DeliveryID).selectedIndex;
        AddProvinceText = document.getElementById("selProvince"+DeliveryID).options[AddProvinceIDIndex].text;
        
        // Update the Shipping fields as well incase Credit card is selected as payment option
        if (PaymentID == 3){
            
            //alert ("AddressID = -2");

            document.getElementById("txtShippingAddress1").value = AddLine1;
            document.getElementById("txtShippingAddress2").value = AddLine2;
            document.getElementById("txtShippingAddress3").value = AddSuburb;
            document.getElementById("txtShippingAddress4").value = AddProvinceText;
            document.getElementById("txtShippingAddress5").value = AddCode;
        }
    }
    else if (AddressID == -5){ // Collect From Office
        if (PaymentID == 3){
                   // alert ("AddressID = -5");

            document.getElementById("txtShippingAddress1").value = "Collect from Rage Office";
        }
    }
    else {
        // Update the Shipping fields as well incase Credit card is selected as payment option
        if (PaymentID == 3){
        
                    //alert ("AddressID = " + AddressID);

            
            AddLine1 = document.getElementById("txtShipping1"+AddressID).value;
            AddLine2 = document.getElementById("txtShipping2"+AddressID).value;
            AddSuburb = document.getElementById("txtShipping3"+AddressID).value;
            AddProvinceText = document.getElementById("txtShipping4"+AddressID).value;
            AddCode = document.getElementById("txtShipping5"+AddressID).value;
            document.getElementById("txtShippingAddress1").value = AddLine1;
            document.getElementById("txtShippingAddress2").value = AddLine2;
            document.getElementById("txtShippingAddress3").value = AddSuburb;
            document.getElementById("txtShippingAddress4").value = AddProvinceText;
            document.getElementById("txtShippingAddress5").value = AddCode;
        }
    }

// DEBUG
/*        var test;
        test = extractFormData("frmCheckout");
        
        document.getElementById("topcontent").innerHTML = test;
        return;
*/      
// DEBUG

    
    // Get the delivery Charge
    DeliveryCharge = document.getElementById("txtDelChargeValue").value;
    
    var AfterSubmit = function(responseText,loadIn,postLoad,extendedData){
        var result = responseText;
        //if (isInteger(result)){
        if (result.substr(0,5).toLowerCase() != "error"){
            var newdata = "OrderID="+result;
            //alert ("before action : " + ComponentID);
            document.getElementById("Variable1").value=result;
            //alert (result);
            document.getElementById("txtMerchantReference").value=result;
            
            //alert ("after action")
            if (PaymentID == 3){
                //alert ("open new window for payment gateway");
                alert ("Order successfully placed. You will now be redirected to our secure payment gateway")
                /*
                alert (document.getElementById("txtItemDescr1").value);
                alert (document.getElementById("txtItemDescr2").value);
                alert (document.getElementById("txtItemDescr3").value);
                */
                
                document.getElementById("frmCheckout").submit();
                //return;
            }
            else{
                getAction("Order Confirmation",ComponentID,newdata);
            }
        }
        else{
            alert (result);
        }
    }

    var data = "action=submitorder&AddressID=" + AddressID + "&PaymentID=" + PaymentID + "&Option=" + Option + "&DeliveryID=" + DeliveryID + "&AddLine1=" + encodeURIComponent(AddLine1) + "&AddSuburb=" + encodeURIComponent(AddSuburb) + "&AddCity=" + encodeURIComponent(AddCity) + "&AddCode=" + encodeURIComponent(AddCode) + "&AddProvince=" + AddProvinceID + "&AddLine2= " + encodeURIComponent(AddLine2) + "&DeliveryCharge=" + DeliveryCharge + "&DestinationUser=" + encodeURIComponent(DestinationUser) + "&DestinationTelNo=" + encodeURIComponent(DestinationTelNo);
    var page = "/modules/Catalogue/CatalogueModule.aspx";
    //alert (data);
    ajaxRequest(page,data,null,null,AfterSubmit,null,null);
}

function SetDeliveryCharge(subtotal, amt){

    document.getElementById("txtDelChargeValue").value = amt;
    document.getElementById("txtShippingCost").value = currencyFormatter(amt, true, false);
    setElementText(document.getElementById("txtDelChargeDisplay"), currencyFormatter(amt, true, false));
    
    var finaltotal = parseFloat(subtotal) + parseFloat(amt);
    
    document.getElementById("txtSTDisplayValueTotal").value = finaltotal;
    setElementText(document.getElementById("txtSTDisplayTextTotal"), currencyFormatter(finaltotal, true, false));
    
    document.getElementById("txtPrice").value = finaltotal;

    // Set the instock / outstock charge

    var instocktotal = parseFloat(document.getElementById("instockvalue").value) + parseFloat(amt);
    var outstocktotal = parseFloat(document.getElementById("outstockvalue").value) + parseFloat(amt);

    //setElementText(document.getElementById("instockdisplay"), currencyFormatter(instocktotal, false, true));
    //setElementText(document.getElementById("outstockdisplay"), currencyFormatter(outstocktotal, false, true));
}

// This function seems to be redundant and should be removed - 12-Jan-2011 - KDT
function HandleOrder(orderdialog){
    
    try{

        //alert ("handle it");
        var optionselected = ReturnSelectedOption(document.getElementsByName("orderoption"));
        
        if(optionselected == -1){
            alert ("Please select an option before continuing");
            return;        
        }

        var AddressID = document.getElementById("txtAddressID").value;
        var PaymentID = document.getElementById("txtPaymentID").value;
        var ComponentID = document.getElementById("txtComponentID").value;
        var DeliveryID = document.getElementById("txtDeliveryID").value

        if (orderdialog == 'dialog_confirm1'){
            $('#dialog_confirm1').jqmHide();
        }
        else if (orderdialog == 'dialog_confirm2'){
            $('#dialog_confirm2').jqmHide();
        }

        //alert ("AddressID : " + AddressID + "\nPaymentID : " + PaymentID + "\nComponentID : " + ComponentID + "\nDeliveryID : " + DeliveryID);
        //return;

        if (optionselected == 1 || optionselected == 5){
            //Hold on to my order and send it within a maximum of 5 days
            PlaceOrder(AddressID, PaymentID, ComponentID, 1, DeliveryID);
        }
        else if (optionselected == 2){
            // Split my order up, send the available items immediately and the rest within a maximum of 5 days
            PlaceOrder(AddressID, PaymentID, ComponentID, 2, DeliveryID);
        }
        else if (optionselected == 3){
            // Remove the unavailable items and send my order immediately
            PlaceOrder(AddressID, PaymentID, ComponentID, 3, DeliveryID);
        }
        else if (optionselected == 4 || optionselected == 7){
            // Return to my order so i can pump up my cart
            return;
        }            
        /*else if (optionselected == 5){
            // Hold on to my order and send it within a maximum of 5 days
            PlaceOrder(AddressID, PaymentID, ComponentID, 5);
        }*/
        else if (optionselected == 6 || optionselected == 8){
            // Cancel my order. I can go without these supplements
            cart_Clear(ComponentID, true);
        }
        /*else if (optionselected == 7){
            // Return to my order so i can pump up my cart
            return;
        }*/ 
    }
    catch(ex){
        alert(ex.message);
    }
}

function ReturnSelectedOption(OptionElement){

    var returnOption = -1;

	if (OptionElement != null){
	    //alert (OptionElement.length);
		if (OptionElement.length){
			for (var j = 0; j < OptionElement.length; j++)
			{
				if (OptionElement[j].checked)
				{
					//alert (OptionElement[j].value);
					returnOption = OptionElement[j].value;
				}
			}
		}
		else
		{
			if (OptionElement.checked)
			{
				returnOption = OptionElement.value;
			}
		}
		
		return returnOption;		
	}	
	else
	{
		return null;
	}
}

function ClearSelectedOption(OptionElement) {
    if (OptionElement != null) {
        //alert (OptionElement.length);
        if (OptionElement.length) {
            for (var j = 0; j < OptionElement.length; j++) {
                OptionElement[j].checked = false;
            }
        }
        else {
            OptionElement.checked = false;
        }
    }
}

function initDeliveryStructure(){

    $("#del1").click(function () {
		$("div.del1").show(1000);
		$("div.del2").hide(1000);
		$("div.del3").hide(1000);
		$("div.del4").hide(1000);
    });
    
    $("#del2").click(function () {
		$("div.del2").show(1000);
		$("div.del1").hide(1000);
        $("div.del3").hide(1000);
		$("div.del4").hide(1000);
    });
    
    $("#del3").click(function () {
		$("div.del3").show(1000);
		$("div.del1").hide(1000);
        $("div.del2").hide(1000);
		$("div.del4").hide(1000);
    });
    
    $("#del4").click(function () {
		$("div.del4").show(1000);
		$("div.del1").hide(1000);
        $("div.del2").hide(1000);
		$("div.del3").hide(1000);
    });   
    
    $("#optPayment1").click(function () {
		$("div.pay1").show(1000);
		$("div.pay3").hide(1000);
    });
    
    $("#optPayment2").click(function () {
		$("div.pay1").hide(1000);
		$("div.pay3").hide(1000);
    });
    
    $("#optPayment3").click(function () {
		$("div.pay1").hide(1000);
		$("div.pay3").show(1000);
    });

    var AfterSelect = function(value, data) {

        if (data) // a value was selected, thus free delivery applies
        {
            var AfterSubmit = function(responseText, loadIn, postLoad, extendedData) {
                var result = responseText;
                //if (isInteger(result)){
                //alert (result);
                if (result.substr(0, 5).toLowerCase() != "error") {

                    var arr = result.split('^^');

                    //alert(arr[0]);
                    //alert(arr[1]);
                    //alert(arr[2]);

                    document.getElementById("txtSuburb1").value = arr[0];
                    document.getElementById("selProvince1").value = arr[1];
                    document.getElementById("txtCode1").value = arr[2];

                    document.getElementById("Radio1").checked = true;
                    document.getElementById("del1").click();
                    document.getElementById("txtNewAddress11").focus();

                }
                else {
                    alert(result);
                }
            }

            var data = "action=getsuburbdetail&IntegrationID=" + data;
            var page = "/modules/Catalogue/CatalogueModule.aspx";
            //alert (data);
            ajaxRequest(page, data, null, null, AfterSubmit, null, null);

        }
    }

    var options, a;
    jQuery(function() {
        options = { serviceUrl: '/modules/catalogue/CheckoutFreeDelCheck.ashx',
            minChars: 2,
            delimiter: /(,|;)\s*/,
            maxHeight: 400,
            width: 300,
            zIndex: 9999,
            deferRequestBy: 0,
            noCache: false,
            //onSelect: function(value, data) { alert('You selected: ' + value + ', ' + data); }
            onSelect: AfterSelect
            //lookup: ['Krugersdorp, Gauteng', 'Krugerpark, Gauteng', 'Durban, Kwa Zulu Natal', 'Durbanville, Western Cape', 'Klerksdorp, North West', 'Richmond, Gauteng', 'Kraai Fontein, Northern Cape', 'Sandton, Gauteng', 'Roodepoort, Gauteng', 'Secunda, Mpumalanga', 'Selby, Mpumalanga'] 
        };
        a = $('#query').autocomplete(options);
        //alert("query initialized");
    });
}

function initSupplementFacts(count) {
    $("#facts1").click(function() {
        $("#divdescr1").hide(1200);
        $("#divwreview1").hide(1200);
        $("#divfacts1").show(1200);
    });
    $("#descr1").click(function() {
        $("#divfacts1").hide(1200);
        $("#divwreview1").hide(1200);
        $("#divdescr1").show(1200);
    });
    $("#wreview1").click(function() {
        $("#divfacts1").hide(1200);
        $("#divdescr1").hide(1200);
        $("#divwreview1").show(1200);
    });
    $("#descrclose1").click(function() {
        $("#divdescr1").hide(1200);
    });
    $("#factsclose1").click(function() {
        $("#divfacts1").hide(1200);
    });
    $("#wreviewclose1").click(function() {
        $("#divwreview1").hide(1200);
    });
    $("#facts2").click(function() {
        $("#divdescr2").hide(1200);
        $("#divwreview2").hide(1200);
        $("#divfacts2").show(1200);
    });
    $("#descr2").click(function() {
        $("#divfacts2").hide(1200);
        $("#divwreview2").hide(1200);
        $("#divdescr2").show(1200);
    });
    $("#wreview2").click(function() {
        $("#divfacts2").hide(1200);
        $("#divdescr2").hide(1200);
        $("#divwreview2").show(1200);
    });
    $("#descrclose2").click(function() {
        $("#divdescr2").hide(1200);
    });
    $("#factsclose2").click(function() {
        $("#divfacts2").hide(1200);
    });
    $("#wreviewclose2").click(function() {
        $("#divwreview2").hide(1200);
    });
    $("#facts3").click(function() {
        $("#divdescr3").hide(1200);
        $("#divwreview3").hide(1200);
        $("#divfacts3").show(1200);
    });
    $("#descr3").click(function() {
        $("#divfacts3").hide(1200);
        $("#divwreview3").hide(1200);
        $("#divdescr3").show(1200);
    });
    $("#wreview3").click(function() {
        $("#divfacts3").hide(1200);
        $("#divdescr3").hide(1200);
        $("#divwreview3").show(1200);
    });
    $("#descrclose3").click(function() {
        $("#divdescr3").hide(1200);
    });
    $("#factsclose3").click(function() {
        $("#divfacts3").hide(1200);
    });
    $("#wreviewclose3").click(function() {
        $("#divwreview3").hide(1200);
    });
    $("#facts4").click(function() {
        $("#divdescr4").hide(1200);
        $("#divwreview4").hide(1200);
        $("#divfacts4").show(1200);
    });
    $("#descr4").click(function() {
        $("#divfacts4").hide(1200);
        $("#divwreview4").hide(1200);
        $("#divdescr4").show(1200);
    });
    $("#wreview4").click(function() {
        $("#divfacts4").hide(1200);
        $("#divdescr4").hide(1200);
        $("#divwreview4").show(1200);
    });
    $("#descrclose4").click(function() {
        $("#divdescr4").hide(1200);
    });
    $("#factsclose4").click(function() {
        $("#divfacts4").hide(1200);
    });
    $("#wreviewclose4").click(function() {
        $("#divwreview4").hide(1200);
    });
    $("#facts5").click(function() {
        $("#divdescr5").hide(1200);
        $("#divwreview5").hide(1200);
        $("#divfacts5").show(1200);
    });
    $("#descr5").click(function() {
        $("#divfacts5").hide(1200);
        $("#divwreview5").hide(1200);
        $("#divdescr5").show(1200);
    });
    $("#wreview5").click(function() {
        $("#divfacts5").hide(1200);
        $("#divdescr5").hide(1200);
        $("#divwreview5").show(1200);
    });
    $("#descrclose5").click(function() {
        $("#divdescr5").hide(1200);
    });
    $("#factsclose5").click(function() {
        $("#divfacts5").hide(1200);
    });
    $("#wreviewclose5").click(function() {
        $("#divwreview5").hide(1200);
    });
    $("#facts6").click(function() {
        $("#divdescr6").hide(1200);
        $("#divwreview6").hide(1200);
        $("#divfacts6").show(1200);
    });
    $("#descr6").click(function() {
        $("#divfacts6").hide(1200);
        $("#divwreview6").hide(1200);
        $("#divdescr6").show(1200);
    });
    $("#wreview6").click(function() {
        $("#divfacts6").hide(1200);
        $("#divdescr6").hide(1200);
        $("#divwreview6").show(1200);
    });
    $("#descrclose6").click(function() {
        $("#divdescr6").hide(1200);
    });
    $("#factsclose6").click(function() {
        $("#divfacts6").hide(1200);
    });
    $("#wreviewclose6").click(function() {
        $("#divwreview6").hide(1200);
    });
    $("#facts7").click(function() {
        $("#divdescr7").hide(1200);
        $("#divwreview7").hide(1200);
        $("#divfacts7").show(1200);
    });
    $("#descr7").click(function() {
        $("#divfacts7").hide(1200);
        $("#divwreview7").hide(1200);
        $("#divdescr7").show(1200);
    });
    $("#wreview7").click(function() {
        $("#divfacts7").hide(1200);
        $("#divdescr7").hide(1200);
        $("#divwreview7").show(1200);
    });
    $("#descrclose7").click(function() {
        $("#divdescr7").hide(1200);
    });
    $("#factsclose7").click(function() {
        $("#divfacts7").hide(1200);
    });
    $("#wreviewclose7").click(function() {
        $("#divwreview7").hide(1200);
    });
    $("#facts8").click(function() {
        $("#divdescr8").hide(1200);
        $("#divwreview8").hide(1200);
        $("#divfacts8").show(1200);
    });
    $("#descr8").click(function() {
        $("#divfacts8").hide(1200);
        $("#divwreview8").hide(1200);
        $("#divdescr8").show(1200);
    });
    $("#wreview8").click(function() {
        $("#divfacts8").hide(1200);
        $("#divdescr8").hide(1200);
        $("#divwreview8").show(1200);
    });
    $("#descrclose8").click(function() {
        $("#divdescr8").hide(1200);
    });
    $("#factsclose8").click(function() {
        $("#divfacts8").hide(1200);
    });
    $("#wreviewclose8").click(function() {
        $("#divwreview8").hide(1200);
    });
    $("#facts9").click(function() {
        $("#divdescr9").hide(1200);
        $("#divwreview9").hide(1200);
        $("#divfacts9").show(1200);
    });
    $("#descr9").click(function() {
        $("#divfacts9").hide(1200);
        $("#divwreview9").hide(1200);
        $("#divdescr9").show(1200);
    });
    $("#wreview9").click(function() {
        $("#divfacts9").hide(1200);
        $("#divdescr9").hide(1200);
        $("#divwreview9").show(1200);
    });
    $("#descrclose9").click(function() {
        $("#divdescr9").hide(1200);
    });
    $("#factsclose9").click(function() {
        $("#divfacts9").hide(1200);
    });
    $("#wreviewclose9").click(function() {
        $("#divwreview9").hide(1200);
    });
    $("#facts10").click(function() {
        $("#divdescr10").hide(1200);
        $("#divwreview10").hide(1200);
        $("#divfacts10").show(1200);
    });
    $("#descr10").click(function() {
        $("#divfacts10").hide(1200);
        $("#divwreview10").hide(1200);
        $("#divdescr10").show(1200);
    });
    $("#wreview10").click(function() {
        $("#divfacts10").hide(1200);
        $("#divdescr10").hide(1200);
        $("#divwreview10").show(1200);
    });
    $("#descrclose10").click(function() {
        $("#divdescr10").hide(1200);
    });
    $("#factsclose10").click(function() {
        $("#divfacts10").hide(1200);
    });
    $("#wreviewclose10").click(function() {
        $("#divwreview10").hide(1200);
    });
    $("#facts11").click(function() {
        $("#divdescr11").hide(1200);
        $("#divwreview11").hide(1200);
        $("#divfacts11").show(1200);
    });
    $("#descr11").click(function() {
        $("#divfacts11").hide(1200);
        $("#divwreview11").hide(1200);
        $("#divdescr11").show(1200);
    });
    $("#wreview11").click(function() {
        $("#divfacts11").hide(1200);
        $("#divdescr11").hide(1200);
        $("#divwreview11").show(1200);
    });
    $("#descrclose11").click(function() {
        $("#divdescr11").hide(1200);
    });
    $("#factsclose11").click(function() {
        $("#divfacts11").hide(1200);
    });
    $("#wreviewclose11").click(function() {
        $("#divwreview11").hide(1200);
    });
    $("#facts12").click(function() {
        $("#divdescr12").hide(1200);
        $("#divwreview12").hide(1200);
        $("#divfacts12").show(1200);
    });
    $("#descr12").click(function() {
        $("#divfacts12").hide(1200);
        $("#divwreview12").hide(1200);
        $("#divdescr12").show(1200);
    });
    $("#wreview12").click(function() {
        $("#divfacts12").hide(1200);
        $("#divdescr12").hide(1200);
        $("#divwreview12").show(1200);
    });
    $("#descrclose12").click(function() {
        $("#divdescr12").hide(1200);
    });
    $("#factsclose12").click(function() {
        $("#divfacts12").hide(1200);
    });
    $("#wreviewclose12").click(function() {
        $("#divwreview12").hide(1200);
    });
    $("#facts13").click(function() {
        $("#divdescr13").hide(1200);
        $("#divwreview13").hide(1200);
        $("#divfacts13").show(1200);
    });
    $("#descr13").click(function() {
        $("#divfacts13").hide(1200);
        $("#divwreview13").hide(1200);
        $("#divdescr13").show(1200);
    });
    $("#wreview13").click(function() {
        $("#divfacts13").hide(1200);
        $("#divdescr13").hide(1200);
        $("#divwreview13").show(1200);
    });
    $("#descrclose13").click(function() {
        $("#divdescr13").hide(1200);
    });
    $("#factsclose13").click(function() {
        $("#divfacts13").hide(1200);
    });
    $("#wreviewclose13").click(function() {
        $("#divwreview13").hide(1200);
    });
    $("#facts14").click(function() {
        $("#divdescr14").hide(1200);
        $("#divwreview14").hide(1200);
        $("#divfacts14").show(1200);
    });
    $("#descr14").click(function() {
        $("#divfacts14").hide(1200);
        $("#divwreview14").hide(1200);
        $("#divdescr14").show(1200);
    });
    $("#wreview14").click(function() {
        $("#divfacts14").hide(1200);
        $("#divdescr14").hide(1200);
        $("#divwreview14").show(1200);
    });
    $("#descrclose14").click(function() {
        $("#divdescr14").hide(1200);
    });
    $("#factsclose14").click(function() {
        $("#divfacts14").hide(1200);
    });
    $("#wreviewclose14").click(function() {
        $("#divwreview14").hide(1200);
    });
    $("#facts15").click(function() {
        $("#divdescr15").hide(1200);
        $("#divwreview15").hide(1200);
        $("#divfacts15").show(1200);
    });
    $("#descr15").click(function() {
        $("#divfacts15").hide(1200);
        $("#divwreview15").hide(1200);
        $("#divdescr15").show(1200);
    });
    $("#wreview15").click(function() {
        $("#divfacts15").hide(1200);
        $("#divdescr15").hide(1200);
        $("#divwreview15").show(1200);
    });
    $("#descrclose15").click(function() {
        $("#divdescr15").hide(1200);
    });
    $("#factsclose15").click(function() {
        $("#divfacts15").hide(1200);
    });
    $("#wreviewclose15").click(function() {
        $("#divwreview15").hide(1200);
    });
    $("#facts16").click(function() {
        $("#divdescr16").hide(1200);
        $("#divwreview16").hide(1200);
        $("#divfacts16").show(1200);
    });
    $("#descr16").click(function() {
        $("#divfacts16").hide(1200);
        $("#divwreview16").hide(1200);
        $("#divdescr16").show(1200);
    });
    $("#wreview16").click(function() {
        $("#divfacts16").hide(1200);
        $("#divdescr16").hide(1200);
        $("#divwreview16").show(1200);
    });
    $("#descrclose16").click(function() {
        $("#divdescr16").hide(1200);
    });
    $("#factsclose16").click(function() {
        $("#divfacts16").hide(1200);
    });
    $("#wreviewclose16").click(function() {
        $("#divwreview16").hide(1200);
    });
    $("#facts17").click(function() {
        $("#divdescr17").hide(1200);
        $("#divwreview17").hide(1200);
        $("#divfacts17").show(1200);
    });
    $("#descr17").click(function() {
        $("#divfacts17").hide(1200);
        $("#divwreview17").hide(1200);
        $("#divdescr17").show(1200);
    });
    $("#wreview17").click(function() {
        $("#divfacts17").hide(1200);
        $("#divdescr17").hide(1200);
        $("#divwreview17").show(1200);
    });
    $("#descrclose17").click(function() {
        $("#divdescr17").hide(1200);
    });
    $("#factsclose17").click(function() {
        $("#divfacts17").hide(1200);
    });
    $("#wreviewclose17").click(function() {
        $("#divwreview17").hide(1200);
    });
    $("#facts18").click(function() {
        $("#divdescr18").hide(1200);
        $("#divwreview18").hide(1200);
        $("#divfacts18").show(1200);
    });
    $("#descr18").click(function() {
        $("#divfacts18").hide(1200);
        $("#divwreview18").hide(1200);
        $("#divdescr18").show(1200);
    });
    $("#wreview18").click(function() {
        $("#divfacts18").hide(1200);
        $("#divdescr18").hide(1200);
        $("#divwreview18").show(1200);
    });
    $("#descrclose18").click(function() {
        $("#divdescr18").hide(1200);
    });
    $("#factsclose18").click(function() {
        $("#divfacts18").hide(1200);
    });
    $("#wreviewclose18").click(function() {
        $("#divwreview18").hide(1200);
    });
    $("#facts19").click(function() {
        $("#divdescr19").hide(1200);
        $("#divwreview19").hide(1200);
        $("#divfacts19").show(1200);
    });
    $("#descr19").click(function() {
        $("#divfacts19").hide(1200);
        $("#divwreview19").hide(1200);
        $("#divdescr19").show(1200);
    });
    $("#wreview19").click(function() {
        $("#divfacts19").hide(1200);
        $("#divdescr19").hide(1200);
        $("#divwreview19").show(1200);
    });
    $("#descrclose19").click(function() {
        $("#divdescr19").hide(1200);
    });
    $("#factsclose19").click(function() {
        $("#divfacts19").hide(1200);
    });
    $("#wreviewclose19").click(function() {
        $("#divwreview19").hide(1200);
    });
    $("#facts20").click(function() {
        $("#divdescr20").hide(1200);
        $("#divwreview20").hide(1200);
        $("#divfacts20").show(1200);
    });
    $("#descr20").click(function() {
        $("#divfacts20").hide(1200);
        $("#divwreview20").hide(1200);
        $("#divdescr20").show(1200);
    });
    $("#wreview20").click(function() {
        $("#divfacts20").hide(1200);
        $("#divdescr20").hide(1200);
        $("#divwreview20").show(1200);
    });
    $("#descrclose20").click(function() {
        $("#divdescr20").hide(1200);
    });
    $("#factsclose20").click(function() {
        $("#divfacts20").hide(1200);
    });
    $("#wreviewclose20").click(function() {
        $("#divwreview20").hide(1200);
    });
}

function RetryOrderPayment(OrderID){
    try{
        var AfterSubmit = function(responseText,loadIn,postLoad,extendedData){
            var result = responseText;
            //if (isInteger(result)){
            //alert (result);
            if (result.substr(0,5).toLowerCase() != "error"){
                var newdata = "OrderID="+result;
                //alert ("before action : " + ComponentID);
                document.getElementById("topcontent").innerHTML = result;
            }
            else{
                alert (result);
            }
        }

        var data = "orderid=" + OrderID; 
        var page = "/modules/Orders/RetryOrderPayment.aspx";
        //alert (data);
        ajaxRequest(page,data,null,null,AfterSubmit,null,null);

    }
    catch(ex){/*alert(ex.message)*/}
}

function submitreview(UserID, StockID, Count) {

    if (UserID != 0) {
        var textarea = document.getElementById("txtReview" + Count);
        var optRating = ReturnSelectedOption(document.getElementsByName("rate" + Count));

        if (textarea.value.trim() == "") {
            alert("Please enter an opinion before submitting this review");
            return;
        }

        if (optRating == -1) {
            alert("Please select a rating from 1-10 before submitting this review");
            return;
        }

        try {
            var AfterSubmit = function(responseText, loadIn, postLoad, extendedData) {
                var result = responseText;
                //if (isInteger(result)){
                //alert (result);
                if (result.substr(0, 5).toLowerCase() != "error") {

                    document.getElementById("txtReview" + Count).value = "";
                    ClearSelectedOption(document.getElementsByName("rate" + Count));

                    alert("Your review was successfully added! Thank you for sharing your opinion!");
                    $("#divwreview" + Count).hide(1200);

                }
                else {
                    alert(result);
                }
            }

            var data = "action=addreview&stockid=" + StockID + "&userid=" + UserID + "&review=" + encodeURIComponent(textarea.value.trim()) + "&rating=" + optRating;
            var page = "/modules/Catalogue/CatalogueModule.aspx";
            //alert(data);
            //return;
            ajaxRequest(page, data, null, null, AfterSubmit, null, null);

        }
        catch (ex) { alert(ex.message); }
    }
    else {
        $("#divwreview1").hide(1200);
    }
}
