function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}
function checkMouseEnter (element, evt) {
  if (element.contains && evt.fromElement) {
    return !element.contains(evt.fromElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}
function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}
function changeColour(element, current, endat)
{
    el = document.getElementById(element);
    var curCol = '#' + toHex(current) + toHex(current) + toHex(current);
    el.style.backgroundColor = curCol;
    if ( current < endat ) 
        current += 1;
    else
        current -= 1;
    if ( current != endat )
    {
        setTimeout('changeColour("'+element.toString()+'",'+current.toString()+','+endat.toString()+');', 50);
    }
}
function toHex(dec) 
{
    // create list of hex characters
    var hexCharacters = "0123456789ABCDEF"
    // if number is out of range return limit
    if (dec < 0)
            return "0"
    if (dec > 16)
            return "F"
    // decimal equivalent of second hex character in converted number
    var i = dec % 16
    // return hexadecimal equivalent
    return hexCharacters.charAt(i)
}
function closePopUp()
{
    sels = document.getElementsByTagName('select');
    for ( selI = 0; selI < sels.length; selI++ )
    {
        sels[selI].style.visibility = 'visible';
    }
    el = document.getElementById('poptpop');
    el.style.display = 'None';
    changeOpacity('poptBc', 8, 0)
    el = document.getElementById('ctl00_ContentPlaceHolder1_selectDD');
    if ( el != null )
        el.style.visibility = 'visible';
}
function showPopUp(id)
{
    reSizePopUp(700, 218);
    changeOpacity('poptBc', 0, 8);
    getData(id);
}
function swapInformation(id)
{
    reSizePopUp(700, 218);
    getData(id);
}
function reSizePopUp(theWidth, theHeight)
{
    sels = document.getElementsByTagName('select');
    for ( selI = 0; selI < sels.length; selI++ )
    {
        sels[selI].style.visibility = 'hidden';
    }
    var winWidth, winHeight, d=document, winClient, scrollTop;
    
    if (typeof window.scrollWidth!='undefined') {
        winWidth = window.scrollWidth;
        winHeight = window.scrollHeight;
        winClient = window.innerHeight;
        scrollTop = window.scrollTop;
    } else if ( d.documentElement
           && typeof d.documentElement.scrollWidth!='undefined'
            && d.documentElement.clientWidth!=0 ) {
                winWidth = d.documentElement.scrollWidth;
                winHeight = d.documentElement.scrollHeight;
                winClient = d.documentElement.clientHeight;
                scrollTop = d.documentElement.scrollTop;
                if ( scrollTop == 0 )
                {
                    // Test to see if a Safari browser!
                    if ( d.body.scrollTop )
                        scrollTop = d.body.scrollTop;
                }
                //alert ( winWidth + ":" + winHeight + ":" +winClient + ":" +scrollTop);
    } else if ( d.body
           && typeof d.body.scrollWidth!='undefined') {
                winWidth = d.body.scrollWidth;
                winHeight = d.body.scrollHeight;
                winClient = d.body.clientHeight;
                scrollTop = d.body.scrollTop;
    }
    el = document.getElementById('poptBc');
    el.style.display = 'Block';
    el.style.height = winHeight+"px";
    el.style.width = winWidth+"px";
    
    el = document.getElementById('poptpop');
    el.style.display = 'Block';
    topwidth = winWidth /2 - (theWidth/2);
    topheight = winClient /2 - (theHeight/2) + scrollTop;
    el.style.width = theWidth + "px";
    el.style.left= topwidth + "px";
    el.style.top = topheight + "px";
    //el.style.height = theHeight+"px";
    el = document.getElementById('ctl00_ContentPlaceHolder1_selectDD');
    if ( el != null )
        el.style.visibility = 'hidden';
}
function changeOpacity(element, current, endat)
{
    var winHeight, d=document;
    
    if (typeof window.scrollWidth!='undefined') {
        winHeight = window.scrollHeight;
    } else if ( d.documentElement
           && typeof d.documentElement.scrollWidth!='undefined'
            && d.documentElement.clientWidth!=0 ) {
                winHeight = d.documentElement.scrollHeight;
    } else if ( d.body
           && typeof d.body.scrollWidth!='undefined') {
                winHeight = d.body.scrollHeight;
                }
    if ( winHeight > 1700 )
        current = endat;

    el = document.getElementById(element);
    el.style.filter = "alpha(opacity="+current*10+")";
    el.style.opacity = "0." + current;
    if ( current < endat ) 
        current += 1;
    else if ( current > endat ) 
        current -= 1;
    if ( current != endat )
    {
        setTimeout('changeOpacity("'+element.toString()+'",'+current.toString()+','+endat.toString()+');', 25);
    }
    else if ( current <= 0 )
    {
        el = document.getElementById('poptBc');
        el.style.display = 'None';
    }
}
function serverHtmlReturn(args,context)
{
   //Expecting the INNERHTML followed by ##Full##You have 1 Item in fred##£ 198.00##(inc £ 214.23)
   // to be shipped back.  If ## does not exist, assume just textual body.
   if ( args.length > 0 )
   {
       strArr = args.split("##");
       document.getElementById("poptpop").innerHTML = strArr[0];
       
       if ( strArr.length > 1 )
       {
           testEL = document.getElementById("bhTxtI");
           if ( testEL != null )
           {
               testEL.src = '/images/basket'+strArr[1]+'.jpg';
               document.getElementById("bhTxtH").innerHTML = strArr[2];
               document.getElementById("bhTxtP").innerHTML = strArr[3];
               document.getElementById("bhTxtV").innerHTML = strArr[4];
           }
       }
   }
}
    
function serverHtmlError(args,context)
{
    document.getElementById("poptpop").innerText = "An error occured whilst communicating. Please retry";
}
function showLarger()
{
    el = document.getElementById('poptpop');
    el.style.top = "0px";
    el = document.getElementById("popt1iX");
    if ( el.style.display == 'none' )
    {
        el.style.display = 'block';
        el = document.getElementById("popt1i");
        el.style.display = 'none';
        reSizePopUp(800,645);
    }
    else
    {
        el.style.display = 'none';
        el = document.getElementById("popt1i");
        el.style.display = 'block';
        reSizePopUp(700, 218);
    }
    
}
function addToBasket(id)
{
    document.getElementById("poptpop").innerHTML = "<center><img src='/Images/wait.gif' alt='Waiting...' /></center>";
    serverHtmlRequest("A" + id.toString(),2);
}
function getData(id)
{
    document.getElementById("poptpop").innerHTML = "<center><img style='padding-top:80px' src='/Images/wait.gif' alt='Waiting...' /></center>";
    serverHtmlRequest("P" + id.toString(),1);
}
function getPartHistory(id)
{
    getDataFromServer(id, "H");
}
function basketHistory(id)
{
    getDataFromServer(id, "B");
}
function customerHistory(id)
{
    getDataFromServer(id, "C");
}
function basketNotes(id)
{
    getDataFromServer(id, "N");
}
var lastClickedRow;
function sortHighlighting(highLightRow)
{
    if ( lastClickedRow != null )
        lastClickedRow.parentNode.parentNode.style.background = "";
    lastClickedRow = highLightRow;
    if ( lastClickedRow != null )
        lastClickedRow.parentNode.parentNode.style.background = "#eeaaaa";
}
function getPH(sender, ID)
{
    sortHighlighting(sender);       
    reSizePopUp(700, 550);
    changeOpacity('poptBc', 0, 8);
    document.getElementById("poptpop").innerHTML = "<center><img style='padding-top:80px' src='/Images/wait.gif' alt='Waiting...' /></center>";
    PageMethods.getPartHistory(ID, "Totals", phCreateTable, vhPerformSearchFailure);   
}
function getSI(sender, suggested, ID)
{
    sortHighlighting(sender);
    reSizePopUp(700, 550);
    changeOpacity('poptBc', 0, 8);
    document.getElementById("poptpop").innerHTML = "<center><img style='padding-top:80px' src='/Images/wait.gif' alt='Waiting...' /></center>";
    PageMethods.getSuppInfo(ID, suggested, phGenBack, vhPerformSearchFailure);   
}
function getPHYear(ID, Year)
{
    //document.getElementById("poptpop").innerHTML = "<center><img style='padding-top:80px' src='/Images/wait.gif' alt='Waiting...' /></center>";
    PageMethods.getPartHistory(ID, Year, phCreateTable, vhPerformSearchFailure);   
}
function getMove(ID)
{
    reSizePopUp(700, 550);
    changeOpacity('poptBc', 0, 8);
    document.getElementById("poptpop").innerHTML = "<center><img style='padding-top:80px' src='/Images/wait.gif' alt='Waiting...' /></center>";
    PageMethods.getMovement(ID, phGenBack, vhPerformSearchFailure);   
}
function vhPerformSearchSuccess(result, eventArgs)
{
    alert(result);
}
function phCreateTable(result, eventArgs)
{
    html = result.substring(0, result.indexOf('###'));
    java = result.substring(result.indexOf('###') + 3, result.length);
    document.getElementById("poptpop").innerHTML = html;
    eval(java);
    
}
function phGenBack(result, eventArgs)
{
    document.getElementById("poptpop").innerHTML = result;
}
function vhPerformSearchFailure(error)
{
    // On a failed search, display an error message to the user
    alert("An unexpected error has occured whilst searching. Please retry.");
}
function getDataFromServer(id, code)
{
    reSizePopUp(700, 300);
    changeOpacity('poptBc', 0, 8);
    document.getElementById("poptpop").innerHTML = "<center><img src='/Images/wait.gif' alt='Waiting...' /></center>";
    serverHtmlRequest(code + id.toString(),3);
}
function saveBasketNotes(id)
{
    var bNotes = document.getElementById('txtArea1');
    serverHtmlRequest("S" + id + "#" + bNotes.value );
    closePopUp();
    // Change the text of the hyperlink depending upon the state of the basket notes field
    noteElement = document.getElementById('bnoteS' + id);
    if (bNotes.value.trim() == "" || bNotes.value.trim() == "No notes exist for this basket")
        noteElement.innerText = "Create some basket notes";
    else
        noteElement.innerText = "View basket notes";
}
var offX = 0;
var offY = 0;
function createExploded(id, imID)
{
    // Variable explodedString is expected to exist after a document.registerscript!
    el = document.getElementById(id);
    // imID is the image tag, needed for X and Y placement!
    elIM = document.getElementById(imID);
    getPosition(elIM);
    offX -= 10;
    offY -= 21;
    if (explodedString != "")
    {
        mySplit = explodedString.split(",");
        for ( i = 0; i < mySplit.length ; i += 3 )
        {
            var newP = document.createElement('p');
            var pNum = mySplit[i];
            if ( pNum.substr(0,1) == "#" )
            {
                pNum = pNum.substr(1);
                newP.style.background = "Red";
            }
            newP.id = 'exPl';
            var newHref = document.createElement('a');
            newP.style.left = parseInt(offX)  + parseInt(mySplit[i+1]) + "px";
            newP.style.top = parseInt(offY)  + parseInt(mySplit[i+2]) + "px";
            newHref.href = "javascript:showPopUp("+pNum+");";
            newHref.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
            newP.appendChild(newHref);
            el.appendChild(newP);
        }
    }
}
function getPosition(ele)
{
    offX += ele.offsetLeft;
    offY += ele.offsetTop;
    if ( ele.parentElement != null )
        getPosition(ele.parentElement);
}
function repToggle(element)
{
    var el = document.getElementById(element);
    if ( el.style.display == 'none' )
        el.style.display = 'block';
    else
        el.style.display = 'none';
}
