﻿// JScript File

function search_onfocus(element)
{
    if (element.value == "Keyword (s) or Item #")
    {
        element.value = "";
    }
}

function search_onblur(element)
{
    if (element.value == "")
    {
        element.value = "Keyword (s) or Item #";
    }
}
function email_onfocus(element)
{
    if (element.value == "Enter your e-mail")
    {
        element.value = "";
    }
}

function email_onblur(element)
{
    if (element.value == "")
    {
        element.value = "Enter your e-mail";
    }
}

function printableVersion()
{
    var url = window.location.toString();
    
    if (url.indexOf("?") == -1)
    {
        url += "?template=print";        
    }
    else
    {
        url += "&template=print";
    }
    
    window.open(url, '', 'menubar=yes,scrollbars=yes,resizable=yes,height=500,width=800');
}

function EmailToFriend()
{
	window.open('EmailPage.aspx?SourcePage=' + document.URL, 'email', 'toolbar=no,menubar=no,scrollbars=yes,height=550,width=600');
}

function validateAPProductForm(itemsIds)
{
    for (var i in itemsIds)
    {
        var id = itemsIds[i];
        
        if (parseInt(document.getElementById('quantity_' + id).value) > 0)
        {
            var k = 0;
            
            while(document.getElementById('option_' + id + '_' + k) != null)
            {
                var element = document.getElementById('option_' + id + '_' + k);

                if (element != null && element.value == '')
                {
                    alert(element.tagName.toUpperCase() == 'SELECT' ? 'Please choose option' : 'Please input quantity');
                    element.focus();
                    return false;
                }

                k++;
            }
        }
        else
        {
            var value = document.getElementById('quantity_' + id).value;
            
            while (value.length != 0 && value.charAt(0) == " ")
            {
                value = value.substring(1, value.length);
            }
            
            while (value.length != 0 && value.charAt(value.length - 1) == " ")
            {
                value = value.substring(0, value.length - 1);
            }
            
            if (value.length == 0)
            {
                alert("Please enter a number for the quantity");
                document.getElementById('quantity_' + id).focus();
                return false;
            }
            
            for (var j in value)
            {
                if (value.charAt(j) < "0" || value.charAt(j) > "9")
                {
                    alert("Please enter a number for the quantity");
                    document.getElementById('quantity_' + id).focus();
                    return false;
                }
            }
        }
    }
    
    return true;
}

function validateProductQuantity(itemsIds)
{
    for (var i in itemsIds)
    {
        var id = itemsIds[i];
        
        var value = document.getElementById('quantity_' + id).value;
        
        while (value.length != 0 && value.charAt(0) == " ")
        {
            value = value.substring(1, value.length);
        }
        
        while (value.length != 0 && value.charAt(value.length - 1) == " ")
        {
            value = value.substring(0, value.length - 1);
        }
        
        if (value.length == 0 || value == "0")
        {
            alert("Please enter a number for the quantity");
            document.getElementById('quantity_' + id).focus();
            return false;
        }
        
        for (var j in value)
        {
            if (value.charAt(j) < "0" || value.charAt(j) > "9")
            {
                alert("Please enter a number for the quantity");
                document.getElementById('quantity_' + id).focus();
                return false;
            }
        }
    }
    
    return true;
}

function quantityValidator(inputId, minVal, maxVal, testQuantityId)
{
    this.TestQuantityId = testQuantityId;
    this.InputId = inputId;
    this.MinVal = minVal;
    this.MaxVal = maxVal;
}

function validateVariantQuantity(validateQuantities)
{    
    for (var i = 0; i < validateQuantities.length; i++)
    {
        if (parseInt(document.getElementById(validateQuantities[i].TestQuantityId).value) <= 0)
        {
            continue;
        }
        
        var input = document.getElementById(validateQuantities[i].InputId);
        var value = input.value;
        
        while (value.length != 0 && value.charAt(0) == " ")
        {
            value = value.substring(1, value.length);
        }
        
        while (value.length != 0 && value.charAt(value.length - 1) == " ")
        {
            value = value.substring(0, value.length - 1);
        }
        
        if (value.length == 0)
        {
            alert("Please enter a number for the quantity");
            input.focus();
            return false;
        }
        
        for (var j = 0; j < value.length; j++)
        {
            if (value.charAt(j) < "0" || value.charAt(j) > "9")
            {
                alert("Please enter a number for the quantity");
                input.focus();
                return false;
            }
        }
        
        value = parseInt(value);
        
        if (value < validateQuantities[i].MinVal || value > validateQuantities[i].MaxVal)
        {
            alert("Quantity has to be between " + validateQuantities[i].MinVal + " and " + validateQuantities[i].MaxVal);
            input.focus();
            return false;
        }
    }
    
    return true;
}
