/* forms validating */

function admin_zone_add_submit(){
	if(document.getElementById('chk1').checked ||
	document.getElementById('chk2').checked ||
	document.getElementById('chk3').checked ||
	document.getElementById('chk4').checked ||
	document.getElementById('chk5').checked || 
    document.getElementById('chk6').checked
    )
	{
		document.getElementById('admin_zone_add').submit();
	}
	else
		alert("Trebuie selectata minim o categorie pentru noua zona");
}


function admin_utilitati_add_submit(){

	if(document.getElementById('chk1').checked ||
	document.getElementById('chk2').checked ||
	document.getElementById('chk3').checked ||
	document.getElementById('chk4').checked ||
	document.getElementById('chk5').checked || 
    document.getElementById('chk6').checked )
	{
		document.getElementById('admin_utilitati_add').submit();
	}
	else
		alert("Trebuie selectata minim o categorie pentru noua utilitate");
}

function admin_agenti_add_submit(){

	if(document.getElementById('idnume').value =="" ||
	document.getElementById('idprenume').value =="" ||
	document.getElementById('idtelefon').value =="")
	{
		alert("Nume,prenume si telefon sunt campuri obligatorii");
	}
	else
		document.getElementById('admin_agenti_add').submit();
}
function admin_agenti_modif_submit(){

	if(document.getElementById('idnume').value =="" ||
	document.getElementById('idprenume').value =="" ||
	document.getElementById('idtelefon').value =="")
	{
		alert("Nume,prenume si telefon sunt campuri obligatorii");
	}
	else
		document.getElementById('admin_agenti_modif').submit();
}
function admin_useri_add_submit(){

	if(!alphanumeric(document.getElementById('username').value))
		alert("Username-ul trebuie sa fie alfanumeric (litere si cifre)");
	else if(document.getElementById('parola').value.length <6 )
		alert("Parola trebuie sa fie de minim 6 caractere");
	else
		document.getElementById('admin_useri_add').submit();

}
function admin_useri_modif_submit(){

	if(!alphanumeric(document.getElementById('username').value))
		alert("Username-ul trebuie sa fie alfanumeric (litere si cifre)");
	else
		document.getElementById('admin_useri_modif').submit();

}

function selectTipImobilChange()
{
	var select_ti = document.getElementById('select_tip_imobil');
	var tiIndex = select_ti.value;


	for(i=1;i<=5;i++)
	{
		var tempId = "select_subtip_imobil_"+i;
		var temp = document.getElementById(tempId);
		temp.style.display = 'none';
	}
       i = 19;
       var tempId = "select_subtip_imobil_"+i;
        var temp = document.getElementById(tempId);
        temp.style.display = 'none';

	if(tiIndex!=2 && tiIndex!=3 && tiIndex!=19) /* daca e casa sau vila sau hotel nu mai afisez nimic */
	{
		var stiId = "select_subtip_imobil_"+tiIndex;
		var select_sti = document.getElementById(stiId);
		select_sti.style.display = '';
	}
	/*==============================*/

	for(i=1;i<=5;i++)
	{
		var tempId = "select_zona_"+i;
		var temp = document.getElementById(tempId);
		temp.style.display = 'none';
	}
        i=19;
        var tempId = "select_zona_"+i;
        var temp = document.getElementById(tempId);
        temp.style.display = 'none';

	var zonaId = "select_zona_"+tiIndex;
	var select_zona = document.getElementById(zonaId);

	select_zona.style.display = '';
}


function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
			 return false;
		  }
		}
 return true;
}
/**
 *
 * @access public
 * @return void
 **/
function isnumeric(alphane){
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58))
		  {
		  }
		else	{
			 return false;
		  }
		}
 return true;
}



function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true
	}

function form_cerere_oferta_submit(id_form){

	var vf = document.getElementById(id_form);

	if(vf["nume"].value == "")
		alert("Numele este obligatoriu");
	else
	if(!isnumeric(vf["telefon"].value))
		alert("Telefonul trebuie sa contina doar cifre");
	else
	if(!emailcheck(vf["email"].value))
		alert("E-mail-ul este gresit");
	else
		vf.submit();


}