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 GetScrollY() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return [ scrOfY];
}

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 Go(ItemID){
       var theform;		
		if (isIE) {
			theform = document.forms[1];
			theform.submit();
		}
		else {
		    document.forms[1].submit();
		}
}

function PutDivInPostion(O1, x, y) {
    if (isIE) {
        O1.style.left = x;
        O1.style.top = y;
    }
    else {
        O1.style.left = x + "px";
        O1.style.top = y + "px";
    }
}

function Show(ToShow) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
    var x = mouseX;
    var y = mouseY - 10 - O1.offsetHeight + parseInt(GetScrollY());
    PutDivInPostion(O1, x, y);
}

function ShowAboveLeft(ToShow) {
    var O1 = document.getElementById(ToShow);
    O1.style.display = '';
    var x = mouseX - O1.offsetWidth - 10;
    var y = mouseY - 10 - O1.offsetHeight + parseInt(GetScrollY());
    PutDivInPostion(O1, x, y);
}

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 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(e){};
}

function getSubStock(ParentID, productID)
{
    AJAXFetchIntoDiv("/includes/ShowSubStock.aspx?productID=" + productID + "Parent=" + ParentID, "divChildrenStock", false);	
}
function Enlarge(Image) {
    AJAXFetchIntoDiv("/includes/ShowImage.aspx?Image=" + Image, "divMainImage", false);
}
function CheckEnlarge(obj) {
    var split = obj.value.split("^");
    if (split[1] && split[1] != "") Enlarge(split[1]);
}

function CheckStockAndImage(obj, productID) {
    if (obj.selectedIndex == 0) {
        PutInDiv("", "divShowChildrenStock");
    }
    else {
        var split = obj.value.split("^");
        var rand_no = Math.random();
        xmlhttp.open("GET", "/Includes/ShowSubStock.aspx?Rand=" + rand_no + "&productID=" + productID + "&parent=" + split[0], true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                PutInDiv(xmlhttp.responseText, "divShowChildrenStock");
                if (split[1] != "") Enlarge(split[1]);
            }
        }
        xmlhttp.send(null);
    }
}

//function CheckStockAndImage(parentID, image) {
//    xmlhttp.open("GET", "/Includes/ShowSubStock.aspx?parent=" + parentID, true);
//    xmlhttp.onreadystatechange = function() {
//        if (xmlhttp.readyState == 4) {
//            PutInDiv(xmlhttp.responseText, "divShowChildrenStock");
//            if (image != "") Enlarge(image);
//        }
//    }
//    xmlhttp.send(null);
//}

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;
}

function GetRequestStockPanel(stockID, fullName) {
    SetStockID(stockID);
    PutInDiv(fullName, 'spanSize');
    ShowAboveLeft('divStockRequest');
}
function SetStockID(stockID) {
    var O1 = document.getElementById('srStockID');
    O1.value = stockID;
}

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 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 CountOptionTotal() {
    var O1ID = "";
    var SBForm = document.getElementById('SBForm');
    var price = document.getElementById('Price').value;

    var url = "/Includes/AddUpSurcharge.aspx?Price=" + price + "&";
    for (var i = 0; i < SBForm.length; i++) {
        var name = SBForm[i].name;
        if (name && name.indexOf('Option') > -1) {
            url += i + "=" + SBForm[i].value + "&";
        }
    }

    AJAXFetchIntoDiv(url, "divPrice", false);
   
}

