var email=/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\.\-]+\.(.)+$/;
var name=/^[a-zA-Z ]{3,50}/;
var pass=/^.{5,20}$/;
var qty=/^[1-9]{1}$/;
var letters=/^[a-zA-Z ]{1,50}$/;
var alphanumeric=/^(.)+$/;

function msg(tmp){
if (tmp!='') alert(tmp);
}

function sterge_inregistrare(filename,id,ref){
if (confirm('Sunteti sigur ca vreti sa stergeti aceasta inregistrare?'))
		window.location.href=filename+'?id='+id+'&ref='+ref;
}

function cauta(){
	document.getElementById('formCautare').submit();
}

function caption(key){
var rlang=(lang==1?'ro':'en');
var cap=new Object();

cap.en=new Array('firstname','surname','company','email','subject','comments','code');
cap.ro=new Array('prenume','nume','companie','mail','subiect','comentarii','cod');
cap.fields=new Array('prenume','nume','companie','email','subiect','mesaj','cod');

for (var i=0; i<cap.fields.length; i++){
	if (cap.fields[i]==key) 
						return (eval('cap.'+rlang+'['+i+']'));
	}
}

function checkForm(e){
//id treb sa fie de forma id_field-lungime_camp-pattern-oblig lung/oblig pattern
//id_field treb sa fie numele campului care o sa apara in msj de alert
var username=/([a-zA-Z0-9\-\._])+/
var phone=/([0-9 \.\-])+/
var letters=/^[a-zA-Z ]{1,50}$/;
var tel=/^[0-9\+\-]{6,20}$/;
var digits=/^[0-9 ]+$/;
var email=/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\.\-]+$/;
var alphanumeric=/^(.)+$/;
var path=/^[a-zA-Z0-9\.:\/]+$/;
var msg='';
var ar=e.elements;

for (var i=0; i<ar.length; i++){
	if (ar[i].type!='submit'){
			elem=ar[i].id.split('-');
			switch(elem[3]){
			case '10': 	//treb verificat doar ca lungine
						if (ar[i].length<=elem[1]) {
									ar[i].style.backgroundColor='#cc0000';
									msg+='Campul '+caption(elem[0])+' este prea scurt! \n';
									}
									else
									ar[i].style.backgroundColor='#FFFFFF';
						break;
			case '01':  //treb verif  pattern ul
							if (eval(elem[2]+'.test(\''+ar[i].value+'\')')==false){
											ar[i].style.backgroundColor='#cc0000';
											msg+='Campul '+caption(elem[0])+' este prea scurt sau contine caractere interzise! \n';
										}
										else
										ar[i].style.backgroundColor='#FFFFFF';
							break;
			case '11':  //treb verif lungimea dar si pattern ul
							if (eval(elem[2]+'.test(\''+ar[i].value+'\')')==false || ar[i].length<elem[1]){
											ar[i].style.backgroundColor='#cc0000';
											msg+='Campul '+caption(elem[0])+' este prea scurt sau contine caractere interzise! \n';
										}
										else
										ar[i].style.backgroundColor='#FFFFFF';
							break;
			}
			//treb verif daca selectul are valoare aleasa
			if (/select/.test(ar[i].type) && elem[3] == '00') {
					if (ar[i].selectedIndex==0){
							ar[i].style.backgroundColor='#cc0000';
							msg+='Nu ati ales o optiune pentru  '+caption(elem[0])+'\n';
						}
						else
						ar[i].style.backgroundColor='#FFFFFF';
			}
	}
}

if (msg=='') return true;
		else alert(msg);
return false;
}

function enlarge(posPozeArray){
window.open('popup_produs_poza.php?file='+poze[posPozeArray],'enlarged','width=100px,height=100px,top=200px,left:400px,scrollbars=0,toolbars=0');
}

function changePic(posPozeArray){
if (posPozeArray<0) 
			{ alert('Aveti selectata prima poza!'); return false; }
if (posPozeArray>poze.length-1) 
			{ alert('Aveti selectata ultima poza!'); return false; }
curent=posPozeArray;
//document.getElementById('poza_produs').src=thumbs[posPozeArray];
document.getElementById('poza_produs').src='poza.php?path='+poze[posPozeArray]+'&w=352&h=270';
//alert(document.getElementById('poza_produs').src);
document.getElementById('pozaCurenta').innerHTML='Poza '+(curent+1)+' din '+poze.length+'.';
return false;
}

function empty_cart(cart_id){
if (confirm('Sunteti sigur ca doriti sa goliti cosul ?')){
	oInput=document.getElementById(cart_id).getElementsByTagName('input')
	for (i=0; i<oInput.length; i++){
			if (oInput[i].type=='text') oInput[i].value=0;
			}
	document.getElementById(cart_id).submit();
	}
}



// --------------------------------------------------------------------ENGINE AJAX---------------------------------------------------------------------- //
function ajax(id,url,after,param){
	var tmp;
	var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	var method=(id==0?"GET":"POST"); 
	
	document.getElementById('loader').style.display='block';
	//fix pt no-cache
	//url+='?date='+new Date().getTime();
	
	if (x){
		x.onreadystatechange = function(){
				if (x.readyState == 4 && x.status == 200) {
							eval(after+'(x.responseText);');
							document.getElementById('loader').style.display='none';
							}
				}
		if (id==0){
				x.open(method, url+'?'+param, true);
				x.send(null);
				}
			else{
				x.open(method, url, true);
				x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				x.setRequestHeader("Content-length", param.length);
				x.setRequestHeader("Connection", "close");
				x.send(param);
				}
		}
}

function calendarRefresh(){
var mon=document.getElementById('luna');
var year=document.getElementById('an');
ajax(0,'box_calendar.php','calendarRefreshResponse','luna='+mon.value+'&an='+year.value);
}

function calendarRefreshResponse(tmp){
document.getElementById('calendar').innerHTML=tmp;
}

function createAccount(frm){
var elem=frm.elements;
var param='';
var err=0;
var errMsg='';
for (i=0; i<elem.length; i++){
		switch(elem[i].id){
					case "email":  if (!email.test(elem[i].value)){ err++; errMsg+='Numele de utilizator introdus nu este corect! \n Exemplu: joe@doe.com \n';  } break;
					case "pass1":  if (!pass.test(elem[i].value)){ err++; errMsg+='Parola trebuie sa aiba minim 5 caractere si maxim 20 \n';  }
																	if (hex_md5(elem[i].value)!=hex_md5(elem[i+1].value)){
																						err++; errMsg+='Confirmarea parolei a esuat! \n'; }
																				else
																						elem[i].value=hex_md5(elem[i].value);
																	break;
					case "name":  if (!name.test(elem[i].value)){ err++; errMsg+='Numele contine caractere interzise sau este prea scurt. \n'; } break;
					case "datan":  if (!/([0-9]{2})-([1-9]|0[1-9]|1[012])-(1|2)[0-9]{3}/.test(elem[i].value)){ err++; errMsg+='Data nasterii nu este in formatul corect. \n Exemplu: 21-08-1980 \n'; } break;
					case "tel":  if (!/(07[0-9]{8})/.test(elem[i].value)){ err++; errMsg+='Numarul de telefon este incorect. \n'; } break;
		}

		if (elem[i].type=='checkbox'){
					if (elem[i].checked) 
										param+=elem[i].id+'='+elem[i].value+'&';
					}
			else
				param+=elem[i].id+'='+elem[i].value+'&';
}
	
	if (err>0)
				alert(errMsg);
		else
				ajax(1,'creeaza_cont_action.php','createAccountResponse',param);
return false;
}

function createAccountResponse(tmp){
var e=document.getElementById('account');
if (!tmp.match(/EROARE/))
		e.innerHTML=tmp;
	else
		alert(tmp);
}

function lostPass(frm){
if (!email.test(frm.elements[0].value))
				alert('Adresa de email introdusa este invalida!');
		else
				ajax('1','creeaza_cont_parola_trimite.php','lostPassResponse','email='+frm.elements[0].value);
return false;
}

function lostPassResponse(tmp){
alert(tmp);
}

function login(frm){
if (email.test(frm.elements[0].value))
			ajax(1,'login.php','loginResponse','email='+frm.elements[0].value+'&parola='+hex_md5(frm.elements[1].value));
	else
			alert('Username-ul este invalid');
return false;
}

function loginResponse(tmp){
window.location='./';
alert(tmp);
}

/*                            CLASA SCROLL                           */
function _scroll(self,idDiv,direction){
		this.self=self;
		this.direction=direction;
		this.scrollId=idDiv;
		this.stop=false;
		this.timeOut='';
		this.ammountPx=1; //distance to be scrolled in px
		this.ammounTime=1; //time between refresh in milliseconds
		
		this.goDown=function(){
					if (this.stop==false){
							document.getElementById(this.scrollId).scrollTop+=this.ammountPx;
							this.timeOut=setTimeout(this.self+".goDown()",this.ammounTime);
							}
							else this.clear();
					return false;
					}
		
		this.goUp=function(){
					if (this.stop==false){
							document.getElementById(this.scrollId).scrollTop-=this.ammountPx;
							this.timeOut=setTimeout(this.self+".goUp()",this.ammounTime);
							}
						else this.clear();
					return false;
					}
					
		this.goLeft=function(){
					if (this.stop==false){
							document.getElementById(this.scrollId).scrollLeft-=this.ammountPx;
							this.timeOut=setTimeout(this.self+".goLeft()",this.ammounTime);
							}
							else this.clear();
					return false;
					}
					
		this.goRight=function(){
		      //document.getElementById('FooterPlacer').innerHTML=document.getElementById(this.scrollId).scrollLeft+' '+this.stop;
					if (this.stop==false){
							document.getElementById(this.scrollId).scrollLeft+=this.ammountPx;
							this.timeOut=setTimeout(this.self+".goRight()",this.ammounTime);
							//document.getElementById('FooterPlacer').innerHTML+=' ///--> '+this.timeOut;
							}
							else this.clear();
					return false;
					}
		
		this.clear=function(){
				this.stop=false;
				clearTimeout(this.timeOut);
				}
				
		this.plus=function(){
		if (this.direction=='V')
          document.getElementById(this.scrollId).scrollTop+=40;
        else
          document.getElementById(this.scrollId).scrollLeft+=40;
		return false;
		}
		
		this.minus=function(){
		if (this.direction=='V')
		    document.getElementById(this.scrollId).scrollTop+=-40;
		  else
		    document.getElementById(this.scrollId).scrollLeft+=-40;
		return false;
		}
}