function __doCyranePostBack(controllerName, secure, control, controlID, eventTarget, eventArgument, qsParams) {
    if(!__validateForm(theForm, control, controlID)) return;
    var protocol = 'http';
    if(secure) protocol += 's';
    theForm.action = protocol + '://' + cyranePortalHTTPAlias + '/CyraneControllers/' + controllerName + 'Controller.aspx';
    if(qsParams != undefined && qsParams.length > 0)
        theForm.action = theForm.action + '?' + qsParams;
    
    theForm.__cyraneControlUniqueID.value = control.id;
    theForm.__cyraneControlID.value = controlID;  
    theForm.__cyraneEventTarget.value = eventTarget;
    theForm.__cyraneEventArgument.value = eventArgument;
    theForm.__cyraneCurrentPage.value = window.location.href;
    theForm.__cyranePortalID.value = cyranePortalID;
    theForm.__cyraneCulture.value = cyraneCulture;
    
    theForm.__VIEWSTATE.name = "ignoreViewState"; //rename viewstate field so target aspx page does not try to process

    theForm.submit();
}


function __validateForm(form, control, controlID) {
    //validate required fields within submitted control scope
    if(!__validateRequiredFields(form, control, controlID)) return false;
    //validate any cross sells
    if (!__validateXSells(form)) return false;
    
    return true;
}

function __validateRequiredFields(form, control, controlID) {
    var controlScope = control.id.substring(0, control.id.length - controlID.length);
    var valid = true;
    //find any required fields within scope of control
    jQuery.each(jQuery(":input"), function(i, val) {
        if (val.id.indexOf("_cyrReq") != -1 && val.id.indexOf(controlScope) != -1) {
            //find required element
            var req_elem = document.getElementById(val.id.substring(0, val.id.length - 7));
            if (req_elem.value.length == "") {
                alert(val.value); //display required field message to user
                req_elem.focus(); //set focus to required field
                valid = false;
                return false;
            }
        }
    });
    return valid;
}

function __validateXSells(form) {
    var valid = true;
    //find any required fields within scope of control
    jQuery.each(jQuery(":checkbox"), function(i, val) {
        if (val.id.indexOf("_chkXsell") != -1 && val.checked) {
            if (!__validateRequiredFields(form, val, "chkXsell")) {
                valid = false;
                return false;
            }
        }
    });
    return valid;
}

function cyrAction() {
	// setup to provide a dummy action for the onclick events.  This is to help show users what the link will do e.g. cyrAction('removeFilter')
	return false;
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
	var anchor = anchors[i];
	if (anchor.getAttribute("href") &&
	   anchor.getAttribute("rel") == "external")
	 anchor.target = "_blank";
	}
}
//window.onload = externalLinks;

function openWindow(url,window_name,window_width,window_height){
	var options='';
	if (!window_width) { window_width = 600; }
	if (!window_height) { window_height = 400; }
	options='menubar=1,toolbar=1,location=1,statusbar=1,scrollbars=1,resizable=1,width='+window_width+'px,height='+window_height+'px';
	window.open(url,name,options);
}

function closeWindow(name){
	window.close(name);
}

//Variant Feature Functions
function VariantFeatureChange(instance, featureName)
{
    eval('varFeatures = ' + instance + 'Features');
    updatedFeatures = new Array(featureName);
    jQuery.each(jQuery.grep(varFeatures, function (val, index) { return val != featureName; }), function(i, val) {
        ValidateFeatureOptions(instance, val, updatedFeatures);
    });
    SetSelectedVariant(instance)
}

function GetSelectedVariant(instance)
{
    variantData = eval(instance + "Data");
    productID = jQuery('#' + instance + '_productID').val();

    return jQuery.grep(variantData, function (val, index) { return val.ID == productID; })[0];
}

function SetSelectedVariant(instance)
{
    variantFeatures = eval(instance + 'Features');
    variantData = eval(instance + "Data");

    jQuery.each(variantData, function(i, val) {
        featuresMatch = true;
        jQuery.each(variantFeatures, function(j, featureName) {
            //featureValue = jQuery('#' + instance + '_' + featureName).val();
            featureIndex = document.getElementById(instance + '_' + featureName).selectedIndex;
            if (eval('val.Features.' + featureName) != featureIndex) {
                featuresMatch = false;
                return false; //break out of loop
            }
        });
        if (featuresMatch) {
            jQuery('#' + instance + '_productID').val(val.ID);
            return false; //break out of loop - confusing but return does not exit function just the jQuery.each loop
        }
    });
    callBack = eval(instance + 'CallBack');
    if (callBack != undefined) eval(callBack);
}

function ValidateFeatureOptions(instance, featureName, updatedFeatures)
{
    featureDropDownName = '#' + instance + '_' + featureName;
    currentValue = jQuery(featureDropDownName).val();
    firstEnabledValue = "";

    //iterate through all the values in child dropdown and check against features array
    jQuery.each(jQuery(featureDropDownName).find('option'), function(i, val)
        {
            if(IsValidOption(instance, featureName, i, updatedFeatures))
            {
                if(firstEnabledValue == "")
                    firstEnabledValue = val.value;
                val.style.color = 'black';
                val.disabled = false;
            }
            else
            {
                val.style.color = 'red';
                val.disabled = true;
                if(val.value == currentValue) //current option is now disabled
                    currentValue = "";
            }
        });
    if(currentValue == "")
        jQuery(featureDropDownName).val(firstEnabledValue);

    updatedFeatures[updatedFeatures.length] = featureName
}

function IsValidOption(instance, featureName, optionIndex, updatedFeatures)
{
    variantData = eval(instance + "Data");
    isValid = false;
    jQuery.each(variantData, function(i, val) {
        if (eval('val.Features.' + featureName) == optionIndex) //this option matches
        {
            updatedFeaturesMatch = true;
            jQuery.each(updatedFeatures, function(j, updatedFeatureName) {
                //updatedFeatureValue = jQuery('#' + instance + '_' + updatedFeatureName).val();
                updatedFeatureIndex = document.getElementById(instance + '_' + updatedFeatureName).selectedIndex;
                //if (eval('val.Features.' + updatedFeatureName) != updatedFeatureValue)
                if (eval('val.Features.' + updatedFeatureName) != updatedFeatureIndex) {
                    updatedFeaturesMatch = false;
                    return false; //break out of loop
                }
            });
            if (updatedFeaturesMatch) {
                isValid = true;
                return false; //break out of loop - confusing but return does not exit function just the jQuery.each loop
            }
        }
    });
    return isValid;
}
//End Variant Feature Functions



function TextCounter(textElement, displayElement, maxChars)
{
    textLength = textElement.value.length;
    var counter = document.getElementById(displayElement + "textCount");
    counter.innerHTML = maxChars - textLength;
    if (maxChars - textLength < 0)
    {
        textElement.value = textElement.value.substring(0, maxChars);
        counter.innerHTML = 0;
    }
}




function changevisibility(status,values){
    var el = document.getElementById(values);

		if (status == 'hide') {
			el.style.display = 'none';
		} else {
			el.style.display = 'block';
		}
	}

function showVarOptions(controlUniqueID,controlID,varDivID)
{
    varDivUniqueID = controlUniqueID.replace(controlID, varDivID);
    var chk = document.getElementById(controlUniqueID);
	if (chk.checked) {
		changevisibility('show',varDivUniqueID)
	} else {
		changevisibility('hide',varDivUniqueID)
	}
}

function DisableEnterKey(e) {
    var evt = window.event || e;
    if (evt.keyCode == 13) {
        if (!evt.target) evt.target = evt.srcElement; //if target not defined then set to srcElement (IE only)
        if (evt.target.type == 'text') return false;
    }
}

document.body.onkeydown = DisableEnterKey;

function addBookmark(title, url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) {
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) {
        return true;
    }
}

function changeImageString(div_id,string){
var myElementText = document.getElementById(div_id);
myElementText.innerHTML = string;
jQuery("a[rel='productImageLightbox']").lightBox();
}

function MaxLength(text, length) {
    
    var maxlength = new Number(length); // Change number to your max length.

    if (text.value.length > maxlength) {

        text.value = text.value.substring(0, maxlength);

    }
}