//Check typing in textbox, only numbers, comma and dot allowed, usage: txbAnything.Attributes["onkeypress"] = "decCheck(event)";
function decCheck(evt)
{
    //Get the event
    var theEvent = evt || window.event;
    
    //Find the keycode
    var key = theEvent.keyCode || theEvent.which;
    
    //If key is backspace, tab, enter, left arrow, right arrow, delete or end then return
    if (key == 8 || key == 9 || key == 13 || key == 37 || key == 39 || key == 46 || key == 35) return;
    
    //Convert the keycode to a string
    key = String.fromCharCode( key );
    
    //Declare the regex pattern
    var regex = /[0-9]|\.|\,/;
    
    //If key test is false then return false
    if(!regex.test(key))
    {
        theEvent.returnValue = false;
        if (theEvent.preventDefault) theEvent.preventDefault();
    }
}

//Check typing in textbox, only numbers allowed, usage: txbAnything.Attributes["onkeypress"] = "numCheck(event)";
function numCheck(evt)
{
    //Get the event
    var theEvent = evt || window.event;
    
    //Find the keycode
    var key = theEvent.keyCode || theEvent.which;
    
    //If key is backspace, tab, enter, left arrow, right arrow, delete or end then return
    if (key == 8 || key == 9 || key == 13 || key == 37 || key == 39 || key == 46 || key == 35) return;
    
    //Convert the keycode to a string
    key = String.fromCharCode( key );
    
    //Declare the regex pattern
    var regex = /[0-9]/;
    
    //If key test is false then return false
    if(!regex.test(key))
    {
        theEvent.returnValue = false;
        if (theEvent.preventDefault) theEvent.preventDefault();
    }
}

function LaunchApp(strCmdLine)
{
    //var obj = new ActiveXObject("LaunchinIE.Launch");
    //obj.LaunchApplication(strCmdLine);
    var ws = new ActiveXObject("WScript.Shell");
    ws.Run('"' + strCmdLine + '"');
}

function OpenDoc(strDoc)
{
    //var obj = new ActiveXObject("LaunchinIE.Launch");
    //obj.ShellExecute("open", strDoc);
    var ws = new ActiveXObject("WScript.Shell");
    ws.Run('"' + strDoc + '"');
}

function numOnly(e)
{
   var key = window.event ? e.keyCode : e.which;
   var keychar = String.fromCharCode(key);
   reg = /\D/;
   return !reg.test(keychar);
}

function GetXmlHttpObject(handler)
{ 
    var objXmlHttp=null
    if (navigator.userAgent.indexOf("MSIE")>=0)
    { 
        var strName="Msxml2.XMLHTTP"
        if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
        {
            strName="Microsoft.XMLHTTP"
        } 
        try
        { 
            objXmlHttp=new ActiveXObject(strName)
            objXmlHttp.onreadystatechange=handler 
            return objXmlHttp
        } 
        catch(e)
        { 
            alert("Error. Scripting for ActiveX might be disabled") 
            return 
        } 
    } 
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
        objXmlHttp=new XMLHttpRequest()
        objXmlHttp.onload=handler
        objXmlHttp.onerror=handler 
        return objXmlHttp
    }
}

//Global stock text vars
var currentStockLabelID = "";
var currentProdNoLabelID = "";
var currentPriceLabelID = "";
var currentDiscPriceLabelID = "";
var currentOrigPriceLabelID = "";
var currentItemID = "0";
var currentDim1Value = "0";
var currentDim2Value = "0";
var currentDim3Value = "0";
var currentLID = "3";
var currentCurrencyID = "0";
var currentInclVAT = "True";
var currentUserID = "0";
var currentImageWidth = "80";
var currentCrop = "crop=none";


function UpdateDimProd(stockLabelID, prodNoLabelID, priceLabelID, discPriceLabelID, origPriceLabelID, itemID, dim1ID, dim2ID, dim3ID, lid, cid, incVat, userID, imgWidth, crop)
{
    //Find the dropdowns
    var dim1 = document.getElementById(dim1ID);
    var dim2 = document.getElementById(dim2ID);
    var dim3 = document.getElementById(dim3ID);
    
    //Find all the values
    var dim1value = '0';
    if (dim1) dim1value = dim1.value;
    
    var dim2value = '0';
    if (dim2) dim2value = dim2.value;
    
    var dim3value = '0';
    if (dim3) dim3value = dim3.value;
    
    //Set the global vars
    currentStockLabelID = stockLabelID;
    currentProdNoLabelID = prodNoLabelID;
    currentPriceLabelID = priceLabelID;
    currentDiscPriceLabelID = discPriceLabelID;
    currentOrigPriceLabelID = origPriceLabelID;
    currentItemID = itemID;
    currentDim1Value = dim1value;
    currentDim2Value = dim2value;
    currentDim3Value = dim3value;
    currentLID = lid;
    currentCurrencyID = cid;
    currentInclVAT = incVat;
    currentUserID = userID;
    currentImageWidth = imgWidth;
    currentCrop = crop;
    
    //Fetch values
    GetProdNo();
    GetStockText();
    GetPrice();
    GetDiscountPrice();
    GetOriginalPrice();
    GetImageURL();
}

function GetStockText()
{
    var stockLabel = document.getElementById(currentStockLabelID);
    if (stockLabel != null)
    {
        //Set the wait indicator
        stockLabel.innerHTML = '<img src="./Startup/Pics/wait3.gif" alt="loading..." />';
        
        //Build the URL
        var url = "./Controls/Toolbox/GetStockText.aspx?ItemID=" + currentItemID + "&Dim1=" + currentDim1Value + "&Dim2=" + currentDim2Value + "&Dim3=" + currentDim3Value + "&LID=" + currentLID;
        
        //Perform the request
        xmlHttp1 = GetXmlHttpObject(GetStockTextReturn);
        xmlHttp1.open("GET", url, true);
        xmlHttp1.send(null);
    }
}

function GetStockTextReturn() 
{
    //If xml http req is ready then inject
    if (xmlHttp1.readyState == 4 || xmlHttp1.readyState == "complete")
    {
        var stockLabel = document.getElementById(currentStockLabelID);
        
        if (stockLabel != null)
            stockLabel.innerHTML = xmlHttp1.responseText;
    }
}

function GetProdNo()
{
    var prodNoLabel = document.getElementById(currentProdNoLabelID);
    if (prodNoLabel != null)
    {
        //Set the wait indicator
        prodNoLabel.innerHTML = '<img src="./Startup/Pics/wait3.gif" alt="loading..." />';
        
        //Build the URL
        var url = "./Controls/Toolbox/GetProdNo.aspx?ItemID=" + currentItemID + "&Dim1=" + currentDim1Value + "&Dim2=" + currentDim2Value + "&Dim3=" + currentDim3Value;
        
        //Perform the request
        xmlHttp2 = GetXmlHttpObject(GetProdNoReturn);
        xmlHttp2.open("GET", url, true);
        xmlHttp2.send(null);
    }
}

function GetProdNoReturn()
{
    //If xml http req is ready then inject
    if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == "complete")
    {
        var prodNoLabel = document.getElementById(currentProdNoLabelID);
        
        if (prodNoLabel != null)
            prodNoLabel.innerHTML = xmlHttp2.responseText;
    }
}

function GetPrice()
{
    var priceLabel = document.getElementById(currentPriceLabelID);
    if (priceLabel != null)
    {
        //Set the wait indicator
        priceLabel.innerHTML = '<img src="./Startup/Pics/wait3.gif" alt="loading..." />';
        
        //Build the URL
        var url = "./Controls/Toolbox/GetPrice.aspx?ItemID=" + currentItemID + "&Dim1=" + currentDim1Value + "&Dim2=" + currentDim2Value + "&Dim3=" + currentDim3Value + "&LID=" + currentLID + "&CurrencyID=" + currentCurrencyID + "&InclVAT=" + currentInclVAT + "&UserID=" + currentUserID;
        
        //Perform the request
        xmlHttp3 = GetXmlHttpObject(GetPriceReturn);
        xmlHttp3.open("GET", url, true);
        xmlHttp3.send(null);
    }
}

function GetPriceReturn()
{
    //If xml http req is ready then inject
    if (xmlHttp3.readyState == 4 || xmlHttp3.readyState == "complete")
    {
        var priceLabel = document.getElementById(currentPriceLabelID);
        
        if (priceLabel != null)
            priceLabel.innerHTML = xmlHttp3.responseText;
    }
}

function GetDiscountPrice()
{
    var discPriceLabel = document.getElementById(currentDiscPriceLabelID);
    if (discPriceLabel != null)
    {
        //Set the wait indicator
        discPriceLabel.innerHTML = '<img src="./Startup/Pics/wait3.gif" alt="loading..." />';
        
        //Build the URL
        var url = "./Controls/Toolbox/GetPrice.aspx?ItemID=" + currentItemID + "&Dim1=" + currentDim1Value + "&Dim2=" + currentDim2Value + "&Dim3=" + currentDim3Value + "&LID=" + currentLID + "&CurrencyID=" + currentCurrencyID + "&InclVAT=" + currentInclVAT + "&UserID=" + currentUserID + "&Type=discount";
        
        //Perform the request
        xmlHttp4 = GetXmlHttpObject(GetDiscountPriceReturn);
        xmlHttp4.open("GET", url, true);
        xmlHttp4.send(null);
    }
}

function GetDiscountPriceReturn()
{
    //If xml http req is ready then inject
    if (xmlHttp4.readyState == 4 || xmlHttp4.readyState == "complete")
    {
        var discPriceLabel = document.getElementById(currentDiscPriceLabelID);
        
        if (discPriceLabel != null)
            discPriceLabel.innerHTML = xmlHttp4.responseText;
    }
}

function GetOriginalPrice()
{
    var origPriceLabel = document.getElementById(currentOrigPriceLabelID);    
    if (origPriceLabel != null)
    {
        //Set the wait indicator
        origPriceLabel.innerHTML = '<img src="./Startup/Pics/wait3.gif" alt="loading..." />';
        
        //Build the URL
        var url = "./Controls/Toolbox/GetPrice.aspx?ItemID=" + currentItemID + "&Dim1=" + currentDim1Value + "&Dim2=" + currentDim2Value + "&Dim3=" + currentDim3Value + "&LID=" + currentLID + "&CurrencyID=" + currentCurrencyID + "&InclVAT=" + currentInclVAT + "&UserID=" + currentUserID + "&Type=original";
        
        //Perform the request
        xmlHttp5 = GetXmlHttpObject(GetOriginalPriceReturn);
        xmlHttp5.open("GET", url, true);
        xmlHttp5.send(null);
    }
}

function GetOriginalPriceReturn()
{
    //If xml http req is ready then inject
    if (xmlHttp5.readyState == 4 || xmlHttp5.readyState == "complete")
    {
        var origPriceLabel = document.getElementById(currentOrigPriceLabelID);
        if (origPriceLabel != null)
            origPriceLabel.innerHTML = xmlHttp5.responseText;
    }
}

function GetImageURL()
{
    var imgImage = document.getElementById("img" + currentItemID);
    if (imgImage != null)
    {
        //Build the URL
        var url = "./Controls/Toolbox/GetImageID.aspx?ItemID=" + currentItemID + "&Dim1=" + currentDim1Value + "&Dim2=" + currentDim2Value + "&Dim3=" + currentDim3Value;
        
        //Perform the request
        xmlHttp6 = GetXmlHttpObject(GetImageURLReturn);
        xmlHttp6.open("GET", url, true);
        xmlHttp6.send(null);
    }
}

function GetImageURLReturn()
{
    //If xml http req is ready then inject
    if (xmlHttp6.readyState == 4 || xmlHttp6.readyState == "complete")
    {
        var imgImage = document.getElementById("img" + currentItemID);
        if (imgImage != null)
        {
            try
            {
                var iDimImageID = parseInt(xmlHttp6.responseText);
                if (iDimImageID > 0)
                {
                    imgImage.src = "ViewImage.aspx?ItemID=" + iDimImageID.toString() + "&width=" + currentImageWidth + "&" + currentCrop;
                }
            }
            catch (Err) { }
        }
    }
}

function openImageWindow(itemID)
{
	win = window.open('Image.aspx?itemID='+itemID,'Preview', 'width=600,height=400,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0'); 
	win.focus;			
	
}
			
function openFileWindow(itemID)
{
	win = window.open('ViewFile.aspx?itemID='+itemID,'Preview', 'width=650,height=650,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=0'); 
	win.focus;			
}

function openLinkWindow(itemID)
{
	win = window.open('Redirect.aspx?itemID='+itemID,'Preview', 'width=650,height=650,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=0'); 
	win.focus;			
}

function SwitchArea(id) {

         if(!document.getElementById) return;

         var Area= document.getElementById(id);

         if(Area.className=='Area_on') {

                 Area.className='Area_off';

         }

         else if(Area.className=='Area_off') {

                 Area.className = 'Area_on';

         }

}



