﻿
String.prototype.replaceAll = stringReplaceAll;

function stringReplaceAll(AFindText, ARepText) {
    raRegExp = new RegExp(AFindText, "g");
    return this.replace(raRegExp, ARepText)
}

function geturl(qtyid)
{
    var txtQty = document.getElementById(qtyid);
    var no = qtyid.substring(6,qtyid.length);
    var strP = /^\s*(\d)?\d*\s*$/;
    var qty;
    try
    {
        if (!strP.test(txtQty.value)) 
        {
            document.getElementById('span' + no).style.visibility='visible';
            return false;
        }
        else
        {
            qty = parseInt(txtQty.value,10);
            if(qty <= 9999 && qty >= 1)
            {
                document.getElementById('span' + no).style.visibility='hidden';
                txtQty.value = qty; 
                return false;          
            }
            else
            {
                document.getElementById('span' + no).style.visibility='visible'; 
                return false;           
            }
        }
    }
    catch(e)
    {
        document.getElementById('span' + no).style.visibility='visible';
        return false;
    }   
}


function AddToCart(qtyid,id,http) {

    var url = document.location.pathname + document.location.search;
    var perUrl = "(" + url.replaceAll('&', '|') + ")";
    
    var txtQty = document.getElementById(qtyid);
    var no = qtyid.substring(6,qtyid.length);
    var strP = /^\s*(\d)?\d*\s*$/;
    var qty;
    try
    {
        if(!strP.test(txtQty.value)||!strP.test(id))
        {
            document.getElementById('span' + no).style.visibility='visible';
            return;
        }
        else
        {
            qty = parseInt(txtQty.value,10);
            if(qty <= 9999 && qty >= 1)
            {
                document.getElementById('span' + no).style.visibility='hidden';
                location.href = http + "/online/Page_Cart/Search_additem.aspx?ID=" + id + "&SearchStat=-1&qty=" + qty + "&previousURL=" + perUrl;         
            }
            else
            {
                document.getElementById('span' + no).style.visibility='visible';            
            }
        }
    }
    catch(e)
    {
        document.getElementById('span' + no).style.visibility='visible';        
    }    
}



function AdjustImage()
{
    var imgbtn = document.getElementById('imgbtnPhoto');
    var image = new Image(); 

    image.src = imgbtn.src; 

    if(image.width > 0 && image.height > 0)
    { 
        var height = image.height;
        var width = image.width;
        var style = imgbtn.style;
            
        if (height > 450)
        {
            width = width * 450 / height;
            height = 450;
        }
        if (width > 730)
        {
            height = height * 730 / width;
            width = 730;
        }
        
        imgbtn.setAttribute('width', width);  
        imgbtn.setAttribute('height', height);  
    }
    else
    {
        imgbtn.style.display = "none"; 
    }
}
