var xmlHttp

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail  (valfield,msgfield)
{
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  
	if (!email.test(tfld)) {  
		document.getElementById(msgfield).style.color='#b02d32';
		return false;
	}else{
		document.getElementById(msgfield).style.color='#40668c';
		return true;
	}  
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}



function validateField(field){

	var inf="inf_"+field;

	if(document.getElementById(field).value.length<=0) {
		document.getElementById(inf).style.color='#b02d32';
		return false;
	}
	else{
		document.getElementById(inf).style.color='#40668c';
		return true;
	}
	
}

//--------------------------------------------------------
//	functions validate User login
//--------------------------------------------------------

function validateUserLogin(field,lang){
	var login = document.getElementById(field).value;  
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	
	var url="existuser.php"
	url=url+"?userName="+login
	url=url+"&lang="+lang
	url=url+"&file=1"
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateUserLogin 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)		
}

function stateUserLogin() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{			
		var result=xmlHttp.responseText 
		if(result!='0' || result.length<=0){
			document.getElementById("inf_userLogin").style.color='#b02d32';
			document.getElementById("errorlogin").innerHTML=xmlHttp.responseText
			document.getElementById("errorlogin").style.color='#b02d32';				
		}
		else{			
			document.getElementById("inf_userLogin").style.color='#494949';
			document.getElementById("errorlogin").innerHTML="";		
			onlyLatin("userLogin");
		}
	}
}

function validateName(){

        var check = 0;

	//get file name
        var field = document.getElementById("fileupload").value;          
        var lastPathDelimiter = field.lastIndexOf("\\");         
        var fileNameOnly = field.substring(lastPathDelimiter+1);
          
	//chek file name
	for (var i=0; i<fileNameOnly.length; i++){
		if(fileNameOnly.charCodeAt(i)>=32 && fileNameOnly.charCodeAt(i)<=126)
		{
			check=check;	
		}	
		else{
			check=check+1;
		}
	}
	if(check!=0) {
		alert("Yor file content cyrillic symbol. Please rename file and try again.")
		return false;
	}else{
		return true;
	}
}

function onlyLatin(field){

        var check = 0;

	//get file name
        var name = document.getElementById(field).value;          
          
	//chek file name
	for (var i=0; i<name.length; i++){
		if((name.charCodeAt(i)>=97 && name.charCodeAt(i)<=122) || (name.charCodeAt(i)>=48 && name.charCodeAt(i)<=57))
		{
			check=check;	
		}	
		else{
			check=check+1;
		}
	}
	if(check!=0) {		
		document.getElementById("errorlogin").innerHTML="Вашето потребителско име съдуржа невалиден символ.";
		document.getElementById("errorlogin").style.color='#b02d32';
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

//--------------------------------------------------------
//end function validate login
//--------------------------------------------------------
