//-----------------------------------------------------------------------------------

var bwIE=1, bwFF=2, bwXX=9, bw=1;
var inUser;
var wCode    = "jsv=7&";                              //**** JS version

var TabId    = "0";
var PageName = "NotSet";

alertSize();   // also determines Internet Explorer;  FireFox  etc

var httpRQ = false;
try {
  httpRQ = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    httpRQ = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      httpRQ = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      httpRQ = false;
    }
  }
}
if (!httpRQ)
   alert("Error initializing XMLHTTP object!");

//-----------------------------------------------------------------------------------


//function Define(pName, pShop, pTab ){
function Define(pName, pwCode, pTab ){
  
    PageName = pName;  //Shop  Basket  Register  PayNow etc
//  ShopCode = pShop;
    wCode    = pwCode + wCode;
    TabId    = pTab;
    inUser   = "";

    try {
      var bt = document.getElementById("txtUserName"); 
      if (bt) inUser=bt.value;

      //  this next line appears to error in Firefox
      document.cookie = "RUcookiesOK"+TabId+"; expires=Thu, 2 Aug 2028 20:47:11 UTC; path=/"; //http://www.quirksmode.org/js/cookies.html  
      if (PageName == "PayNow"){
        var i=document.cookie.indexOf("RUcookiesOK"+TabId);  // if (i == -1) ; then cookies not enabled
        if (i == -1) {
          var fm,fl;
          fm  = document.getElementById("frmPaypal");   if (fm) fm.style.visibility='hidden';
          fl  = document.getElementById("lblPayPal");   if (fl) fl.style.visibility='visible';
        }
      }
    }catch(err){};
}
//-----------------------------------------------------------------------------------------------------
// these run even if the user get to the page by using his Back button, or History
//   it checks the page is still valid ...

function CheckPayValid(oid) {   //PayNow page
	try{
    httpRQ.open("GET", "ajaxwritecart.aspx" + wCode + "chkoi=" + oid , true); 
	  httpRQ.onreadystatechange = CheckRsp; 
	  httpRQ.send(null);
  }catch(err){ window.location.href ="shop.aspx" + wCode; };
}

function CheckBasketValid(uid,winc){    //Basket page
	try{
    httpRQ.open("GET", "ajaxwritecart.aspx" + wCode + "chkui=" + uid + "&i=" + winc , true); 
	  httpRQ.onreadystatechange = CheckRsp; 
	  httpRQ.send(null);
  }catch(err){ window.location.href ="shop.aspx" + wCode; };
}

function CheckRsp() {
  if (httpRQ.readyState == 4) {
    try{
        var response = httpRQ.responseText;
        if (response == "ok") return;
        var i=response.indexOf("ttp:");
        if (i>0) {window.location.href = response; return;}
        window.location.href ="shop.aspx" + wCode;
    }catch(err){};
  }
}
//-----------------------------------------------------------------------------------------------------



//                                  http://www.xoc.net/works/tips/non-scrolling-region.asp
function SetupScroll() {
  window.onresize=ResizeScrolling;
  ResizeScrolling();

}


function ResizeScrolling(){
  try{	

    var i=0, h=0, j=0, t=0;
    h=alertSize();  if (bw ==0  || h<100) return;
    
    if (bw == bwIE)  j =  nonscrolling.offsetHeight ;   //IE
    if (bw == bwFF)  {     
      var div = document.getElementById("nonscrolling");  //FF
      j = div.clientHeight+4;
    }
    if (j<20) return;

    i= h - j - 30; // if (i<600) i=600;

    var tabi = document.getElementById("TbImgs");
    if (tabi != null) {
      t = tabi.clientHeight;
      j=0;
      if (i < j+t) i=j+t;
    } else {  if (i<500) i=500; }

    if (bw == bwIE) scrolling.style.height = i; 
    if (bw == bwFF) {
      div = document.getElementById("scrolling");  //FF
      div.style.height = i + "px";
    }
  }catch(err){};
}

//                                  http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

function alertMsg(msg, href) {
  try{
      if (msg != "")    alert(msg);
      if (href == null) href = "";
      if (href != "")   window.location.href = href;
  }catch(err){};
}


function alertSize() {   

// http://www.w3schools.com/js/js_browser.asp
//var browser=navigator.appName;     //why does this say Netscape when i open it with Mozilla Firefox ???
//var b_version=navigator.appVersion;
//var version=parseFloat(b_version);
//alert(browser);
       
  var  myWidth = 0, myHeight = 0; 
  if( typeof( window.innerWidth ) == 'number' ) {
    myHeight = window.innerHeight;                    
    bw=bwFF; return myHeight;
  } 

  myHeight = document.documentElement.clientHeight; 
  return myHeight;
  
  return 0;
}
//----------------------------------- some things


function CaptureEnter(e)   {

  try{
      var kk=0; utab = "x";
      
      if (bw == bwIE) kk = e.keyCode;
      if (bw == bwFF) kk = e.which;

      if (kk != 13)  return true;  //-------------------------->>
      
      var activeCtrl, lCaseId, activeCtrlId, s;

      if (bw == bwIE) activeCtrl = e.srcElement;
      if (bw == bwFF) activeCtrl = e.target;
      if (activeCtrl == null) return;  //----------------------->>

      activeCtrlId = activeCtrl.id; // alert(id);

      s = activeCtrlId.toLowerCase();          
      if (s.indexOf("btn")   >=0 ) {activeCtrl.click;  return; } //-------------->>
      if (s.indexOf("cmd")   >=0 ) {activeCtrl.click;  return; } //-------------->>
      if (activeCtrlId.indexOf("txtUserNote") >= 0) return; //------------>>
      if (activeCtrlId.indexOf("txtAddress") >= 0) return; //------------>>    11/4/09

      if (bw == bwIE){  e.cancelBubble = true;  e.returnValue = false; }
      if (bw == bwFF)   e.preventDefault();

      if (PageName == "Register") {
        if (activeCtrlId.indexOf("txtPostcode") >= 0) {        // allow Enter if postcode
          s = activeCtrl.value;
          if (s != "") {
            btn = document.getElementById("cmdFind");
            btn.click();
          }
        }
        if (activeCtrlId.indexOf("lstAddresses") >= 0) {        // allow Enter if postcode
            btn = document.getElementById("cmdSelAddr");
            btn.click();
        }
        return; //------------>>
      }

      if (activeCtrlId.indexOf("txtPassword") >=0) {        // allow Enter if password has been typed
        s=activeCtrl.value;
        if (s != "") {
          btn= document.getElementById("imgBtnLogin");
          btn.click();
        }
        return; //------------------->>
      }


      if (activeCtrlId.indexOf("cboOrders") >=0) {        // allow Enter if password has been typed
        btn= document.getElementById("cmdViewOrders");
        btn.click();
        return; //------------------->>
      }


      if (activeCtrlId.indexOf("txtSearch") >=0) {        // allow Enter if password has been typed
        s=activeCtrl.value;
        if (s != "") {
          btn= document.getElementById("btnSearch");
          btn.click();
        }
        return; //------------------->>
      }


      if ( (PageName == "Shop") || (PageName == "BigImage0") ) {
   	    if (activeCtrlId.indexOf("tbQty") == 0 ) {
          s= activeCtrlId.substr(5);
          if (!writeCart2(s)) return;  //------------------->>
        }        
      }
      
      var i, b=0, t="x";
      for (i=0; i<document.Form1.elements.length; i++) {
          s = document.Form1.elements[i].id; // alert(s); 
       	  if (s.indexOf("tbQty") >=0  && t=="x") t=document.Form1.elements[i];
     	    if (s == activeCtrlId) {b=1;}
          else{
         	  if (s.indexOf("tbQty") >=0  && b==1){
         	    document.Form1.elements[i].select() ; return; // ------------>>
     	      }
     	    }
      }
      
      if (PageName == "Basket") {
        utab = "t";
        activeCtrl.blur(); 
        t = document.getElementById("btnBuy");
        t.setActive();
      }   
     
   }catch(err){};
 }
//-----------------------------------------------------------------------------------
 function clrTxtUserName() {
   var lt = document.getElementById("txtUserName");
   var u = lt.value;
   if (u.indexOf("Enter ") == 0) lt.value = "";
 }
 function hintTxtUserName() {
   var lt = document.getElementById("txtUserName");
   var u = lt.value;
   if (u == "") lt.value = "Enter your email address here";
 }

 function clrTxtSearch() {
   var lt = document.getElementById("txtSearch");
   var u = lt.value;
   if (u.indexOf("Search ") == 0) lt.value = "";
 }
 function hintTxtSearch() {
   var lt = document.getElementById("txtSearch");
   var u = lt.value;
   if (u == "") lt.value = "Search for keywords";
 }
 //-----------------------------------------------------------------------------------
 function BasketToShop() {
   try {
     window.location = "shop.aspx" + wCode + "c=" + TabId;
   } catch (err) { };
   return false; 
 }
/*
function showOrder() {
  try {
    var lt = document.getElementById("cboOrders"); // alert (lt.value);
    var u= lt.value; // alert(u);

    if (u < 0) return false;
    return true;
//    __doPostBack('cmdViewOrders','');

  }catch(err){};
}
*/
 function showOrder() { 
  try {
    var lt = document.getElementById("cboOrders"); // alert (lt.value);
    var u = lt.value; // alert(u);
    if (u >=0) window.location = "basket.aspx" + wCode + "c=" + TabId + "&oi=" + u;
  }catch(err){};
 }
//-----------------------------------------------------------------------------------
var upf = "", utab = "x";
function UpdBasket(tid,v) {
   var lt = document.getElementById(tid); // alert (lt.value);
   var u= lt.value; // alert(u);
    
//   if (u == v) return false;
//   if (upf == "x;") return;

   if ((u != v) && (upf != "x;")) {
     upf = "x;"

     try {
       var xx = document.getElementById("xxSubTotal"); xx.innerHTML = "";
       xx = document.getElementById("xxDiscount"); xx.innerHTML = "";
       xx = document.getElementById("xxTotal"); xx.innerHTML = "";
       xx = document.getElementById("xxTotal"); xx.innerHTML = "";
       xx = document.getElementById("xxDelivery"); xx.innerHTML = "";
       xx = document.getElementById("xxVAT"); xx.innerHTML = "";
     } catch (err) { };

     try {
       var bt, lt, i;

       bt = document.getElementById("btnQuote"); bt.style.visibility = 'hidden';
       lt = document.getElementById("txtQtys"); lt.value = "Click Update to get a new total";
       bt = document.getElementById("btnBuy"); bt.src = "images/nav/BasketUpdate.jpg";  
                                               bt.style.visibility='visible';  //v2.4.54
       
     } catch (err) { };
   }
   
   try {
     if (utab == "t") document.getElementById("btnBuy").setActive();
     utab = "x";
   } catch (err) { };
    
   return false;
}

//--------------------
function doBtnBuy(qq) {
//   var lt = document.getElementById("btnBuy"); 
   if ( bsktQtys(qq) ) {
//     if (lt) lt.disabled=true; 
//     __doPostBack('btnBuy','')
   } 
}

function bsktQtys(qq) {
   var i;   var s;

   if (qq != null) upf = qq + ";";

   try{  
        var lt = document.getElementById("txtQtys"); 
        if(lt.value.indexOf("ank")>0) return false ;
          
//        if (upf == "") {
//            if ( confirm("Please click OK to order these items") == false) return false;
//        }

        for (i=0; i<document.Form1.elements.length; i++) {
            s = document.Form1.elements[i].id; // alert(s); 
     	      if (s.indexOf("tbQty") >=0){ upf += s + "=" + document.Form1.elements[i].value + ";" ;	}
        } 

        var lt = document.getElementById("txtQtyN"); 
        lt.value=upf;

        return true;
   }catch(err){alert('3. '+err);}
 }
//-----------------------------------------------------------------------------------

function splitEmail(){
  try{
    var ts = document.getElementById("txtSurname"); if (ts.value != "") return;
    var tn = document.getElementById("txtName");    if (tn.value != "") return;
    var ee = document.getElementById("txtEMail").value; if (ee.indexOf("@") < 3) return;
    var i=0,a;
    var valchrs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   
    for (i=0; i<99; i++){
      a = ee.charAt(i);
      if (valchrs.indexOf(a,0) >= 0){
        if ( (ts.value=="") && (valchrs.indexOf(a,0)>25) ) a = valchrs.charAt(valchrs.indexOf(a,0)-26);
        ts.value += a;
      }
      if (a == ".") {tn.value = ts.value; ts.value=""}
      if (a == "@") return;
    }        

  }catch(err){}
}
//-----------------------------------------------------------------------------------
/* The POST method works on most browsers except FireFox 3.0.5
    it appears that using POST the rest of the Form action happens so quickly it prematurely terminates the POST
    -  that explaination is only a guess!
*/

var payok = "";
function postGoogle(oi){PayingOnline(oi,"Google")}
function postPaypal(oi) { PayingOnline(oi, "PayPal") }
//function postSecureTrading(oi) { PayingOnline(oi, "SecureTrading") }


function postSecureTrading2(oi) {   //  v2.4.33
  payok = "ST";
  PayingOnline(oi, "SecureTrading");
  return false;  //v2.4.53
}

function PayingOnline(oi,pp){
    try{  
        var ux;
        var lt = document.getElementById("txtUserNote"); 
        ux = lt.value;
        if (ux == null) ux = "";
        if (ux.length > 250) ux=""
        ux = ValidStr(ux);
    }catch(err){ux = ""};

    try{
        httpRQ.open("GET", "ajaxwritecart.aspx" + wCode + pp + "=" + oi + "&ux=" + ux);
        httpRQ.onreadystatechange = gotOnline; // cannot pass parameters to this function, apparently...
        httpRQ.send(null);
    }catch(err){}
}

function gotOnline() {
  if (httpRQ.readyState == 4) {
    try {
      if (httpRQ.responseText == "ok") {
        if (payok == "ST") {
          document.getElementById("frmST").submit();
        }
       };
    }catch(err){}
  }
}
//-----------------------------------------------------------------------------------
function checkPostcode() {    //v2.4.37
  try{
    var lt = document.getElementById("txtPostcode");
    var u = lt.value;
    if (u == "") { showPostCodeMsg("visible"); return }
    lt.value = u.toUpperCase();
    httpRQ.open("GET", "ajaxwritecart.aspx" + wCode + "chkPostCode=" + u);
    httpRQ.onreadystatechange = gotchkPostCode; // cannot pass parameters to this function, apparently...
    httpRQ.send(null);
  } catch (err) {};
}
function gotchkPostCode() {
  if (httpRQ.readyState == 4) {
    try {
      if (httpRQ.responseText == "ok") {showPostCodeMsg("hidden")}
      if (httpRQ.responseText == "fx") {showPostCodeMsg("visible")}
    } catch(err) { }
  }
}
function showPostCodeMsg(v) {
  var lt = document.getElementById("lblPostcodeW");
  lt.style.visibility = v;
}
//-----------------------------------------------------------------------------------


function ValidStr(parm) {
  var valchrs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789£$.,-/";   // 17/3/09  add /
  if (parm == "") return "";
  var res = "";
  for (i=0; i<parm.length; i++) {
    if (parm.charAt(i) == "\n")  res += "%0A"; 
    if (parm.charAt(i) == " " )   res += "%20"; 
    if (valchrs.indexOf(parm.charAt(i),0) >= 0) res +=  parm.charAt(i);
  }
  return res;
}

//function reqEmail(uid,ShopCode){
function reqEmail(uid){
  try{

    var bt = document.getElementById("txtUserName"); 

    var uu = bt.value;
    if (uu == null) {uu="nobody";}
    if (uu.indexOf("@") < 3) {alert("Please enter your email addres"); return false;}

    if ( confirm("We will send you an email with your password" ) == false) return false;

//  httpRQ.open("GET", "ajaxwritecart.aspx?ui=" + uid + "&uu=" + uu + "&w=" + ShopCode, true); 
	  httpRQ.open("GET", "ajaxwritecart.aspx" + wCode + "ui=" + uid + "&uu=" + uu , true); 
	  httpRQ.onreadystatechange = gotEmail; // cannot pass parameters to this function, apparently...
	  httpRQ.send(null);
  }catch(err){alert('51. '+err);}

}
function gotEmail() {
  if (httpRQ.readyState == 4) {
    try{
        var response = httpRQ.responseText;
        alert(response);
    }catch(err){alert('52. '+err); }
  }
}

//-----------------------------------------------------------------------------------
var divID;

function writeCart(pid, imm) {
  try{
    var tid = "tbQty" + pid; //  bid;
    var txt = document.getElementById(tid);
    if (txt.value =="") txt.value="1";
    writeCart2(pid,imm);
  }catch(err){}
}

function writeCart2(pid,imm) {
  if ( imm == null ) {imm="0";};
  try{                                        //  alert(pid);

 //   if (document.cookie.indexOf("RUcookiesOK"+TabId) == -1) {alert("Cookies must be enabled please"); return false}

    var bt = document.getElementById("txtUserName"); 
    if (inUser != bt.value) {alert(" Username has changed \n Login please"); return false;}
  }catch(err){}
  
  try{

    var stv = "0";
    var qi = 0;
    var tid = "tbQty" + pid; //  bid;
    var txt = document.getElementById(tid);
    divID = "DivId" + pid; //bid;

    try{
      var sid = "Style" + pid; //bid;
      var sty = document.getElementById(sid);
      stv = sty.value;    
      if ((stv == "0") && (txt.value == "")) return true;
      if  (stv == "0") {alert("Select a Style please"); return false;}
    }catch(err){}


    if (stv == "MultiBuy") {   
//      window.location="BigImage.aspx?w=" + ShopCode + "&m=" + TabId + "&i=" + (pid-100000) ;
        window.location="BigImage.aspx" + wCode + "m=" + TabId + "&i=" + (pid-100000) ;
        return false;
    }

    if ((stv == "0") && (txt.value == "")) return true;

    if (txt.value != "0"){
      qi = parseInt(txt.value);
      if (txt.value != qi.toString()) {alert("Invalid quantity"); return false;    }
//    if ((qi < 1) || (qi > 9999)) { alert("Quantity should be 0-9999"); return false; }
      if ((qi < 1) || (qi > 100000)) { alert("Quantity should be 0-100000"); return false; }  //v2.4.46
      txt.value = qi;
    }

	  var my_date = new Date();
//  var params = "?pid=" + pid + "&q=" + txt.value + "&s=" + stv + "&c=" + TabId + "&w=" + ShopCode + "&im=" + imm;
	  var params = wCode + "pid=" + pid + "&q=" + txt.value + "&s=" + stv + "&c=" + TabId + "&im=" + imm;
 //   if (document.cookie.indexOf("RUcookiesOK"+TabId) == -1) params = params + "&ck=no";

	  httpRQ.open("GET", "ajaxwritecart.aspx"+params, true); 
	  httpRQ.onreadystatechange = updateDiv; // cannot pass parameters to this function, apparently...
	  httpRQ.send(null);
	  //return false;
    return true;
  }catch(err){alert('1. '+err);}
}


function updateDiv() {
  if (httpRQ.readyState == 4) {
    try{
        var i,bb;
        var response = httpRQ.responseText;

        i=response.indexOf("ttp:");
        if (i==1){ 
      //      window.open(response);
//          window.location = response;
          window.location.href = response;
//          i= response.indexOf("basket");
//          window.location.href = response.substr(i);
            return;
        }

        i=response.indexOf('\n'); 
        if (i>0) { alert(response);}
        else {
              i=response.indexOf('=='); 
              if (i>5){
                bb=document.getElementById("lblBasket");    if (bb != null) bb.innerHTML = response.substr(i+2);
                response = response.substr(0,i-1);
              }
              bb =document.getElementById(divID);           if (bb != null) bb.innerHTML = response;
        }
    }catch(err){alert('2. '+err); }
  }
}
//----------------------------------------------------


