  var geocoder=top.geocoder;

//change lat & lng for lat & lng of an address
function setLatLng(form,zipcode,city,street,lat,lng)
{
  var address = street+" "+city+" "+zipcode; //declare a address with zipcode city ans street
  top.geocoder.getLatLng(address, function (point) { //try to localise this address and execute function
        if (!point) {
          alert(address + VARADRESSNOTFOUND);
        } else {
        top.map.setCenter(point, 14);
		lat.value=point.lat(); // get lat of the point
		lng.value=point.lng(); // get mng of the point
		// var vartext='top.document:'
		// for(i in top.document)
		// {
			// vartext=vartext+'\n ** '+i+' ==> '+top.document[i];
		// }
		// alert(vartext);
		form.submit(); // send the form
	  }
  });
}

//check if the mail's field value is an email type
function validateEmail(field,msg)
{
	with (field)
	{	
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		{
			field.value="";
			alert(msg);
			return false;
		}
		else if (!validationChars(field,msg,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@_-."))
		{
			return false;
		}
		return true;
	}
}

// check if the field contain just number
function validationNum(field,msg)
{
	validChars="0123456789 +-._";
    for (i=0; i<field.length; i++)
	{
		if (!(validChars.indexOf(field.charAt(i)) != -1))
		{
			alert(msg);
			return false;
		}
	}
	return true;
}

// check if a field is empty
function isEmpty(field,msg) 
{
    if (field.value == '') 
	{
		alert(msg);
		field.focus();
		if(field.style){field.style.backgroundColor = 'red';}
		return false;
    }
	if(field.style){field.style.backgroundColor = 'white';}
    return true;
}

// check if the second password is the same than the first
function checkPassword(field,msg)
{
    if (field.value != document.getElementById("PASSWORD").value) 
	{
		alert(msg);
		if(field.style){
		field.style.backgroundColor = 'red';}
		return false;
    }
	if(field.style){
	field.style.backgroundColor = 'white';}
    return true;
}

// check if field contain just valid chars
function validationChars(field,msg,validChars)
{
    for (i=0; i<field.value.length; i++)
	{
		if (!(validChars.indexOf(field.value.charAt(i)) != -1))
		{
			field.focus();
			alert(msg);
			if(field.style){field.style.backgroundColor = 'red';}
			return false;
		}
		if(field.style){field.style.backgroundColor = 'white';}
	}
	return true;
}

// ask db if login already exist
function loginExist(field,msg)
{
	if (field.value){sendRequest(field,field.value,msg);}
	else {sendRequest(0,"");}
}

function loginExist2(field, sessionLogin)
{
	if (sessionLogin == field.value){handleReponse(field,0,"0");}
	else{
		if (field.value){sendRequest(field,field.value,"0");}
		else {sendRequest(0,"0","0");}
	}
}

function sendRequest(field,login,msg)
{
	var requestHttp=getRequestHttp();
	if (requestHttp==null)
	{
		alert(VARUNABLEDTOUSEAJAX);
	}
	else
	{
		requestHttp.open('POST','../db/loginExist.php5',true);
		requestHttp.onreadystatechange=function() {response(field,requestHttp,msg);};
		requestHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		requestHttp.send('login=' + escape(login));
	}
return;
}

function getRequestHttp() // object requestHttp
{
	var requestHttp;
	if (window.XMLHttpRequest)
	{	// Mozilla
		requestHttp=new XMLHttpRequest();
		if (requestHttp.overrideMimeType)
		{ // firefox
			requestHttp.overrideMimeType('text/xml');
		}
	}
	else
	{
		if (window.ActiveXObject)
		{	//Internet explorer < IE7
			try
			{
				requestHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					requestHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					requestHttp=null;
				}
			}
		}
	}
	return requestHttp;
}

function response(field,requestHttp,msg) // lesten the state of request to lunch handle when it done
{
	if (requestHttp.readyState==4)
	{
		if (requestHttp.status==200)
		{
			handleReponse(field,requestHttp.responseText,msg);
		}
		else
		{
		//	alert("request cannot be executed");
		}
	}
}

function handleReponse(field,data,msg) // whrite in field the last information 
{
	if (msg=="0")
	{
		if(field.style){
		field.style.backgroundColor = 'red';}
		if (data!=0)
		{
			if(field.style){
			field.style.backgroundColor = 'white';}
			resize();
		}
	}
	else
	{
		document.getElementById("loginExist").innerHTML="";
		if(field.style){
		field.style.backgroundColor = 'white';}
		if (data!=0)
		{
			document.getElementById("loginExist").innerHTML="<br>"+msg;
			if(field.style){
			field.style.backgroundColor = 'red';
			field.focus();}
			resize();
		}
	}
}

// ask db if email already exist
function emailExist(field,msg)
{
	var email=field.value;
	if (email){sendRequest1(field,email,msg);}
	else {sendRequest1(0,"");}
}

function sendRequest1(field,email,msg)
{
	var requestHttp=getRequestHttp();
	if (requestHttp==null)
	{
		alert(VARUNABLEDTOUSEAJAX);
	}
	else
	{
		requestHttp.open('POST','../db/emailExist.php5',true);
		requestHttp.onreadystatechange=function() {response1(field,requestHttp,msg);};
		requestHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		requestHttp.send('email=' + escape(email));
	}
return;
}

function response1(field,requestHttp,msg) // lesten the state of request to lunch handle when it done
{
	if (requestHttp.readyState==4)
	{
		if (requestHttp.status==200)
		{
			handleReponse1(field,requestHttp.responseText,msg);
		}
		else
		{
		//	alert("request cannot be executed");
		}
	}
}

function handleReponse1(field,data,msg) // whrite in field the last information 
{
	//alert(data);
	document.getElementById("emailExist").innerHTML="";
	if(field.style){
	field.style.backgroundColor = 'white';}
	if (data!=0) 
	{
		document.getElementById("emailExist").innerHTML="<br>"+msg;
		if(field.style){
		field.style.backgroundColor = 'red';
		field.focus();}
		resize();
	}
}

// change the size of the windows popup to its child
function resize()
{	
	var scrollW=false;
	var scrollH=false;
	if (document.getElementById('tableForm').offsetHeight > top.window.innerHeight-100){
		var height=top.window.innerHeight-100;
		scrollW=true;
	}
	else
		var height=document.getElementById('tableForm').offsetHeight;
		
	if (document.getElementById('tableForm').offsetWidth > top.window.innerWidth-100){
		var width=top.window.innerWidth-100;
		scrollH=true;
	}
	else
		var width=document.getElementById('tableForm').offsetWidth;
		
	if (scrollW)width+=32;
	if (scrollH)height+=32;
	top.win.setSize(width,height);
	top.win.showCenter();
}











