
var aHtml=Array(), aHtml_url=Array();

function makeRequest(id_tag, url, img_name, parameters, cache){		
	if(!img_name) img_name='<center class="text_rosu_16px_slim">va rugam sa asteptati...</center>'; //loader_alb.gif";	
	else if(!isNaN(x=parseInt(img_name))) img_name='<center><table height="'+x+'"><tr valign=top><td align="center" class="text_rosu_16px_slim">va rugam sa asteptati...</td></tr></table></center>';

	try{
		//for(i in aHtml_url) alert(''+i+': ');
		if(cache && aHtml_url[url]){			
			document.getElementById(id_tag).innerHTML=aHtml_url[url];
			return false;
		}
	}catch(e){ }
	
	//se adauga la url un text. Daca nu se adauga acest text atunci nu se va cere pagina de pe server ci va fi folosita o pagina cache-uita de browser
	if(url) url+=((url.indexOf('?')==-1) ? '?' : '&')+'js_time='+new Date().getTime();
		
	//memorez contentul tag-ului pentru o eventuala refacere ulterioara
	aHtml[id_tag]=document.getElementById(id_tag).innerHTML;	
	
	document.getElementById(id_tag).innerHTML=((img_name.indexOf(' ')!=-1) ? img_name : "<center><img src=images/"+img_name+"></center>");
	
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			//try { 
			http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
			//} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { alertContents(id_tag, http_request, url); };
	//http_request.open('GET', url, true);
	//http_request.send(null);
	
	http_request.open((parameters ? 'POST' : 'GET'), url, true);
	
	if(parameters){
	  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	  http_request.setRequestHeader("Content-length", parameters.length);
	  http_request.setRequestHeader("Connection", "close");
	}else parameters=null;
	http_request.send(parameters);	
	
	return false;

}

function alertContents(id_tag, http_request, url) {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {			
			document.getElementById(id_tag).innerHTML = http_request.responseText;  //aici se modifica informatia din tagul cu id=id_tag (ex: <span id='abc'></span> )

			try{ 
				var posx=url.indexOf('&js_time=');				
				if(posx>0) aHtml_url[url.substring(0,posx)]=http_request.responseText;				
			}catch(e){ }
			
			//daca exista textul "err725" in contentul venit de la server atunci afisez alert... si reincarc pagina
			if(http_request.responseText.indexOf('err725')!=-1){
				alert('Sessiunea a expirat. Va rugam sa va logati.');
				window.location.href='index.php?pn=cont';
			}
			//daca s-a adaugat un cod atunci se face refresh listei de coduri introduse (istoric coduri) 
			//daca in responseText exista "cod_introdus"
			if((id_tag=='td_add_cod') && (http_request.responseText.indexOf('<!-- cod_introdus -->')!=-1)){
				return makeRequest('tabs_cont_on', 'index.php?js_get_pg=pgx,istoric_coduri', '');
			}
			if((id_tag=='td_recomanda') && document.frm_recomanda && document.frm_recomanda.dela_nume)
				document.frm_recomanda.dela_nume.focus();
		} else {
			window.location.href=url+'&js_get_pg=';
			//alert('There was a problem with the request.');
		}
	}

}


function on_off_tag(id_tag, arata){
	if(!document.getElementById || !document.getElementById(id_tag)) return false;
	//if(arata==null) arata=(document.getElementById(id_tag).style.visibility=='hidden');
	if(arata==null) arata=(document.getElementById(id_tag).style.display=='none');
	try{
		//document.getElementById(id_tag).style.visibility=(arata ? '' : 'hidden');
		//if(!arata) aTAG[id_tag]=document.getElementById(id_tag).innerHTML;
		//document.getElementById(id_tag).innerHTML=(arata ? (aTAG[id_tag] ? aTAG[id_tag] : '.') : '');		
		//document.getElementById(id_tag).style.visibility=(arata ? '' : 'hidden');
		//document.getElementById(id_tag).style.position=(arata ? '' : 'absolute');
		document.getElementById(id_tag).style.display=(arata ? '' : 'none');
	}catch(e){ }
	return false;
}

function get_var(variable, url) {
  var query = (url==null) ? window.location.search.substring(1) : url.substring(url.indexOf('?')+1);
  var vars = query.split("&"), ret='';
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable)  ret=pair[1];    
  } 
  return ret;
}

function get(frm){
  var sget='', n=null, v=null;
  if(!frm || !frm.length) return '';
  for(i=0; i<frm.length; i++) if((n=frm.elements[i].name) && ((v=frm.elements[i].value) || (v=='')))
  	sget+=(sget ? '&' : '')+n+'='+encodeURI(v);
  return sget;
}

