// $Id: func.js,v 1.16.2.15 2007/01/05 08:06:25 max Exp $

var current_taxes = [];
var product_thumbnail = document.getElementById('product_thumbnail');
var availObj = document.getElementById('product_avail');

/* 
	
*/

function switch_hidden_option(color_dropdown_other, option_classid, option_id, style_code, in_category, option_code) {
		
	var original_code = style_code;
	if (in_category == null) { in_category = false; }
	if (color_dropdown_other == 'color') {

		if (!in_category) {
			
			document.getElementById("color_code_active").value=option_code;
			var color_selected=document.getElementById(option_classid+"_option_"+option_id).name;
			grayout_empty(color_selected);
		}
		
		//document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).class="colorButton";
		//document.getElementById(option_classid + "_option_" + option_id).class="colorButtonSelected";

		/* document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="2px solid #E6E1CD"; */
		document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="2px solid #d0d0d0";
		/* document.getElementById(option_classid + "_option_" + option_id).style.border="2px solid #2BA1E9"; */
		document.getElementById(option_classid + "_option_" + option_id).style.border="2px solid #FFFFFF";
        
		var tempArrayIndex = "allColorsArray['" + option_code + "']";
		document.getElementById("ColorIndicator" + style_code).innerHTML = eval(tempArrayIndex);
	}

	else if (color_dropdown_other == 'colorThumb') { 
		/* document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="2px solid #E6E1CD"; */
		document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="2px solid #d0d0d0";
		/* document.getElementById(option_classid + "_option_" + option_id).style.border="2px solid #2BA1E9"; */
		document.getElementById(option_classid + "_option_" + option_id).style.border="2px solid #FFFFFF";

		document.getElementById(style_code + "_thumb").src="http://s7d2.scene7.com/is/image/WhiteandWarren/" + style_code + "_" + option_code + "?wid=63&$img_config$";

		var tempArrayIndex = "allColorsArray['" + option_code + "']";
		document.getElementById("ColorIndicator" + style_code).innerHTML = eval(tempArrayIndex);
	}

	else if (color_dropdown_other == 'dropdown') {
	
	}

	else {

		if (document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).getAttribute("clickback") !== "" && document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).getAttribute("clickback") !== null) {

			document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.color="#C0BCB3";
			/* document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.background="#DBD6C3"; */
			document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.background="#d5d5d5";
			document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="1px solid #C0BCB3";
		}

		else {

			document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.color="#C0BCB3";
			document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.background="#FFFFFF";
			/* document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="1px solid #9A968D"; */
			document.getElementById(option_classid + "_option_" + document.getElementById("po" + option_classid).value).style.border="1px solid #bcbcbc";
		}

		document.getElementById(option_classid + "_option_" + option_id).style.color="#FFFFFF";
		/* document.getElementById(option_classid + "_option_" + option_id).style.background="#65584F";
		document.getElementById(option_classid + "_option_" + option_id).style.border="1px solid #65584F"; */
    
    document.getElementById(option_classid + "_option_" + option_id).style.background="#5b5b5b";
		document.getElementById(option_classid + "_option_" + option_id).style.border="1px solid #5b5b5b";


		//Added on 30-nov by Sreejith
		if(document.getElementById("size_selected")!='null')
			document.getElementById("size_selected").value=option_code;
		//End 

		var tempArrayIndex = "allSizesArray['" + option_code + "']";
		document.getElementById("SizeIndicator" + style_code).innerHTML = eval(tempArrayIndex);
	}
	
	document.getElementById("po" + option_classid).value = option_id;

	check_options(option_classid, style_code, null, in_category);

	if (!in_category) {
		//Check if Out of stock
		var qty_obj		=document.getElementById("product_avail");

		if (qty_obj == null) {
			qty_obj = document.getElementById('product_avail' + style_code);
		}

	if (color_dropdown_other == 'color')
	{
	var quantity_min=qty_obj.value;
	if(quantity_min==0)
		{
		change_size_selected(style_code,in_category);
		}
	}	
		
	}


}

function change_size_selected(style_code,in_category)
	{
	var color_selected		=document.getElementById("color_code_active").value;
	var size_count_tmp	  	=document.getElementById("size_list_count").value;
	var size_string_tmp		=document.getElementById("size_list").value;
	var size_list_array		=size_string_tmp.split("-");
	var default_classid		=document.getElementById("classid_default").value;
				
				
	if(color_selected!="")
		{
		for(i=0;i<size_count_tmp;i++)
			{
			var size_active	=	size_list_array[i];
			var option_tmp	=	document.getElementById(size_active).value;
			var quantity	=	document.getElementById(color_selected+"_"+size_active).value;
			if(quantity>0)
				{
				switch_hidden_option("other", default_classid, option_tmp, style_code, in_category, size_active);
				break;
				}
			}
		}
	}

/*
	Stop anchor element from firing onclick function
*/

function disableAnchor(elementID) {

	if (document.getElementById(elementID).getAttribute("clickback") == null || document.getElementById(elementID).getAttribute("clickback") == "") {

		document.getElementById(elementID).setAttribute("clickback", document.getElementById(elementID).getAttribute("onclick"));
		document.getElementById(elementID).setAttribute("onclick", null);
	}
}

/*
	Allow anchor element to fire onclick again
*/

function enableAnchor(elementID) {

	if (document.getElementById(elementID).getAttribute("clickback") !== "" && document.getElementById(elementID).getAttribute("clickback") !== null) {

		document.getElementById(elementID).setAttribute("onclick", document.getElementById(elementID).attributes["clickback"].nodeValue);
		document.getElementById(elementID).setAttribute("clickback", null);
	}
}

function grayout_empty(color_selected)
	{
	var size_string	=document.getElementById("size_list").value;
	var size_count	=document.getElementById("size_list_count").value;

	if(size_count<=1)
		return true;
	
	var size_list	=size_string.split("-");
	/* var grayoutcolor	="#dbd6c3"; */
	var grayoutcolor	="#d5d5d5";
	
	for(i=0;i<size_count;i++)
		{
			var quantity	=document.getElementById(color_selected+"_"+size_list[i]).value;
					
			//alert(color_selected+"_"+size_list[i]+"_"+quantity);
			if(quantity==0)
				{
				var classid=document.getElementById("classid_default").value;
				if(document.getElementById(size_list[i]).value!="")
					{
					var optionid=document.getElementById(size_list[i]).value;
					document.getElementById(classid+"_option_"+optionid).style.backgroundColor=grayoutcolor;
					disableAnchor(classid+"_option_"+optionid);
					}								
				}
			else
				{
				var classid=document.getElementById("classid_default").value;
				if(document.getElementById(size_list[i]).value!="")
					{
					var optionid=document.getElementById(size_list[i]).value;
					enableAnchor(classid+"_option_"+optionid);
					var bg_color=document.getElementById(classid+"_option_"+optionid).style.backgroundColor;
					
					/* if(bg_color=="rgb(219, 214, 195)" || bg_color==grayoutcolor) */
					if(bg_color=="rgb(213, 213, 213)" || bg_color==grayoutcolor)
						{
						document.getElementById(classid+"_option_"+optionid).style.backgroundColor="";
						}
										
					}	
				}
			
		}
	}

    
/* Igor for flash image*/    
function outputStatus(e){
                    
                    if(e.success != true){
                    document.getElementById('FlashOrThumbnailDivSub').innerHTML = '<img alt="" src="http://s7d2.scene7.com/is/image/WhiteandWarren/' + scene7ID + '" id="product_thumbnail" width="371" height="518" />'; 
                    }
}
    
/*
	Rebuild page if some options is changed
*/

function check_options(option_classid, style_code, default_color, multi_products) {


	if(document.getElementById("style_code")!='null')
		document.getElementById("style_code").value=style_code;
	if(document.getElementById("color_code")!='null')
		{
		if(default_color!=null)	
		document.getElementById("color_code").value=default_color;	
		}
	
	if(default_color!=null)
		{
		grayout_empty(default_color);
		}
	
	if (multi_products == null) { multi_products = false; }

	var local_taxes = [];
	var is_rebuild_wholesale = false;
	var variantid = false;

	for (var t in taxes)
		local_taxes[t] = taxes[t][0];
	price = default_price;

	/* Find variant */
	for (var x in variants) {

		if (variants[x][1].length == 0)
			continue;

		variantid = x;

		
		for (var c in variants[x][1]) {
			if (getPOValue(c) != variants[x][1][c] || ((variants[x][1][option_classid] === undefined) && multi_products)) {

				variantid = false;
				break;
			}
		}

		/* if (variants[x][1][option_classid] === undefined) {

			variantid = false;
		} */

		//alert("for " + option_classid + ", option is " + variants[x][1][option_classid]);

		if (variantid) {

			//alert(variantid);

			break;
		}
	
	
	}

	/* var wholevariants = "";
	for (var x in variants) { wholevariants += x + " " ; }
	alert(wholevariants); */

	// alert("variantid is " + variantid + ", po is " + option_classid + " and getpov is " + getPOValue());

	/* If variant found ... */
	if (variantid) {
		//alert(variantid);
		price = variants[variantid][0][0];
		orig_price = variants[variantid][0][4];
		avail = variants[variantid][0][1];
	
		//alert("avail is " + avail + " for style code " + style_code + " and variantid " + variantid);

		/* if (undefined !== eval('window.avail' + style_code)) {

			eval('avail' + style_code) = variants[variantid][0][1];
		} */

		/* Get variant wholesale prices */
		if (variants[variantid][3]) {
			product_wholesale = [];
			for (var t in variants[variantid][3]) {
				var _tmp = modi_price(variants[variantid][3][t][2], cloneObject(variants[variantid][3][t][3]), variants[variantid][3][t][4]);
				product_wholesale[t] = [
					variants[variantid][3][t][0], 
					variants[variantid][3][t][1], 
					_tmp[0],
					[]
				];

				/* Get variant wholesale taxes */
				for (var c in _tmp[1]) {
					product_wholesale[t][3][c] = _tmp[1][c];
				}
			}
			is_rebuild_wholesale = true;
		}

		/* Get variant taxes */
		for (var t in local_taxes) {
			if (variants[variantid][2][t])
				local_taxes[t] = parseFloat(variants[variantid][2][t]);
		}

		if (!product_thumbnail)
			product_thumbnail = document.getElementById('product_thumbnail');

		/* Change product thumbnail */

		/* If we got a style code and a color option, try to get the Flash from Scene7; otherwise fall back on XCart image handling */
        var cur_category = '';
        if (document.getElementById('categoryID')) {
            cur_category = '_' + document.getElementById('categoryID').value;
        }


		if (style_code == null) {

			if (product_thumbnail) {
				if (variants[variantid][0][2].src && variants[variantid][0][2].width > 0 && variants[variantid][0][2].height > 0) {
					if (product_thumbnail.src != variants[variantid][0][2].src) {
						product_thumbnail.src = variants[variantid][0][2].src;
						product_thumbnail.width = variants[variantid][0][2].width;
						product_thumbnail.height = variants[variantid][0][2].height;
					}
				} else if (document.getElementById('product_thumbnail').src != product_image.src) {
					product_thumbnail.src = product_image.src;
					if (product_image.width > 0 && product_image.height > 0) {
						product_thumbnail.width = product_image.width;
						product_thumbnail.height = product_image.height;
					}
				}
			}
		}

		else if ((option_classid != null) && (names[option_classid].class_name.toLowerCase() == 'color')) {

			final_color_name = names[option_classid]['options'][document.getElementById("po"+option_classid).value].toUpperCase();
			final_color_name = final_color_name.replace(" ", "");

			document.getElementById("color_code").value=final_color_name;

			if (document.getElementById('FlashOrThumbnailDiv') !== null) {
				scene7ID = style_code + '_' + final_color_name + cur_category;

				/* replacementString = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ID="' + scene7ID + '" WIDTH="371" HEIGHT="518" ALIGN="">\n';
				replacementString += '<PARAM NAME=movie VALUE="http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/' + scene7ID + '&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf">\n';
				replacementString += '<PARAM NAME=menu VALUE=false>\n';
				replacementString += '<PARAM NAME=quality VALUE=high>\n';
				replacementString += '<PARAM NAME=scale VALUE=noscale>\n';
				replacementString += '<PARAM NAME=salign VALUE=LT>\n';
				replacementString += '<PARAM NAME=bgcolor VALUE=#FFFFFF>\n';
				replacementString += '<param name="wmode" value="opaque"/>\n';
				replacementString += '<EMBED src="http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/' + scene7ID + '&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf" QUALITY="high" BGCOLOR="#ffffff" WIDTH="371" HEIGHT="518" SWLIVECONNECT="true" NAME="10293_REDLANTERN" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="opaque">\n';
				replacementString += '</EMBED>\n';
				replacementString += '</OBJECT>'; */
			
				//document.getElementById('FlashOrThumbnailDiv').innerHTML = replacementString;
                
                if(document.getElementById('FlashOrThumbnailDivSub') == null)
                    document.getElementById('FlashOrThumbnailDiv').innerHTML='<div id="FlashOrThumbnailDivSub"></div>';
                
                
                var flashvars = false;
                var params = {};
                params.menu = "false";
                params.quality = "high";
                params.scale = "noscale";
                params.salign = "LT";
                params.bgcolor = "#FFFFFF";
                params.wmode = "wmode";
                params.swliveconnect = "true";
              
                var attributes = {};
               attributes.name = "10293_REDLANTERN";
               attributes.id = scene7ID;
               attributes.classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
               
               swfobject.embedSWF("http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/" + scene7ID + "&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf", "FlashOrThumbnailDivSub", "371", "518", "6.0.0",
                   "", flashvars, params, attributes,outputStatus);
                   

                
                
                
                
			}
		}

		else if ((option_classid == null) && (default_color != null)) { 

			scene7ID = style_code + '_' + default_color.toUpperCase() + cur_category;
			//alert (scene7ID);
			/*  replacementString = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ID="' + scene7ID + '" WIDTH="371" HEIGHT="518" ALIGN="">\n';
			replacementString += '<PARAM NAME=movie VALUE="http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/' + scene7ID + '&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf">\n';
			replacementString += '<PARAM NAME=menu VALUE=false>\n';
			replacementString += '<PARAM NAME=quality VALUE=high>\n';
			replacementString += '<PARAM NAME=scale VALUE=noscale>\n';
			replacementString += '<PARAM NAME=salign VALUE=LT>\n';
			replacementString += '<PARAM NAME=bgcolor VALUE=#FFFFFF>\n';
			replacementString += '<param name="wmode" value="opaque"/>\n';
			replacementString += '<EMBED src="http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/' + scene7ID + '&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf" QUALITY="high" BGCOLOR="#ffffff" WIDTH="371" HEIGHT="518" SWLIVECONNECT="true" NAME="10293_REDLANTERN" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="opaque">\n';
			replacementString += '</EMBED>\n';
			replacementString += '</OBJECT>'; */
			
			//document.getElementById('FlashOrThumbnailDiv').innerHTML = replacementString;
             if(document.getElementById('FlashOrThumbnailDivSub') == null)
                    document.getElementById('FlashOrThumbnailDiv').innerHTML='<div id="FlashOrThumbnailDivSub"></div>';

            
            var flashvars = false;
             var params = {};
                params.menu = "false";
                params.quality = "high";
                params.scale = "noscale";
                params.salign = "LT";
                params.bgcolor = "#FFFFFF";
                params.wmode = "wmode";
                params.swliveconnect = "true";
              
                var attributes = {};
               attributes.name = "10293_REDLANTERN";
               attributes.id = scene7ID;
               attributes.classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
               
               swfobject.embedSWF("http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/" + scene7ID + "&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf", "FlashOrThumbnailDivSub", "371", "518", "6.0.0",
                   '', flashvars, params, attributes,outputStatus);

                
                
                        
            /* http://white.lbi.net/shop-by-category/cardigans/cashmere-cable-open-cardigan.html */
            
            
		}

		else {	}

		/* Change product weight */
		if (document.getElementById('product_weight'))
			document.getElementById('product_weight').innerHTML = price_format(variants[variantid][0][3]);
		if (document.getElementById('product_weight_box'))
			document.getElementById('product_weight_box').style.display = parseFloat(variants[variantid][0][3]) > 0 ? "" : "none";

		/* Change product code */
		if (document.getElementById('product_code'))
			document.getElementById('product_code').innerHTML = variants[variantid][0][5];

	}

	// else { alert("avail2 is " + avail + " for style code " + style_code + " and option_classid " + option_classid + ". getpv is " + getPOValue(option_classid)); }

	if (pconf_price > 0)
		price = pconf_price;

	/* Find modifiers */
	var _tmp = modi_price(price, local_taxes, orig_price);
	price = _tmp[0];
	local_taxes = _tmp[1];
	if (!variantid) {
		product_wholesale = [];
		for (var t in _product_wholesale) {
			_tmp = modi_price(_product_wholesale[t][2], _product_wholesale[t][3].slice(0), _product_wholesale[t][4]);
			product_wholesale[t] = [
				_product_wholesale[t][0],
				_product_wholesale[t][1],
				_tmp[0],
				_tmp[1]
			];
		}
		is_rebuild_wholesale = true;
	}

	/* Update taxes */
	for (var t in local_taxes) {
		if (document.getElementById('tax_'+t)) {
			document.getElementById('tax_'+t).innerHTML = currency_symbol+price_format(local_taxes[t] < 0 ? 0 : local_taxes[t]);
		}
		current_taxes[t] = local_taxes[t];
	}

	if (is_rebuild_wholesale)
		rebuild_wholesale();

	/* Update form elements */
	/* Update price */
	if (document.getElementById('product_price'))
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(price < 0 ? 0 : price);

	/* Update alt. price */
	if (alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price')) {
		var altPrice = price*alter_currency_rate;
		document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(altPrice < 0 ? 0 : altPrice)+")";
	}

	/* Update Save % */
	if (document.getElementById('save_percent') && document.getElementById('save_percent_box') && list_price > 0 && dynamic_save_money_enabled) {
		var save_percent = Math.round(100-(price/list_price)*100);
		if (save_percent > 0) {
			document.getElementById('save_percent_box').style.display = '';
			document.getElementById('save_percent').innerHTML = save_percent;
		} else {
			document.getElementById('save_percent_box').style.display = 'none';
			document.getElementById('save_percent').innerHTML = '0';
		}
	}

	if (undefined !== eval('window.avail' + style_code) && undefined === window.avail) {

		avail = eval('avail' + style_code);
	}

	if (undefined !== eval('window.min_avail' + style_code)) {

		min_avail = eval('min_avail' + style_code);
	}

	/* Update product quantity */
	if (document.getElementById('product_avail_txt')) {
		if (avail > 0) {
			document.getElementById('product_avail_txt').innerHTML = substitute(txt_items_available, "items", (variantid ? avail : product_avail));
		} else {
			document.getElementById('product_avail_txt').innerHTML = lbl_no_items_available;
		}
	}

	if ((mq > 0 && avail > mq+min_avail) || is_unlimit) {
		avail = mq+min_avail-1;
		//alert("changing avail to mq + min_avail");
	}

	var select_avail = min_avail;
	/* Update product quantity selector */
	//if (!availObj) {
		availObj = document.getElementById('product_avail');
	//}

	if (availObj === null) {
		availObj = document.getElementById('product_avail' + style_code);
	}

	if (availObj && availObj.tagName.toUpperCase() == 'SELECT') {
		if (!isNaN(min_avail) && !isNaN(avail)) {
			var first_value = -1;
			if (availObj.options[0]) {
				first_value = availObj.options[0].value;
			}

			if (first_value == min_avail) {

				/* New and old first value in quantities list is equal */
				if ((avail-min_avail+1) != availObj.options.length) {

					// alert ("new and old first value in qts list is equal");

					if (availObj.options.length > avail) {
						var cnt = availObj.options.length;
						
						for (var x = (avail < 0 ? 0 : avail); x < cnt; x++)
							availObj.options[availObj.options.length-1] = null;
					} else {
						var cnt = availObj.options.length;
						/* availObj.options[0] = new Option("--", "0");
						alert("test1"); */
						for (var x = cnt+1; x <= avail; x++)
							availObj.options[cnt++] = new Option(x, x);
					}
				}
			} else {

				// alert("new and old first value in qts list is different");
				//alert("avail is " + avail);

				/* New and old first value in quantities list is differ */
				while (availObj.options.length > 0)
					availObj.options[0] = null;

				var cnt = 0;
				if (multi_products) { availObj.options[cnt++] = new Option("--", "0"); }

				for (var x = min_avail; x <= avail; x++)
					availObj.options[cnt++] = new Option(x, x);
			}
			if (availObj.options.length == 0)
				availObj.options[0] = new Option(txt_out_of_stock, 0);
		}
		select_avail = availObj.options[availObj.selectedIndex].value;
	}

	check_wholesale(select_avail);

	if ((alert_msg == 'Y') && (min_avail > avail))
		alert(txt_out_of_stock);
	
	/* Check exceptions */
	var ex_flag = check_exceptions();
	if (!ex_flag && (alert_msg == 'Y'))
		alert(exception_msg);
			
	if (document.getElementById('exception_msg'))
		document.getElementById('exception_msg').innerHTML = (ex_flag ? '' : exception_msg_html+"<br /><br />");

	return true;
}

/*
	Calculate product price with price modificators 
*/
function modi_price(_price, _taxes, _orig_price) {
var return_price = round(_price, 2);

	/* List modificators */
	for (var x2 in modifiers) {
		var value = getPOValue(x2);
		if (!value || !modifiers[x2][value])
			continue;

		/* Get selected option */
		var elm = modifiers[x2][value];
		return_price += parseFloat(elm[1] == '$' ? elm[0] : (_price*elm[0]/100));

		/* Get tax extra charge */
		for (var t2 in _taxes) {
			if (elm[2][t2]) {
				_taxes[t2] += parseFloat(elm[1] == '$' ? elm[2][t2] : (_orig_price*elm[2][t2]/100));
			}
		}
	}

	return [return_price, _taxes];
}

/*
	Check product options exceptions
*/
function check_exceptions() {
	if (!exceptions)
		return true;

	/* List exceptions */
	for (var x in exceptions) {
		if (isNaN(x))
			continue;

		var found = true;
        for (var c in exceptions[x]) {
			var value = getPOValue(c);
			if (!value)
				return true;

            if (value != exceptions[x][c]) {
				found = false;
				break;
			}
		}
		if (found)
			return false;
	}

	return true;
}

/*
	Rebuild wholesale tables
*/
function rebuild_wholesale() {

	var obj = document.getElementById('wl_table');
	if (!obj)
		return false;

	/* Clear wholesale span object if product wholesale prices service array is empty */
	if (!product_wholesale || product_wholesale.length == 0) {
		obj.innerHTML = "";
		return false;
	}

	/* Display headline */
	var str = '';
	var i = 0;
	for (var x in product_wholesale) {
		if (product_wholesale[x][0] == 0)
			continue;

		if (i == 0)
			str += '<br /><table cellpadding="2" cellspacing="2"><tr class="TableHead"><td align="right"><b>'+lbl_quantity+':&nbsp;</b></td>';

		str += '<td>'+product_wholesale[x][0];
		if (x == product_wholesale.length-1) {
			str += '+';
		} else if (product_wholesale[x][0] < product_wholesale[x][1]) {
			str += '-'+product_wholesale[x][1];
		}
		str += '&nbsp;'+(product_wholesale[x][0] == 1 ? lbl_item : lbl_items)+'</td>';
		i++;
	}

	if (i == 0)
		return false;

    /* Display wholesale prices taxes */
	var tax_str = '';
    if (taxes.length > 0) {
        for (var x in taxes) {
            if (current_taxes[x] > 0)
                tax_str += substitute(lbl_including_tax, 'tax', taxes[x][1])+'<br />';
        }
    }

	/* Display wholesale prices */
	str += '</tr><tr bgcolor="#EEEEEE"><td align="right"><b>'+lbl_price+(tax_str.length > 0 ? '*' : '')+':&nbsp;</b></td>';
	for (var x in product_wholesale) {
		if (product_wholesale[x][0] == 0)
			continue;
		str += '<td>'+price_format(product_wholesale[x][2] < 0 ? 0 : product_wholesale[x][2])+'</td>';
	}

	str += '</tr></table>';

	if (tax_str.length > 0)
		str += '<br /><table><tr><td class="FormButton" valign="top"><b>*'+txt_note+':</b>&nbsp;</td><td nowrap="nowrap" valign="top">'+tax_str+'</td></tr></table>';

	str += '<br />';
	obj.innerHTML = str;

	return true;
}

/*
	Display current wholesale price as product price
*/
function check_wholesale(qty) {
	if (product_wholesale.length == 0)
		return true;

	var wl_taxes = current_taxes.slice(0);
	var wl_price = price;
	var found = false;
	for (var x = 0; x < product_wholesale.length; x++) {
		if (product_wholesale[x][0] <= qty && (product_wholesale[x][1] >= qty || product_wholesale[x][1] == 0)) {
			wl_price = product_wholesale[x][2];
			wl_taxes = product_wholesale[x][3].slice(0);
			found = true;
		}
		if (document.getElementById('wp'+x)) {
			var wPrice = price-default_price+product_wholesale[x][2];
			document.getElementById('wp'+x).innerHTML = currency_symbol+price_format(wPrice < 0 ? 0 : wPrice);
		}
	}

	if (document.getElementById('product_price'))
		document.getElementById('product_price').innerHTML = currency_symbol+price_format(wl_price < 0 ? 0 : wl_price);
	if (alter_currency_rate > 0 && alter_currency_symbol != "" && document.getElementById('product_alt_price')) {
		var altPrice = wl_price*alter_currency_rate;
		document.getElementById('product_alt_price').innerHTML = "("+alter_currency_symbol+" "+price_format(altPrice < 0 ? 0 : altPrice)+")";
	}

	/* Update Save % */
	if (document.getElementById('save_percent') && document.getElementById('save_percent_box') && list_price > 0 && dynamic_save_money_enabled) {
		var save_percent = Math.round(100-((wl_price < 0 ? 0 : wl_price)/list_price)*100);
		if (save_percent > 0) {
			document.getElementById('save_percent_box').style.display = '';
			document.getElementById('save_percent').innerHTML = save_percent;
		} else {
			document.getElementById('save_percent_box').style.display = 'none';
			document.getElementById('save_percent').innerHTML = '0';
		}
	}


	for (var x in taxes) {
		if (document.getElementById('tax_'+x) && wl_taxes[x] && current_taxes[x]) {
			document.getElementById('tax_'+x).innerHTML = currency_symbol+price_format(wl_taxes[x] < 0 ? 0 : wl_taxes[x]);
		}
	}

	return true;
}

/*
	Get product option value
*/
function getPOValue(c) {
	if (!document.getElementById('po'+c) || document.getElementById('po'+c).tagName.toUpperCase() != 'INPUT')
		return false;
	return document.getElementById('po'+c).value;
}

/*
    Get product option object by class name / class id
*/
function product_option(classid) {
	if (!isNaN(classid))
		 return document.getElementById("po"+classid);

	if (!names)
		return false;

	for (var x in names) {
		if (names[x]['class_name'] != classid)
			continue;
		return document.getElementById('po'+x);
    }

	return false;
}

/*
	Get product option value by class name / or class id
*/
function product_option_value(classid) {
	var obj = product_option(classid);
	if (!obj)
		return false;

	if (obj.type != 'select-one')
		return obj.value;

	var classid = parseInt(obj.id.substr(2));
	var optionid = parseInt(obj.options[obj.selectedIndex].value);
	if (names[classid] && names[classid]['options'][optionid])
		return names[classid]['options'][optionid];

	return false;
}
function style_selected(style_code,default_color)
	{
        var cur_category = '';
        if (document.getElementById('categoryID')) {
            cur_category = '_' + document.getElementById('categoryID').value;
        }
			scene7ID = style_code + '_' + default_color.toUpperCase() + cur_category;

			replacementString = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ID="' + scene7ID + '" WIDTH="371" HEIGHT="518" ALIGN="">\n';
			replacementString += '<PARAM NAME=movie VALUE="http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/' + scene7ID + '&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf">\n';
			replacementString += '<PARAM NAME=menu VALUE=false>\n';
			replacementString += '<PARAM NAME=quality VALUE=high>\n';
			replacementString += '<PARAM NAME=scale VALUE=noscale>\n';
			replacementString += '<PARAM NAME=salign VALUE=LT>\n';
			replacementString += '<PARAM NAME=bgcolor VALUE=#FFFFFF>\n';
			replacementString += '<param name="wmode" value="opaque"/>\n';
			replacementString += '<EMBED src="http://s7d2.scene7.com/is-viewers/flash/genericzoom.swf?serverUrl=http://s7d2.scene7.com/is/image/&contentRoot=http://s7d2.scene7.com/skins/&image=WhiteandWarren/' + scene7ID + '&config=WhiteandWarren/zoom_new&serverSupport=1&splashScreen=WhiteandWarren/SWFs/preloader.swf" QUALITY="high" BGCOLOR="#ffffff" WIDTH="371" HEIGHT="518" SWLIVECONNECT="true" NAME="10293_REDLANTERN" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" wmode="opaque">\n';
			replacementString += '</EMBED>\n';
			replacementString += '</OBJECT>';
			
			document.getElementById('FlashOrThumbnailDiv').innerHTML = replacementString;
	}