/**
*Face managementul sirurilor care trebuie verificate pt valoari goale
*
*@param nrFields	numaru de cimpuri de verificat (poate sa fie 1 sau 2)
*@param fldAray		sirul de cimpuri sub forma de array
**/
function checkEmpty(nrFields, fldAray){
	var arTmp, idTmp, msgTmp;
	if(fldAray.constructor == Array){
		switch(nrFields){
			case "1":
				if(fldAray[0] != 'undefined' && fldAray[0] != ""){
					if(!checkValue(fldAray[0], fldAray[1])){
						return false;
					}
				}			  
				break;    
				
			case "2":
			  for(var i = 0; i < fldAray.length; i++){
			  	 arTmp  = fldAray[i];
				 idTmp  = arTmp[0];
				 msgTmp = arTmp[1];
				 if(!checkValue(idTmp, msgTmp)){
			 		 return false;
			 	 }
			  }
			  break;
		}
		
		return true;
	}
}

/**
*Verifica cimpurile date de valuri goale
*
*@param idFld	id-ul cimpului de verificat
*@param msgAlert	mesajul care va fi afisat in cazul in care un cimpul verificat este gol
**/
function checkValue(idFld, msgAlert){
	if(document.getElementById(idFld)){
		var objFld = document.getElementById(idFld);
		if(objFld.value == "" || objFld.value == 'undefined'){
			alert(msgAlert);
			try{objFld.focus();}catch(err){}
			return false;
		}
		
		return true;	
	}	
}

/**
*Verifica un cimp dupa expresii regulate
*
*@param nrFields	numaru de cimpuri de verificat (poate sa fie 1 sau 2)
*@param fldAray		sirul de cimpuri sub forma de array
*@param regExx 		the regular expresion
**/
function checkWithRegEx(nrFieldsx, fldArayx, regExx){
	var arTmpx, idTmpx, msgTmpx;
	if(fldArayx.constructor == Array){
		switch(nrFieldsx){
			case "1":
				if(fldArayx[0] != 'undefined' && fldArayx[0] != ""){
					var objFld_1 = document.getElementById(fldArayx[0]);
					if(!machtRegEx(objFld_1.value, regExx)){
						alert(fldArayx[1]);
						try{objFld_1.focus();}catch(err){}
						return false;
					}
				}			  
				break;    
				
			case "2":
			  for(var i = 0; i < fldArayx.length; i++){
			  	 arTmpx  = fldArayx[i];
				 idTmpx  = arTmpx[0];
				 msgTmpx = arTmpx[1];
				 var objFld_2 = document.getElementById(idTmpx);
				 if(!machtRegEx(objFld_2.value, regExx)){
 						alert(msgTmpx);
						try{objFld_2.focus();}catch(err){}
			 		 return false;
			 	 }
			  }
			  break;
		}
		
		return true;
	}
}

/*
*Find if a string maches a regular expresion
*
*@param strSource	the string
*@param regEx		regular expresion
*/
function machtRegEx(strSource, regEx)
{
	var re = new RegExp(regEx);
	if(strSource.match(re))
		return true;
	else
		return false;
}

/**
*Generic function to stop the bubbling efect of events
*
*@param e	the event
**/
function stopBubble(e){
	// If an event object is provided, then this is a non-IE browser
	if ( e && e.stopPropagation )
		// and therefore it supports the W3C stopPropagation() method
		e.stopPropagation();
	else
		// Otherwise, we need to use the Internet Explorer way of cancelling event bubbling
		window.event.cancelBubble = true;
}

/**
*Display a confirmation message
*
*@param msg		the message to be displayd
**/
function confirmation(msg)
{
	if(confirm(msg))
		return true;
	else
		return false;
}

/**
*Delete a given row
*
*@param row				the row to be deleted
*@param returnOptio		can be 'return'.if given the function return the row number in a table
**/
function deleteRow(row, returnOption){
	while (row.tagName.toLowerCase() !='tr')
	{
		if(row.parentElement)
			row = row.parentElement;
		else if(row.parentNode)
			row = row.parentNode;
		else
			return;
	}
		
	var rowNum = row.rowIndex;
			
	if(returnOption == "return") 
		return rowNum;
			
	while (row.tagName.toLowerCase() !='table')
	{
		if(row.parentElement)
			row = row.parentElement;
		else if(row.parentNode)
			row = row.parentNode;
		else
			return;
	}

	row.deleteRow(rowNum);
}


/**
*Ajax function
*
*@param method			can be 'GET' or 'POST'
*@param scriptServ		the script executed on server. If you have GET parameters append them here (ex: script.php?p=1&q=2 ...)
*@param sincron			the way ajax its working .Can be true (asincron) or false (sincron)
*@param paramPost		post parameters (ex: p=1&q=2 ...)
**/
function ajaxFunction(method, scriptServ, sincron, paramPost)
{
  var xmlHttp;
  var varReturn;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }

    xmlHttp.onreadystatechange = function()
    {
    	if(xmlHttp.readyState == 4)
        {
	    	varReturn = xmlHttp.responseText;
			if(varReturn.indexOf("[-eval-]") != -1)
			{
				var evalCode = varReturn.split("[-eval-]");
				if(evalCode[1])
					eval(evalCode[1]);
			}
        }
    }
    
	xmlHttp.open(method, scriptServ, sincron);
	 if(method == "POST")
	    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	 if(method == "POST")
        xmlHttp.send(paramPost);
	 else
        xmlHttp.send(null);
//alert(varReturn);
  return varReturn;	 
}


/**
*Shows or hide a div or an array of divs
*
*@param divId  				div's id or an array of divs
*@param displayDiv			can be 1(display the div) or 0(hide the div)
*@param paramVisibility 	what style shud be modified.Can be 'sDisplay' for 'display style'
*									or 'sVisibility' for 'visibility style'
**/
function showHideDivs(divId, displayDiv, paramVisibility)
{
	if(divId.constructor == Array)
	{
		for(var i = 0; i <= divId.length; i++)
		{
			if(divId[i] != 'undefined' && document.getElementById(divId[i]))
			{
				if(displayDiv == 1)
				{
					if(paramVisibility == 'sDisplay')
						document.getElementById(divId[i]).style.display = 'block';
					else if(paramVisibility == 'sVisibility')
						document.getElementById(divId[i]).style.visibility = 'visible';
				}
				else if(displayDiv == 0)
				{
					if(paramVisibility == 'sDisplay')
						document.getElementById(divId[i]).style.display = 'none';
					else if(paramVisibility == 'sVisibility')
						document.getElementById(divId[i]).style.visibility = 'hidden';							
				}
			}
		}
	}
else if(divId.constructor == String)
	{
		if(divId != 'undefined' && document.getElementById(divId))
		{
			if(displayDiv == 1)
			{	
				if(paramVisibility == 'sDisplay')
					document.getElementById(divId).style.display = 'block';
				else if(paramVisibility == 'sVisibility')
				document.getElementById(divId).style.visibility = 'visible';
			}
			else if(displayDiv == 0)
			{
				if(paramVisibility == 'sDisplay')
					document.getElementById(divId).style.display = 'none';
			    else if(paramVisibility == 'sVisibility')
					document.getElementById(divId).style.visibility = 'hidden';
		    }
		}
  	}
}


/**
*Submit the page when triggerd
**/
function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/**
*Sterge toate optiunile dintrub select box
@param selectbox	numele select-boxului
**/
function removeAllOptions(selectbox){
	var i;
	for(i=selectbox.options.length-1;i>=0;i--){
		selectbox.remove(i);
	}
}


/**
*Adauga optiuni intr-un select box
@param selectbox	numele select-boxului
@param text			textul care il vede useru
@param value		valoarea
**/
function addOption(selectbox, text, value ){
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}