﻿String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}; 
function mynRatingHover(img)
{
    try
    {
    
        var imgID = img.id;
        var idx = imgID.substr(imgID.length - 1,1);
        var baseStr = imgID.substr(0,imgID.length - 1);        
        var hostURL = getHostUrl(); 
        for (i=1 ; i < 6; i++)
        {
            var cImgId = baseStr + i;
            var cImg = document.getElementById(cImgId);
            if (cImg)
            {
                if (i > idx)
                {
                    cImg.src = hostURL + "images/starempty.gif";
                }
                else
                {
                    cImg.src = hostURL + "images/starhover.gif";
                }
            }
        }
    }
    catch(err) {        
        //no action
    }
}
function mynRatingReset(img)
{
    try
    {
    
        var imgID = img.id;
        var idx = imgID.substr(imgID.length - 1,1);
        var baseStr = imgID.substr(0,imgID.length - 1);        
        var hostURL = getHostUrl(); 
        for (i=1 ; i < 6; i++)
        {
            var cImgId = baseStr + i;
            var cImg = document.getElementById(cImgId);
            if (cImg)
            {
                cImg.src = hostURL + "images/starempty.gif";
            }
        }        
    }
    catch(err)    
    {
        //no action
    }
}
function updateRating(siteID,userName,prodCode,img) 
{ 
    try
    {
        
        var imgID = img.id;
        window.baseImgID = imgID.substr(0,imgID.length - 1);        
        var idx = imgID.substr(imgID.length - 1,1);    
        if (!window.XMLHttpRequest)
        {
            window.XMLHttpRequest = function()
            {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }    
        window.xr = new XMLHttpRequest();
        var hostURL = getHostUrl(); 
        var openString = hostURL + "Services.asmx/UpdateRating?siteID=" + siteID + "&Username=" + userName + "&StockCode=" + prodCode + "&Rating=" + idx;        
        window.xr.open("GET", openString);
        window.xr.onreadystatechange = UpdateRatingComplete;
        window.xr.send(null);
        /*
        progressDisplay.style.display = "";
        itemQuantityDisplay.style.display = "none";
        */
    }
    catch(err)    
    {
        var em = "";
        //no action
    }    
}
function checkCode(siteID,lookUpField,lookUpTable,lookUpCondition,tbx)
{
    try {        
        tbx.value = tbx.value.replace(/^\s+|\s+$/g, "");
        if ((tbx.value == null) || tbx.value == '') {
            //alert("Check Code Fail 1");
            return;
        }
        if (!window.XMLHttpRequest)
        {
            window.XMLHttpRequest = function()
            {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }    
        window.xr = new XMLHttpRequest();
        var hostURL = getHostUrl();
        var openString = hostURL + "Services.asmx/CheckLookupCode?siteID=" + siteID + "&LookUpField=" + lookUpField + "&LookUpTable=" + lookUpTable + "&LookUpCondition=" + lookUpCondition + "&CodeToCheck=" + tbx.value + "&ReqId=" + tbx.id;
        //alert("open string is " + openString);
        window.xr.open("GET", openString);
        window.xr.onreadystatechange = lookupComplete;
        window.xr.send(null);
    }
    catch(err)    
    {
        var em = "";
        alert("Error");
        //no action
    }    
}
function checkPrice(siteID, cusAcno, stkCode, stkType, caseType, qty) {
    try {
        if (!window.XMLHttpRequest) {
            window.XMLHttpRequest = function() {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
        }                
        window.xr = new XMLHttpRequest();
        var hostURL = getHostUrl();
        var openString = hostURL + "Services.asmx/CheckPrice?siteID=" + siteID + "&acno=" + cusAcno + "&stkCode=" + stkCode + "&stkType=" + stkType + "&caseType=" + caseType + "&qty=" + qty;
        //alert("open string is " + openString);
        window.xr.open("GET", openString);
        window.xr.onreadystatechange = checkPriceComplete;
        window.xr.send(null);
    }
    catch (err) {
        var em = "";
        alert("Error");
        //no action
    }
}
function getHostUrl()
{
    try
    {
        var hostURL = "";                        
        var mp = window.location.pathname.toLowerCase().indexOf('a_webdev');        
        if (mp > -1)
        {
            hostURL = window.location.protocol + "//" + window.location.host + "/a_webdev/";
        }
        else
        {
            hostURL = window.location.protocol + "//" + window.location.host + "/";
        }            
        return hostURL;
    }
    catch(err)
    {
        return "";
    }
}
function UpdateRatingComplete()
{ 
    if (xr.readyState == 4) 
    {        
        if (xr.status == 200)
        {
            try
            {
                var doc = xr.responseXML;            
                var qty;            
                if (doc.evaluate)
                {
                    qty = doc.evaluate("//text()", doc,
                                null,
                                XPathResult.STRING_TYPE, null).stringValue;
                }
                else
                {
                   qty = doc.selectSingleNode("//text()").data;
                }
            }
            catch(err)
            {
                return;
            }
            if (window.baseImgID)
            {
                var hostURL = getHostUrl();
                for (i=1 ; i < 6; i++)
                {
                    var cImgId = window.baseImgID + i;
                    var cImg = document.getElementById(cImgId);
                    if (cImg)
                    {
                        if (i > qty)
                        {
                            cImg.src = hostURL + "images/starempty.gif";
                        }
                        else
                        {
                            cImg.src = hostURL + "images/starfull.gif";
                        }
                        cImg.onmouseover=null;
                        cImg.onmouseout=null;
                        cImg.onmouseup=null;                                        
                    }                
                }
                if (window.baseImgID.length > 3)
                {
                    var cMsgID = window.baseImgID.substr(0,window.baseImgID.length - 3) + 'Msg';
                    var cMsg = document.getElementById(cMsgID);
                    if (cMsg)
                    {
                        cMsg.innerHTML = "You rated this " + qty + " out of 5";
                    }
                }
            }
        } else
        {
            //itemQuantityDisplay.innerHTML = "Error retrieving quantity";
            //itemQuantityDisplay.className = "Error";
        }    
        //itemQuantityDisplay.style.display = "";
        //progressDisplay.style.display = "none";
    }
}
function lookupComplete() {    
    if (xr.readyState == 4) {        
        if (xr.status == 200)
        {
            try
            {
                var doc = xr.responseXML;
                var retVal;
                if (doc.evaluate) {
                    //firefox
                    retVal = doc.evaluate("//text()", doc,
                                null,
                                XPathResult.STRING_TYPE, null).stringValue;
                }
                else if (doc.text) {
                    //IE
                    retVal = doc.text;
                }                    
                if (retVal.length > 1) {
                    if (retVal.substr(0, 1) == 'n') {
                        var cid = retVal.substr(1, retVal.length - 1);
                        var ctl = document.getElementById(cid);
                        if (ctl && !ctl.disabled) {
                            alert("Invalid Code");
                            ctl.focus();
                        }
                    }
                }                
            }
            catch(err)
            {
                return;
            }
        }
    }
}
function checkPriceComplete() {
    if (xr.readyState == 4) {
        if (xr.status == 200) {
            try {
                var doc = xr.responseXML;
                var retVal;
                if (doc.evaluate) {
                    //firefox
                    retVal = doc.evaluate("//text()", doc,
                                null,
                                XPathResult.STRING_TYPE, null).stringValue;
                }
                else if (doc.text) {
                    //IE
                    retVal = doc.text;
                }
                if (retVal.length > 1) {
                    if (retVal.substr(0, 1) == 'y') {
                        var rpr = retVal.substr(1, retVal.length - 1);
                        var cid = 'bi__NetUnitPrice';
                        var ctl = document.getElementById(cid);
                        if (ctl && !ctl.disabled) {
                            ctl.value = rpr; ;
                            if (__setlinetotals) {
                                __setlinetotals();                            
                            }
                        }
                    }
                }
            }
            catch (err) {
                return;
            }
        }
    }
} 

