/* General.js */

function setImage(imageId, imageURL)
{   
    var image = new Image();
    image.src = imageURL;
    var img = document.getElementById(imageId);    
    if (img)
    {
        img.src = imageURL;
    }
}

function setImageWithZoom(imageId, imageURL, zoomId, zoomURL)
{   
    var image = new Image();
    var zoom = new Image();
    image.src = imageURL;
    zoom.src = zoomURL;
    var img = document.getElementById(imageId);
    var zoomimg = document.getElementById(zoomId);
    if (img)
    {
        img.src = imageURL;
    }
    if (zoomimg)
    {
        zoomimg.src = zoomURL;
    }
}

function setInnerText(elementId,changeVal)
{
    var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;

    var elem = document.getElementById(elementId);
    if (elem)
    {
        if(!hasInnerText)
        {
            elem.textContent = changeVal;
        }
        else
        {
            elem.innerText = changeVal;
        }
    }
}

function setClass(elementId, className)
{
    var elem = document.getElementById(elementId);
    if (elem)
    {
        elem.className = className;
    }
}


function setVisibility(elementId, visible) {
    element = document.getElementById(elementId);
    if (element) {
        if (visible)
            element.style.display = "block";
        else
            element.style.display = "none";
    }
}

function showIfChecked(elementId, checkBoxId)
{
    checkBox = document.getElementById(checkBoxId);
    setVisibility(elementId,checkBox.checked);
}

function hideIfChecked(elementId, checkBoxId)
{
    checkBox = document.getElementById(checkBoxId);
    setVisibility(elementId,!checkBox.checked);
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function textAreaMaxLength(textArea, elementId, maxLength)
{
    if(textArea.value.length > maxLength)
    {        
        textArea.value = textArea.value.substring(0, maxLength);
        document.getElementById(elementId).innerHTML = 0;
    }    
    else
    {
        document.getElementById(elementId).innerHTML = (maxLength-textArea.value.length);
    }
}

function addClickEvent(buttonId)
{
    var button = document.getElementById(buttonId);
    if (button && typeof(button.click) == 'undefined')
    {
        button.click = function()
        {
            var result = true;
            if (button.onclick) result = button.onclick();
            if (typeof(result) == 'undefined' || result)
            {
                eval(button.getAttribute('href'));
            }
        }
    }
}

function ensureChecked(source, args)
    {
        var controlId = source.getAttribute('controltovalidate');
        if(controlId!="" ||controlId!=null)
        {
            var control = document.getElementById(controlId);            
            if ( control.checked == true )
                args.IsValid = true;
            else 
                args.IsValid = false;
         }
         else
            args.IsValid = false;

    }

/*=============================================
Product
==============================================*/	

function AddToCart(uid)
{
    document.body.style.cursor='wait';
    document.getElementById("imgProgress").style.display='';
    var query = "Task=AddToCart&Type=Standard&UId=" + uid;
    AjaxSendRequest(query, 'returnAddToCart', AjaxPageUrl);
    return false;
}

function AddToCart()
{
    document.getElementById("imgProgress").style.display='';
    document.body.style.cursor='wait';
    var query = "Task=AddToCart&Type=Matrix&UId=" + ProductId + "&selectedIds=" + GetSelectedMatrixValueList();        
    AjaxSendRequest(query, 'returnAddToCart', AjaxPageUrl);
    return false;
}

function returnAddToCart(text)
{
    if (isNaN(text))
    {
        document.getElementById("divAddToCartMessage").innerHTML = text;
    }
    else
    {
        document.getElementById("divAddToCartMessage").innerHTML = '';
    }
    UpdateMiniCart(AjaxPageUrl);    
    document.body.style.cursor='';
    document.getElementById("imgProgress").style.display='none';      
}


/*=============================================
For Login.aspx
==============================================*/

function displayNewCustomerSection()
{
    setVisibility('divNewCustomer',true);
    setVisibility('divExistingCustomer',false);
}

function displayExistingCustomerSection()
{
    setVisibility('divNewCustomer',false);
    setVisibility('divExistingCustomer',true);
}

function displayShipAddressSection(checkboxId)
{        
    showIfChecked('divShippingAddress',checkboxId);
}

function displayBillAddressSection(checkboxId)
{
    showIfChecked('divBillingAddress',checkboxId);
}
    

/*==============================================
customeraddress/shipbill
===============================================*/
function CheckOne(rdButton)
{
    for(i=0; i<document.forms[0].length; i++) 
    { 
        var o = document.forms[0][i]; 
        if (o.type == 'radio') 
        { 
            if (rdButton.id != o.id)
            {
                o.checked = false; 
            } 
        } 
    } 
}

function displayNewShipAddress(rbId)
{
    showIfChecked('divNewShipAddress',rbId);
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function showNewShipAddress()
{
    setVisibility('divNewShipAddress',true);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function showNewBillingAddress()
{
    setVisibility('divNewBillingAddress',true);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function hideNewBillingAddress()
{
    setVisibility('divNewBillingAddress',false);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function hideNewShipAddress()
{
    setVisibility('divNewShipAddress',false);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function hideCurrentShipAddress()
{
    setVisibility('divCurrentShipAddress',false);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function hideCurrentBillingAddress()
{
    setVisibility('divCurrentBillingAddress',false);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function showCurrentShipAddress()
{
    setVisibility('divCurrentShipAddress',true);       
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function showEditShipAddress()
{
    setVisibility('divEditShipAddress',true);
    var hdn =  document.getElementById('hdnshowingeditshipaddress');
    hdn.value ="true";
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
    
}

function showEditBillingAddress()
{
    setVisibility('divEditBillingAddress',true);
     var hdn =  document.getElementById('hdnshowingeditbiladdress');
     hdn.value ="true";
     if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function hideEditShipAddress()
{
    setVisibility('divEditShipAddress',false);
    var hdn =  document.getElementById('hdnshowingeditshipaddress');
     hdn.value ="false";
     if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function hideEditBillingAddress()
{
    setVisibility('divEditBillingAddress',false);
    var hdn =  document.getElementById('hdnshowingeditbiladdress');
    hdn.value ="false";
    if(typeof(CheckValidators)!= 'undefined')
        CheckValidators();
}

function setPostbackReference(controlId, postbackReference)
{
    var control = document.getElementById(controlId);
    if (postbackReference) 
    {
        control.href = "javascript:" + postbackReference;
        control.onclick = function() { postbackReference };
    }
}

function CheckAddressValidators(newShipPostBackScript, newBillPostBackScript, defaultPostBack, rbNewShipAddrId
, rbNewBillShipAddrId, lbtnSaveId,chkBill)
{
    var rbNewShipAddr = document.getElementById(rbNewShipAddrId);
    var rbNewBillShipAddr = document.getElementById(rbNewBillShipAddrId);
    var chkBillAddr = document.getElementById(chkBill);
    
    if(rbNewShipAddr.checked)
    {
        setPostbackReference(lbtnSaveId, newShipPostBackScript);
    }
    else if (rbNewBillShipAddr.checked)
    {
        if (!chkBillAddr.checked)
        {
            setPostbackReference(lbtnSaveId, newBillPostBackScript);
        }
        else
        {
            setPostbackReference(lbtnSaveId, defaultPostBack);
        }
    }
    else
    {
        setPostbackReference(lbtnSaveId, defaultPostBack);
    }

}

function disableValidator(validatorId) 
{
    validator = $get(validatorId);
    ValidatorEnable(validator, false);
}

function enableValidator(validatorId) 
{
    validator = $get(validatorId);
    ValidatorEnable(validator, true);
}

/*=====================================================
Partner
=====================================================*/
function OnDelete()
{
     var value = confirm("Are you sure you want to delete this customer?");
     var field = document.getElementById("bDelete");
     field.value = value;
   
}

/*==================================================
GiftWrap
====================================================*/
function showMessageBoxes(did, lid)
{
    setVisibility(did,true);       
    setVisibility(lid,false);       
}
    
function showProductInfo(did)
{
    setVisibility(did,true);       
}
    
function checkGiftWrap()
{
    for(i=0; i<document.forms[0].length; i++) 
    { 
        var o = document.forms[0][i]; 
        if (o.type == 'checkbox') 
        { 
            o.checked = true; 
        } 
    } 
}

/*================================================
shipbilladdress
================================================*/

function CheckOne(rdButton, divname)
{            
    div = document.getElementById(divname);
    elements = div.getElementsByTagName('*');
    for(i=0; i< elements.length; i++) 
    { 
        var o = elements[i]; 
        if (o.type == 'radio') 
        { 
            if (rdButton.id != o.id)
            {
                o.checked = false; 
            } 
        } 
    } 
}

function displayNewAddressDiv(divId, hide)
{
    setVisibility(divId, !hide);            
}

function showCurrentBillingAddress()
{
    setVisibility('divCurrentBillingAddress',true);
}

if (typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();


