﻿//This code was created by the fine folks at Switch On The Code - http://blog.paranoidferret.com
//This code can be used for any purpose

function animate(elementID, newLeft, newTop, newWidth, newHeight, time, callback)
{
    var el = document.getElementById(elementID);
    if(el == null)
        return;

    var cLeft = parseInt(el.style.left);
    var cTop = parseInt(el.style.top);
    var cWidth = parseInt(el.style.width);
    var cHeight = parseInt(el.style.height);

    var totalFrames = 1;
    if(time> 0)
        totalFrames = time/40;

    var fLeft = newLeft - cLeft;
    if(fLeft != 0)
        fLeft /= totalFrames;

    var fTop = newTop - cTop;
    if(fTop != 0)
        fTop /= totalFrames;

    var fWidth = newWidth - cWidth;
    if(fWidth != 0)
        fWidth /= totalFrames;

    var fHeight = newHeight - cHeight;
    if(fHeight != 0)
        fHeight /= totalFrames;
 
    doFrame(elementID, cLeft, newLeft, fLeft, cTop, newTop, fTop, cWidth, newWidth, fWidth, cHeight, newHeight, fHeight, callback);
}

function doFrame(eID, cLeft, nLeft, fLeft, cTop, nTop, fTop, cWidth, nWidth, fWidth, cHeight, nHeight, fHeight, callback)
{
    var el = document.getElementById(eID);
    if(el == null)
        return;

    cLeft = moveSingleVal(cLeft, nLeft, fLeft);
    cTop = moveSingleVal(cTop, nTop, fTop);
    cWidth = moveSingleVal(cWidth, nWidth, fWidth);
    cHeight = moveSingleVal(cHeight, nHeight, fHeight);

    el.style.left = Math.round(cLeft) + 'px';
    el.style.top = Math.round(cTop) + 'px';
    el.style.width = Math.round(cWidth) + '%';
    el.style.height = Math.round(cHeight) + 'px';

    if(cLeft == nLeft && cTop == nTop && cHeight == nHeight && cWidth == nWidth)
    {
        if(callback != null)
            callback();
        return;
    }
 
    setTimeout( 'doFrame("'+eID+'",'+cLeft+','+nLeft+','+fLeft+','+cTop+','+nTop+','+fTop+','+cWidth+','+nWidth+','+fWidth+','+cHeight+','+nHeight+','+fHeight+','+callback+')', 40);
}

function moveSingleVal(currentVal, finalVal, frameAmt)
{
    if(frameAmt == 0 || currentVal == finalVal)
        return finalVal;
 
    currentVal += frameAmt;
    if((frameAmt> 0 && currentVal>= finalVal) || (frameAmt <0 && currentVal <= finalVal))
    {
        return finalVal;
    }
    return currentVal;
}

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.getElementsByTagName("body")[0].getElementsByTagName("*").length; i++) {
        elm = document.getElementsByTagName("body")[0].getElementsByTagName("*")[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

//function AddSample(colorChartId, price, colorId, productId, productName, imageLabel) {
//    openwin('/control/cartConsoleAddColorSample?&colorChartID=' + colorChartId + '&price=' + price + '&colorID=' + colorId + '&productID=' + productId + '&productName=' + productName, 840, 550);
//}

function AddSample(colorChartId, price, colorId, productId, productName, imageLabel) {
  var theSrcUrl = '/control/cartConsoleAddColorSample?&colorChartID=' + colorChartId + '&price=' + price + '&colorID=' + colorId + '&productID=' + productId + '&productName=' + productName;
  JQ.ajax({
    url: theSrcUrl, 
    beforeSend: function () {
		JQ('#samplePopup .samplePopupMessage').html('Adding Sample to Cart, please wait.');
		JQ('#samplePopup').show();
    },
    complete: function () {
		JQ('#samplePopup .samplePopupMessage').html('You Sample has been added to your cart.');
    }
  }); 
}
function CloseAddSample() {
  JQ('#samplePopup').hide();
 }

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function getQueryParameter(parameterName) {
    var queryString = window.top.location.search.substring(1);
    var parameterName = parameterName + "=";
    if (queryString.length > 0) {
        begin = queryString.indexOf(parameterName);
        if (begin != -1) {
            begin += parameterName.length;
            end = queryString.indexOf("&", begin);
            if (end == -1) {
                end = queryString.length
            }
            return unescape(queryString.substring(begin, end));
        }
    }
    return "null";
}