﻿function doJavaTest()
{ 
//    var IE6 = false;
//    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
//    { //test for MSIE x.x;
//        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
//        if(ieversion<=6)//switch off ajax/jscript
//        {
//            IE6 = true;
//        }
//        
//    }

//    if(IE6 == false)
//    {
       if(document.getElementById("UcBrowserCheck1_hdnJavaEnabled") != null)//javaDiv
        {
           document.getElementById("UcBrowserCheck1_hdnJavaEnabled").style.visibility = 'visible';
        }
        //this works but if you view source it doesnt show updated value in memory...
        if((document.getElementById("UcBrowserCheck1_hdnJavaEnabled") != null)
        && (document.getElementById("UcBrowserCheck1_hdnJavaEnabled").value == "0"))//hdnJavaCheck
        {
           
          document.getElementById("UcBrowserCheck1_hdnJavaEnabled").value = "1"; //hdnJavaCheck
          setJavaScript(); 
        }
//    }
}
function setJavaScript()
{
//    if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_pnlPopUpUpdatePrice") != null)
//    {
//        var pnlPopup = document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_pnlPopUpUpdatePrice"); 
//        // make it visible
//        pnlPopup.style.display = '';  
//    }
    generalServices.setJavaScriptEnabled(true, setJSOK);

}
// This is the callback function that
// processes the Web Service return value.
function setJSOK(result)
{
    //do nothing
}
function setupCart()
{
    var IP = location.hostaddress;
    generalServices.setupUserCart(IP, setJSOK);//dont need to callback to user so just give it a finish point..
}
function SortProducts()
{
    var strCriteria = "";
    var strResultsPerPage = "20";
    var strDirection = "ASC";

    //get sort values
    if(document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortCriteria") != null)
    {
        strCriteria = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortCriteria").value;
        strResultsPerPage = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVAResultsPerPage").value;
        //strDirection = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortDirection").value;
    }
    else if(document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVASortCriteria") != null)
    {
        strCriteria = document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVASortCriteria").value;
        strResultsPerPage = document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVAResultsPerPage").value;
        //strDirection = document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVASortDirection").value;
    }
   //alert("criteria: " + strCriteria + " and results per page: " + strResultsPerPage + " and direction: " + strDirection);
    
   // document.location.href= "?sort=" + strCriteria + "," + strResultsPerPage + "," + strDirection;
    generalServices.SortProducts(strCriteria, strResultsPerPage, SortedProductsCallBack);
    
}
function SortedProductsCallBack(result)
{
    var strCriteria = "";
    var strResultsPerPage = "";
    var blInSearchResults = false;
    //var strDirection = "";

    if(document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortCriteria") != null)
    {
        blInSearchResults = false;
        strCriteria = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortCriteria").value;
        strResultsPerPage = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVAResultsPerPage").value;
        //strDirection = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortDirection").value;
    }
    else if(document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVASortCriteria") != null)
    {
        blInSearchResults = true;
        strCriteria = document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVASortCriteria").value;
        strResultsPerPage = document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVAResultsPerPage").value;
       // strDirection = document.getElementById("UcSearchResults1_UcSortBar1_ddlJAVASortDirection").value;

    }
    if(blInSearchResults == true)
    {
       //force postback to refresh product data
        //document.location.href= document.location.href + "&sort=" + strCriteria + "," + strResultsPerPage;// + "," + strDirection;
        __doPostBack('UcSearchResults1_upSortDepartments', '');
    }else
    {
         //force postback to refresh product data
        //document.location.href= "?sort=" + strCriteria + "," + strResultsPerPage;// + "," + strDirection;
        __doPostBack('UcStoreDepartments1_upSortProducts', '');
        
    }
   // __doPostBack('UcStoreDepartments1_upSortProducts', '');
}
function sortProductPaging(intFrom, offset, intPage)
{
  //var strCriteria = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortCriteria").value;
  //  var strResultsPerPage = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVAResultsPerPage").value;
   // var strDirection = document.getElementById("UcStoreDepartments1_UcSortBar1_ddlJAVASortDirection").value;

    generalServices.SelectProductPage(intFrom, SelectedProductPageCallback);
   // document.location.href= "?sort=" + strCriteria + "," + strResultsPerPage + "," + strDirection + "," + intPage;
  
}
function SelectedProductPageCallback(result)
{
    //force postback to refresh product data
   //document.getElementById("UcStoreDepartments1_UcSortBar1_lblJAVAProductsinView").value = result;
    __doPostBack('UcStoreDepartments1_upSortProducts', '');
   // generalServices.GetProductSortedPageView(ProductSortedPageViewCallBack);
     //document.getElementById("UcStoreDepartments1_UcSortBar1_lblJAVAProductsinView").value = "You are viewing products "  + intFrom + " to " + (intFrom + offset) + ".";
}
function ResetSort()
{
    generalServices.ResetSort(ResetSortCallBack);
}
function ResetSortCallBack(result)
{
    __doPostBack('UcStoreDepartments1_upSortProducts', '');
}
function gotoPage(intPageNum)
{
     toTopOfProdList();
    generalServices.SelectProductPage(intPageNum, gotoPageCallBack);
}
function gotoPageCallBack(result)
{
    if(document.getElementById("UcStoreDepartments1_UcSortBar1_lblJAVAProductsinView") != null)
    {
        document.getElementById("UcStoreDepartments1_UcSortBar1_lblJAVAProductsinView").value = result;
        __doPostBack('UcStoreDepartments1_upSortProducts', '');
    }
    else if (document.getElementById("UcSearchResults1_UcSortBar1_lblJAVAProductsinView") != null)
    {
        document.getElementById("UcSearchResults1_UcSortBar1_lblJAVAProductsinView").value = result;
        __doPostBack('UcSearchResults1_upSortProducts', '');
    }

   
    
}
function refreshProductPrice(object)
{
    var thisname = object.name;
    var thisid = object.value;
    var pnlPopup = document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_pnlPopUpUpdatePrice"); 
    // make it visible
    pnlPopup.style.display = '';  
    
    generalServices.refreshPrice(thisname, thisid, refreshProdPriceCallBack);
            ////generalServices.getOptionPrice(thisname, thisid, refreshProdOptionPriceCallBack);
            ////        var thisvalue = object.value;
            ////    if(object.value != null)//&& (object.value != "Please Select...")
            ////    {
            
         //*** just unchecked this 15-05-08
    generalServices.getOptionPrice(thisname, thisid, refreshDDProdPriceCallBack);
            ////    }
}
function refreshProductPriceFromCB(object)
{ 
var pnlPopup = document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_pnlPopUpUpdatePrice"); 
    // make it visible
    pnlPopup.style.display = '';  
    var ObjSplit = object.name.split('$');
    if(ObjSplit.length>0)
    {
        var thisname = ObjSplit[0] + "$" + ObjSplit[1]  + "$" +  ObjSplit[3];
        var thisid = ObjSplit[2];
        var thisvalue = object.value;
        if(object.checked == true)
        {
            generalServices.refreshPriceforCB(thisname, thisid, refreshProdPriceCallBackFromCB);
            generalServices.getOptionPrice(thisname, thisid, refreshCBProdPriceCallBack);
         }
         else
         {
         //commented 19-05-08 because dont want previousd options to not remain checked if they were checked.
//            generalServices.refreshPrice(thisname, "", refreshProdPriceCallBack);
              generalServices.removeCheckBoxOption(thisname, thisid, refreshProdPriceCallBackFromCB);
//            if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + thisid) != null)
//            {
//                document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + thisid).innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
//            }
//            changeQty();
         }
         //*** just unchecked this 15-05-08
         // generalServices.updateCheckBoxClientProductOptionPrice(thisname, thisvalue, refreshCBProdPriceCallBack);
     }
}
function refreshProductPriceFromDD(object)
{
    var pnlPopup = document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_pnlPopUpUpdatePrice"); 
    // make it visible
    pnlPopup.style.display = '';  
    var ObjSplit = object.name.split('$');
    
    if(ObjSplit.length>0)
    {
        var thisname = ObjSplit[0] + "$" + ObjSplit[1]  + "$" +  ObjSplit[3];
        var thisvalue = object.value;
        if(object.value != null)//&& (object.value != "Please Select...")
        {
            generalServices.refreshPriceFromDD(thisname, thisvalue, refreshProdPriceCallBack);
            generalServices.updateClientDropDownProductOptionPrice(thisname, thisvalue, refreshDDProdPriceCallBack);
            
        }
    }
}
function refreshProdPriceCallBackFromCB(result)
{
    var retValue = "";
    if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[1]) != null)
    {
        if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[1]).parentNode != null)// + "_OPTIONS").parentNode != null)
        {
           var title =  document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[1]).parentNode.title;// + "_OPTIONS").parentNode.title;
           if(title.length >62)
           {//is it a double line?
            retValue  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;";
           }
           else
           {//add single line return
            retValue  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
           }
        } 
        else
        {
            retValue  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[1]).innerHTML = retValue;//"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

    }
        if((document.getElementById("txtRemainingBalance") !=null) && (result[2] != null))
        {
            document.getElementById("txtRemainingBalance").innerHTML = result[2];
        }
    changeQty();
}
function refreshProdPriceCallBack(result)
{
//TODO:commented 09-04-08 as sub total costs not needed.  only overall
//    if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_lblTotalActualCost") != null)
//    {
//        document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_lblTotalActualCost").innerHTML = result[0];
//    }else
//    {
//        if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_lblTotalActualCost")!=null)
//        {
//            document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_lblTotalActualCost").innerHTML = result[0];
//        }else
//        {
//            //ignore it alert('object not found to update price');
//        }
//    }
    var retValue = "";
   if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[1]) != null)
   {
        if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[1]).parentNode != null)
        {
           var title =  document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[1]).parentNode.title;
           if(title.length >62)
           {//is it a double line?
            retValue  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;";
           }
           else
           {//add single line return
            retValue  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
           }
        } 
        else
        {
            retValue  = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }

     document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[1]).innerHTML = retValue;
   }
            if((document.getElementById("txtRemainingBalance") !=null) && (result[2] != null))
        {
            document.getElementById("txtRemainingBalance").innerHTML = result[2];
        }
//   changeQty(); 07/08/08: commented cos radio buttons call this twice 
}
function refreshDDProdPriceCallBack(result)
{
    var retValue = "";
   if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[0]) != null)
   {
      if((document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[0]) !=null)
      && (document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[0]).parentNode != null))
        {
           var title =  document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[0]).parentNode.title;
          
           if(title.length >62)
           {//is it a double line?
            retValue  = "&nbsp;<br />" + result[1];
           }
           else
           {//add single line return
            retValue  = result[1];
           }
        } 
        else
        {
            retValue  = result[1];
        }
     document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[0]).innerHTML = retValue;
   }
       if((document.getElementById("txtRemainingBalance") !=null) && (result[2] != null))
        {
            document.getElementById("txtRemainingBalance").innerHTML = result[2];
        }
   changeQty();
}
function refreshCBProdPriceCallBack(result)
{
   var retValue = "";
   if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[0]) != null)
   {
        if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[0]).parentNode != null)// + "_OPTIONS").parentNode != null)
        {
            var title =  document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_" + result[0]).parentNode.title;// + "_OPTIONS").parentNode.title;
            if(title.length >62)
            {//is it a double line?
               retValue  = "&nbsp;<br />" + result[1];
            }
            else
            {//add single line return
               retValue  = result[1];
            }
        } 
        else
        {
            retValue  = result[1];
        }
        document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_price-" + result[0]).innerHTML = retValue;
   }
   changeQty();
}
function changeQty()//(object)
{
    //var Qtyvalue = object.value;
    if(document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_txtQty") != null)
    {
        Qtyvalue = document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_txtQty").value;
    }
    generalServices.refreshPriceForQty(Qtyvalue, changeQtyCallBack);
}
function changeQtyCallBack(result)
{
    if((document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_lblTotal") != null) 
    && (result[0] != null))
    {
        document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_lblTotal").innerHTML = result[0];
    }
    
    if((document.getElementById("txtRemainingBalance") !=null) && (result[1] != null))
    {
        document.getElementById("txtRemainingBalance").innerHTML = result[1];
    }
    var pnlPopup = document.getElementById("UcStoreProduct1_ProfileProductOptionsBasic_pnlPopUpUpdatePrice");
    // make it invisible
    if(pnlPopup != null)
    {
         pnlPopup.style.display = 'none';  
    }
}
function changeBasketQty(strProductID, object)
{
    if(object != null)
    {
        var ID = object.id;
        ID = ID.replace("-qty","");
        var split = ID.split('_');
        generalServices.changeBasketQty(strProductID, object.value, split[1], ID,  changeBasketQtyCallback);
    }
}
function changePSItemBasketQty(strProductID, intCartPSIID, object)
{
    if(object != null)
    {
        var ID = object.id;
        ID = ID.replace("-qty","");
        var split = ID.split('_');
        generalServices.changePSItemBasketQty(strProductID, intCartPSIID, object.value, split[1], ID,  changeBasketQtyCallback);
    }
}
function changeBasketQtyCallback(result)
{
    if(result[1] != null)
    {
        document.getElementById(result[1] + "-subTotal").innerHTML = result[0];
    }
    updateOverallBasketCost();
}
function updateOverallBasketCost()
{
    generalServices.refreshOverallBasketTotal(updateBasketCostCallBack);
}
function updateBasketCostCallBack(result)
{
    if(result != null)
    {
        document.getElementById("UcBasket1_txtTotalQty").value = result[0];
        document.getElementById("UcBasket1_basketTotal").innerHTML = result[1];
    }
}
function removeBasketItem(strUniqueID, strCartID)
{
    generalServices.removeBasketItem(strUniqueID, strCartID, removeBasketItemCallBack);
}
function removeBasketItemCallBack(result)
{
    if(result != null)
    {
        //display total items
        document.getElementById("UcBasket1_txtTotalQty").value = result[0];
        if(result[0] == "0")
        {
            document.getElementById("UcBasket1_lblBasketHeader").innerHTML = "Your Basket is Empty";
        }
       // document.getElementById("UcShopRightHandSide_UcBasket1_lblNumberofItems").innerHTML = result[0];
       // document.getElementById("UcShopRightHandSide_UcBasket1_lblTotal").innerHTML = result[1];
    }

    var button = document.getElementById("UcBasket1_lnkUpdateBasket");
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        // IE
        button.click();
    }
    else
    {
        // FireFox
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        button.dispatchEvent(e);
    }
}
function checkedChangeDeliveryAddress()
{
//    if(document.getElementById("UcCreateAccount1_chkAlternativeDelivery").checked == true)
//    {
//        document.getElementById("UcCreateAccount1_chkAlternativeDelivery").checked = false;
//    }
//    else
//    {
//        document.getElementById("UcCreateAccount1_chkAlternativeDelivery").checked = true;
//    }
}
function toTopOfProdList()
{
    window.location.hash = '#ProdViewTopAnchor';
}
function SubmitEmailStockNotification()
{
     var strEmail = document.getElementById("UcStoreProduct1_ProfileProductOutofStock_txtEmailAddress").value;
     var blSuccess = generalServices.RequestBackInStockNotification(strEmail, displayBISNotifierMessageResponse);
     
}
function displayBISNotifierMessageResponse(result)
{
  if(result != null)
    {
        if(result == true)
         {
            //TODO: Trigger success
            $find('modalEmailBackInStock').hide();
            $find('mpeSuccess').show();
           //document.getElementById("UcStoreProduct1_ProfileProductOutofStock_mpeSuccess").show();
            
         }else
         {
            //TODO:Trigger failure.
            $find('mpeFailedSave').show();
            //document.getElementById("UcStoreProduct1_ProfileProductOutofStock_mpeFailedSave").show();
         }
     }
}
//// JScript File
//// Adds event to window.onload without overwriting currently 
//// assigned onload functions.
////function AddOnload(myfunc)
////{
////if(window.addEventListener)
////window.addEventListener('load', myfunc, false);
////else if(window.attachEvent)
////window.attachEvent('onload', myfunc);
////}
////AddOnload(doJavaTest);
////if (window.onload) {
////var func = window.onload; 
////window.onload = function() {
////func();
////doJavaTest();
////}
////} else {
////window.onload = doJavaTest;
////} 
//// Dean Edwards/Matthias Miller/John Resig
//function init() {
//// quit if this function has already been called
//if (arguments.callee.done) return;
//// flag this function so we don't do the same thing twice
//arguments.callee.done = true;
//// kill the timer
//if (_timer) clearInterval(_timer);
//// do stuff
//doJavaTest;
//};
///* for Mozilla/Opera9 */
//if (document.addEventListener) {
//document.addEventListener("DOMContentLoaded", init, false);
//}
///* for Internet Explorer */
///*@cc_on @*/
//@if (@_win32)
//document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
//var script = document.getElementById("__ie_onload");
//script.onreadystatechange = function() {
//if (this.readyState == "complete") {
//init(); // call the onload handler
//}
//};
///*@end @*/
///* for Safari */
//if (/WebKit/i.test(navigator.userAgent)) { // sniff
//var _timer = setInterval(function() {
//if (/loaded|complete/.test(document.readyState)) {
//init(); // call the onload handler
//}
//}, 10);
//}
///* for other browsers */
//window.onload = init;