var max_sw = screen.width?screen.width-200:800;
var max_sh = screen.height?screen.height-200:600;

function popup(url,w,h){
var sb='no';
if(w>max_sw) {
w=max_sw;
sb='yes';
}
if(h>max_sh) {
h=max_sh;
sb='yes';
}
var win=window.open('','_blank','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars='+sb+',resizable=yes,directories=no,location=no,width='+w+',height='+h+'left=100,top=100');
win.document.write('<HTML><HEAD><TITLE>ZOOM</TITLE></HEAD>');
win.document.write('<BODY topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
win.document.write('<IMG src="'+ url + '">');
win.document.write('</BODY></HTML>');
win.document.close();
}
var ratio = 1.0;

function setpWidth(v) {
var ppc=getPpcValue();
updateValues(v, Math.round(v/ratio), Math.round(v/ppc), Math.round(v/ratio/ppc));
}
function pWidthChanged(newVal)
{
	var ppc=getPpcValue();
	if(isFinite(ppc))
	{
		var v=getValue(newVal);
		if(v && v>0) updateValues(-1, Math.round(v/ratio), Math.round(v/ppc), Math.round(v/ratio/ppc));
		else updateValues(-1, 0, 0, 0);
	}
}
function pHeightChanged(newVal)
{
	var ppc=getPpcValue();
	if(isFinite(ppc))
	{
		var v=getValue(newVal);
		if(v && v>0) updateValues(Math.round(v*ratio), -1, Math.round(v*ratio/ppc), Math.round(v/ppc));
		else updateValues(0, -1, 0, 0);
	}
}
function tWidthChanged(newVal)
{
	var ppc=getPpcValue();
	if(isFinite(ppc))
	{
		var v=getValue(newVal);
		if(v && v>0) updateValues(Math.round(v*ppc), Math.round(v*ppc/ratio), -1, Math.round(v/ratio));
		else updateValues(0, 0, -1, 0);
	}
}
function tHeightChanged(newVal)
{
	var ppc=getPpcValue();
	if(isFinite(ppc))
	{
		var v=getValue(newVal);
		if(v && v>0) updateValues(Math.round(v*ppc*ratio), Math.round(v*ppc), Math.round(v*ratio), -1);
		else updateValues(0, 0, 0, -1);
	}
}
function getPpcValue()
{
	var ppcs=document.getElementById("ppc");
	return parseFloat(ppcs.options[ppcs.selectedIndex].value);
}

function ppcChanged(ppcs)
{
	var ppc=parseFloat(ppcs.options[ppcs.selectedIndex].value);
	if(isFinite(ppc))
	{
		var v=getValue(document.getElementById("Tw").value);
		if(v && v>0) updateValues(Math.round(v*ppc), Math.round(v*ppc/ratio), -1, -1);
		else updateValues(0, 0, -1, 0);
	}
}

function getValue(str)
{
	for(var i=0;i<str.length;i++)
	{
		if(str.charAt(i)<'0' || str.charAt(i)>'9')
		{
			return null;
		}
	}
	var v=parseFloat(str);
	return (v && isFinite(v))?v:null;
}

function updateValues(Pw,Ph,Tw,Th)
{
	var err=(Pw==0||Ph==0||Tw==0||Th==0||(Tw!=-1&&Tw<3)||(Th!=-1&&Th<3)||Tw>35||Th>35);
	updateValue('Pw', Pw, err);
	updateValue('Ph', Ph, err);
	updateValue('Tw', Tw, err);
	updateValue('Th', Th, err);
}
function updateValue(sname,val,err)
{
	if(document.getElementById)
	{
		var tf = document.getElementById(sname);
		if(tf)
		{
			if(val>0) tf.value=(val==0?'':val);
			tf.style.background=err?'#FFAAAA':'#FFFFFF';
			//alert(tf.value);
		}
	}
}

function checkEmail(emailField, errorMsg) {
	var verif     = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{1,}[.][a-zA-Z]{2,3}$/
	if (verif.exec(emailField.value) == null) {
		alert(errorMsg);
		return false;
	}
	return true;
}
function switchVisibleForId(eltId){
	document.getElementById(eltId).style.display=document.getElementById(eltId).style.display=='none'?'block':'none';
}

