// $Id: func.js,v 1.16.2.17 2007/09/25 13:23:12 ferz Exp $ 

var current_taxes = [];
var product_thumbnail = document.getElementById('product_thumbnail');
var availObj = document.getElementById('product_avail');
var min_avail;
/*
	Rebuild page if some options is changed
*/

function check_options() {
	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]) {
				variantid = false;
				break;
			}
		}

		if (variantid)
			break;
	}

	/* If variant found ... */
	if (variantid) {
		price = variants[variantid][0][0];
		orig_price = variants[variantid][0][4];
		avail = 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 (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;
				}
			}
		}

		/* 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];

	}

	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';
		}
	}

	/* 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;

	avail = avail > mq ? mq : avail;

	var select_avail = min_avail;
	/* Update product quantity selector */
	if (!availObj)
		availObj = document.getElementById('product_avail');

	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) {
					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;
						for (var x = cnt+1; x <= avail; x++) 
							availObj.options[cnt++] = new Option(x, x);
					}
				}
			} else {

				/* New and old first value in quantities list is differ */
				while (availObj.options.length > 0)
					availObj.options[0] = null;
				var cnt = 0;
				for (var x = min_avail; x <= avail; x++)
					availObj.options[cnt++] = new Option(x, x);

			}
			if (availObj.options.length == 0 || min_avail > avail) 
				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() != 'SELECT')
		return false;
	return document.getElementById('po'+c).options[document.getElementById('po'+c).selectedIndex].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;
}

// ML colour & validation functions

var idBubbleT;
var chrono = null;
var delai = "50";
 
function showBubble(idBubble, parent)
{
  var bubble = document.getElementById(idBubble);
  var offset;
  var exp = new RegExp("^td_","gi");
 
  if (chrono!=null)
  {
     clearTimeout(chrono);
     hideBubbleT();
  }

  document.getElementById('ColorRolloverFielsImageContainer').style.display = "block"; 
  bubble.style.display = "block";
  //bubble.style.top="10px"

  if (exp.test(idBubble)==false)
  {
    if (parent.offsetLeft-bubble.offsetWidth<0)
       offset = 0;
    else
       offset = -bubble.offsetWidth;
  
    bubble.style.left = parent.offsetLeft+offset+"px";
  }
}
 
function hideBubbleT()
{
  document.getElementById(idBubbleT).style.display = "none";
  chrono = null;
}
 
function hideBubble(idBubble)
{
  idBubbleT = idBubble;
  chrono = setTimeout("hideBubbleT()",delai);
 
  //document.getElementById(idBulle).style.display = "none";
}
 
function mouseOverBubble()
{
  clearTimeout(chrono);
  chrono = null;
}
 
function mouseOutBubble()
{
   chrono = setTimeout("hideBubbleT()",delai);
}

var eMessageBlock=document.getElementById("errorMessage")
function errorMissing( elem, msg ){
if(elem.selectedIndex == 0){
	elem.className += (elem.className=='')?'error':' error';
	eMessageBlock.innerHTML+=msg+'<br>';
	eMessageBlock.style.display = "block";
	alert(msg); 
	elem.focus();
	return false;
}
return true;
}

function errorClear( elem ){
	if (elem) if (elem.className){
	elem.className=elem.className.replace(' error', '');
	elem.className=elem.className.replace('error', '');
	}
}
function errorReset( ){
	eMessageBlock.style.display = "none";
	eMessageBlock.innerHTML='';
for (i in document.multipleform.elements){
errorClear(document.multipleform.elements[i])
}
}

var rowSuffix=0;

function addRow(rowID,trigger){
	var myTR=document.getElementById(rowID);
	var newRow;
	newRow = myTR.cloneNode(true);
	// now change the form IDs to add :suffix
	// the form elements are in the grandchildren of the row
	rowSuffix++;

	for (var i in newRow.childNodes)
	{
		if(newRow.childNodes[i])
		{
			var c;
			var c=newRow.childNodes[i]
			for (var j in c.childNodes)
			{
				var g;
				if(c.childNodes[j])
				{
					var g=c.childNodes[j];
					if(g.nodeName)
						if((g.nodeName=="SELECT")||(g.nodeName=="INPUT")||(g.nodeName=="SPAN"))
						{
							g.id=g.id.split(':')[0]+":"+rowSuffix;
							if ((g.id.split(':')[0] == "product_avail") || (g.id.split(':')[0] == "m_product_avail"))g.name = 'empty';
							
				 			if((g.id.split(':')[0]== "m_availP") || (g.id.split(':')[0] == "m_priceP") || (g.id.split(':')[0] == "availP") || (g.id.split(':')[0] == "priceP"))
							{
								if((g.nodeName=="SELECT")||(g.nodeName=="INPUT"))g.value='';
								if(g.nodeName=="SPAN")g.innerHTML='';
							}
						}
				}
			}
		}
	}
	//myTR.parentNode.insertBefore(newRow, myTR.nextSibling);
	myTR.parentNode.insertBefore(newRow, document.getElementById(trigger));
	return false;
}

function showprice(elem,aid) {
// find the matched set of 5 elems
var id=elem.id.split(':');
var suffix= (id[1])?':'+id[1]:'';

var col=document.getElementById('color'+suffix);
var siz=document.getElementById('size'+suffix);
//var sty=document.getElementById('style'+suffix);
var pri=document.getElementById('priceP'+suffix);
var ava=document.getElementById('availP'+suffix);
var amt=document.getElementById('product_avail'+suffix);

if((col.selectedIndex != 0) && (siz.selectedIndex != 0)) {
//  var aid=sty.options[sty.selectedIndex].value;

  var color = col.options[col.selectedIndex].value;
  var size  = siz.options[siz.selectedIndex].value;

  var amount = amt.options[amt.selectedIndex].value;
  var amt_sel = amt.selectedIndex;
  
  var max_amount = (eval('product['+aid+']['+color+']['+size+']')[3]);
    /* mq - maximum quantity in selectbox , maximum_order_items - maximum of total orders */
    if ((min_avail > max_amount) || (min_avail > mq))
    max_amount = 0;


  /* add if neccessary */
  if (amount > max_amount)
  {
  	amount = max_amount;
	amt_sel = max_amount+1;
  }


  amt.selectedIndex = amt_sel;  
  
  if (max_amount == 0)
  {
	amt.selectedIndex = 0;
	amt.options[0].value = 0;
  }
/*  else
  {
    amt.options[0].value = min_avail;
	if (amt_sel == 0)amount = min_avail;
  }
*/
  /* Update available amount */
  /* delete if neccessary */
  /*
  if ((amt.options.length-1) > max_amount)
  {
		amt.options.length = max_amount+1;
		if (amt_sel == amt.options.length)
		  amt.selectedIndex = amt_sel-1;  
  }
  */

  amt.options.length = 1;
  /* add if neccessary */
  if ((amt.options.length <= max_amount) && (amt.options.length<mq))
  {
//	  for (var x = amt.options.length; (x <= max_amount) && (x<mq); x++)
	  for (var x = min_avail; (x <= max_amount) && (x<=mq); x++)
	  {
	  	  var na;
		  na = new Option(x,x,false,true);
		  amt.options[amt.options.length] = na;
	  }
  }

  if ((amt_sel ==0) && (amt.options.length > 1)){amt_sel = 1;}
  if (amt_sel > amt.options.length-1){amt_sel = amt.options.length-1;}
  amt.selectedIndex = amt_sel; 

  amount = amt.options[amt.selectedIndex].value;  
  price = new String((eval('product['+aid+']['+color+']['+size+']')[2])*amount);

  //if(price.indexOf('.') < 0) {price += '.00';}
  if(price != 0)
  {
	if((price.indexOf('.') == (price.length-2)) && (price.indexOf('.') != -1)){price += '0';}  
	if (price.indexOf('.') != -1)price = price.substring(0,price.indexOf('.')+3);	
  	
//  	if(alter_currency_symbol == '')
//  	{
//  		pri.value = currency_symbol+price;
//  	}
//  	else
//  	{   
//    	pri.value = alter_currency_symbol+price;
//  	}
		pri.value =  get_currency_symbol(price);
  }
  else if (price == 0)pri.value ='';
  ava.innerHTML = eval('product['+aid+']['+color+']['+size+']')[0];
  if (max_amount>0)ava.style.color = 'green';
  if (max_amount<=0)ava.style.color = 'red';
  amt.name='amount['+ eval('product['+aid+']['+color+']['+size+']')[1] +']';  
  
}

if(((col.selectedIndex == 0) || (siz.selectedIndex == 0) || (amt.selectedIndex == 0)) && (max_amount != 0))  {
	ava.innerHTML = '';
	pri.value = '';
}

return false;
}
/*
function m_showprice(elem) {
// find the matched set of 5 elems
var id=elem.id.split(':');
var suffix= (id[1])?':'+id[1]:'';

var col=document.getElementById('m_Color'+suffix);
var siz=document.getElementById('m_Size'+suffix);
var sty=document.getElementById('style'+suffix);
var pri=document.getElementById('m_priceP'+suffix);
var ava=document.getElementById('m_availP'+suffix);
var amt=document.getElementById('m_product_avail'+suffix);
if((col.selectedIndex != 0) && (sty.selectedIndex != 0) && (siz.selectedIndex != 0)) {
  var aid=sty.options[sty.selectedIndex].value;
  var colorIndex = col.selectedIndex-1;
  var sizeIndex = siz.selectedIndex-1;
  pri.value = '$'+eval('priceArray'+aid+'['+colorIndex+']['+sizeIndex+']');
  ava.value = eval('optionsArray'+aid+'['+colorIndex+']['+sizeIndex+']');
  amt.name='amount['+ eval('variantArray'+aid+'['+colorIndex+']['+sizeIndex+']') +']';  
}


return false;
}
*/

function m_showprice(elem) {
// find the matched set of 5 elems
var id=elem.id.split(':');
var suffix= (id[1])?':'+id[1]:'';

var col=document.getElementById('m_Color'+suffix);
var siz=document.getElementById('m_Size'+suffix);
var sty=document.getElementById('style'+suffix);
var pri=document.getElementById('m_priceP'+suffix);
var ava=document.getElementById('m_availP'+suffix);
var amt=document.getElementById('m_product_avail'+suffix);

var aid=sty.options[sty.selectedIndex].value;

/* Update Color and Size codes */
if(id[0] == 'style')
{
 for (var x = 0;x < col.options.length-1;x++)
 {
 	for (var y = 0;y < eval('colors['+aid+']').length-1;y++)
	{
//		alert(aid+'#'+(eval('colors['+aid+']')[y][1])+'# #'+col.options[x+1].innerHTML+'#');
		if((eval('colors['+aid+']')[y][1]) == col.options[x+1].innerHTML)		
		 	col.options[x+1].value = (eval('colors['+aid+']')[y][0]);
	}
 }
 
 for (var x = 0;x < siz.options.length-1;x++)
 {
 	for (var y = 0;y < eval('sizes['+aid+']').length-1;y++)
		if ((eval('sizes['+aid+']')[y][1]) == siz.options[x+1].innerHTML)
		 	siz.options[x+1].value = (eval('sizes['+aid+']')[y][0]);
 }

}

if((col.selectedIndex != 0) && (sty.selectedIndex != 0) && (siz.selectedIndex != 0)) {
  
  var color = col.options[col.selectedIndex].value;
  var size  = siz.options[siz.selectedIndex].value;


  var amount = amt.options[amt.selectedIndex].value;
  var amt_sel = amt.selectedIndex;
try{
  var max_amount = (eval('product['+aid+']['+color+']['+size+']')[3]);
  var min_amount = (eval('product['+aid+']['+color+']['+size+']')[4]);

}
catch (e)
{
	//document.getElementById('develop').innerHTML += '1';
	var max_amount = 0;
//    var min_amount = (eval('product['+aid+']['+color+']['+size+']')[4]);
}
 /*
  function errorerror(message,file,line){
  	document.getElementById('develop').innerHTML = 'Hi' + message;
  }
  
  window.onerror = errorerror;
  */



    /* mq - maximum quantity in selectbox , maximum_order_items - maximum of total orders */
    if ((min_amount > max_amount) || (min_amount > mq))
    max_amount = 0;


  /* add if neccessary */
  if (amount > max_amount)
  {
  	amount = max_amount;
	amt_sel = max_amount+1;
  }


  amt.selectedIndex = amt_sel;  
  
  if (max_amount == 0)
  {
	amt.selectedIndex = 0;
	amt.options[0].value = 0;
  }
/*  else
  {
    amt.options[0].value = min_amount;
	if (amt_sel == 0)amount = min_amount;
  }
*/
  /* Update available amount */
  /* delete if neccessary */
  /*
  if ((amt.options.length-1) > max_amount)
  {
		amt.options.length = max_amount+1;
		if (amt_sel == amt.options.length)
		  amt.selectedIndex = amt_sel-1;  
  }
  */

  amt.options.length = 1;
  /* add if neccessary */
  if ((amt.options.length <= max_amount) && (amt.options.length<mq))
  {
//	  for (var x = amt.options.length; (x <= max_amount) && (x<mq); x++)
	  for (var x = min_amount; (x <= max_amount) && (x<=mq); x++)
	  {
	  	  var na;
		  na = new Option(x,x,false,true);
		  amt.options[amt.options.length] = na;
	  }
  }
  if ((amt_sel ==0) && (amt.options.length > 1)){amt_sel = 1;}  
  if (amt_sel > amt.options.length-1){amt_sel = amt.options.length-1;}
  amt.selectedIndex = amt_sel; 

  amount = amt.options[amt.selectedIndex].value;  
try{  

  price = new String((eval('product['+aid+']['+color+']['+size+']')[2])*amount);
}
catch (e)
{
	//document.getElementById('develop').innerHTML += '2';
  var price =  0;
}
  //if(price.indexOf('.') < 0) {price += '.00';}

  if(price != 0)
  {
	if((price.indexOf('.') == (price.length-2)) && (price.indexOf('.') != -1)){price += '0';}  	
	if (price.indexOf('.') != -1)price = price.substring(0,price.indexOf('.')+3);

	// get currency symbol
	// alter_currency_symbol_position
	pri.value = get_currency_symbol(price);
//  	if(alter_currency_symbol == '')
//  	{
//  		pri.value = currency_symbol+price;
//  	}
//  	else
//  	{
//    	pri.value = alter_currency_symbol+price;
//  	}
  }
  else
  { if (price == 0)pri.value ='';
  }
  
try{
  ava.innerHTML = eval('product['+aid+']['+color+']['+size+']')[0];
 }
catch (e)
{
	//document.getElementById('develop').innerHTML += '3';
  ava.innerHTML = txt_out_of_stock;
}

  if (max_amount>0)ava.style.color = 'green';
  if (max_amount<=0)ava.style.color = 'red';

 
try{
  amt.name='amount['+ eval('product['+aid+']['+color+']['+size+']')[1] +']';  
 }
catch (e)
{
	//document.getElementById('develop').innerHTML += '4';
  amt.name='amount[]';  
}


}

if(((col.selectedIndex == 0) || (siz.selectedIndex == 0) || (amt.selectedIndex == 0) || (sty.selectedIndex == 0)) && (max_amount != 0))  {
	ava.innerHTML = '';
	pri.value = '';
}

return false;
}



function related_showprice(elem,aid) {
// find the matched set of 5 elems
var id=elem.id.split(':');
var suffix= (id[1])?':'+id[1]:'';

var col=document.getElementById('product_options_0'+suffix);
var siz=document.getElementById('product_options_1'+suffix);
//var sty=document.getElementById('style'+suffix);
var pri=document.getElementById('related_priceP'+suffix);
var ava=document.getElementById('related_availP'+suffix);
var amt=document.getElementById('amount_related'+suffix);

if((col.selectedIndex != 0)  && (siz.selectedIndex != 0)) {

//  var aid=sty.options[sty.selectedIndex].value;
  var color = col.options[col.selectedIndex].value;
  var size = siz.options[siz.selectedIndex].value;


  var amount = amt.options[amt.selectedIndex].value;
  var amt_sel = amt.selectedIndex;

  var max_amount = (eval('product['+aid+']['+color+']['+size+']')[3]);
  var min_amount = (eval('product['+aid+']['+color+']['+size+']')[4]);
    /* mq - maximum quantity in selectbox , maximum_order_items - maximum of total orders */
    if ((min_amount > max_amount) || (min_amount > mq))
    max_amount = 0;


  /* add if neccessary */
  if (amount > max_amount)
  {
  	amount = max_amount;
	amt_sel = max_amount+1;
  }


  amt.selectedIndex = amt_sel;  

  if (max_amount == 0)
  {
	amt.selectedIndex = 0;
	//amt.options[0].value = 0;
  }
/*  else
  {
    amt.options[0].value = min_amount;
	if (amt_sel == 0)amount = min_amount;
  }
*/

  /* Update available amount */
  /* delete if neccessary */
  /*
  if ((amt.options.length-1) > max_amount)
  {
		amt.options.length = max_amount+1;
		if (amt_sel == amt.options.length)
		  amt.selectedIndex = amt_sel-1;  
  }
  */

  amt.options.length = 1;
  /* add if neccessary */
  if ((amt.options.length <= max_amount) && (amt.options.length<mq))
  {
//	  for (var x = amt.options.length; (x <= max_amount) && (x<mq); x++)
	  for (var x = min_amount; (x <= max_amount) && (x<=mq); x++)
	  {
		  na = new Option(x,x,false,true);
		  amt.options[amt.options.length] = na;
	  }
  }
  if ((amt_sel ==0) && (amt.options.length > 1)){amt_sel = 1;}  
  if (amt_sel > amt.options.length-1){amt_sel = amt.options.length-1;}
  amt.selectedIndex = amt_sel; 

  amount = amt.options[amt.selectedIndex].value;  
  
  price = new String((eval('product['+aid+']['+color+']['+size+']')[2])*amount);
  //if(price.indexOf('.') < 0) {price += '.00';}
  if(price != 0)
  {
	if((price.indexOf('.') == (price.length-2)) && (price.indexOf('.') != -1)){price += '0';}  
	if (price.indexOf('.') != -1)price = price.substring(0,price.indexOf('.')+3);
		
//  	if(alter_currency_symbol == '')
//  	{
//  		pri.value = currency_symbol+price;
//  	}
//  	else
//  	{
//    	pri.value = alter_currency_symbol+price;
//  	}
		pri.value =  get_currency_symbol(price);
  }
  else if (price == 0)pri.value ='';
  ava.innerHTML = eval('product['+aid+']['+color+']['+size+']')[0];
  if (amount>0)ava.style.color = 'green';
  if (amount<=0)ava.style.color = 'red';

  amt.name='amount['+ eval('product['+aid+']['+color+']['+size+']')[1] +']';  
  
}

if(((col.selectedIndex == 0) || (siz.selectedIndex == 0) || (amt.selectedIndex == 0)) && (max_amount != 0))  {
	ava.innerHTML = '';
	pri.value = '';
}

return false;
}



function showprice_onepiece(elem) {
var pri=document.getElementById('priceP');
var ava=document.getElementById('availP');
var amt=document.getElementById('product_avail');

var amount = amt.options[amt.selectedIndex].value;
var amt_sel = amt.selectedIndex;
var max_amount = (eval('product['+elem+']')[3]);

  /* mq - maximum quantity in selectbox , maximum_order_items - maximum of total orders */
  if ((min_avail > max_amount) || (min_avail > mq))
  max_amount = 0;

 /* add if neccessary */
 if (amount > max_amount)
 {
 	amount = max_amount;
	amt_sel = max_amount+1;
 }

 amt.selectedIndex = amt_sel;  
 
 if (max_amount == 0)
 {
	amt.selectedIndex = 0;
	amt.options[0].value = 0;
 }
/*
 amt.options.length = 1;
 if ((amt.options.length <= max_amount) && (amt.options.length<mq))
 {
//	  for (var x = amt.options.length; (x <= max_amount) && (x<mq); x++)
  for (var x = min_avail; (x <= max_amount) && (x<=mq); x++)
  {
	  na = new Option(x,x,false,true);
	  amt.options[amt.options.length] = na;
  }
 }
*/
 if (amt_sel > amt.options.length-1){amt_sel = amt.options.length-1;}
 amt.selectedIndex = amt_sel; 

 amount = amt.options[amt.selectedIndex].value;  
 price = new String(eval('product['+elem+'][2]')*amount);
 //if(price.indexOf('.') < 0) {price += '.00';}
 if(price != 0)
 {
	if((price.indexOf('.') == (price.length-2)) && (price.indexOf('.') != -1)){price += '0';}  
	if (price.indexOf('.') != -1)price = price.substring(0,price.indexOf('.')+3);	
//	 	if(alter_currency_symbol == '')
//	 	{
//	 		pri.value = currency_symbol+price;
//	 	}
//	 	else
//	 	{   
//	   	pri.value = alter_currency_symbol+price;
//	 	}
		pri.value =  get_currency_symbol(price);
	 ava.innerHTML = eval('product['+elem+'][0]');
	 if (max_amount>0)ava.style.color = 'green';
	 if (max_amount<=0)ava.style.color = 'red';
 }
 else if (price == 0){pri.value =''; ava.innerHTML = '';}

return false;
}

function get_currency_symbol(price)
{
  	if(alter_currency_symbol == '')
  	{
  		return currency_symbol+price;
  	}
  	else
  	{
	//    	pri.value = alter_currency_symbol+price;
		if(alter_currency_symbol_position)
                {
			if(alter_currency_symbol_position == 'after')
				return price + alter_currency_symbol;
			else
				return alter_currency_symbol + price ;
                }
                else
                    return alter_currency_symbol + price ;
	
  	}
}

function validLine(type, suffix){
if (type==1){

// Felix 140408 , Do not must buy first item
//if (suffix=='') return true
//return (document.getElementById('product_avail'+suffix).selectedIndex!=0);
if ((!document.getElementById('size')) && (!document.getElementById('color')) && (document.getElementById('product_avail')))
	{return true;}
	else
	{
		return ((document.getElementById('size'+suffix).selectedIndex != 0) ||
		    (document.getElementById('color'+suffix).selectedIndex != 0) ||
		    (document.getElementById('product_avail'+suffix).selectedIndex != 0));
	}
}
if (type==2){
return ((document.getElementById('style'+suffix).selectedIndex != 0) ||
    (document.getElementById('m_Size'+suffix).selectedIndex != 0) ||
    (document.getElementById('m_Color'+suffix).selectedIndex != 0) ||
    (document.getElementById('m_product_avail'+suffix).selectedIndex != 0));
}
if (type==3){
return ((document.getElementById('product_options_0'+suffix).selectedIndex != 0) ||
    (document.getElementById('product_options_1'+suffix).selectedIndex != 0) ||
    (document.getElementById('amount_related'+suffix).selectedIndex != 0));
}
return true;
}

function validField(e){
if(e){
if (e.id){
var id=e.id.split(':');
var suffix= (id[1])?':'+id[1]:'';
var f=e.form;
switch (id[0]){
case 'size':
if (validLine(1,suffix)){ return errorMissing(e, 'Please select a size')};
break;
case 'color':
if (validLine(1,suffix)){ return errorMissing(e, 'Please select a color')};
break;
case 'product_avail': 
 if (validLine(1,suffix)){ return errorMissing(e, 'Please select a quantity')};

//if (document.getElementById('product_avail'+suffix).options[document.getElementById('product_avail'+suffix).selectedIndex].value == 0)
//{ return errorMissing(e, 'Sorry, this product is sold out.')};




break;
case 'style':
if (validLine(2,suffix)){ return errorMissing(e, 'Please select a style')};
break;
case 'm_Size':
if (validLine(2,suffix)){ return errorMissing(e, 'Please select a size')};
break;
case 'm_Color':
if (validLine(2,suffix)){ return errorMissing(e, 'Please select a color')};
break;
case 'm_product_avail':
if (validLine(2,suffix)){ return errorMissing(e, 'Please select a quantity')};
break;
case 'product_options_0':
if (validLine(3,suffix)){ return errorMissing(e, 'Please select a color')};
break;
case 'product_options_1':
if (validLine(3,suffix)){ return errorMissing(e, 'Please select a size')};
break;
case 'amount_related':
if (validLine(3,suffix)){ return errorMissing(e, 'Please select a quantity')};
break;

default:
return true
}
}
}
return true
}





