﻿/* * * * * * * *
 *	Preloader  *
 */
 
// image preloader
// imageArray is an array of imageURLs
function preloadImages(imageArray)
{
	var imageObj = new Image();
	
	for (key in imageArray)
	{
		imageObj.src = imageArray[key];
	}
}

/* * * * * * * * * * * *
 * string manipulation *
 */
 
function leftTrim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) 
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trim(sString) 
{
	if (!sString)
		return "";
	else
		return rightTrim(leftTrim(sString));
}

/* * * * * * * * * * */
/* Form fields       */
/* * * * * * * * * * */

function toggleFormField(id, disableClassName, b)
{
    if (b)
    {
        enableFormField(id, disableClassName);
    }
    else
    {
        disableFormField(id, disableClassName);
    }
}

function enableFormField(id, disableClassName)
{
    var emt = document.getElementById(id);
    if (emt)
    {
        var oldClass = emt.className;
        var newClass = oldClass.replace(disableClassName, '');
        
        emt.className = newClass.trim();
        emt.readOnly = false;
    }
}

function disableFormField(id, disableClassName)
{
    var emt = document.getElementById(id);
    if (emt)
    {
        var oldClass = emt.className;
        var newClass = oldClass + ' ' + disableClassName;
        
        emt.className = newClass;
        emt.readOnly = true;
    }
}

/* * * * * * * * * * *
 * helper functions  *
 */
 
function elmt(id)
{
	return document.getElementById(id);
}

function copyValue(id1, id2)
{
	elmt(id1).value = elmt(id2).value;
}

/* * * * * * * * * * * * *
 * Image Popup			 *
 */
 
function showLargeImage(ProductId, CatalogueId)
{
    window.open('LargeImage.aspx?ProductID=' + ProductId + '&CatalogueID=' + CatalogueId,'_blank','Width=600,height=500,menubar=yes,resizable=no,scrollbars=no,toolbar=no,titlebar=yes,status=yes');
}

/* * * * * * * *
 * Blink basket
 * * * * * * * */
 
function blinkBasket(fromHexColor, toHexColor)
{
    // validate input
    var regExpHexColor = /^#[0-9A-F]{6}/i;
    if (!regExpHexColor.test(fromHexColor) || !regExpHexColor.test(toHexColor))
    {
        return;
    }

    var intensity;
    
    if (blinkCounter < 100)
    {
        intensity = blinkCounter;
    }
    else if (blinkCounter >= 200)
    {
        return;
    }
    else
    {
        intensity = 200 - blinkCounter;
    }
          
    var blinkElmt = document.getElementById('basketSummary');
    if (blinkElmt)
    {
        // get start and end channel values
        var redFrom = h2d(fromHexColor.substr(1, 2));
        var greenFrom = h2d(fromHexColor.substr(3, 2));
        var blueFrom = h2d(fromHexColor.substr(5, 2));
        var redTo = h2d(toHexColor.substr(1, 2));
        var greenTo = h2d(toHexColor.substr(3, 2));
        var blueTo = h2d(toHexColor.substr(5, 2));
        
        // get current channel values
        var red = calculateChannelValue(redFrom, redTo, intensity);
        var green = calculateChannelValue(greenFrom, greenTo, intensity);
        var blue = calculateChannelValue(blueFrom, blueTo, intensity);

        // apply new color and call recursively
        blinkElmt.style.backgroundColor = 'rgb(' + red + ',' + green + ',' + blue + ')';
        blinkCounter += 4;
        setTimeout('blinkBasket("' + fromHexColor + '", "' + toHexColor + '");', 10);
    }
}

function calculateChannelValue(start, end, intensity)
{
    return start + (end - start) * (intensity / 100.0);
}

function d2h(d) {return d.toString(16);}
function h2d(h) { return parseInt(h, 16); } 

/*
	temporary popups
*/

function showTempPopup(popupId, timeout) {

	var popup = document.getElementById(popupId);
	if (popup) {
	   popup.style.display = "block";
	   setTimeout("hideTempPopup('" + popupId + "');", timeout);
	}
}

function hideTempPopup(popupId) {

	var popup = document.getElementById(popupId);
	if (popup) {
		popup.style.display = "none";
	}
}
