

//==============================================================================================================================
//function synchronization
// Function.prototype.sequence=function(g) {
//   var f=this;
//   return function() {
//     f();g();
//   }
// };
// function alertFrst() { execute('ajax.php', 'action=showTipMasina', 'idTdTipMasina'); }
// function alertSec() { showFields('TipMasina');changeTipMasina(document.getElementById('idTipMasina')); }
// //setTimeout( alertFrst.sequence(alertSec), 5000);

var selectedList;
var availableList;
var submitting;

function createListObjects(){
    //availableList = document.getElementById("locdisp");
    //selectedList = document.getElementById("selectedOptions");
    //submitting = false;
}
function delAttribute(){

    var selIndex = selectedList.selectedIndex;
    if(selIndex < 0) {
        return;
    }
    availableList.appendChild(selectedList.options.item(selIndex))
    selectNone(selectedList,availableList);
    //setSize(availableList,selectedList);
    //listHiddenParam();

}
function addAttribute(){
	 
    var addIndex = availableList.selectedIndex;
		
    if(addIndex < 0) {
        return;
    }

    selectedList.appendChild(availableList.options.item(addIndex));
    selectNone(selectedList,availableList);
    //setSize(selectedList,availableList);
		//listHiddenParam();    

}
function delAll(){
    var len = selectedList.length -1;
    for(i=len; i>=0; i--){
        availableList.appendChild(selectedList.item(i));
    }
    selectNone(selectedList,availableList);
    setSize(selectedList,availableList);
    
}
function addAll(){
    var len = availableList.length -1;
    for(i=len; i>=0; i--){
        selectedList.appendChild(availableList.item(i));
    }
    selectNone(selectedList,availableList);
    setSize(selectedList,availableList);
    
}
function selectNone(list1,list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}
function setSize(list1,list2){
    list1.size = getSize(list1);
    list2.size = getSize(list2);
}
function getSize(list){
    /* Mozilla ignores whitespace, IE doesn't - count the elements in the list */
    var len = list.childNodes.length;
    var nsLen = 0;
    //nodeType returns 1 for elements
    for(i=0; i<len; i++){
        if(list.childNodes.item(i).nodeType==1)
            nsLen++;
    }
    if(nsLen<2)
        return 2;
    else
        return nsLen;
}
function showSelected(){
    var optionList = document.getElementById("selectedOptions").options;
    var data = '';
    var len = optionList.length;
    for(i=0; i<len; i++){
        if(i>0)
            data += ',';
        data += optionList.item(i).value;
    }
    alert(data);
}

function listHiddenParam() {
	 data = '<input type="hidden" name="nrloc" value="' + selectedList.length + '">';
	 for(i=0; i<selectedList.options.length; i++){

        data += '<input type="hidden" name="loc'+i+'" value="' + selectedList.options.item(i).value + '">';
    }
   alert(data);
   document.write(data);
}


//general function, it gets the object for a specified id, depending on the browser
function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else return false;
}

//general function, changes the object's display (identified by 'objectId' id) from current state to 'newDisplay' state
function changeObjectDisplay(objectId, newDisplay) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
      	styleObject.display = newDisplay;
      	return true;
    } else return false;
} 

function showFields(str){
    var vect = str.split(",");
    for (i=0;i<vect.length;i++) {
    	if (!vect[i].match(/!/)) vect[i] = 'div'+vect[i];
		else vect[i] = vect[i].replace(/!/,''); //for custom names on fields
		changeObjectDisplay(vect[i],'');
	}
}

function showFieldsTimeOut(str,timeout){
	setTimeout("showFields('"+str+"')",timeout);
}
function hideFields(str){
    var vect = str.split(",");
    for (i=0;i<vect.length;i++) {
    	if (!vect[i].match(/!/)) vect[i] = 'div'+vect[i];
		else vect[i] = vect[i].replace(/!/,''); //for custom names on fields
		changeObjectDisplay(vect[i],'none');
	}
}
function eraseCBFieldsValue(str){
    var vect = str.split(",");
    var tmp;
    for (var i=0;i<vect.length;i++) {
    	if (!vect[i].match(/!/)) vect[i] = 'idTd'+vect[i];
		else vect[i] = vect[i].replace(/!/,''); //for custom names on fields
		tmp = document.getElementById(vect[i]);
		if (tmp) document.getElementById(vect[i]).innerHTML="";
	}return;
    for (var i=0;i<vect.length;i++) {
        if (!vect[i].match(/!/)) vect[i] = 'idSel'+vect[i];
		else vect[i] = vect[i].replace(/!/,''); //for custom names on fields
//alert(vect[i]); 
		tmp = document.getElementById(vect[i]);
		//alert(vect[i]);
        if (tmp){
        	//alert(tmp.name+' = '+tmp.type+' = '+tmp.value);
			if (tmp.type == 'select-multiple') {
				for (var j=0;j<tmp.length;j++) tmp.options[j].selected = false;
				
			}
			if (tmp.type == 'select-single') tmp.options[tmp.selectedIndex].value = "";
        	if (tmp.type == 'textarea') {tmp.value = "";}
        	if (vect[i] == "idSelConstructor") alert(tmp.value);
        }
        //alert(vect[i]+' = '+tmp.options[tmp.selectedIndex].value);
    }
}
function eraseRadioFieldsValue(str){
    var vect = str.split(",");
    var tmp;
    for (i=0;i<vect.length;i++) {
        tmp = document.getElementById(vect[i]);
        if (tmp) tmp.checked = false;
    }
}

function executeTimeOut(var1, var2, var3, time){
    setTimeout("execute('"+var1+"', '"+var2+"', '"+var3+"')",time);
}

//============start AJAX stuff ================





var ajaxReq;
var ajaxField;
var globalFunc;

//function that launches a php script on the server
function execute(page, params, camp) {
	//while (ajaxReq == null) ;	
    var url=page+"?"+params;
    ajaxField = camp;

    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        ajaxReq = new XMLHttpRequest();
        ajaxReq.onreadystatechange = processReqChange;
        ajaxReq.open("GET", url, true);
        ajaxReq.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (ajaxReq) {
            ajaxReq.onreadystatechange = processReqChange;
            ajaxReq.open("GET", url, true);
            ajaxReq.send(null); 
        }
    }
}

//process request changes from a xmlhttprequest call
function processReqChange() {
	//if (!ajaxReq) {alert('could not create ActiveXObject!');return;}
	if (!ajaxReq) return;
    // only if req shows "loaded"
    if (ajaxReq.readyState == 4) {
        // only if "OK"
        if (ajaxReq.status == 200) {success();}
        else {failure();}
        //ajaxReq = null;
        if (globalFunc && globalFunc != "") {setTimeout("eval(globalFunc);",15); }
    }
}

//if the process request from a xmlhttprequest call was successful, this method is called
function success(){
//	alert(ajaxField);
	var tmpStr = ajaxReq.responseText; ajaxReq = null;
	document.getElementById(ajaxField).innerHTML = "";
	document.getElementById(ajaxField).innerHTML = tmpStr;
	//document.getElementById(ajaxField).innerHTML = ajaxReq.responseText;
	
	
//alert(ajaxReq.responseText);
}	

//if the process request from a xmlhttprequest call was NOT successful, this method is called
function failure(){
    alert("There was a problem retrieving the XML data:\n" +  ajaxReq.statusText);
}
//============stop AJAX stuff ================


//names = comma separated value of all element's ids that are required to be != ''
//commands = the commands to be executed after all 'names' are not empty
//whichInterval = global parameter, used by javascript for clearInterval
//preReq = global parameter - checking if prerequisites have been accomplished or not
//postRez = global parameter - announcing other possible threads that this thread is done
var interval = new Array(); //arrays, because maybe I'll need more than one thread running at a time...
//if (preReq[500]==0) alert(preReq[500]);
var postReq	= new Array();
function checkResponse(names, commands, whichInterval, preReq_, postReq_){
//if (whichInterval == "interval[4]" || whichInterval == "interval[3]" || whichInterval == "interval[5]") 
//if (dbgg==1) alert('names='+names+', commands='+commands+', whichInterval='+whichInterval+', preReq='+preReq_+', postReq='+postReq_);
//alert(document.getElementById('idTdModel').innerHTML);
//alert(postReq['changeTipOferta_1']);
	var preReqV = new Array();
	if (preReq_.match(/,/)) preReqV = preReq_.split(','); else preReqV[0] = preReq_;
	for (var i=0;i<preReqV.length;i++) {
		//alert(preReqV[i]+' '+preReqV.length);
//alert("cmd:"+commands+",prereq(postReq['"+preReqV[i]+"'])="+eval("postReq['"+preReqV[i]+"']"));

// 		alert(document.getElementById('idTdTipMasina').innerHTML+
// 		'\n\n '+document.getElementById('idTdConstructor')+
// 		'\n\n '+postReq['forceSelectTipMasina_0']+
// 		'\n\n'+ commands+
// 		'\n\n'+'postReq['+preReqV[i]+']'+
// 		'\n\n ctm-1: '+eval("postReq['changeTipMasina_-1']"));
		
		if (eval("postReq['"+preReqV[i]+"']") == 0) {
			//alert('not done yet: postReq['+preReqV[i]+']='+eval("postReq['"+preReqV[i]+"']"));
			return; //prerequisites not accomplished yet
		}
	}

    var namesV = new Array();
    if (names.match(/,/)) namesV = names.split(',');
    else namesV[0] = names;
    //alert(namesV[0]);
    if (namesV[0]!="") for (var i=0;i<namesV.length;i++) {
    	//alert(namesV[i]+' = '+document.getElementById(namesV[i]).innerHTML);
		if (document.getElementById(namesV[i]).innerHTML == '') return;
	}

// 	if (commands == "changeTipMasina();"){
// 		eval("postReq['"+postReq_+"'] = 1;");
// 		window.clearInterval(eval(whichInterval)); eval(whichInterval+' = "";');
// 	}

	window.clearInterval(eval(whichInterval)); eval(whichInterval+' = "";');
	eval(commands);	
	eval("postReq['"+postReq_+"'] = 1;");

	//alert("dupa eval: cmd:"+commands+",prereq(postReq['"+postReq_+"'])="+eval("postReq['"+postReq_+"']"));
	//alert(eval(whichInterval));
	
	
} 

function startThread(interval_, fields_, commands_, preReq_, postReq_){
//if (interval_ =="interval[1]") alert(interval_);
// 	if (preReq_ == 1) preReqX = 1; 
// 	else {
// 		preReqX = "postReq[\\\\\'"+preReq_+"\\\\\']"; 
// 		eval("postReq['"+postReq_+"'] = 0;"); //initialize first with 0
// 		//alert("setat pe 0: postReq['"+postReq_+"']"+eval("postReq['"+postReq_+"']"));
// 	}
// 	postReqX = "postReq[\\\\\'"+postReq_+"\\\\\']";
// 	eval(interval_+
// 	" = window.setInterval(\"checkResponse('"+fields_+"',\\\""+commands_+"\\\",'"+interval_+"','"+preReqX+"','"+postReqX+"');\",10)");	

// 	if (preReq_ == '1') {preReqX = 1; preReq_='1'; postReq['1']=1;} 
// 	else {
// 	//alert(preReq_);return;
// 		//preReqX = "postReq[\\\\\'"+preReq_+"\\\\\']";
//		eval("postReq['"+postReq_+"'] = 0;"); //initialize first with 0 
// 		var preReqV = new Array();
// 		if (preReq_.match(/,/)) preReqV = preReq_.split(','); else preReqV[0] = preReq_;
// 		for (var i=0;i<preReqV.length;i++) {
// 			eval("postReq['"+preReqV[i]+"'] = 0;");  //initialize first with 0
// 			//alert("setat pe 0: postReq['"+preReqV[i]+"']"+eval("postReq['"+preReqV[i]+"']"));
// 			//return;
// 		}
//	}
	eval("postReq['"+postReq_+"'] = 0;"); //initialize first with 0
	//alert('nu ajung aici');return;
	//postReqX = "postReq[\\\\\'"+postReq_+"\\\\\']";
	eval(interval_+
	" = window.setInterval(\"checkResponse('"+fields_+"',\\\""+commands_+"\\\",'"+interval_+"','"+preReq_+"','"+postReq_+"');\",10)");	

}
	
function forceSelectTipMasina(){
//"setTimeout(\\\\\\\"changeTipMasina(document.getElementById('idSelTipMasina'));\\\\\\\",200);";
	var commands;
// 	var commands = 
// 			"showFields('TipMasina');"+
// 			"changeTipMasina(document.getElementById('idSelTipMasina'));";
	
	if (document.getElementById('idTdTipMasina').innerHTML == "") {
			globalFunc = "showFields('TipMasina');changeTipMasina();globalFunc=\"\";";
			execute('ajax.php', 'action=showTipMasina', 'idTdTipMasina');
			
// 			commands = "showFields('TipMasina');";
// 			startThread('interval[0]', 'idTdTipMasina', commands, 1,'forceSelectTipMasina_0');

	} else {
		showFields('TipMasina');
		changeTipMasina();
	}
}	

function changeTipOferta(tip){
	if (tip == 'pFizica'){
		//var unusedFields = 'idTipOfertaElem,idSelConstructor,idSelModel,idSelAnFabricatie,idSelTip,idSelTipCombustibil,idSelTipPiese,idSelDenumireTipPiese';
		var unusedFields = 'TipOfertaElem,Constructor,Model,AnFabricatie,Tip,TipCombustibil,TipPiese,DenumireTipPiese,Pret';
		hideFields(unusedFields+',orar_functionare'); eraseCBFieldsValue(unusedFields+',!orar_functionare');
		eraseRadioFieldsValue('idPieseAccMoto1,idPieseAccMoto2');
		forceSelectTipMasina();
		hideFields('PersoanaContact,ExempluNumeJuridic,StelutaAdresa,StelutaConstructor,StelutaMotoModel');
	
//		interval[0] = window.setInterval("checkResponse('idTdTipMasina',\""+commands+"\",'interval[0]',1,'postReq[0]');",100);		
	}
	if (tip == 'pJuridica'){
		//var unusedFields = 'idTipMasina,divPieseAccMoto,idConstructor,idSelModel,idSelAnFabricatie,idSelTip,idSelTipCombustibil,idSelTipPiese,idSelDenumireTipPiese';
		var unusedFields = 'Constructor,PieseAccMoto,Model,AnFabricatie,Tip,TipCombustibil,TipPiese,DenumireTipPiese,Pret';
		hideFields(unusedFields); eraseCBFieldsValue(unusedFields);
		eraseRadioFieldsValue('idPieseAccMoto1,idPieseAccMoto2');
		
// 		execute('ajax.php', 'action=showTipOfertaElem', 'idTdTipOfertaElem');
// 		executeTimeOut('ajax.php', 'action=showTipMasina', 'idTdTipMasina',100);
// 		executeTimeOut('ajax.php', 'action=showConstructor&idTipMasina=1&cbSelectMode=multiple', 'idTdConstructor',200);
// 		showFields('TipOfertaElem,TipMasina,Constructor,orar_functionare');
 		
		var commands;
		execute('ajax.php', 'action=showTipOfertaElem', 'idTdTipOfertaElem');
		
		var indexTipMasina = 1;
		if (document.getElementById('idTdTipMasina').innerHTML == "") {
			commands = "execute('ajax.php', 'action=showTipMasina', 'idTdTipMasina');";
			startThread('interval[1]', 'idTdTipOfertaElem', commands, '1','changeTipOferta_1');
		} else {
			tmpObj = document.getElementById('idSelTipMasina');
			indexTipMasina = tmpObj.options[tmpObj.selectedIndex].value;
			postReq['changeTipOferta_1'] = 1;
		}
//		interval[0] = window.setInterval("checkResponse('idTdTipOfertaElem',\""+commands+"\",'interval[0]',1,'postReq[0]');",100);

		commands = "execute('ajax.php', 'action=showConstructor&idTipMasina="+indexTipMasina+"&cbSelectMode=multiple', 'idTdConstructor');";
		startThread('interval[2]', 'idTdTipMasina,idTdTipOfertaElem', commands, 'changeTipOferta_1','changeTipOferta_2');
//		interval[1] = window.setInterval("checkResponse('idTdTipMasina',\""+commands+"\",'interval[1]','postReq[0]','postReq[1]');",100);
		
		commands = "showFields('TipOfertaElem,TipMasina,Constructor,orar_functionare');";
		startThread('interval[3]', 'idTdConstructor', commands, 'changeTipOferta_2','changeTipOferta_3');
		
		//commands = "document.getElementById('divorar_functionare').display='block';";
		//startThread('interval[3]', 'idTdConstructor', commands, 'postReq[2]','postReq[3]');
//		interval[2] = window.setInterval("checkResponse('idTdConstructor',\""+commands+"\",'interval[2]','postReq[1]','postReq[2]');",100);
		showFields('PersoanaContact,ExempluNumeJuridic,StelutaAdresa,StelutaConstructor');
	}
}


function clearIntervals(maxNo){
	for (var i=0;i<maxNo;i++){
		if (interval[i] && interval[i]!="") {
			window.clearInterval(interval[i]); interval[i] = "";
		}
	}
}

function changeTipMasina(){
	//first, cancel any operation being in progress and only then proceed with changeTipMasina...
	clearIntervals(15);
	
	var thisObj = document.getElementById('idSelTipMasina');
	var selIndex = thisObj.options[thisObj.selectedIndex].value;
	var unusedFields;
	var params = 'idTipMasina='+selIndex+'&action=';
	
	var objTipOferta2 = document.getElementById('idTipOferta2');
	if (objTipOferta2 && objTipOferta2.checked == true) { //selected 'pers juridica'
		hideFields('PieseAccMoto,Model,AnFabricatie,Tip,TipCombustibil,TipPiese,DenumireTipPiese,Pret,orar_functionare');
		var params = 'idTipMasina='+selIndex+'&action=';
		execute('ajax.php', params+'showConstructor&cbSelectMode=multiple', 'idTdConstructor');
		startThread('interval[0]', 'idTdTipMasina,idTdConstructor', "showFields('Constructor,orar_functionare');", '1','changeTipMasina_0');
		return;
	}
	
	//selected 'pers fizica'
	if (selIndex != 2) {    //daca nu a selectat moto, va afisa constructor CB!
		//unusedFields = 'idSelModel,idSelTip,idSelTipCombustibil,idSelDenumireTipPiese';
		unusedFields = 'Constructor,PieseAccMoto,Model,AnFabricatie,Tip,TipCombustibil,TipPiese,DenumireTipPiese,Pret,StelutaMotoModel';
	    hideFields(unusedFields); eraseCBFieldsValue(unusedFields);
	    eraseRadioFieldsValue('idPieseAccMoto1,idPieseAccMoto2');

	    
//  	    commands = "execute('ajax.php', '"+params+"showConstructor', 'idTdConstructor');";
//  		startThread('interval[-1]', 'idTdTipMasina', commands, 'forceSelectTipMasina_0', 'changeTipMasina_-1');
//  		startThread('interval[1]', 'idTdTipMasina,idTdConstructor', "showFields('Constructor');", 'changeTipMasina_-1', 'changeTipMasina_1');

		execute('ajax.php', params+'showConstructor', 'idTdConstructor');
		startThread('interval[1]', 'idTdTipMasina,idTdConstructor', "showFields('Constructor');", '1', 'changeTipMasina_1'); 
	    if (selIndex == 1 || selIndex == 3 || selIndex == 4) {	    	
			commands = "var objConstructor = document.getElementById('idSelConstructor');var constructorIndex = objConstructor.options[objConstructor.selectedIndex].value;"+
			"execute('ajax.php', '"+params+"showModel&idConstructor='+constructorIndex, 'idTdModel');";
			startThread('interval[2]', 'idTdConstructor', commands, 'changeTipMasina_1', 'changeTipMasina_2');
			//showFields('Model');
			startThread('interval[3]', 'idTdModel', "showHideModel();", 'changeTipMasina_2', 'changeTipMasina_3');
	    } else postReq['changeTipMasina_3'] = 1;
 
	    commands = "execute('ajax.php', '"+params+"showAnFabricatie', 'idTdAnFabricatie')";
	    startThread('interval[4]', 'idTdConstructor', commands, 'changeTipMasina_1,changeTipMasina_3', 'changeTipMasina_4');
		if (selIndex == 1 || selIndex == 5) {
	        commands = "execute('ajax.php', '"+params+"showTip', 'idTdTip');"
	        startThread('interval[5]', 'idTdAnFabricatie', commands, 'changeTipMasina_4', 'changeTipMasina_5');
	        startThread('interval[6]', 'idTdTip', "showFields('Tip');", 'changeTipMasina_5', 'changeTipMasina_6');
	        if (selIndex == 1) {
				commands = "execute('ajax.php', '"+params+"showTipCombustibil', 'idTdTipCombustibil')";
				startThread('interval[7]', 'idTdTip', commands, 'changeTipMasina_6', 'changeTipMasina_7');
				
				//commands = "execute('ajax.php', '"+params+"showDenumireTipPiese&idTipPiese=0', 'idTdDenumireTipPiese');"
				//startThread('interval[8]', 'idTdTipCombustibil', commands, 'changeTipMasina_7', 'changeTipMasina_8');
				postReq['changeTipMasina_8'] = 1;
				
				//commands = "showFields('TipCombustibil,DenumireTipPiese');";
				commands = "showFields('TipCombustibil');";
				//startThread('interval[9]', 'idTdDenumireTipPiese', commands, 'changeTipMasina_8', 'changeTipMasina_9');
				startThread('interval[9]', 'idTdTipCombustibil', commands, 'changeTipMasina_8', 'changeTipMasina_9');
			} else postReq['changeTipMasina_9'] = 1;
	    } else {postReq['changeTipMasina_6'] = 1; postReq['changeTipMasina_9'] = 1;}
	    
		if (selIndex == 3){
			commands = "execute('ajax.php', '"+params+"showTipCombustibil', 'idTdTipCombustibil')";
			startThread('interval[10]', 'idTdAnFabricatie', commands, 'changeTipMasina_4', 'changeTipMasina_10');
			commands = "showFields('TipCombustibil');";
			startThread('interval[11]', 'idTdTipCombustibil', commands, 'changeTipMasina_10', 'changeTipMasina_11');			
		} else {postReq['changeTipMasina_11'] = 1;}
		
	    commands = "execute('ajax.php', '"+params+"showTipPiese', 'idTdTipPiese');"
	    startThread('interval[12]', 'idTdAnFabricatie', commands, 'changeTipMasina_4,changeTipMasina_6,changeTipMasina_9,changeTipMasina_11', 'changeTipMasina_12');
	    //alert('dd');
	    commands = "execute('ajax.php', '"+params+"showPret&showPretMode=interval', 'idTdPret');";
	    startThread('interval[13]', 'idTdTipPiese', commands, 'changeTipMasina_12', 'changeTipMasina_13');
	    commands = "showFields('Constructor,AnFabricatie,TipPiese,Pret');";
	    startThread('interval[14]', 'idTdPret', commands, 'changeTipMasina_13', 'changeTipMasina_14');
	}else{                  //daca a selectat moto, nu va afisa constructor!
		//unusedFields = 'idSelConstructor,idSelModel,idSelAnFabricatie,idSelTip,idSelTipCombustibil,idSelTipPiese,idSelDenumireTipPiese';
		unusedFields = 'Constructor,Model,AnFabricatie,Tip,TipCombustibil,TipPiese,DenumireTipPiese';
	    hideFields(unusedFields); eraseCBFieldsValue(unusedFields);
		showFields('PieseAccMoto');
	}
}


var vectConstructorMultiple = new Array();
function changeConstructor(){
	var objTipOferta2 = document.getElementById('idTipOferta2');
	if (objTipOferta2 && objTipOferta2.checked == true) { //selected 'pers juridica'
		//all this shit to limit the maximum number of selected options to a specified value!
		var idConstructorObj = document.getElementById('idSelConstructor');
		var i; var countSel = 0;
		for (i=0;i<idConstructorObj.length;i++){
			if (idConstructorObj.options[i].selected == true){
				if (countSel >= 6) {
					alert('Nu puteti selecta mai mult de 6 constructori !');
					for (var j=0;j<idConstructorObj.length;j++){
						//deselect the latest selected field! (for this, you need history from vectConstructorMultiple)
						if (vectConstructorMultiple[j] != idConstructorObj.options[j].selected)
							idConstructorObj.options[j].selected = false;
					}
					return;
				}
				countSel++;
			}
		}
		for (i=0;i<idConstructorObj.length;i++){
			vectConstructorMultiple[i] = idConstructorObj.options[i].selected;
		}
		return;
	}
	
	//selected 'pers fizica'
	var thisObj = document.getElementById('idSelConstructor');
	var idConstructor = thisObj.options[thisObj.selectedIndex].value;
	
	var tipMasinaObj = document.getElementById('idSelTipMasina');
	var idTipMasina = tipMasinaObj.options[tipMasinaObj.selectedIndex].value;
	
	var objFormCautareSU1 = document.getElementById('formCautareSU1');
	var moto1 = document.getElementById('idPieseAccMoto1');
	var moto2 = document.getElementById('idPieseAccMoto2');
    if (!( objFormCautareSU1 && 
	 ( (moto1 && moto1.checked == true) || (moto2 && moto2.checked == true) ) )
	) { //not show model in search mode with 'motociclete' selected 
		var params = 'action=showModel&idTipMasina='+idTipMasina+'&idConstructor='+idConstructor;
		globalFunc = "showHideModel();globalFunc=\"\";";
		execute('ajax.php', params, 'idTdModel');
		//setTimeout("changeObjectDisplay('divModel','')",100);
	}
}

function changeTipCombustibil(){
	var idTipMasinaObj = document.getElementById('idSelTipMasina');
	var idTipMasina = idTipMasinaObj.options[idTipMasinaObj.selectedIndex].value;
	var idTipPieseObj = document.getElementById('idSelTipPiese');
	var idTipPiese = idTipPieseObj.options[idTipPieseObj.selectedIndex].value;
	var thisObj = document.getElementById('idSelTipCombustibil');
	var idTipCombustibil = thisObj.options[thisObj.selectedIndex].value;
 
	if (idTipMasina!= 3) {
		var params = 'action=showDenumireTipPiese&idTipMasina=$idTipMasina&idTipPiese='+idTipPiese+'&idTipCombustibil='+idTipCombustibil;
    	globalFunc = "showHideDenumireTipPiese();globalFunc=\"\";";
		execute('ajax.php', params, 'idTdDenumireTipPiese');
    	//setTimeout("changeObjectDisplay('divDenumireTipPiese','')",100);
    }
}
function changeTipPiese(){
	var idTipMasinaObj = document.getElementById('idSelTipMasina');
	var idTipMasina = idTipMasinaObj.options[idTipMasinaObj.selectedIndex].value;
	var thisObj = document.getElementById('idSelTipPiese');
	var idTipPiese = thisObj.options[thisObj.selectedIndex].value;

	if (idTipMasina == 1) { //only if it selects auto
		var idTipCombustibilObj = document.getElementById('idSelTipCombustibil');
		var idTipCombustibil = idTipCombustibilObj.options[idTipCombustibilObj.selectedIndex].value;
		var params = 'action=showDenumireTipPiese&idTipCombustibil='+idTipCombustibil+'&idTipMasina='+idTipMasina+'&idTipPiese='+idTipPiese;
    	globalFunc = "showHideDenumireTipPiese();globalFunc=\"\";";
		execute('ajax.php', params, 'idTdDenumireTipPiese');
    }
}
function showHideDenumireTipPiese(){
    var objDenTipPiese = document.getElementById('idSelDenumireTipPiese');
    //alert(objDenTipPiese.options[0].innerHTML+' '+objDenTipPiese.length);
    if (objDenTipPiese.length == 0 
		|| (objDenTipPiese.length == 1 && objDenTipPiese.options[0].innerHTML == "Oricare")
	   	) {
        changeObjectDisplay('divDenumireTipPiese','none');
        eraseCBFieldsValue('DenumireTipPiese');
    }
    else changeObjectDisplay('divDenumireTipPiese','');
}

function showHideModel(){
    var objModel = document.getElementById('idSelModel');
    //alert(objDenTipPiese.options[0].innerHTML+' '+objDenTipPiese.length);
    if (!objModel) return;
    if (objModel.type=="text") { //'add form' case with motociclete/piese moto selected 
		changeObjectDisplay('divModel','');
		return;
	}
	if (objModel.length == 0 
		|| (objModel.length == 1 && objModel.options[0].innerHTML == "Oricare")
	   	) {
        changeObjectDisplay('divModel','none');
        eraseCBFieldsValue('Model');
    }
    else changeObjectDisplay('divModel','');
}

function changePieseAccMoto(tip){
	clearIntervals(7);
	if (tip == 'piese') {
    	//var unusedFields = 'idSelTip,idSelTipCombustibil,idSelTipPiese,idSelDenumireTipPiese';
    	var unusedFields = 'Tip,TipCombustibil,TipPiese,DenumireTipPiese';
        hideFields(unusedFields); eraseCBFieldsValue(unusedFields);
        var fieldsToShow = 'Constructor,AnFabricatie,TipPiese';
        
        var idTipMasinaObj = document.getElementById('idSelTipMasina');
        var params = 'idTipMasina='+idTipMasinaObj.options[idTipMasinaObj.selectedIndex].value+'&action=';
        //execute('ajax.php', params+'showConstructor', 'idTdConstructor');
        commands = "execute('ajax.php', '"+params+"showConstructor', 'idTdConstructor');";
        startThread('interval[1]', 'idTdTipMasina', commands, '1', 'changePieseAccMoto_0');
        
        var objFormCautareSU1 = document.getElementById('formCautareSU1');
        var mandatoryFields = "idTdConstructor";
    	if (!objFormCautareSU1){ //not the 'search form' case, but the 'add form' case
    		mandatoryFields+=",idTdModel";
			commands = "var thisObj = document.getElementById('idSelConstructor');"+
						"var idConstructor = thisObj.options[thisObj.selectedIndex].value;"+
						"execute('ajax.php', '"+params+"showModel&idConstructor='+idConstructor, 'idTdModel');";
	    	startThread('interval[2]', 'idTdConstructor', commands, 'changePieseAccMoto_0', 'changePieseAccMoto_1');
	    } else { postReq['changePieseAccMoto_1'] = 1;}

	    commands = "execute('ajax.php', '"+params+"showAnFabricatie', 'idTdAnFabricatie');";
	    startThread('interval[3]', mandatoryFields, commands, 'changePieseAccMoto_1', 'changePieseAccMoto_2');
	    	    
	    commands = "execute('ajax.php', '"+params+"showTipPiese', 'idTdTipPiese');";
	    startThread('interval[4]', 'idTdAnFabricatie', commands, 'changePieseAccMoto_2', 'changePieseAccMoto_3');

	    commands = "execute('ajax.php', '"+params+"showPret&showPretMode=interval', 'idTdPret');";
	    startThread('interval[5]', 'idTdTipPiese', commands, 'changePieseAccMoto_3', 'changePieseAccMoto_4');

		commands = "showFields('"+fieldsToShow+"');showHideModel();showFields('Pret,StelutaMotoModel');";
	    startThread('interval[6]', 'idTdTipPiese', commands, 'changePieseAccMoto_4', 'changePieseAccMoto_5');
	    
//         executeTimeOut('ajax.php', params+'showModel&idConstructor=0', 'idTdModel',100);
//         executeTimeOut('ajax.php', params+'showAnFabricatie', 'idTdAnFabricatie',200);
//         executeTimeOut('ajax.php', params+'showTipPiese', 'idTdTipPiese',300);
//         showFields('Constructor,Model,AnFabricatie,TipPiese');
    }
    if (tip == 'accesorii') {
		//var unusedFields = 'idSelConstructor,idSelModel,idSelAnFabricatie,idSelTipCombustibil,idSelTipPiese,idSelDenumireTipPiese';
		var unusedFields = 'Constructor,Model,AnFabricatie,TipCombustibil,TipPiese,DenumireTipPiese';
		hideFields(unusedFields); eraseCBFieldsValue(unusedFields);
		
		var idTipMasinaObj = document.getElementById('idSelTipMasina');
		var params = 'idTipMasina='+idTipMasinaObj.options[idTipMasinaObj.selectedIndex].value+'&action=';
		execute('ajax.php', params+'showTip', 'idTdTip');
	    startThread('interval[7]', 'idTdTip', "showFields('Tip');", '1', 'changePieseAccMoto_6');
		//showFields('Tip');
	}
}

var vectDenumireTipPiese = new Array();
function changeDenumireTipPiese(){
	var idTipPieseObj = document.getElementById('idSelTipPiese');
	var idTipPiese = idTipPieseObj.options[idTipPieseObj.selectedIndex].value;
	var valTipPiese = idTipPieseObj.options[idTipPieseObj.selectedIndex].text;
	var idDenumireTipPieseObj = document.getElementById('idSelDenumireTipPiese');
	
	var mComplet=false; var mCompletIndex=-1; var i; var countSel = 0;
	if (valTipPiese == 'piese de motor'){ //selected 'piese de motor'
		for (i=0;i<idDenumireTipPieseObj.length;i++){
			if (idDenumireTipPieseObj.options[i].selected == true){
				if (countSel >= 3) {
					alert('Nu puteti selecta mai mult de 3 piese!');
					for (var j=0;j<idDenumireTipPieseObj.length;j++){
						//deselect the latest selected field! (for this, you need history from vectDenumireTipPiese)
						if (vectDenumireTipPiese[j] != idDenumireTipPieseObj.options[j].selected)
							idDenumireTipPieseObj.options[j].selected = false;
					}
					return;
				}
				countSel++;
				if (idDenumireTipPieseObj.options[i].text == 'motor complet'){
					mComplet=true; mCompletIndex = i;
				}
			}
		}
		for (i=0;i<idDenumireTipPieseObj.length;i++){
			vectDenumireTipPiese[i] = idDenumireTipPieseObj.options[i].selected;
		}
		if (mComplet == true){
			for (i=0;i<idDenumireTipPieseObj.length;i++) 
				if (i != mCompletIndex) idDenumireTipPieseObj.options[i].selected = false;
			if (countSel>1) alert('Daca selectati motor complet, nu mai puteti selecta alte piese!');
		}
	}
}

function showAfisareRezultatePiese(linkPieseHref){
	var xDisplacement = 100; var yDisplacement = 100;
	var xLocationFF = window.screenX+xDisplacement; 	var yLocationFF = window.screenY+yDisplacement; /*Firefox*/
	var xLocationIE = window.screenLeft+xDisplacement; 	var yLocationIE = window.screenTop+yDisplacement; 	/*IE*/
	/*alert(xLocationFF);*/
	var xLocation, yLocation;
	if (isNaN(xLocationFF)) xLocation = xLocationIE; else xLocation = xLocationFF;
	if (isNaN(yLocationFF)) yLocation = yLocationIE; else yLocation = yLocationFF;
	/*height=840 - ca sa vezi 6 anunturi pe pagina*/
	wind = window.open(linkPieseHref,'mywindow','location=1,status=1,scrollbars=1,resizable=0,width=600,height=580,left='+xLocation+',top='+yLocation+',screenX='+xLocation+',screenY='+yLocation);
	wind.focus();
}

function showAnuntDetaliatDinAfisareRezultatePiese(linkPieseHref){
	var xDisplacement = 100; var yDisplacement = 100;
	var xLocationFF = window.screenX+xDisplacement; 	var yLocationFF = window.screenY+yDisplacement; /*Firefox*/
	var xLocationIE = window.screenLeft+xDisplacement; 	var yLocationIE = window.screenTop+yDisplacement; 	/*IE*/
	/*alert(xLocationFF);*/
	var xLocation, yLocation;
	if (isNaN(xLocationFF)) xLocation = xLocationIE; else xLocation = xLocationFF;
	if (isNaN(yLocationFF)) yLocation = yLocationIE; else yLocation = yLocationFF;
	/*height=840 - ca sa vezi 6 anunturi pe pagina*/
	wind = window.open(linkPieseHref,'mywindow2','height=500,width=625,status=no,scrollbars=yes,toolbar=no,menubar=no,location=yes,left='+xLocation+',top='+yLocation+',screenX='+xLocation+',screenY='+yLocation);
	wind.focus();
}

