// JavaScript Document
function menuOver(no,obj)
{
	if(no!=crt_pg)
	{
		obj.src = "images/"+crt_lg+"/menu_sel_0"+no+".jpg";
		var tdLine = document.getElementById("td_line"+no);
		tdLine.style.backgroundColor = "#FFFFFF";
	}
}
function menuOut(no,obj)
{
	if(no!=crt_pg)
	{
		obj.src = "images/"+crt_lg+"/menu_0"+no+".jpg";
		var tdLine = document.getElementById("td_line"+no);
		tdLine.style.background = "none";
	}
}

//checks if a field is not empty
function field_required(input,idErr)
{
	document.getElementById(input).value=trim(document.getElementById(input).value);	
	if(document.getElementById(input).value=="")
	{
		document.getElementById(input).style.border = "1px solid #FFE0A0";
		//Effect.Shake(input);
		var divErr = document.getElementById(idErr);
		divErr.style.display='block';
		return false;
	}
	return true;
}
//checks if a field is valid calling the specific function
function field_validate(input,checkFunction,idErr)
{
	document.getElementById(input).value=trim(document.getElementById(input).value);	
	if(document.getElementById(input).value!="")
	{
		checkValue = eval(checkFunction+'(document.getElementById("'+input+'").value)');
		if(checkValue==false)
		{
			document.getElementById(input).style.border = "1px solid #FFE0A0";
			//Effect.Shake(input);
			var divErr = document.getElementById(idErr);
			divErr.style.display='block';
			return false;
		}
	}
	return true;
}
//reset error div
function reset_error(input,err)
{
	document.getElementById('input_'+input).style.border = "1px solid #B5B5B5";
	document.getElementById('div_error_'+err).style.display = "none";
}
function testDbValue(input,fileName,idErr)
{
	var inputValue = trim(document.getElementById(input).value);
	if(inputValue!="")
	{
		var xmlHttp;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		xmlHttp.open("GET","front/ajax_"+fileName+".php?val="+inputValue,false);
		xmlHttp.send(null);
		res=xmlHttp.responseText; 
		if(res=="true")
			return true;
		else
		{
			document.getElementById(input).style.border = "1px solid #FFE0A0";
			//Effect.Shake(input);
			var divErr = document.getElementById(idErr);
			divErr.style.display='block';
			return false;
		}
	}
	else
		return true;
}
function validate_message()
{
	var ok = true;
	ok &= field_required('input_name','div_error_name');
	ok &= field_required('input_mail','div_error_mail');
	ok &= field_validate('input_mail','check_mail','div_error_mail2');
	ok &= field_required('input_message','div_error_message');
	ok &= field_required('input_code','div_error_code');
	ok &= testDbValue('input_code','securitycode','div_error_code2');
	if(ok==false)
		return false;
	return true;
}
//eliminates white spaces from a string
function trim(str)
{  
	while(str.charAt(0) == (" ") )
		str = str.substring(1);
  	while(str.charAt(str.length-1) == " " )
		str = str.substring(0,str.length-1);
  return str;
}
function check_mail(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }
	 return true;					
}