var gUpdateName=false;

var gCatId=0;

var gItemId=0;

var gSwapId=0;





// something like this is needed for IE 6 to prevent it resizing too small

//window.onresize= checkItem;

//window.onload= checkItem;

function checkItem() {

  var offsetWidth=document.body.offsetWidth;

  if(offsetWidth<963) offsetWidth=963;

  document.getElementById('pageCon').style.width=offsetWidth+'px';

}



/*

* ajax call to retrieve and possible execute code on response from server

*

*/

function doTask(params) {

  if (document.implementation && window.document.implementation.createDocument)

  {

    xmlDoc = document.implementation.createDocument("", "", null);

    xmlDoc.onload = doResponse;

  }

  else if (window.ActiveXObject)

  {

    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

    xmlDoc.onreadystatechange = function () {

      if (xmlDoc.readyState == 4) doResponse()

    };

  }

  else

  {

    return false;

  }

  d = new Date; // Generic - use to create unique url requests

  var rand = d.getTime();

  var url=document.location.protocol+'//'+document.location.host+'/alt/doTask.xml?rand='+rand+'&'+params;



  try{

    xmlDoc.load(url); // this is what we like to do

  } catch(e) { // if failed try using httprequest as a fallback - (e.g. Safari)

    try {

      req = new XMLHttpRequest();

    } catch(e) {

      req = false;

    }

    if(req) {

      req.onreadystatechange = processHttpRequest;

      req.open("GET", url, true);

      req.send("");

    }



  }



  return false;

}



/*

* some browsers require this extra step to catch the http reponse

*

*/

function processHttpRequest() {

  if (req.readyState == 4) {

        if (req.status == 200) {

      xmlDoc=req.responseXML;

      doResponse();

        }

    }

}



/*

* process the response from server to the doTask function,

*

*/

function doResponse() {

  var tasks = xmlDoc.getElementsByTagName('task');

  var cmd=null;



  for(var i=0;i<tasks.length;i++) {

    cmd=tasks[i].getElementsByTagName('cmd')[0];



    if (cmd.childNodes[0]) {

      switch(cmd.childNodes[0].nodeValue) {

        case 'replace': // replace the innerHTML of an element

          var id=tasks[i].getElementsByTagName('id')[0].childNodes[0].nodeValue;

          var content=''; //tasks[i].getElementsByTagName('content')[0].childNodes[0].nodeValue;



          /*rebuild the content - FF splits nodes > 4096 bytes*/

          var cnt=tasks[i].getElementsByTagName('content')[0].childNodes.length;

          for(var j=0;j<cnt;j++) {

            content += tasks[i].getElementsByTagName('content')[0].childNodes[j].nodeValue;

          }



          //window.clipboardData.setData("Text",content);



          if (id.length>0 && document.getElementById(id)) document.getElementById(id).innerHTML=content;

          break;

        case 'execute': // run some js

          var script=tasks[i].getElementsByTagName('script')[0].childNodes[0].nodeValue;

          eval(script);

          break;



      }

    }

  }

  

  if(gUpdateName) {

    gUpdateName=false;

    doTask('mod=shop&action=catname&cat='+gCatId+'&item='+gItemId+'&sw='+gSwapId);

  }



  // needed for the product page

  try {

    window.flashMovie = document.getElementById('flashMovie');

  }

  catch(e) {

    ;

  }



}





/*

* use JS to hide controls which are only required when JS is not enabled

* - do this by adding hideJS as a class to the css

*/

function hideElements() {

  if (document.styleSheets) {

    if (document.styleSheets[0].addRule) {    // IE

      document.styleSheets[0].addRule('.hideJS','display: none',0);

    } else {    // !IE

      document.styleSheets[0].insertRule('.hideJS {display: none}', 0);

    }

  }



}



/*

* Add the onload function to any other onload function

*/

function addLoadEvent(func) {

  var oldonload = window.onload;



  if (typeof window.onload != 'function') {

    window.onload = func;

  } else {

    window.onload = function() {

      oldonload();

      func();

    }

  }

}



addLoadEvent(hideElements);



// show the form wait box

function showWait(id) {

  if (document.getElementById(id)) {

    var el=document.getElementById(id);



    el.style.position='absolute';

    el.style.left='50%';

    el.style.top='50%';

    el.style.display='block';

    el.style.marginLeft="-" + parseInt(el.offsetWidth  / 2)+'px';

    el.style.marginTop ="-" + parseInt(el.offsetHeight / 2)+'px';

    // IE has a quirk which means we need to reload the animated gif to make it animate

    if (document.getElementById(id+'wImg')) setTimeout("document.getElementById('"+id+"wImg').src=document.getElementById('"+id+"wImg').src",20);

  }

}



function swapColor(catId,itemId,element,curId) {

 var swapId=element.options[element.selectedIndex].value;

 if(swapId>0 && swapId!=curId) {



    gCatId=catId;

    gItemId=itemId;

    gSwapId=swapId;

    gUpdateName=true;

    doTask('mod=shop&action=item&cat='+catId+'&item='+itemId+'&sw='+swapId);

 }



}



function qChange(fm) {

  try {

    fm.form.action=document.location.href;

    fm.form.submit();

  }

  catch(e) {

    var rmNode=fm.form.action.removeNode();

    fm.form.action=document.location.href;

    fm.form.appendChild(rmNode);

    fm.form.submit();

  }

}





function qOnkeydown(e, el, url, id) {

  var keyNum=0;



  if(window.event) {

    keynum=event.keyCode;

  }else if(e.which){

    keynum=e.which;

  }

  if(keynum==13) {

    //el.form.attributes.getNamedItem('action').value=url;

    //el.form.submit();

    doTask('mod=basket&action=bag&id='+id+'&q='+el.value+'&params='+url);

    return false;

  } else {

    return true;

  }

}



function toggleDelivery(e) {



  if(!e) e=window.event;

  var el=e.target?e.target:e.srcElement;

  var div=document.getElementById('altdelivery');



  if(el.checked) disp='block';

  else disp='none';



  div.style.display=disp;

}



var currImg=null;

function tsi(e) {

  if(!e) e=window.event;

  var el=e.target?e.target:e.srcElement;



  /*clear prev item*/

  csi(false);



  /*set selected class*/

  el.parentNode.parentNode.className="thumb sel";



  currImg=el;

}

function csi(clr) {

  if(currImg) currImg.parentNode.parentNode.className="thumb";

  if(clr) currImg=null;

  return false;

}



function mpi(e) {

  csi(false);



  if(currImg) {

    // get the previous one

    var n;

    var p=currImg.parentNode.parentNode;

    if(p.previousSibling) {

      if(p.previousSibling.nodeType==3) n=p.previousSibling.previousSibling;

      else n=p.previousSibling;

    }



    currImg=n;

  } else {

    currImg = document.getElementById("thumbScroll").childNodes[0];

  }



  /*we're at the beginning, so get the last one*/

  if(!currImg) {

    currImg=document.getElementById("thumbScroll").childNodes[document.getElementById("thumbScroll").childNodes.length-1];

    if(currImg.nodeType==3)

      currImg=document.getElementById("thumbScroll").childNodes[document.getElementById("thumbScroll").childNodes.length-2];

  }



  if(currImg.nodeType==3) {

    currImg = document.getElementById("thumbScroll").childNodes[1];

  }

  if(currImg.childNodes[0].nodeType==3) {

    currImg = currImg.childNodes[1];

  } else {

    currImg = currImg.childNodes[0];

  }

  if(currImg.childNodes[0].nodeType==3) {

    currImg = currImg.childNodes[1];

  } else {

    currImg = currImg.childNodes[0];

  }



  currImg.parentNode.parentNode.className="thumb sel";

  var id=currImg.parentNode.name.replace("img","");

  doTask('mod=content&action=media&mediaType=image&img='+id);

}



function mni(e) {



  csi(false);



  if(currImg) {

    // get the next one

    var n;

    var p=currImg.parentNode.parentNode;

    if(p.nextSibling) {

      if(p.nextSibling.nodeType==3) n=p.nextSibling.nextSibling;

      else n=p.nextSibling;

    }



    currImg=n;

  } else {

    currImg = document.getElementById("thumbScroll").childNodes[0]

  }



  /*we're at the end, so get the first one*/

  if(!currImg) currImg = document.getElementById("thumbScroll").childNodes[0];



  if(currImg.nodeType==3) {

    currImg = document.getElementById("thumbScroll").childNodes[1];

  }

  if(currImg.childNodes[0].nodeType==3) {

    currImg = currImg.childNodes[1];

  } else {

    currImg = currImg.childNodes[0];

  }

  if(currImg.childNodes[0].nodeType==3) {

    currImg = currImg.childNodes[1];

  } else {

    currImg = currImg.childNodes[0];

  }



  currImg.parentNode.parentNode.className="thumb sel";

  var id=currImg.parentNode.name.replace("img","");

  doTask('mod=content&action=media&mediaType=image&img='+id);

}



function GetFlashContainerWidth() {

   return document.getElementById('flash-content').offsetWidth;

}



var size_set=false;

var force=false;



function toggleSizeChart(disp) {



  if(size_set && !force) return false;



  if(disp=='') {

    if(document.getElementById('size_chart').style.display=='none') disp='block';

    else disp='none';

  }



  document.getElementById('size_chart').style.display=disp;

  document.getElementById('size_x').style.display    =disp;



  force=false;



  return false;

}



function toggleMoreInfo() {

  var disp,add,selects;

  

  if(document.getElementById('more_info').style.display=='none') {

    disp='block';

    add='none';

    selects='none';

  } else {

    disp='none';

    add='block';

    selects='';

  }

  

  document.getElementById('more_info').style.display=disp;

  

  document.getElementById('item_add').style.display=add;

  

  if(selects=='none') {

    document.getElementById('addToBasket_option').className='selfield hidden';

    document.getElementById('addToBasket_itemQ').className='selfield hidden';

  } else {

    document.getElementById('addToBasket_option').className='selfield';

    document.getElementById('addToBasket_itemQ').className='selfield';

  }

  

  return false;

  

}



function sizeChanged(obj) {

  if(obj.value != "0") {

    toggleSizeChart('none');

    size_set=true;

  } else {

    size_set=false;

  }

}



// needed for flash

function LaunchMediaPopup(id) {

  /*var owin=window.open('/popup/content/?mediaType=video&vid='+id,'mediaPopup','status=0,toolbar=0,directories=0,menubar=0,height=370,width=585');

  try {

    owin.focus();

  }

  catch(e) {

  }*/

  window.location='/media/?mediaType=video&vid=20&delay=1';

}



function PositionSizeChart() {

  var page_width=document.body.offsetWidth;

  var flash_width=document.getElementById('flashMovie').offsetWidth;

  var size_width=document.getElementById('size_chart').offsetWidth;

  var rcol_width=document.getElementById('prsub').offsetWidth;

  var padding=20;

  var adjust=7;

  

  var flash_vis_width=(page_width > (flash_width+padding+rcol_width)) ? flash_width+14 : page_width-rcol_width-adjust;

  var lpos = flash_vis_width-size_width;

  

  document.getElementById('size_chart').style.left=lpos+'px';  

}



function PositionMoreInfoBox() {

  var page_width=document.body.offsetWidth;

  var flash_width=document.getElementById('flashMovie').offsetWidth;

  var info_width=document.getElementById('more_info').offsetWidth;

  var rcol_width=document.getElementById('prsub').offsetWidth;

  var padding=20;

  

  var flash_vis_width=page_width-rcol_width;

  var lpos = flash_vis_width-info_width;



  document.getElementById('more_info').style.left=lpos+'px';  

}



function faqPopup() {

  var el=document.getElementById('faqPopup');

  if(el.style.display!='block') {

    el.style.display='block';

    if(document.getElementById('item_add')) document.getElementById('item_add').style.display='none';

  } else {

    el.style.display='none';

    if(document.getElementById('item_add')) document.getElementById('item_add').style.display='block';

  }

}



function BHTAPopup() {

  var oh=document.getElementById('BHTAPopup');

  if(oh.style.display!='block') {

    oh.style.display='block';

    if(document.getElementById('item_add')) document.getElementById('item_add').style.display='none';

  } else {

    oh.style.display='none';

    if(document.getElementById('item_add')) document.getElementById('item_add').style.display='block';

  }

}