// Window pop_up
function toggle_page()
{
	var locvar	= '' + window.location;
	var prefix	= '';
	var oldloc	= locvar;

	if (locvar.substr(7, 3) != 'www')
		locvar = oldloc.substr(0, 7) + 'www.' + oldloc.substr(7);

	if (locvar.substr(0, 13) == 'http://www.fr')
	{
		prefix	= 'http://www' + locvar.substr(13);
	}
	else
	{
		prefix	= 'http://www.fr' + locvar.substr(10);
	}
	window.location = prefix;
}

function pop_window(link, message)
{
	windowsprops = "height=50,width=50,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes";
	//windowsprops = "height=250,width=620,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes";
	
	n = window.open (link, "Popup", windowsprops);
	n.focus();
}

function pop_contact_window(link, message)
{
	windowsprops = "height=500,width=600,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes";
	//windowsprops = "height=250,width=620,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes";
	
	n = window.open (link, "Popup", windowsprops);
	n.focus();
}

function synchronize_commission_selector(M, S)
{
	if (M [1].selected == true)
	{
	   S [6] = new Option ('20" and under', 5);
	}
	else
	{
		if (S [6] != null)
			if (S [6].selected == true)
				S [0].selected = true;

		S [6] = null;
	}
	
	M.style.backgroundColor = M [0].selected == true ? 0xE057FD: 0x9EBED6;
	colorize_size_selector(S);
}

function colorize_size_selector(obj)
{
	obj.style.backgroundColor = obj [0].selected == true ? 0xE057FD: 0x9EBED6;
}

function compute_commission_price(F)
{
	if (F.smedium.value != "" && F.ssize.value != -1 && F.p_subject.value != -1 && F.p_framining.value != -1)
	{
		var base = compute_commission_base_price(F.smedium.value, F.ssize.value);
		
		total_price  = base;	
		total_price += base * compute_commission_subject_framing_factor(F.smedium.value, F.p_subject.value, F.p_framining.value);
		total_price += base * compute_commission_background_detail_factor(F.smedium.value, F.p_background.value, F.p_detail.value);
		
	
		var nf = new NumberFormat(total_price);
		nf.setPlaces(0);
		nf.setSeparators(true);

		F.cprice.value = "US$ " + nf.toFormatted();
	}
	else
		F.cprice.value = "--";
}

function compute_commission_base_price(medium, size)
{
	switch (medium)
	{
		case 'acrylic':
			base_array = new Array(50, 100, 200, 350, 500, 700);
			break;
			
		case 'watercolor':
			base_array = new Array(50, 100, 175, 300, 450);
			break;
			
		case 'colored pencil':
			base_array = new Array(40, 75, 150, 250, 400);
			break;
			
		case 'graphite':
			base_array = new Array(30, 65, 130, 250, 400);
			break;
			
		default:
			base_array = new Array(-1, -1, -1, -1, -1, -1);
	}
	
	return base_array [size];
}

function compute_commission_subject_framing_factor(medium, subjects, framing)
{
	switch (medium)
	{
		case 'acrylic':
			subject_fees = new Array(0.0, 0.10, 0.30);
			framing_fees = new Array(0.0, 0.05, 0.20);
			break;
			
		case 'watercolor':
			subject_fees = new Array(0.0, 0.10, 0.30);
			framing_fees = new Array(0.0, 0.05, 0.20);
			break;
			
		case 'colored pencil':
			subject_fees = new Array(0.0, 0.10, 0.30);
			framing_fees = new Array(0.0, 0.05, 0.20);
			break;
			
		case 'graphite':
			subject_fees = new Array(0.0, 0.10, 0.30);
			framing_fees = new Array(0.0, 0.05, 0.20);
			break;
			
		default:
			subject_fees = new Array(0.0, 0.0, 0.0);
			framing_fees = new Array(0.0, 0.0, 0.0);
	}
	
	return subject_fees [subjects] + framing_fees [framing];
}

function compute_commission_background_detail_factor(medium, background_x, detail_x)
{
	switch (medium)
	{
		case 'acrylic':
			background = compute_exp_cooef(0.5,  5, 1, 20);
			detail     = compute_exp_cooef(0.5, 10, 1, 60);
			break;
			
		case 'watercolor':
			background = compute_exp_cooef(0.5,  5, 1, 30);
			detail     = compute_exp_cooef(0.5, 10, 1, 60);
			break;
			
		case 'colored pencil':
			background = compute_exp_cooef(0.5, 10, 1, 30);
			detail     = compute_exp_cooef(0.5, 10, 1, 60);
			break;
			
		case 'graphite':
			background = compute_exp_cooef(0.7, 31, 1, 50);
			detail     = compute_exp_cooef(0.5, 10, 1, 60);
			break;
			
		default:
			background = compute_exp_cooef(0, 0, 1, 0);
			detail     = compute_exp_cooef(0, 0, 1, 0);
	}
	
	return exponentialize(background, background_x) + exponentialize(detail, detail_x);
}

function exponentialize(coeff, value)
{
	return (coeff [0] * (Math.exp(coeff [1] * (value / 100)) - 1)) / 100;
}

function compute_exp_cooef(x1, y1, x2, y2)
{
	var b = Math.log( (y1 + 1) / (y2 + 1) ) / (x1 - x2);
	var a = (y1 + 1) * Math.exp( -b * x1 );
	
	return new Array (a, b);
}

function check_commission_request(obj)
{
	if (error_commission_request_object(obj.first_name)
		| error_commission_request_object(obj.last_name)
		| error_commission_request_object(obj.email)
		| error_commission_request_object(obj.zip_code)
		| error_commission_request_object(obj.country) )
	{
		alert ("Please fill in the requested fields (*)");
	}
	else
		obj.submit ();
}

function error_commission_request_object(obj)
{
	if (obj.value == "")
	{
		obj.style.backgroundColor = 0xE057FD;
		return true;
	}
	
	return false;
}
