var xmlHttp;

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;
}


function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

function persist_categorie(callback, nume, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/categorii_validator.php";
	url += "?nume="+ nume;
	if(id){
		url += "&id="+ id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function delete_categorie(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/categorii_validator.php";
	url += "?id="+ id;
	url += "&del=true";
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


function persist_subcategorie(callback, nume, id_categorie, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/subcategorii_validator.php";
	url += "?nume="+ nume;
	if(null != id_categorie){
		url += "&id_categorie="+ id_categorie;
	}
	
	if(id){
		url += "&id="+ id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function delete_subcategorie(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/subcategorii_validator.php";
	url += "?id="+ id;
	url += "&del=true";
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function getListSubcategorii(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/list_subcategorii.php?";
	url += "id="+ id;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			callback(xmlHttp.responseText);
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function getSelectSubcat(callback, id, isAdmin){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = isAdmin ? "select_subcateg.php?" : "admin/select_subcateg.php?";
	url += "id_categorie="+ id;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			callback(xmlHttp.responseText);
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


function persist_marca(callback, nume, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/marca_validator.php";
	url += "?nume="+ nume;
	if(id){
		url += "&id="+ id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function delete_marca(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/marca_validator.php";
	url += "?id="+ id;
	url += "&del=true";
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


function persist_model(callback, nume, id_marca, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/modele_validator.php";
	url += "?nume="+ nume;
	if(null != id_marca){
		url += "&id_marca="+ id_marca;
	}
	
	if(id){
		url += "&id="+ id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function delete_model(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/modele_validator.php";
	url += "?id="+ id;
	url += "&del=true";
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function getListModele(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/list_modele.php?";
	url += "id="+ id;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			callback(xmlHttp.responseText);
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function getSelectModele(callback, id, isAdmin){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = isAdmin ? "select_modele.php?" : "admin/select_modele.php?";
	url += "id_marca="+ id;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			callback(xmlHttp.responseText);
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


function persist_specific(callback, specific){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/specific_validator.php";
	url += "?submodel="+ specific.submodel;
	url += "&an="+ specific.an;
	url += "&sf_an="+ specific.sf_an;
	url += "&cod_motor="+ specific.cod_motor;
	url += "&cilindri="+ specific.cilindri;
	url += "&kw="+ specific.kw;
	url += "&cp="+ specific.cp;
	url += "&capacitate="+ specific.capacitate;
	
	if(null != specific.id_model){
		url += "&id_model="+ specific.id_model;
	}
	
	if(specific.id){
		url += "&id="+ specific.id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function delete_specific(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/specific_validator.php";
	url += "?id="+ id;
	url += "&del=true";
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function getListSpecific(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/list_specific.php?";
	url += "id="+ id;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			callback(xmlHttp.responseText);
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

function persist_produs(callback, produs){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/produs_validator.php";
	url += "?cod="+ produs.cod;
	url += "&producator="+ produs.producator;
	url += "&denumire="+ produs.denumire;
	url += "&descriere="+ produs.descriere;
	url += "&pret="+ produs.pret;
	url += "&moneda="+ produs.moneda;
	url += "&livrare="+ produs.livrare;
	url += "&cost="+ produs.cost;
	url += "&mentiuni="+ produs.mentiuni;
	url += "&url_img_prod="+ produs.url_img_prod;
	
	//for(var k = 0; k < produs.array_sub_ctg.length; k++)
		url += "&sub_ctg="+ produs.array_sub_ctg;
	
	
	//for(var k = 0; k < produs.array_masini.length; k++)
		url += "&masina="+ produs.array_masini;
	
	
	if(produs.id){
		url += "&id="+ produs.id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function delete_produs(callback, id){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "php_script/produs_validator.php";
	url += "?id="+ id;
	url += "&del=true";
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

function persist_cont(callback, cont){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "admin/php_script/cont_validator.php";
	url += "?c_nume="+ cont.c_nume;
	url += "&c_prenume="+ cont.c_prenume;
	url += "&c_tel="+ cont.c_tel;
	url += "&c_email="+ cont.c_email;
	url += "&c_pass="+ cont.c_pass;
	url += "&c_soc="+ cont.c_soc;
	url += "&c_soc_adr="+ cont.c_soc_adr;
	url += "&c_adr="+ cont.c_adr;
	url += "&c_cif="+ cont.c_cif;
	url += "&c_com="+ cont.c_com;
	url += "&c_web="+ cont.c_web;
	
	if(cont.c_id){
		url += "&id="+ cont.c_id;
	}
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function valid_cont(callback, cont){ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "admin/php_script/cont_validator.php";
	url += "?valid=true";
	url += "&c_email="+ cont.email;
	url += "&c_pass="+ cont.pass;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			var response = xmlHttp.responseText;
			var status = new Object();
			if(response.indexOf('success') == -1){
				status.error = 'Internal error: '+ response;
			}
			
			callback(status);
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function logout(callback){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "admin/php_script/cont_validator.php";
	url += "?logout=true";
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				if(callback){
					callback();
				}
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


function cos_operation(operation, callback){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request")
		return
 	} 

	var url = "admin/php_script/cos_operator.php";
	url += "?"+ operation.type +"=true";
	url += "&id_produs="+ operation.id_produs;
	url += "&denumire="+ operation.denumire;
	url += "&nr="+ operation.nr;
	
	//alert("URL: "+ url);
	
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			if(xmlHttp.responseText == ""){
				callback();
			}
			else {
				alert('Internal error: '+ xmlHttp.responseText);
			}
		} 
	}

	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
