// JavaScript Document
var user_valid=true;
var email_valid=true;
function goAjax()
{
var req = false;
try
  {
    req = new XMLHttpRequest(); /* e.g. Firefox */
  }
catch(err1)
  {
  try
    {
     req = new ActiveXObject("Msxml2.XMLHTTP");
  /* some versions IE */
    }
  catch(err2)
    {
    try
      {
       req = new ActiveXObject("Microsoft.XMLHTTP");
  /* some versions IE */
      }
      catch(err3)
        {
         req = false;
        }
    }
  }
return req;
}

var myRequest = goAjax();

function validateUser(ceva,altceva) {
// declare a variable to hold some information
// to pass to the server
// build the URL of the server script we wish to call
if(ceva==''){
	alert("Completati campul <Nume utilizator>!");
	return;
}
var url = "valid.php?username=" + ceva+"&email="+altceva;
// ask our XMLHTTPRequest object to open a
// server connection
myRequest.open("GET", url, true);
// prepare a function responseAjax() to run when
// the response has arrived
myRequest.onreadystatechange = responseAjax;
// and finally send the request
myRequest.send(null);
}

//////////
function validateUser2(ceva,altceva) {
// declare a variable to hold some information
// to pass to the server
// build the URL of the server script we wish to call
if(ceva==''){
	alert("Completati campul <Nume utilizator>!");
	return;
}
var url = "valid.php?username=" + ceva+"&email="+altceva;
// ask our XMLHTTPRequest object to open a
// server connection
myRequest.open("GET", url, true);
// prepare a function responseAjax() to run when
// the response has arrived
myRequest.onreadystatechange = responseAjax2;
// and finally send the request
myRequest.send(null);
}
//////////

function responseAjax2() {
    // we are only interested in readyState of 4,
    // i.e. "loaded"
    if(myRequest.readyState == 4) {
        // if server HTTP response is "OK"
		if(myRequest.responseText=="user invalid"){
		alert("Acest nume de utilizator este ocupat. Va rugam alegeti altul!");
		document.pers_fizica.Nume_Utilizator.focus();
				ebine=false;
				return false;
		}else if(myRequest.responseText=="user valid email invalid"){
		//document.getElementById('emaill').style.display="block";
		//document.getElementById('mail').innerHTML="<br>Aceasta adresa de email este deja folosita de catre un utilizator!<br><br>";
		alert("Acest nume de utilizator este liber si il puteti folosi!");
				esmail=false;
				return false;
		} else{
			esmail=true;
		}
		}
}

///////////
var ebine=true
var esmail=false
//////////

function responseAjax() {
    // we are only interested in readyState of 4,
    // i.e. "loaded"
    if(myRequest.readyState == 4) {
        // if server HTTP response is "OK"
		if(myRequest.responseText=="user valid"){
			alert("Acest nume de utilizator este liber si il puteti folosi!");
		ebine=true;
		}
		else if(myRequest.responseText=="user invalid"){
		alert("Acest nume de utilizator este ocupat. Va rugam alegeti altul!");
		document.pers_fizica.Nume_Utilizator.focus();
				ebine=false;
				return false;
		}else if(myRequest.responseText=="user valid email invalid"){
		//document.getElementById('emaill').style.display="block";
		//document.getElementById('mail').innerHTML="<br>Aceasta adresa de email este deja folosita de catre un utilizator!<br><br>";
		alert("Acest nume de utilizator este liber si il puteti folosi!");
				esmail=false;
				ebine=true;
				return false;
		} else{
			esmail=true;
		}
		}
}

/////////

/////////

function validare(ceva,cat){
var required = ceva.split(",");
 for(i=0;i<required.length;i++){
if(document.forms[cat].elements[required[i]].value==""){
var nume = document.forms[cat].elements[required[i]].name.split("_");
if(nume[1]!=undefined)
alert("Nu ati completat campul "+nume[0]+" "+nume[1]);
else
alert("Nu ati completat campul "+document.forms[cat].elements[required[i]].name);
document.forms[cat].elements[required[i]].focus();
return false;
}
}
if(!iscnp(document.forms[cat].cnp.value)){
	return false;
}

if(document.forms[cat].Parola.value!=document.forms[cat].Confirmare_Parola.value){
	alert("Cele doua parole nu coincid!");
	return false;
}
if(!isEmail(document.forms[cat].Email.value)){
	window.alert("Adresa de email introdusa nu este valida!");
	return false;
}
if(ebine){
	return true;
} else{
	alert("Va rugam verificati valabilitatea username-ului!");
	return false;
}

if(esmail){
	return true;
} else{
	alert("Adresa de email este deja folosita de catre o alta persoana!");
	return false;
}



}

///////////
function validare2(ceva,cat){
var required = ceva.split(",");
 for(i=0;i<required.length;i++){
if(cat.elements[required[i]].value==""){
var nume = cat.elements[required[i]].name.split("_");
if(nume[1]!=undefined)
alert("Nu ati completat campul "+nume[0]+" "+nume[1]);
else
alert("Nu ati completat campul "+cat.elements[required[i]].name);
cat.elements[required[i]].focus();
return false;
}
}
if(!iscnp(cat.cnp.value)){
	return false;
}

if(cat.Parola.value!=cat.Confirmare_Parola.value){
	alert("Cele doua parole nu coincid!");
	return false;
}
if(!isEmail(cat.Email.value)){
	window.alert("Adresa de email introdusa nu este valida!");
	return false;
}

if(isNaN(cat.Telefon.value)){
	alert("Campul Telefon trebuie sa fie numeric.");
	return false;
}
if(isNaN(cat.Mobil.value)){
	alert("Campul Telefon trebuie sa fie numeric.");
	return false;
}



if(ebine){
	return true;
} else{
	alert("Va rugam verificati valabilitatea username-ului!");
	return false;
}


if(esmail){
	return true;
} else{
	alert("Adresa de email este deja folosita de catre o alta persoana!");
	return false;
}


}

////////////

function isEmail(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function resetare(ceva){
for(i=0;i<ceva.length;i++)
ceva.elements[i].value="";
}
function mailExist(ceva){
	email=ceva.email2.value;
		
		
	var cerere = goAjax();
	url="valid.php?email2="+email;
	cerere.open("GET",url,false);
	cerere.send(null);

if(email==""){
alert("Va rugam completati adresa de email!");	
ceva.email2.focus();
	 return false;
	} 
	if(!isEmail(email)){
alert("Adresa de email nu este valida!");
	 return false; 
	 } 
	 if(cerere.readyState == 4) {
		 if(cerere.responseText=="0"){
			 alert("Aceasta adresa de email nu exista in baza noastra de date!");
			 return false;
		 }
	 }
}

function toogle(ceva){
	if(document.getElementById(ceva).style.display=="none")
	document.getElementById(ceva).style.display="block";
	else
	document.getElementById(ceva).style.display="none";
}

function empty(){
	if(confirm("Sunteti sigur ca doriti sa goliti cosul?")){
		window.location="do.php?act=gol";
	}
}

function vezi(){
	if(document.form5.username.value=="" || document.form5.password.value==""){
		alert("Campurile User si Parola sunt obligatorii!");
		return false;
	}
}

function anulare_comanda(){
	if(confirm("Sunteti sigur ca doriti sa anulati comanda?")){
		if(confirm("Atentie! Odata cu anularea comenzii, cosul va fi golit. Sunteti sigur?")){
			window.location="do.php?act=gol";
		}
	}
}

function submitere(){
	ok=true;
	document.form_nu.informatii.value=document.form_da.informatii.value;
		if(document.agree.chck.checked==false){
		ok=false;
		alert("Pentru a putea trece la pasul urmator, trebuie sa fiti de acord cu termenii si conditiile de utilizare.");
	} else
	if(document.form_da.destinatar.checked)
		var formular=document.form_da;
		else
		var formular=document.form_nu;
		if(formular.nume.value==""){
			alert("Va rugam sa completati campul nume.");
			ok=false;
			formular.nume.focus();
		} else 
		if(formular.prenume.value==""){
			alert("Va rugam sa completati campul prenume.");
			ok=false;
			formular.prenume.focus();
		} else
	      if(formular.telefon.value=="" && formular.mobil.value==""){
			  ok=false;
		   alert("Este necesar cel putin un numar de telefon.");
		   formular.telefon.focus();
	   } else
	
	   if(!iscnp(formular.cnp.value)){
				 formular.cnp.focus();
				 ok=false;
	   }else
				 if(formular.strada_fact.value=="" || formular.nr_fact.value=="" || formular.oras_fact.value==""){
					 alert("Campurile strada, numarul si orasul sunt obligatorii.");
					 ok=false;
				 } else {
					 if(isNaN(formular.telefon.value) || isNaN(formular.mobil.value)){
						 alert("Numarul de telefon introdus este incorect.");
						 ok=false;
					 }
				 }
				 if(ok)
				 formular.submit();
				 
}
////////////////

function iscnp(ceva){
	if(isNaN(ceva)){
		alert("Va rugam sa completati cu atentie codul numeric personal.");
		return false;
			}
			if(ceva==""){
				alert("Va rugam sa completati codul numeric personal.");
				return false;
			}
		if(ceva.length!=13){
			alert("Va rugam sa completati cu atentie codul numeric personal. Acesta trebuie sa aiba 13 cifre.");
			return false;
		}
		if(ceva.substr(0,1)!=1){
			if(ceva.substr(0,1)!=2){
				alert("Atentie! Prima cifra a codului numeric personal trebuie sa fie 1 sau 2.");
				return false;
			}
		}
		if(ceva.substr(3,2)>12){
			alert("Codul numeric personal este incorect!");
			return false;
		}
				if(ceva.substr(5,2)>31){
			alert("Codul numeric personal este incorect!");
			return false;
		}
		return true;
}
///////////////////

function deschide(ceva,altceva){
	window.open("continut_comanda.php?comanda_id="+ceva+"&data="+altceva,"","width=800,height = 400,status=no,menu=no,scrollbars=yes");

}
///////////////
function comenteaza(){
	document.getElementById('comenteaza_off').style.display="none";
		document.getElementById('comenteaza_on').style.display="block";
}

var query = goAjax();
////////////////////
function post_comment(titlu,comentariu,articol_id) {

// declare a variable to hold some information
// to pass to the server
// build the URL of the server script we wish to call
var url = "add_comment.php";

// ask our XMLHTTPRequest object to open a
// server connection
//query.open("GET", url);
// prepare a function responseAjax() to run when
// the response has arrived
if(titlu=="" || comentariu==""){
	alert("Campurile <Titlu> si <Comentariu> sunt obligatorii!");
} else {
var params = "titlu=" + titlu+"&comentariu="+comentariu+"&articol_id="+articol_id+"&act=add";

query.open("POST", url, true);

//Send the proper header information along with the request
query.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
query.setRequestHeader("Content-length", params.length);
query.setRequestHeader("Connection", "close");

query.onreadystatechange = raspuns;
// and finally send the request
query.send(params);
}
}

///////////////

function raspuns() {
    // we are only interested in readyState of 4,
    // i.e. "loaded"
	
    if(query.readyState == 4) {
        // if server HTTP response is "OK"
		
if(query.responseText){
	//alert(query.responseText);
		document.getElementById('comenteaza_off').style.display="none";
		document.getElementById('comenteaza_on').style.display="none";
			document.getElementById('mesaj_trimis').style.display="block";
			document.getElementById('mesajul').innerHTML="<center><strong>Comentariul dumneavoastra a fost trimis catre validare.</strong></center>";
} else{
alert("Mesajul nu a fost trimis!");	
}
		}
}
///////////////

function concurs(ceva){
	if(ceva.poza.value==""){
		alert("Va rugam selectati o poza!");
		return false;
	}
	if(ceva.nume.value==""){
		alert("Completati campul <Nume>");
		ceva.nume.focus();
		return false;
	}
		if(ceva.varsta.value==""){
		alert("Completati campul <Varsta>");
		ceva.varsta.focus();
		return false;
	}
		if(isNaN(ceva.varsta.value)){
		alert("Nu ati completat corect campul <Varsta>");
		ceva.varsta.focus();
		return false;
	}
		if(ceva.descriere.value==""){
		alert("Adaugati o scurta descrere a concurentului.");
		ceva.descriere.focus();
		return false;
	}
}

////////////////////



		//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

//kinda important
var AutoClose = true;

//don't touch
function popImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'function resizeWinTo() {\n'+
    'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
    'var oH = document.images[0].height, oW = document.images[0].width;\n'+
    'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
    'window.doneAlready = true;\n'+ //for Safari and Opera
    'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
    'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
    'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
    'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
    'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
    'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
    'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
    'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
    'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
    'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
    '}\n'+
    '<\/script>'+
    '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
    '<img src="'+imageURL+'" alt="Loading image ..." title="" onload="resizeWinTo();">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}
///////////////////


function validare_newsletter(ceva){
	if(ceva.nnume.value==""){
		alert("Va rugam completati campul <Nume>");
		ceva.nnume.focus();
		return false;
	}
		if(ceva.nemail.value==""){
		alert("Va rugam completati campul <Email>");
		ceva.nemail.focus();
		return false;
	}
	if(!isEmail(ceva.nemail.value)){
		alert("Adresa de email introdusa nu este valida!");
		return false;
	}
}

///////////////////////////
function submitere2(){
	if(document.form5.persoana[0].checked==true){
		document.pers_fizica.submit();
	} else {
		document.pers_juridica.submit();
	}
}
/////////////////////////////
function submitere3(){
	document.form_nu.informatii.value=document.form_da.informatii.value;
	ok=true;
	if(document.agree.chck.checked==false){
		ok=false;
		alert("Pentru a putea trece la pasul urmator, trebuie sa fiti de acord cu termenii si conditiile de utilizare.");
	} else
	if(document.form_da.destinatar.checked==true)
		formular=document.form_da
		else
		formular=document.form_nu;
		if(formular.Firma.value==""){
			alert("Va rugam sa completati numele firmei.");
			ok=false;
			formular.Firma.focus();	
		} else if(formular.Cod_fiscal.value==""){
			alert("Va rugam sa completati codul fiscal.");
			ok=false;
			formular.Cod_fiscal.focus();		
		}else 
		if(formular.nume.value==""){
			alert("Va rugam sa completati numele persoanei de contact.");
			ok=false;
			formular.nume.focus();
		} else 
		if(formular.prenume.value==""){
			alert("Va rugam sa completati prenumele persoanei de contact.");
			ok=false;
			formular.prenume.focus();
		} else
	      if(formular.telefon.value=="" && formular.mobil.value==""){
			  ok=false;
		   alert("Este necesar cel putin un numar de telefon.");
		   formular.telefon.focus();
	   } else
	
	   if(!iscnp(formular.cnp.value)){
				 formular.cnp.focus();
				 ok=false;
	   }else
				 if(formular.strada_fact.value=="" || formular.nr_fact.value=="" || formular.oras_fact.value==""){
					 alert("Campurile strada, numarul si orasul sunt obligatorii.");
					 ok=false;
				 } else {
					 if(isNaN(formular.telefon.value) || isNaN(formular.mobil.value)){
						 alert("Ne pare rau dar numarul de telefon este incorect!");
						 ok=false;
					 }
				 }
				 if(ok)
				 formular.submit();
				 
}
////////////////////
function preview_comanda(){
	var url = "?nume="+document.form_da.nume.value+"&prenume="+document.form_da.prenume.value+"&strada_fact="+document.form_da.strada_fact.value+"&nr_fact="+document.form_da.nr_fact.value+"&bloc_fact="+document.form_da.bloc_fact.value+"&scara_fact="+document.form_da.scara_fact.value+"&apart_fact="+document.form_da.apart_fact.value+"&oras_fact="+document.form_da.oras_fact.value+"&judet_fact="+document.form_da.judet_fact.value;
	window.open("preview_comanda.php"+url,"","width=600,height=400,scrollbars=yes,menu=no,status=no");
}
//////////
function loginForum(username,password){

	var forum = goAjax();
	
	var iasessid = goAjax();
	iasessid.open("GET","forum/login2.php",false);
	iasessid.onreadystatechange = function(){
		if(iasessid.readyState==4){
			alert(iasessid.responseText);
		}
	}
iasessid.send(null);
// declare a variable to hold some information
// to pass to the server
// build the URL of the server script we wish to call
var url = "forum/login.php";

// ask our XMLHTTPRequest object to open a
// server connection
//query.open("GET", url);
// prepare a function responseAjax() to run when
// the response has arrived

var params = "username=" + username+"&password="+password+"&redirect=&login=Autentificare";
forum.open("POST", url, true);

//Send the proper header information along with the request
forum.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
forum.setRequestHeader("Content-length", params.length);
forum.setRequestHeader("Connection", "close");

forum.onreadystatechange = function(){
	if(forum.readyState==4){
			//alert(username+'//'+password+'//'+sid);
	}
}
// and finally send the request
forum.send(params);
}
//////////
function page_appear(){
	document.getElementById("corp").style.display="block";
}
//////////
var cat = 80;
function appear(){
	cat-=5;
	document.getElementById("preloader").style.filter="alpha(opacity="+cat+")";
	document.getElementById("preloader").style.opacity=cat;
	document.getElementById("preloader").style.MozOpacity = cat/90;
	if(cat<5){
	document.getElementById("preloader").style.display="none";
	clearInterval(timer);
	}
	if(timer!=null)
	clearInterval(timer);
	timer = setInterval(appear,20);
	
}
function fadeOut(init,cine){
	init-=5;
	document.getElementById(cine).style.filter="alpha(opacity="+init+")";
	document.getElementById(cine).style.opacity=init;
	document.getElementById(cine).style.MozOpacity = init/90;
	if(init<5){
	document.getElementById(cine).style.display="none";
	clearInterval(timer);
	}
	if(timer!=null)
	clearInterval(timer);
	timer = setInterval(fadeOut,50);
	
}

function logare_forum(username,password){
	var logforum = goAjax();
	var url = "forum/login.php";
	var params = "username="+username+"&password="+password+"&autologin=&redirect=&login=Autentificare";
	
	logforum.open("POST",url,true);
	
	logforum.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	logforum.setRequestHeader("Content-length", params.length);
	logforum.setRequestHeader("Connection", "close");
	
	logforum.onreadystatechange=function(){
		if(logforum.readyState==4){
			window.status="Ati fost logat si in forum!";
		}
	}
	
	logforum.send(params);
}
function logare_site(username,password){
	var logforum = goAjax();
	var url = "../index.php";
	var params = "uname="+username+"&pass="+password+"&ceva=asasdasdasd";
	
	logforum.open("POST",url,true);
	
	logforum.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	logforum.setRequestHeader("Content-length", params.length);
	logforum.setRequestHeader("Connection", "close");
	
	logforum.onreadystatechange=function(){
		if(logforum.readyState==4){
			window.location = "index.php";
		}
	}
	
	logforum.send(params);
}
