﻿function checkInfoCountry() {
  var elem = document.getElementById("selCountry");
  var valCountry = elem[elem.selectedIndex].value;
  
  /*var doShowIta = (valCountry == "96");
  if (doShowIta)
    divItaShow();
  else
    divItaHide();*/
  
  var doShowRoProv = (valCountry == "159");
  var doShowMoProv = (valCountry == "129");
  
  if (doShowRoProv) {
    
    listRoProvShow();
    listMoProvHide();
  
  } else if (doShowMoProv) {
    
    listMoProvShow();
    listRoProvHide();
  
  } else {
    
    listMoProvHide();
    listRoProvHide();
    
  }
}

function listRoProvHide() {
  var elem = document.getElementById("listjudetul");
  window.document.frmRegister.rProvinciaJudetul.selectedIndex = 0;
  elem.style.visibility = "hidden";
  elem.style.display = "none";
  elem.style.height = "1px";
}

function listMoProvHide() {
  var elem = document.getElementById("listraion");
  window.document.frmRegister.rProvinciaRaion.selectedIndex = 0;
  elem.style.visibility = "hidden";
  elem.style.display = "none";
  elem.style.height = "1px";
}

function listRoProvShow() {
  var elem = document.getElementById("listjudetul");
  elem.style.visibility = "visible";
  elem.style.display = "block";
  elem.style.height = "24px";
}

function listMoProvShow() {
  var elem = document.getElementById("listraion");
  elem.style.visibility = "visible";
  elem.style.display = "block";
  elem.style.height = "24px";
}

function divItaShow() {
  var elem = document.getElementById("infoita");
  elem.style.visibility = "visible";
  elem.style.display = "block";
  elem.style.height = "";
}

function divItaHide() {
  var elem = document.getElementById("infoita");
  elem.style.visibility = "hidden";
  elem.style.display = "none";
  elem.style.height = "1px";
}

function trim(str) {
  str = "" + str;
  return str.replace(/^\s*|\s*$/g,"");
}

function validate() {
  var validacampi=true;

  // Nome
  var tmp = doCapitalize(window.document.frmRegister.Nome.value);
  /*if ((trim(tmp) == '') && (validacampi==true)) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Te rog insereaza-ti numele");
  } else {
    window.document.frmRegister.Nome.value = tmp;
  }

  if (!validChars(tmp) && validacampi==true) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Câmpul numelui contine caractere nevalide");
  }

	// Cognome
	tmp = doCapitalize(window.document.frmRegister.Cognome.value);
	if ((trim(tmp) == '')&& (validacampi==true)) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Rog insereaza-ti numele de familie");
	} else {
	  window.document.frmRegister.Cognome.value = tmp;
	}

	if (!validChars(tmp) && validacampi==true) {
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Câmpul numelui de familie contine caractere nevalide");
  }*/

	if ((trim(window.document.frmRegister.selD.value) == '' || trim(window.document.frmRegister.selM.value) == '' || trim(window.document.frmRegister.selY.value) == '' ) && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Data nasterii nu este valida");
	}

	if ((window.document.frmRegister.Sesso[0].checked == false && window.document.frmRegister.Sesso[1].checked == false) &&  ( validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Lipseste câmpul sexului");
	}

	/*if ((trim(window.document.frmRegister.Country.value)=='') && (validacampi==true)){
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Country is missing");
	}*/

	if ((trim(window.document.frmRegister.Email.value) =='')&& (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Rog insereaza adresa e-mail");
		}

	if (!emailCorretta(window.document.frmRegister.Email.value) && validacampi)
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Adresa e-mail nu este valida");
		}
  
  if ((trim(window.document.frmRegister.Country.value) =='')&& (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Naţiune nu este valida");
		}
  
  if (document.getElementById("listjudetul").style.visibility == "visible" && 
      (trim(window.document.frmRegister.rProvinciaJudetul.value) =='') && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Judeţul nu este valida");
		}
  
  if (document.getElementById("listraion").style.visibility == "visible" && 
      (trim(window.document.frmRegister.rProvinciaRaion.value) =='') && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Raion nu este valida");
		}

	/*if ((trim(window.document.frmRegister.Nick.value) == '' ) && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Alege-ti un nume de utilizator");
	}*/


	/*if (!validaCaratteri(trim(window.document.frmRegister.Nick.value)) && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Numele de utilizator contine caractere nevalide");
		}*/

	if ((trim(window.document.frmRegister.Pwd.value)=='') && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Lipseste parola");
		}
		
  if ((trim(window.document.frmRegister.Pwd.value) == trim(window.document.frmRegister.Email.value)) && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Parola şi E-Mail trebuie sǎ fie diferite");
		}


	if (!validaCaratteri(trim(window.document.frmRegister.Pwd.value)) && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Parola contine caractere nevalide");
		}

	if (window.document.frmRegister.Pwd.value != window.document.frmRegister.rePwd.value && (validacampi==true))
		{
		validacampi=false;
		window.document.frmRegister.buttvalue.value='NO';
		alert("Parola repetata este incorecta");
		}

	if (validacampi==true)
		{
		window.document.frmRegister.buttvalue.value="OK";
		window.document.frmRegister.submit();
		}
}


function emailCorretta(stringa){
		var valida = false;
		var c;
		var found_chiocciola = 0;
		var found_punto = false;
		var found_spazio = false;
		var found_virgola = false;

	  	// CONTROLLO solo 1 "@" e almeno 1 "." e nessuno " " e nessuna ","
	  	if (stringa.length > 6) {
		  	for (i=0;i<stringa.length;i++){
		  		c = stringa.charAt(i);
		  		if (c==".") found_punto = true;
		  		if (c=="@") found_chiocciola++;
		  		if (c==" ") found_spazio = true;
		  		if (c==",") found_virgola = true;
		  	}
		}
		if (found_chiocciola==1 && found_punto) valida = true;
		if (found_spazio) valida = false;
		if (found_virgola) valida = false;


	  	// CONTROLLO "@" e "." all'inizio oppure alla fine
	  	if (valida) {
		  	c = stringa.charAt(0);
		  	if (c=="@" || c==".") {
		  		valida = false;
		  	}
		  	c = stringa.charAt(stringa.length-1);
		  	if (c=="@" || c==".") {
		  		valida = false;
		  	}
		}

		// CONTROLLO "@." e ".@"
		if (valida) {
			var aParts = stringa.split("@");
		  	var err=false;
		  	for (i=0;i<aParts.length;i++) {
		  		if (aParts[i].charAt(aParts[i].length-1)==".") {
		  			valida=false;
		  			break;
		  		}
		  		if (aParts[i].charAt(0)==".") {
		  			valida=false;
		  			break;
		  		}
		  	}
		}
	return valida;
}

function sceltaPartner(){
	var l = window.document.frmRegister.ComeFrom.length;
	var t = window.document.frmRegister.ComeFrom.type;
	if (t=="hidden") {
		var atLeastOne = true;
	}
	else {
		var atLeastOne = false;
	}

	for (i=0;i<l;i++) {
		if (window.document.frmRegister.ComeFrom[i].checked) {
			atLeastOne = true;
			break;
		}
	}
	return atLeastOne;
}

function validaCaratteri(s){
	var c,i;
	var valida=false;
	for (i=0;i<s.length;i++){
		c = s.charCodeAt(i);
		// lettere minuscole,     lettere maiuscole,    numeri
		if ( (c>=97 && c<=122) || (c>=65 && c<=90) || (c>=48 && c<=57) ) {
			valida=true;
		}
		else {
			valida=false;
			break;
		}
	}
	return valida;
}

function capitalize(obj) {
  var s = trim(obj.value);
  obj.value = doCapitalize(s);
}

function doCapitalize(s) {
  var newVal = '';
  s = s.toLowerCase();
  var val = s.split(' ');
  for(var c=0; c < val.length; c++) {
    newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
  }
  s = newVal;
  return trim(s);
}

function validChars(s) {
  var c,i;
	var valida=false;
	for (i=0;i<s.length;i++){
		c = s.charCodeAt(i);
		if (
		    (c==39) || (c==32) ||
		    (c>=48 && c<=57) ||
		    (c>=65 && c<=90) ||
		    (c>=97 && c<=122) ||
		    (c>=192 && c<=255)
		    ) {
			valida=true;
		}
		else {
			valida=false;
			//alert(i+"="+c);
			break;
		}
	}
	return valida;
}

