﻿jQuery.noConflict();

function popupOpen(url,name, b, h, scrolling_yes_no) {
      // zentriert 
      links = (screen.width/2)-(b/2);
      oben =(screen.height/2)-(h/2);
      if(typeof win != "undefined" ){ 
    if(win.closed==false){
    win.close()
    }
      } 
      win=window.open(url,name,"height="+h+",width="+b+",status = no,toolbar = no,menubar = no,location = no,resizable = no,titlebar = no,scrollbars ="+scrolling_yes_no+",fullscreen = no,top ="+oben+",left ="+links);
      win.focus()
}

///////////////////////////////////////////////////////////////////////////////////////////
// verifica userName la inregistrare
function checkRegUser(objID){
	
	objValue = jQuery('#'+objID).val();

	//adresa trebuie sa contina minim 4 caractere si maxim 32 caractere!
	if ((objValue.length<4)||(objValue.length>32)){
		insert_after(objID, false);
		return false;
	}
	
	//primul caracter trebuie sa fie litera 
	var matchFirstLetter = /[a-zA-Z]/.test(objValue.substr(0,1));
	if ( !matchFirstLetter ) {
		insert_after(objID, false);
		return false;
	}
	
	//adresa contine carcatere invalide
	var matchAll = /^[0-9a-zA-Z_.]+$/.test(objValue);
	if (!matchAll){
		insert_after(objID, false);
		return false;
	}
	
	//adresa trebuie sa contina un singur caracter punct (.)
	var firstDotPos = objValue.indexOf('.');
	if (firstDotPos!=-1){
		if (objValue.indexOf('.', firstDotPos+1)!=-1){
			insert_after(objID, false);
			return false;
		}
	}
	
	//verifica daca mai exista username in baza de date
	jQuery.post('../ajax/check_db.php', { user: jQuery('#'+objID).val()}, function(data) {
		 jQuery('#live_'+objID).val(data);
		 
		 if(data=='exista')		insert_after(objID, false);
		 else				  	insert_after(objID, true);
	});
	
	if ( jQuery('#live_'+objID).val()=='exista'){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
	
}

///////////////////////////////////////////////////////////////////////////////////////////
// campul trebuie sa contina minim nMin caractere!
function checkMin(objID, nMin){
	if (jQuery('#'+objID).val().length<nMin){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
//
function selSex(objID){
	jQuery('#male').attr('src', 'images/male_dis.png');
	jQuery('#female').attr('src', 'images/female_dis.png');
	
	jQuery('#'+objID).attr('src', 'images/'+objID+'.png');
	
	if (objID=='female') {

		                   jQuery('#fostul_nume').removeClass('hiding');							   
		                   jQuery('#fostul_nume').addClass('showing');							   						   
	                     } 
						 
	else {
	
		                   jQuery('#fostul_nume').removeClass('showing');							   
		                   jQuery('#fostul_nume').addClass('hiding');							   						   
	                     } 						 
	
	jQuery('#register_sex').val(objID);
	insert_after('register_sex', true);
}

///////////////////////////////////////////////////////////////////////////////////////////
//
function selActive(){
	
	if (jQuery('#activ').val()=='1') {
		                           jQuery('#active').attr('src', 'images/standart_off.jpg');
	                               jQuery('#activ').val('0');
	                             }
	else {
		                           jQuery('#active').attr('src', 'images/standart_on.jpg');
	                               jQuery('#activ').val('1');		
		 }

}

///////////////////////////////////////////////////////////////////////////////////////////
//
function checkSex(objID){
	if (jQuery('#'+objID).val()=='neutru'){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
}

function checkSelected(objID){
	if (jQuery('#'+objID).val()=='0'){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////
//
function submit_register(){

	checkRegUser('register_user');
	checkMin('register_parola',4);
	checkMin('register_nume',3);
	checkMin('register_prenume',3);
	checkSex('register_sex');
	checkSelected('judet');
	checkSelected('unitate');
	
	if( checkRegUser('register_user')&&checkMin('register_parola',4)&&checkMin('register_nume',3)
			&&checkMin('register_prenume',3)&&checkSex('register_sex')
			&&checkSelected('judet')&&checkSelected('unitate') ){
		document.getElementById('register').submit();
	}else{
		return false;
	}
}

function submit_message(){

	checkMin('register_nume',3);
    checkEmail2('email_contact');	
    checkMin('mesaj',3);

	if (checkMin('register_nume',3)&& checkEmail2('email_contact')&&checkMin('mesaj',3)){
		document.getElementById('mesaj_form').submit();
	}else{
		return false;
	}
}

function submit_password(){


    checkEmail3('email_contact');	


	if (checkEmail3('email_contact')){
		document.getElementById('mesaj_form').submit();
	}else{
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
//
function submit_addLocatie(){

	checkMin('locatie_nume',4);
	checkMin('locatie_adresa',4);
	
	if( checkMin('locatie_nume',4) && checkMin('locatie_adresa',4) ){
		document.getElementById('addLocatie').submit();
	}else{
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////////////////
//
function checkEmail(objID){
	objValue = jQuery('#'+objID).val();
	if (objValue.length==0){
		insert_after(objID, true);
		return true;
	}
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(objValue)==false){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
}

function checkEmail2(objID){
	objValue = jQuery('#'+objID).val();
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(objValue)==false){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
}

function checkEmail3(objID){
	objValue = jQuery('#'+objID).val();
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(objValue)==false){
		insert_after(objID, false);
		return false;
	}else{
		insert_after(objID, true);
		return true;
	}
}



///////////////////////////////////////////////////////////////////////////////////////////
//
function saving(format){

	if(format=='Infos') {
		jQuery('#saving_infos').slideDown('fast');
		setTimeout("jQuery('#saving_infos').slideUp('fast');",1000);
		setTimeout("document.forms.datele_mele.submit();",1000);
	}
}

function submit_datele_mele(){
	if(!checkEmail('email_public')){
		return false;
	}
	if(!checkEmail('email_alternativ')){
		return false;
	}

	jQuery('#saving_infos').slideDown('fast');
	setTimeout("jQuery('#saving_infos').slideUp('fast');",1000);
	setTimeout("document.forms.datele_mele.submit();",1000);
}


//--------------------------------------------------------------------------------------------------------------------------
function insert_after(sp2_id, ok){
	var sp2 = document.getElementById(sp2_id);
	var parentDiv = sp2.parentNode;
	
	if(ok)	{	
		img='images/form_bifat.gif';
		sp2.style.borderColor='#888888';
	}
	else	{	
		img='images/form_gresit.gif';
		sp2.style.borderColor='#B00001';
	}

	var sp1 = document.getElementById("img_"+sp2_id);
	if(!sp1){//element does not exist
		//create the img element
		var sp1 = document.createElement("img");
		sp1.setAttribute("id", "img_"+sp2_id);

		// insert the new element into the DOM after sp2
		parentDiv.insertBefore(sp1, sp2.nextSibling);
	}
	

	sp1.setAttribute("src", img);
	sp1.setAttribute("style","padding-bottom:3px;");
	//sp2.setAttribute("style","border:1px solid;");		
		
	if(ok==-1){//delete the image
		parentDiv.removeChild(sp1);
	}
}


function IsNumeric(sText){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

	for(i=0; i<sText.length && IsNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1){
			IsNumber = false;
		}
	}
	return IsNumber;
}
