function QS() {
    this.qs = {};
    var s = location.search.replace(/^\?|#.*$/g, '');
    if (s) {
        var qsParts = s.split('&');
        var i, nv;
        for (i = 0; i < qsParts.length; i++) {
            nv = qsParts[i].split('=');
            this.qs[nv[0]] = nv[1];
        }
    }
}
QS.prototype.add = function(name, value) {
    if (arguments.length == 1 && arguments[0].constructor == Object) {
        this.addMany(arguments[0]);
        return;
    }
    this.qs[name] = value;
}
QS.prototype.addMany = function(newValues) {
    for (nv in newValues) {
        this.qs[nv] = newValues[nv];
    }
}
QS.prototype.remove = function(name) {
    if (arguments.length == 1 && arguments[0].constructor == Array) {
        this.removeMany(arguments[0]);
        return;
    }
    delete this.qs[name];
}
QS.prototype.replace = function(name, value) {
    this.remove(name);
    this.add(name, value);
    return;
}
QS.prototype.removeMany = function(deleteNames) {
    var i;
    for (i = 0; i < deleteNames.length; i++) {
        delete this.qs[deleteNames[i]];
    }
}
QS.prototype.getQueryString = function() {
    var nv, q = [];
    for (nv in this.qs) {
        q[q.length] = nv + '=' + this.qs[nv];
    }
    return q.join('&');
}
QS.prototype.toString = QS.prototype.getQueryString
var setVariantMode = false;
$(document).keydown(function(event) {
    if (setVariantMode && event.keyCode != 13) {
        redirectToVariantPage(String.fromCharCode(event.which));
    } else {
        if (event.ctrlKey && event.altKey) {
            if (event.which == 86) {
                setVariantMode = true;
                alert('Type the variantId.');
            }
        }
    }
})

function redirectToVariantPage(variantId) {
    var url = '/clearSession.aspx';

    var redirectLocation = location.href.substring(0, location.href.length - location.search.length);
    var qy = new QS();
    qy.remove('vx');

    if (variantId == ' ') { variantId = ''; }
    qy.add('vx', variantId);

    redirectLocation = redirectLocation + '?' + qy.getQueryString();
    alert('Transferring to variantId="' + variantId + '"');
    window.location.href = url + '?url=' + escape(redirectLocation);
}

function performRedirection(variantId) {
    var redirectLocation = location.href.substring(0, location.href.length - location.search.length);
    var qy = new QS();
    qy.remove('vx');

    if (variantId == ' ') { variantId = ''; }
    qy.add('vx', variantId);

    redirectLocation = redirectLocation + '?' + qy.getQueryString();
    alert('Transferring to variantId="' + variantId + '"');
    window.location.href = redirectLocation;
}

//position: absolute;
function showRomanShadesChart(id) {
	docSection = document.getElementById(id);
	docSection.style.visibility = 'visible';
	docSection.style.zIndex = '50';
	docSection.style.position = 'absolute';
	//docSection.style.margin = 'auto';
	return false;
}

function hideRomanShadesChart(id) {
	docSection = document.getElementById(id);
	docSection.style.visibility = 'hidden';
	docSection.style.zIndex = '-1';
	docSection.style.position = 'absolute';
	//docSection.style.margin = 'auto';
}

function colorOptionHasSecondaryColors(selectValue, colorOptionsDialogId) {
    if (selectValue.indexOf('true') != -1) {
        var splitList = selectValue.split('|');
        if (splitList.length == 4) {
            showColorOptionsDialog(colorOptionsDialogId, splitList[3], splitList[2]);
        }
        return true;
    }
    return false;
}

function showColorOptionsDialog(id, colorOptionList, primaryColorName) {
    docSection = document.getElementById(id);
    docSection.style.visibility = 'visible';
    docSection.style.zIndex = '50';
    docSection.style.position = 'absolute';

    // Set the primary color name.
    var primaryColorNameHTML = document.getElementById('PrimaryColorNameHTML');
    if (primaryColorNameHTML != null && primaryColorNameHTML != null) {
        if (primaryColorName != null) primaryColorNameHTML.innerHTML = primaryColorName;
        else primaryColorNameHTML.innerHTML = "Make a selection";
    }

    // Set the secondary colors.
    secondaryColorsHTML = document.getElementById('SecondaryColorsHTML');
    if (secondaryColorsHTML != null && colorOptionList != null) {
        secondaryColorsHTML.innerHTML = generateSecondaryColorsHTML(colorOptionList);
    }
    
    return false;
}

function generateSecondaryColorsHTML(secondaryColorList) {    
    var rootDirectoryLevel = '../';
    // Parse the list and create the selectable options.
    var splitList = secondaryColorList.split(',');
    var maxRowCount = 4;
    var currentRowCount = 0;

    var colorHtml = '<div id="ColorOptionsDialogHeader" class="ColorOptionsDialogRow"><div><table style="width: 560px; overflow: hidden;" cellspacing="10" cellpadding="10"><tr><td align="center"><div class="ColorOptionsDialogRowNI">';
    for (index = 0; index < splitList.length; index++) {
        var secondaryColorInfo = splitList[index].replace(/^\s+|\s+$/g, '');
        if (secondaryColorInfo != '') {
            if (currentRowCount == maxRowCount) {
                colorHtml += '</div><br /><div style="clear:both"></div></td></tr><tr><td align="center"><div class="ColorOptionsDialogRowNI">';
                currentRowCount = 0;
            }
            // The color comes in the format ColorID:ColorName
            var colorInfo = secondaryColorInfo.split(':');
            if (colorInfo.length == 2) {
                colorHtml += '<div class="secondary-color-item">';
                colorHtml += '<a href="#SetColor" onclick="SetPreviewColor2(' + colorInfo[0] + ',1,\'' + rootDirectoryLevel + '\');CalcPrice(); hideColorOptionsDialog(\'ColorOptionsDialog\')" ><img src="' + rootDirectoryLevel + 'images/blinds/thumbnail/blindcolor_' + colorInfo[0] + '.jpg" class="cursor" \/' + '></a>';
                colorHtml += '<div style="clear: both;width: 100%;text-align: center">' + colorInfo[1] + '</div></div>';
            }
        }
        currentRowCount = currentRowCount + 1;
    }
    colorHtml += '</div></td></tr></table></div></div>'
    // Draw the footer.
    colorHtml += '<div id="ColorOptionsDialogFooter"></div>';

    return colorHtml;
}

function hideColorOptionsDialog(id) {
    docSection = document.getElementById(id);
    docSection.style.visibility = 'hidden';
    docSection.style.zIndex = '-1';
    docSection.style.position = 'absolute';    
}


function validateMeasureInstall(frmEle) {
    if (frmEle.FirstName.value == "") {
        alert("Please enter your Firstname!");
        frmEle.FirstName.focus();
        return false;
    }
    if (frmEle.LastName.value == "") {
        alert("Please enter your Lastname!");
        frmEle.LastName.focus();
        return false;
    }
    if (frmEle.Address.value == "") {
        alert("Please enter your Address!");
        frmEle.Address.focus();
        return false;
    }
    if (frmEle.City.value == "") {
        alert("Please enter your City!");
        frmEle.City.focus();
        return false;
    }
    if (frmEle.State.value == "" || frmEle.State.value == "0") {
        alert("Please enter your State!");
        frmEle.State.focus();
        return false;
    }
    if (frmEle.Zip.value == "") {
        alert("Please enter your Zip!");
        frmEle.Zip.focus();
        return false;
    }
    if (frmEle.PhoneNumber.value != "0") {
        if (frmEle.PhoneNumber.value == "") {
            alert("Please enter your Phone!");
            frmEle.PhoneNumber.focus();
            return false;
        }
    }
    if (frmEle.Email) {
        if (frmEle.Email.value == "") {
            alert("Please enter your Email!");
            frmEle.Email.focus();
            return false;
        }
        var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
        var check = /@[\w\-]+\./;
        var checkend = /\.[a-zA-Z]{2,4}$/;

        emailad = frmEle.Email.value;

        if (((emailad.search(exclude) != -1) || (emailad.search(check))
		== -1) || (emailad.search(checkend) == -1)) {
            alert("Incorrect email address!  Please ammend.");
            frmEle.Email.focus();
            return false;
        }
    }
    if (frmEle.BestTimeToCall.value != "0") {
        if (frmEle.BestTimeToCall.value == "") {
            alert("Please enter the best time to call.");
            frmEle.BestTimeToCall.focus();
            return false;
        }
    }
}

function showLeftNavImage(leftNavImageId, showingElementId) { 
    if (leftNavImageId) {
        var showingSpanElement = $('#spanImage_' + leftNavImageId);
        var hiddenElement = $('#inputImages_' + leftNavImageId);                       
        var showingColorsHtml = "<img class='cursor' style='border: solid 2px #000000' src='images/products/" + hiddenElement.val() + "' />";
        showingSpanElement.html(showingColorsHtml);

        var imageCount = 1;
        var loadedImageCnt = 0;
        $('#spanImage_' + leftNavImageId + " img").hide().each(function () {
            var imgs = $('img');
            $(this).bind('load', function () {
                loadedImageCnt++;
                if (loadedImageCnt >= imageCount) {
                    imgs.fadeIn("slow");
                } else {
                    //show images are still downloading
                }
            });
        });                
        document.getElementById("LeftNavImage_" + leftNavImageId).style.display = 'inline';
    }
}

function hideLeftNavImage(leftNameImageId) {
    document.getElementById("LeftNavImage_" + leftNameImageId).style.display = 'none';
}
