function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function openNewWindow(file,width,height)
{
	var newwindow = open(file,"","width=" + width + ",height=" + height + ",taskbar=no, scrollbars=yes, menubar=no,left=200,top=200,screenX=0,screenY=0", "");
	if (!newwindow)
	{
		alert("Sorry, this window failed to open, make sure you haven't a pop-up blocker installed, and your Javascript settings are set correctly.");
	}
	newwindow.document.close();
}

function GetWindowWidth() {
    var myWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
    } else if (document.documentElement && (document.documentElement.clientWidth)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
    } else if (document.body && (document.body.clientWidth)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

isIE = document.all;

function Go(ItemID) {
    var theform;
    if (isIE) {
        theform = document.forms[0];
        theform.submit();
    }
    else {
        document.forms[0].submit();
        //theform = document.PostBack;
        //PostBack.submit();
    }
}

function CheckIfFaulty(ddl) {
    if (ddl[ddl.selectedIndex].value == "Faulty") {
        alert("If returning an item as faulty please describe the fault in the Exchange Request box.\n\nWe can only accept faulty returns with an adequate description.");
    }
}

function getMouseXY(e) {
    try {
        if (!e) e = window.event;
        if (e) {
            mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
            mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
        }
    }
    catch (err) { }
}
document.onmousemove = getMouseXY;

function GetRequestStockPanel2(e) {

    if (e.options[e.selectedIndex].text.lastIndexOf("X") == e.options[e.selectedIndex].text.length - 1) {
        SetStockID(e.options[e.selectedIndex].value);
        PutInDiv(e.options[e.selectedIndex].text, 'spanSize');
        if (isIE) {
            ShowInPositionWithScroll('divStockRequest', 200, 200);
        }
        else {
            ShowInPositionWithScroll('divStockRequest', 200, 200);
        }
    }
}

function GetRequestStockPanel(stockID, fullName) {

    SetStockID(stockID);
    PutInDiv(fullName, 'spanSize');
    if (isIE) {
        ShowInPositionWithScroll('divStockRequest', 700, 10);
    }
    else {
        ShowInPositionWithScroll('divStockRequest', 700, 10);
    }
}

function GetChildrenSizes(parentStock) {
    alert(parentStock);
}

function SwapImage(ImageID, NewSRC) {
    var Image = document.getElementById(ImageID);
    Image.src = NewSRC;
}

function Show(ToShow) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';

    O1.style.left = mouseX;
    O1.style.top = (mouseY + getScroll()) - O1.offsetHeight;
}

function MakeVisible(ToShow) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
}

function ShowBelow(ToShow) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';

    O1.style.left = 50 + "px";
    O1.style.top = mouseY + 10 + "px";
}


function ShowInPosition(ToShow, x, y) // For Firefox primarily
{
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
    O1.style.left = x + "px";
    O1.style.top = y + "px";
}

function ShowInPosition(ToShow, x) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
    O1.style.left = x + "px";
    if (isIE) {
        O1.style.top = (mouseY - 300 + getScroll()) + "px";
    }
    else {
        O1.style.top = (mouseY - 270) + "px";
    }
}

function ShowInPositionWithScroll(ToShow, x, y) // For Firefox primarily
{
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
    O1.style.left = x + "px";
    O1.style.top = (y + getScroll()) + "px";
}

function ShowAboveLeft(ToShow) // Tweaked positions (mainly for IE7) 26-08-06
{
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
    
    O1.style.left = (mouseX - 10) + "px";
    O1.style.top = (mouseY + getScroll() - O1.offsetHeight) + "px";
}

function ShowLeft(ToShow)
{
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';


    O1.style.left = (mouseX - 10);
    O1.style.top = (mouseY + getScroll() - (O1.offsetHeight / 2));
}

function getScroll() {
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop
    else if (document.body.scrollTop) return document.body.scrollTop;
    else return 0;
}

function ShowBelowMiddle(ToShow) // Tweaked positions (mainly for IE7) 26-08-06
{
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';

    O1.style.left = (mouseX - (O1.offsetWidth/2) - 10) + "px";
    O1.style.top = (mouseY + getScroll() + 10) + "px";
}

function ShowTop(ToShow)
{
	var O1 = document.getElementById(ToShow);
    O1.style.display = '';      
    O1.style.left = 50 + "px";
    O1.style.top = (50 + getScroll()) + "px";
}
     
      
function Hide(ToShow)
{
	var O1 = document.getElementById(ToShow);
	O1.style.display = 'none';			
}

function SendData()
{
	var O1 = document.getElementById('PID');
	var O2 = document.getElementById('YourName');
	var O3 = document.getElementById('YourEmail');
	var O4 = document.getElementById('FriendsName');
	var O5 = document.getElementById('FriendsEmail');
	var O6 = document.getElementById('Message');
	var URL = "/SendData.aspx?PID=" + URLEncode(O1.value) + 
	"&YourName=" + URLEncode(O2.value) + 
	"&YourEmail=" + URLEncode(O3.value) +
	"&FriendsName=" + URLEncode(O4.value) +
	"&FriendsEmail=" + URLEncode(O5.value) +
	"&Message=" + URLEncode(O6.value);
	AJAXFetch(URL);
	alert("Thankyou. Your message has been sent");
	Hide("EmailToFriendDiv");	
}

function LimitedCollectionSignup()
{
	var O2 = document.getElementById('YourName');
	var O3 = document.getElementById('YourEmail');

	if (O2.value.length < 1 || O2.value == "enter your name here..") alert("Please enter your name.");
	else if (O3.value.length < 3 || O3.value == "enter your email here..") alert("Please enter a valid email address.");
	else{
	    var URL = "/SendData.aspx?Action=LimitedCollectionSignup" + 
	    "&Name=" + URLEncode(O2.value) + 
	    "&Email=" + URLEncode(O3.value);
	    AJAXFetch(URL);
	    alert("Thankyou. We have signed you up.");
	    Hide("divSlideInContainer");	
	}
}

function SetStockID(stockID) {
    var O1 = document.getElementById('srStockID');
	O1.value = stockID;
}
	
function SendStockRequest() {
	var O1 = document.getElementById('srStockID');
	var O2 = document.getElementById('srYourName');
	var O3 = document.getElementById('srYourEmail');
	var O4 = document.getElementById('srComingSoon');
	var URL = "/SendData.aspx?Action=StockRequest" +
	"&StockID=" + URLEncode(O1.value) +
	"&YourName=" + URLEncode(O2.value) +
	"&YourEmail=" + URLEncode(O3.value);
	if (O4) URL += "&ComingSoon=" + URLEncode(O4.value);
	AJAXSend(URL);	
	alert("Thankyou. Your request has been received.");
	Hide("divStockRequest");	
}

function SetAsViewed() {
    try {
        var O1 = document.getElementById('PID');
        var O2 = document.getElementById('User');

        var URL = "/SendData.aspx?SetAsViewed=True" +
	"&PID=" + URLEncode(O1.value) +
	"&User=" + URLEncode(O2.value);
        AJAXFetch(URL);
    }
    catch (err) { }
}


function getSubStock(ParentID)
{ 
    AJAXFetchIntoDiv("/includes/ShowSubStock.aspx?Parent=" + ParentID, "Step1", false);	
}

function CheckSizes(Size)
{
    var SBForm = document.getElementById('SBForm');
    var radioLength = SBForm.length;
   	        	
	for(var i = 0; i < SBForm.length; i++) {
	
	    if (SBForm[i].name == "TheLength")
	    {
	        var theValue = SBForm[i].value;
	        
	        var O1 = document.getElementById(Size + " > " + theValue);
    		
		    if (O1) // Stock Size Exists
		    {
    		    SBForm[i].disabled = false;
		        SBForm[i].className = '';
		    }
		    else  // Stock Size Doesnt Exists
		    {		
		        SBForm[i].disabled = true;
		        SBForm[i].className = 'NoStockBox';
		    }
	    }	
	}
}

function SelectLength(theSID)
{
    var O1ID = "";
   var SBForm = document.getElementById('SBForm');
    
    for(var i = 0; i < SBForm.length; i++) {
	
	    if (SBForm[i].checked)
	    {
	        if (O1ID == "") O1ID =  SBForm[i].value + " > "
	        else O1ID += SBForm[i].value;   
	    }
	}
	
	var O1 = document.getElementById(O1ID);
   
    if (O1) // Stock Size Exists
	{
         var SID = document.getElementById("SID");
        SID.value = O1.value;
	} 		
}

function GetAjaxImage(hiddenDiv, imageHTML, Top) {
    document.getElementById(hiddenDiv).innerHTML = URLDecode(imageHTML);	
    if (Top == "Top")
    {
        ShowTop(hiddenDiv);
    }
    else Show(hiddenDiv);
}

function AJAXInputCommand(inputBoxID, argument, command, guid) {
    var inputBox = document.getElementById(inputBoxID);
    var theValue = inputBox.value;
    var url = "/Admin/AJAX/AjaxCommand.aspx?GUID=" + guid + "&Command=" + command + "&Argument=" + argument + "&Value=" + theValue;
    AJAXSend(url);
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
    output = output.replace('+', ' ');
  }
  return output;
}


function AddToWishList() {
    document.getElementById("Wishlist").value = "True";
    document.SBForm.submit();
}

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

var clicked = "False";

function HideIfValid(div) {
    if (div == "AddButton") clicked = "True";
    else {
        if (clicked == "True") {
            clicked = "False";
        }
        else {
            Hide('divAddedToBasket');
        }
    }
}
