function sortArrayPrice(a,b){
	return(a-b);
}
/*
var myarray = rollerrm_arr[3];
var discount_ = rollerrm_arr[0];
var minPrice = eval('Array('+myarray.sort(sortArrayPrice)+')');

function getMinimumPrice(){
	var GetMinPrice = minPrice[0]  * (1-discount_);
	document.getElementById('minprice').innerHTML = GetMinPrice.toFixed(2);
	}

*/

var arr = new Array();
arr = eval('Array('+rollerrm_arr[3]+')');
var sorter = new Array();
sorter = arr.sort(sortArrayPrice);
	
function getMinimumPrice(){
	minP = sorter;
	var discount_ = rollerrm_arr[0];
	document.getElementById('minprice').innerHTML = (minP[0].toFixed(2) * (1-discount_)).toFixed(2);
	}


function getParams()
{
	var idx = document.URL.indexOf('?');
	var params = new Array();
	if (idx != -1)
	{
		var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
		for (var i=0; i<pairs.length; i++)
		{
			nameVal = pairs[i].split('=');
			params[nameVal[0]] = nameVal[1];
		}
	}
	return params;
}

function LoadParams()
{
	params = getParams();
	width = params["width"];
	drop = params["drop"];
	if((drop!="") && (typeof(drop) != "undefined"))
	{
		document.form_calcrollerrm.customdrop.value = unescape(drop);
		AddWidths(document.form_calcrollerrm.customdrop.value, document.form_calcrollerrm.customwidth.value);
	}
	if((width!="") && (typeof(width) != "undefined"))
	{
		document.form_calcrollerrm.customwidth.value = unescape(width);
		GetCustomPrice();
	}
}

function formatDecimal(argvalue, addzero, decimaln)
{
	var numOfDecimal = (decimaln == null) ? 2 : decimaln;
	var number = 1;
	number = Math.pow(10, numOfDecimal);
	argvalue = Math.round(parseFloat(argvalue) * number) / number;
	argvalue = "" + argvalue;
	if (argvalue.indexOf(".") == 0)
	argvalue = "0" + argvalue;
	if (addzero == true)
	{
		if (argvalue.indexOf(".") == -1)
		argvalue = argvalue + ".";
		while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
		argvalue = argvalue + "0";
	}
	return argvalue;
}

function GetCustomPrice()
{
	document.form_calcrollerrm.price.value = '';
	document.form_calcrollerrm.saveprice.value = '';
	for(i=0; i<rollerrm_arr[1].length; i++)
	{
		if(document.form_calcrollerrm.customwidth.value == rollerrm_arr[1][i] && document.form_calcrollerrm.customdrop.value == rollerrm_arr[2][i])
		{
			var salesave = rollerrm_arr[3][i]*rollerrm_arr[0];;
			var saleprice = rollerrm_arr[3][i]-salesave;
			document.form_calcrollerrm.price.value=formatDecimal(saleprice, true, 2);
			document.form_calcrollerrm.saveprice.value=formatDecimal(salesave, true, 2);
			document.form_calcrollerrm.rollerrm.value=i+1;
		}
	}
}

function AddWidths()
{
	sel_width = document.form_calcrollerrm.customwidth.value;
	sel_width_poz = document.form_calcrollerrm.customwidth.selectedIndex;
	sel_drop = document.form_calcrollerrm.customdrop.value;
        sel_drop_poz = document.form_calcrollerrm.customdrop.selectedIndex;

	for(k=document.form_calcrollerrm.customwidth.options.length-1; k>=0; k--)
		document.form_calcrollerrm.customwidth.options[k]=null;

	for(i=0; i<rollerrm_arr[1].length; i++)
	{
		var found=false;
		for(j=0; j<document.form_calcrollerrm.customwidth.options.length; j++)
			if(document.form_calcrollerrm.customwidth.options[j].value==rollerrm_arr[1][i])
				found=true;
		if(!found)
		{
			if (sel_width_poz == j || sel_width == rollerrm_arr[1][i])
			{
				document.form_calcrollerrm.customwidth.options[document.form_calcrollerrm.customwidth.options.length]=new Option(rollerrm_arr[1][i]+"mm", rollerrm_arr[1][i], true, true);
			}
			else
				document.form_calcrollerrm.customwidth.options[document.form_calcrollerrm.customwidth.options.length]=new Option(rollerrm_arr[1][i]+"mm", rollerrm_arr[1][i]);
		}
	}
	AddDrops(document.form_calcrollerrm.customwidth.value, sel_drop, sel_drop_poz);
}

function AddDrops(width, sel_drop, sel_drop_poz)
{
	for(k=document.form_calcrollerrm.customdrop.options.length-1; k>=0; k--)
		document.form_calcrollerrm.customdrop.options[k]=null;
	
	for(i=0; i<rollerrm_arr[2].length; i++)
	{
		if (rollerrm_arr[1][i] == width)
			if (sel_drop_poz == i || sel_drop == rollerrm_arr[2][i])
                        {
				document.form_calcrollerrm.customdrop.options[document.form_calcrollerrm.customdrop.options.length]=new Option(rollerrm_arr[2][i]+"mm", rollerrm_arr[2][i], true, true);
                        }
			else
                        {
				document.form_calcrollerrm.customdrop.options[document.form_calcrollerrm.customdrop.options.length]=new Option(rollerrm_arr[2][i]+"mm", rollerrm_arr[2][i]);
                        }
	}
	GetCustomPrice();
}

function VerifyrollerrmForm()
{
	if(document.form_calcrollerrm.price.value==0)
	{
		alert("Please select a ready made blind!");
		return false;
	}
	return true;
}

function SelectProdType(prodtype)
{
		window.location = prodtype;
}

