var bCount = 0;

// Creates & returns an ajax object
function newAjaxObject()
{
	var xmlhttp = null;
	
	try
	{
		// Mozilla, Opera, Safari, Konqueror and everything else uses this method
		xmlhttp = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			// IE lower than 6 implements Msxml lib (only 5.xx versions,
			// versions below 5.0 does not support asynchronous queries
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				// For IE 6 and later
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				// No asynchronous support provided by this browser...
				alert("Your browser doesn't support Ajax Requests\n\nTherefore, certain features may not be available to you.")
			}
		}
	}
	return xmlhttp;
}

// Init some new ajax objects
var ajax = newAjaxObject(); // this is globally used
var ajaxBusy = false;

// Makes a query using the ajax object ajaxObj to the page serverPage
// using GET or POST (sendMethod) and puts the response into the div with the id divID
// If the POST method is prefered, the params to be sent must be given as a string of this form:
// field1=value1&field2=value2&field3=value3 (etc.)
function makequery( ajaxObj, serverPage, divID, sendMethod, params )
{
	// Get the container's handler
	var div = document.getElementById( divID );
	
	if( ajaxBusy )
	{
		ajaxObj.onreadystatechange = function() {}
		ajaxObj.abort();
	}
	
	if( ajaxObj )
	{
		if( sendMethod == "GET" )
		{
			ajaxObj.open( "GET", serverPage, true );
			ajaxBusy = true;
			ajaxObj.onreadystatechange = function()
			{
				if( ajaxObj.readyState == 4 && ajaxObj.status == 200 )
				{
					div.innerHTML = ajaxObj.responseText;
				}
				ajaxBusy = false;
			}
			ajax.send( null );
		}
		else if( sendMethod == "POST" )
		{
			ajaxObj.open( "POST", serverPage, true );
			ajaxBusy = true;
			ajaxObj.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
			ajaxObj.onreadystatechange = function()
			{
				if( ajaxObj.readyState == 4 && ajaxObj.status == 200 )
				{
					div.innerHTML = ajaxObj.responseText;
				}
				ajaxBusy = false;
			}
			ajaxObj.send( params );
		}
		else
		{
			// No other valid known methods for passing the data...
			alert( "Invalid or no method passed to the function!" );
			return;
		}
	}
}

function updateSubdomenii( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/domenii.php?s=' + selected, 'updater_dom', "GET" );
}

function updateSubdomeniiReg( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/domenii_reg.php?s=' + selected, 'updater_dom', "GET" );
}

function updateLocalitati( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/localitati.php?s=' + selected, 'updater_jud', "GET" );
}

function updateCartiere( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/cartiere.php?s=' + selected, 'updater_loc', "GET" );
}

function updateLocalitatiCom( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/localitati_com.php?s=' + selected, 'updater_jud', "GET" );
}

function updateCartiereCom( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/cartiere_com.php?s=' + selected, 'updater_loc', "GET" );
}

function updateDateContact( obj )
{
  var selected = obj.options[obj.selectedIndex].value;
  
  makequery( ajax, 'pages/update_date_contact.php?s=' + selected, 'updater_date_contact', "GET" );
}

function updateJudet( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_judet.php?s=' + selected, 'updater_judet', "GET" );
}

function updateLocalitate( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_localitate.php?s=' + selected, 'updater_localitate', "GET" );
}

function updateJudetAdmin( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_judet.php?s=' + selected, 'updater_locatie', "GET" );
}

function updateLocalitateAdmin( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_localitate.php?s=' + selected, 'updater_locatie', "GET" );
}

function updateDomeniu0( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_dom0.php?s=' + selected, 'updater_dom', "GET" );
}

function updateDomeniu1( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_dom1.php?s=' + selected, 'updater_dom', "GET" );
}

function updateDomeniu2( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_dom2.php?s=' + selected, 'updater_dom', "GET" );
}

function updateDomeniu3( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_dom3.php?s=' + selected, 'updater_dom', "GET" );
}

function updateNrIntr( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_intrebari.php?s=' + selected, 'updater_intrebari', "GET" );
}

function updateNrTxt( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_texte.php?s=' + selected, 'updater_texte', "GET" );
}

function updateRaspunsuri( obj, id )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_raspunsuri.php?s=' + selected, 'updater_raspunsuri_' + id, "GET" );
}

function updateCounter( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_sms_counter.php?s=' + selected, 'updater_sms_counter', "GET" );
}

function updateCounterOnLoad( obj )
{
	makequery( ajax, 'pages/update_sms_counter.php?s=' + obj, 'updater_sms_counter', "GET" );
}

function check_date( sel, date )
{
	var x = sel.split('-');
	var y = date.split('-');
	if	( Date.UTC(x[2],x[1],x[0],0,0,0) < Date.UTC(y[2],y[1],y[0],0,0,0) )
	{
		alert('Va rugam introduceti o data valida, mai mare sau egala cu data curenta!');
	}
}

function check_dur( val )
{
	if (!parseInt(val))
	{
		alert('Va rugam sa specificati durata (numai cu cifre)!');
		focus();
	}
}

function check_dom( jud, loc, car )
{
	if ((parseInt(jud)) && (parseInt(loc)))
	{
		document.getElementById('form_comanda_home').submit();
	}
	else if (!parseInt(jud.value))
	{
		alert('Va rugam selectati judetul!');
	}
	else if (!parseInt(loc.value))
	{
		alert('Va rugam selectati localitatea!');
	}
	else if ( car )
	{
		if (parseInt(car))
		{
			document.getElementById('form_comanda_home').submit();
		}
		else if (!parseInt(car.value))
		{
			alert('Va rugam selectati cartierul!');
		}
		else
		{
			document.getElementById('form_comanda_home').submit();
		}
	}
	else 
	{
		document.getElementById('form_comanda_home').submit();
	}
}

function check_subdom( dom, subdom, jud, loc, car )
{
	if ((parseInt(jud)) && (parseInt(loc)))
	{
		window.location=('index.php?pag=comanda&domeniu=' + dom + '&dom=' + subdom + '&zona=reg&j=' + jud + '&l=' + loc + '&c=' + car);
	}
	else if (!parseInt(jud.value))
	{
		alert('Va rugam selectati judetul!');
	}
	else if (!parseInt(loc.value))
	{
		alert('Va rugam selectati localitatea!');
	}
	else if ( car )
	{
		if (parseInt(car))
		{
			window.location=('index.php?pag=comanda&domeniu=' + dom + '&dom=' + subdom + '&zona=reg&j=' + jud + '&l=' + loc + '&c=' + car);
		}
		else if (!parseInt(car.value))
		{
			alert('Va rugam selectati cartierul!');
		}
		else
		{
			window.location=('index.php?pag=comanda&domeniu=' + dom + '&dom=' + subdom + '&zona=reg&j=' + jud.value + '&l=' + loc.value + '&c=' + car.value);
		}
	}
	else 
	{
		window.location=('index.php?pag=comanda&domeniu=' + dom + '&dom=' + subdom + '&zona=reg&j=' + jud.value + '&l=' + loc.value);
	}
}

function nrCriterii( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_cauta_com.php?s=' + selected, 'updater_cauta_com', "GET" );
}

function nrCriteriiCl( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_cauta_client.php?s=' + selected, 'updater_cauta_client', "GET" );
}

function nrCriteriiPr( obj )
{
	var selected = obj.options[obj.selectedIndex].value;
	makequery( ajax, 'pages/update_cauta_prestator.php?s=' + selected, 'updater_cauta_prestator', "GET" );
}

function doFocus( id )
{
	return document.getElementById(id).focus();
}

function showCalendar( obj, id )
{
	if (obj == 'data')
	{
		document.getElementById(id).style.display='inline';
	}
	else
	{
		document.getElementById(id).style.display='none';
	}
}

function adZona( jud, loc, car )
{
	if (jud.options[jud.selectedIndex].value != 'selecteaza') var selected_jud = jud.options[jud.selectedIndex].value;
	else alert('Va rugam alegeti judetul!');
	if (loc && loc.options[loc.selectedIndex].value != 'selecteaza') var selected_loc = loc.options[loc.selectedIndex].value;
	else alert('Va rugam alegeti localitatea!');
	if (car && car.options[car.selectedIndex].value != 'selecteaza') var selected_car = car.options[car.selectedIndex].value;
	else if (car && car.options[car.selectedIndex].value == 'selecteaza') alert('Va rugam alegeti cartierul!');
	else if (jud.options[jud.selectedIndex].value != 'selecteaza' && loc && loc.options[loc.selectedIndex].value != 'selecteaza' && !car) var selected_car = 0;
	if ( parseInt(selected_jud) && parseInt(selected_loc) && (parseInt(selected_car) || selected_car == 0 ) )
	{
		makequery( ajax, 'pages/ad_zona.php?j=' + selected_jud + '&l=' + selected_loc + '&c=' + selected_car, 'ad_zona', "GET" );
		setTimeout("window.location=window.location",1200);
	}
}

function adZona2( jud, loc, car )
{
	if (jud.options[jud.selectedIndex].value != 'selecteaza') var selected_jud = jud.options[jud.selectedIndex].value;
	if (loc && loc.options[loc.selectedIndex].value != 'selecteaza') var selected_loc = loc.options[loc.selectedIndex].value;
	if (car && car.options[car.selectedIndex].value != 'selecteaza') var selected_car = car.options[car.selectedIndex].value;
	else if (jud.options[jud.selectedIndex].value != 'selecteaza' && loc && loc.options[loc.selectedIndex].value != 'selecteaza' && !car) var selected_car = 0;
	if ( parseInt(selected_jud) && parseInt(selected_loc) && (parseInt(selected_car) || selected_car == 0 ) )
	{
		makequery( ajax, 'pages/ad_zona.php?j=' + selected_jud + '&l=' + selected_loc + '&c=' + selected_car, 'ad_zona', "GET" );
		setTimeout("window.location='index.php?pag=home&act=vezi'",1200);
	}
	else window.location='index.php?pag=home&act=vezi';
}

function resetPnote(id,which)
{
	var cont = document.getElementById(id);
	var as = cont.getElementsByTagName('a');
	
	for( i = 0;i < as.length;i++ )
	{
		as[i].style.backgroundColor = '#FFD9A6';
		as[i].style.color = '#AA5A03';
	}
	
	which.style.backgroundColor = '#AA5A03';
	which.style.color = '#FFD9A6';
}

function trColor(id)
{
	var tr = document.getElementById('tr_id_' + id);
	var tds = tr.getElementsByTagName('td');
	for	(i=0;i<tds.length;i++)
	{
		if (tds[i].style.color == '') tds[i].style.color = '#731D00';
		else tds[i].style.color = '';
	}
}

function updateModContact(sel)
{
	stil = (navigator.appName == 'Microsoft Internet Explorer') ? 'block' : 'table-row';
	switch(sel)
	{
		case 0:
			document.getElementById('mobil').style.display='none';
			document.getElementById('email').style.display='none';
			break;
		case 1:
			document.getElementById('mobil').style.display=stil;
			document.getElementById('email').style.display='none';
			break;
		case 2:
			document.getElementById('mobil').style.display='none';
			document.getElementById('email').style.display=stil;
			break;
		case 3:
			document.getElementById('mobil').style.display=stil;
			document.getElementById('email').style.display=stil;
			break;
	}
}

function updateModContactProfil(sel)
{
	stil = (navigator.appName == 'Microsoft Internet Explorer') ? 'block' : 'table-row';
	switch(sel)
	{
		case 0:
			document.getElementById('mobil').style.display=stil;
			document.getElementById('email').style.display='none';
			break;
		case 1:
			document.getElementById('mobil').style.display='none';
			document.getElementById('email').style.display=stil;
			break;
		case 2:
			document.getElementById('mobil').style.display=stil;
			document.getElementById('email').style.display=stil;
			break;
	}
}

function displayComp(sel)
{
	stil = (navigator.appName == 'Microsoft Internet Explorer') ? 'block' : 'table-row';
	switch(sel)
	{
		case 0:
			document.getElementById('trCompanie').style.display='none';
			break;
		case 1:
			document.getElementById('trCompanie').style.display='none';
			break;
		case 2:
			document.getElementById('trCompanie').style.display=stil;
			break;
	}	
}

function displaySam(sel)
{
	stil = (navigator.appName == 'Microsoft Internet Explorer') ? 'block' : 'table-row';
	switch(sel)
	{
		case 0:
			document.getElementById('trSam').style.display='none';
			break;
		case 1:
			document.getElementById('trSam').style.display=stil;
			break;
		case 2:
			document.getElementById('trSam').style.display='none';
			break;
	}	
}

function activate_select(sel, actId)
{
	if (sel == 0) document.getElementById(actId).disabled=1;
	else if (sel == 1) document.getElementById(actId).disabled=0;
}

function appendNewLine(divID, i)
{
	var oldLines = document.getElementById(divID).innerHTML;
	var newLine = '<table><tr><td colspan="2" align="center">Referinta '+(i+1)+'</td></tr><tr><td>Nume:</td><td><input type="text" name="nume['+(i+1)+']" /></td></tr><tr><td>Telefon:</td><td><input type="text" name="tel['+(i+1)+']" maxlength="10" /></td></tr><tr><td>Lucrare:</td><td><textarea name="lucrare['+(i+1)+']"></textarea></td></tr><tr><td colspan="2" align="right"><a id="new_'+(i+1)+'" href="javascript:appendNewLine(\''+divID+'\', '+(i+1)+');">adauga inca un camp pentru referinte</a></td></tr><tr><td colspan="2" align="center"><hr /></td></tr></table>';
	document.getElementById(divID).innerHTML = oldLines + newLine;
	document.getElementById('new_'+i).style.display = 'none';
}

function show(id)
{
	if (document.getElementById(id)) document.getElementById(id).style.display='block';
}

function limitText(limitField, limitCount, limitNum) 
{
	if (limitField.value.length > limitNum) 
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else 
	{
		limitCount.value = limitNum - limitField.value.length;
	}
}
