var xmlHttp

function arataOferte(page, tip_oferta, contract, ordonare, apart){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url="php/action.php?page="+ page;
	url+="&tip_oferta="+ tip_oferta;
	url+="&contract="+ contract;
	url+="&ordonare="+ ordonare;
	url+="&apart="+ apart;
	
	
	
	
	xmlHttp.onreadystatechange = stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function arataHarta() {
//	window.open('images/harta_agentie_roman.jpg', 'Agentia', 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=no, scrollbars=no, width=608, height=554'); 
	window.open('images/harta_agentie_roman.jpg', 'Agentia', 'width=645, height=585'); 
}

// afisare poza cu oferta zilei
function altaPagina(url_pagina){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 
	
	var url = url_pagina;
	xmlHttp.onreadystatechange = stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)

//	document.getElementById("first_page").innerHTML = "<iframe src='"+ url_pagina +"'></iframe>";

}


// afisare oferte speciale din baza de date
function arataOferteSpeciale(){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 
	
	var url="php/speciale.php";
	xmlHttp.onreadystatechange = stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}





function arataCursValutar(){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	
	var url="php/curs_valutar.php";

	xmlHttp.onreadystatechange = stateChanged  
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function changeEventSort(obj, page, tip_oferta, contract, apart) {   
	var index = obj[obj.selectedIndex].value;
	
	
	
	switch(index){
		case '1':
		//	alert(1);
			arataOferte(page, tip_oferta, contract, 3, apart);
			break;
			
		case '2':
		//	alert(2);
			arataOferte(page, tip_oferta, contract, '5, 6', apart);
			break;
		
		case '3':
		//	alert(3);
			arataOferte(page, tip_oferta, contract, '2, 4', apart)
			break;
			
		case '4':
		//	alert(4);
			arataOferte(page, tip_oferta, contract, 8, apart);
			break;
	}
} 



function changeEventApart(obj, page, tip_oferta, contract, ordonare) {   
	var index = obj[obj.selectedIndex].value;
	
	switch(index){
		case '0':
		//	alert(0);
			arataOferte(page, tip_oferta, contract, ordonare, 0);
			break;
			
		case '1':
		//	alert(1);
			arataOferte(page, tip_oferta, contract, ordonare, 1);
			break;
		
		case '2':
		//	alert(2);
			arataOferte(page, tip_oferta, contract, ordonare, 2);
			break;
			
		case '3':
		//	alert(3);
			arataOferte(page, tip_oferta, contract, ordonare, 3);
			break;
		
		case '4':
		//	alert(4);
			arataOferte(page, tip_oferta, contract, ordonare, 4);
			break;
	}
} 


function connectDB(){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	
	var url="php/connect_db.php";

	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function calculeazaTaxa(){
	var input_tax  = Number(document.getElementById('input_tax').value);
	
	if(!input_tax || input_tax<0){
		alert('Introduceti: [taxa > 0]');
		return;
	}
	
	var result_tax = 0;
	
	
	// 2.5% dar nu mai putin de 150 RON + TVA
	if(input_tax <= 13696){
		result_tax = 1.19*2.5*input_tax/100;
		
		if(result_tax < 150)
			result_tax = 178;
	}
	// 342 RON + 2% pentru suma ce depaseste 13696 + TVA
	else if(input_tax > 13696 && input_tax <= 27499){
		result_tax = (342 + 2*input_tax/100)*1.19;
	}
	// 618 RON + 1.5% pentru suma ce depaseste 27449 + TVA
	else if(input_tax > 27499 && input_tax <= 54891){
		result_tax = (618 + 1.5*input_tax/100)*1.19;
	}
	// 1029 RON + 1% pentru suma ce depaseste 54891 + TVA
	else if(input_tax > 54891 && input_tax <= 274662){
		result_tax = (1029 + input_tax/100)*1.19;
	}
	// 3226 RON + 0.75% pentru suma ce depaseste 274662 + TVA
	else if(input_tax > 274662 && input_tax <= 549231){
		result_tax = (3226 + 0.75*input_tax/100)*1.19;
	}
	// 5286 RON + 0.5% pentru suma ce depaseste 549231 + TVA
	else if(input_tax > 549231){
		result_tax = (5286 + 0.5*input_tax/100)*1.19;
	}
	// INVALID
	else
		alert('Taxa de intrare invalida!');
		
	document.getElementById('result_tax').value = result_tax;
}

function convert(){
	var euro = document.getElementById('euro').value;
	var usd  = document.getElementById('usd').value;
	
	var index1 = document.getElementById('select3').selectedIndex;
	var index2 = document.getElementById('select4').selectedIndex;
	
	var suma = document.getElementById('suma').value;
	
	var moneda 	= new Array();
	moneda[0] 	= 'x';
	moneda[1]	= 1;		// RON
	moneda[2]	= usd;		// USD
	moneda[3]	= euro;		// EURO
	
	var total = suma*moneda[index1]/moneda[index2];		// TOTAL convertit
	document.getElementById('total').value = total;
	
//	alert('EURO: '+ euro +'; USd: '+ usd +'; i1='+ index1 +"; i2="+ index2 +'; TOTAL='+ total);
}

// cauta rapid oferte
function cautaRapidOferte(page){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 
	
	var i;
	var contract 	= "";
	
	for(i=0; i<2; i++)
		if(document.getElementById('contract'+ i +'x').checked == true)
			contract = document.getElementById('contract'+ i +'x').value;
	
	
	
//	var tip_imobil 	= document.getElementById('radiobuttonContract').value;
	var pretMin 	= Number(document.getElementById('pretMin').value);
	var pretMax 	= Number(document.getElementById('pretMax').value);
	var warn = "Introduceti: ";
	var error = 0;
	var index = 0;
	
	// MIN
	if(!pretMin){
		if(pretMin != 0){
			warn += "[pretMin >= 0] ";
			error = 1;
		}
	}
	else if(pretMin<0){
		warn += "[pretMin >= 0] ";
		error = 1;
	}
	
	
	// MAX
	if(!pretMax){
		if(pretMax != 0){
			warn += "[pretMax >= 0] ";
			error = 1;
		}
	}
	else if(pretMax<0){
		warn += "[pretMax >= 0] ";
		error = 1;
	}
	
	
	// MIN < MAX
	if(error == 0 && pretMin >= pretMax){
		warn += "[pretMin < pretMax] ";
		error = 1;
	}
	
	
	if(error == 1){
		alert(warn);
		return;
	}
	
	// tipul ofertei: 9 tipuri
	index = document.getElementById('cautaOferta').selectedIndex;
	
	
	var url="php/cauta_oferte.php?page="+ page;
	url+="&contract="+ contract;
	url+="&index="+ index;
	url+="&pretmin="+ pretMin;
	url+="&pretmax="+ pretMax;

	xmlHttp.onreadystatechange = stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function validateMail(){
	var email 	= document.mailform.email.value;
	var subiect = document.mailform.subiect.value;
	var mesaj 	= document.mailform.mesaj.value;
	var nume	= document.mailform.nume.value;
	var tara	= document.mailform.tara.value;
	
	var name 	= nume;
	
	// validare adresa de mail
	refno = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
	if ( refno.test(email) ){
	//	alert('email format OK!');
	}
	else{
		alert('Formatul adresei de email este INVALID! (ex: andreipopescu@yahoo.com)');
		return;
	}
	
	if( !tara ){
		alert('Va rugam introduceti [tara] in care va aflati!');
		return;
	}

	if(nume == "")
		name = "Necunoscut";

	var url = "php/mail.php?from="+ email;
	url += "&subject="+ subiect;
	url += "&message="+ mesaj;
	url += "&nume="+ name;
	url += "&tara="+ tara;
	
	altaPagina(url);
	altaPagina("multumim_mesaj.html");
}


function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 		document.getElementById("loadingbar").innerHTML = '<img src="images/load_bar_static.jpg" width="780" height="2">';
		document.getElementById("first_page").innerHTML=xmlHttp.responseText 
	
	//	alert(xmlHttp.responseText);
	} 
	else{
		document.getElementById("loadingbar").innerHTML = '<img src="images/load_bar_anim.gif" width="780" height="2">';
	}
	
}

function stateLoad(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 		document.getElementById("curs_valutar").innerHTML=xmlHttp.responseText 
	} 
}


function GetXmlHttpObject(){
	var xmlHttp=null;

	try{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e){
 		//Internet Explorer
 		try{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}

	return xmlHttp;
}
