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();
}

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 GetRequestStockPanel(stockID, fullName) {

    SetStockID(stockID);
    PutInDiv(fullName, 'spanSize');
    if (isIE) {
        Show('divStockRequest');
    }
    else {
        ShowInPosition('divStockRequest', window.innerWidth/2, 190);
    }
}

function GetChildrenSizes(parentStock) {
    alert(parentStock);
}

function Show(ToShow) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';

    O1.style.left = mouseX;
    O1.style.top = mouseY - O1.offsetHeight;
}

function ShowInPosition(ToShow, x, y) // For Firefox primarily
{
    var O1 = document.getElementById('divStockRequest');
    O1.style.display = '';
    O1.style.left = x + "px";
    O1.style.top = y + "px";
}

function ShowAboveLeft(ToShow) // Tweaked positions (mainly for IE7) 26-08-06
{
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';

    if (isIE) {
        O1.style.left = e.pageX - O1.offsetWidth + 130;
        O1.style.top = e.pageY - O1.offsetHeight + 385;
    }
}

function ShowTop(ToShow)
{
	var O1 = document.getElementById(ToShow);
    O1.style.display = '';      
    O1.style.left = 50 + "px";
    O1.style.top = (50 + document.body.scrollTop) + "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 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 URL = "/SendData.aspx?Action=StockRequest" +  
	"&StockID=" + URLEncode(O1.value) +
	"&YourName=" + URLEncode(O2.value) + 
	"&YourEmail=" + URLEncode(O3.value);
	AJAXSend(URL);	
	alert("Thankyou. Your request has been received.");
	Hide("divStockRequest");	
}

function SetAsViewed()
{
	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);
}


function getSubStock(ParentID)
{ 
    AJAXFetchIntoDiv("/includes/ShowSubStock.aspx?Parent=" + ParentID, "Step1", false);	
}


function HideAll()
{
if(document.getElementById('div1')) Hide("div1");
if(document.getElementById('div2')) Hide("div2");
if(document.getElementById('div3')) Hide("div3");
if(document.getElementById('div4')) Hide("div4");
if(document.getElementById('div5')) Hide("div5");
if(document.getElementById('div6')) Hide("div6");
if(document.getElementById('div7')) Hide("div7");
if(document.getElementById('div8')) Hide("div8");
if(document.getElementById('div9')) Hide("div9");
}
	

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 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;
}

