// Utils v1.0 by Dragos Dumitrescu
//select a specified row from a table by painting it with a specified color
var oldIdx;
function showSelection(idx,table,color)
{
 	//show selection
    if (oldIdx!=null)
    {
    row=table.rows[oldIdx+1];
    for(var i=0;i<row.cells.length;i++)
    row.cells[i].style.backgroundColor=oldColor;
    }
    
    row=table.rows[idx+1];
    oldIdx=idx;
    oldColor=row.cells[0].style.backgroundColor;
    for(var i=0;i<row.cells.length;i++)
    row.cells[i].style.backgroundColor=color;

}
function setLang(newLang)
{
	document.controls.lang.value=newLang;
   document.controls.submit();
}
//enables or disables all controls from a form  
function enableControls(myForm,enable)
{
	for (var i=0;i<myForm.length;i++)
    {
    	
      	if(myForm[i].type != 'hidden')
        	myForm[i].disabled=!enable;
    }
} 
//set to "" or 0 all controls from a form
function resetControls(myForm)
{
	for (var i=0;i<myForm.length;i++)
  {
	
  	if(myForm[i].type == 'hidden')
			continue;
 
	switch (myForm[i].type)
    {
    	case 'select-one':
        	//myForm[i].value=0;
        	break;
        case 'checkbox':
        	myForm[i].checked=false;
        	break;
        case 'text':
        	myForm[i].value='';
        	break;
        case 'textarea':
        	myForm[i].value='';
    }                       
  }  
} 
//move selected options from a source list to a target list
function moveSelOptions(sourceList,targetList)
{
	var n=sourceList.options.length;
	for (var i=0;i<n;i++)
  {
		if (sourceList.options[i].selected)
    {
    	targetList.appendChild(sourceList.options[i].cloneNode(true));
        sourceList.removeChild(sourceList.options[i]);
        n--;
        i--;
    }
  }
}
//finds and selects a option from a list by id
function findAndSelectOption(list,id)
{
	var n=list.options.length;
  for (var j=0;j<n;j++)
  {list.options[j].selected=false;}
	for (var j=0;j<n;j++)
  {
		if (list.options[j].value==id)
    {
    	try
    	{
		    list.options[j].selected=true;
	      }
        catch(e) {;}
      n--;
    }
  }
}
//gets the text of a option by having the id
function getTextById(list,id)
{
	var n=list.options.length;
  for (var i=0;i<n;i++)
    if (list.options[i].value == id)
    	return list.options[i].text;
}
//replace <br /> with \n 
function br2nl(myString)
{
	if (myString=="") return myString;
	s2=myString.replace("<br />","\n");
    while (s2!=myString)
    {
    	myString=s2;
        s2=myString.replace("<br />","\n");
    }
    return myString;
}
//return the index of an array element
function getIndex(myArray,myValue)
{
	for (i=0;i<myArray.length;i++)
  {
  	if (myArray[i]==myValue)
    	return i;
  }
  return -1;
}
//set the control with the name='sel'+fldArr 
//fldArr = array of field names
//valArr = array of values that will be applied to the controls

function setControls(fldArr,valArr,myForm)
{
   if(myForm == null) myForm=document.controls;
   
	for(var i=0;i<fldArr.length;i++)
	{
        
        ctrName=fldArr[i];
        controlType="null";
        try
			{controlType=myForm[ctrName].type;}
        catch(e)
            {continue;}
        switch (controlType)
        {
        	case 'checkbox':
            	try
                  {myForm[ctrName].checked=valArr[i]==1?true:false;}
                  catch(e)
                  {;}
            	break;
            case 'textarea':
            	try
                	{
                    	myForm[ctrName].value=br2nl(valArr[i]);
                        myForm[ctrName].value=fixDiacr(document.controls[ctrName].value);
                    }
                catch(e)
                {;}
                break;     

            default :
               try
                {
                	myForm[ctrName].value=valArr[i];
                    myForm[ctrName].value=fixDiacr(document.controls[ctrName].value);
					//try to set "title"
					myForm[ctrName].title=valArr[i];
					if(myForm[ctrName].alt=='checkboxes')
						field2chkboxes(ctrName,myForm);					
				}
                catch(e)
                {;}
        }
		
	}
	
}
function field2chkboxes(ctrName,myForm) //valoarea a mai multe checkboxuri se stocheaza binar intr-un intreg care apare hidden
{
	//var nrChk=myForm[ctrName].size;
	var nrChk=50;
	var val=myForm[ctrName].value;
	for(var i=0;i<nrChk;i++)
		{
		var chkName=ctrName+"_"+i;
		try
		{myForm[chkName].checked=val%2?true:false;}
		catch(e)
			{
				break;
			}
		val=parseInt(val/2);
		}
}
function chkboxes2fields(myForm)//caut cimpurile care au ALT=checkboxes 
{
	for(var i=0;i<myForm.length;i++)	
	{
		var alt="";
		try
		{
		alt=myForm[i].alt;
		//var chkName=myForm[i].name;
		//var nrChk=myForm[i].size;
		}
		catch(e){;}
		if(alt == "checkboxes")
		chkboxes2field(myForm[i].name,myForm);
	}
}
function chkboxes2field(ctrName,myForm) //valoarea a mai multe checkboxuri se stocheaza binar intr-un intreg care apare hidden
{
			var totVal=0;//valChk=1;
			var nrChk=10;//un nr maxim de chk
			for(var j=0;j<nrChk;j++)
			{
				var chkName=ctrName+"_"+j;
				try
				{
				if(myForm[chkName].checked)
					totVal=totVal+Math.pow(2,j);
				}
				catch(e){;}
				
			}
			myForm[ctrName].value=totVal;
}
function getFilesSize(fileArray)
{
	//file Array is an array of fileNames
	var sumSize=0;
    var file;
    var size;
    var oas;
    try
    	{oas = new ActiveXObject("Scripting.FileSystemObject");}
        catch(e)
        {
        	//alert('Pentru a verifica dimensiunea fisierelor trebuie ca acest site sa fie in lista "Trusted Sites" si sa acceptati rularea...');
            return 0;
        }
	for (i=0;i<fileArray.length;i++)
    {
    	if (fileArray[i] == "")
        	continue;
    	file=oas.getFile(fileArray[i]);
        size=file.size;
    	sumSize+=size;
    }
    return sumSize;
}
function checkImgSize(myCtr,maxSize,myImg)
{
	if(myCtr.value=='' )
        return -1;

	var ext=myCtr.value;
    
    ext=ext.substring(ext.length-4,ext.length)
    ext=ext.toUpperCase();
    
  	if(ext!='.JPG' && ext!='JPEG')
    {
    	alert('I cannot upload the picture. The only accepted format is JPEG/JPG');
        return 0;
    }
    //var maxSize=maxVal;//document.controls.MAX_FILE_SIZE.value*1;
	//var img=new Image();
	//img.src='file:///'+myCtr.value;
	
    //var size=img.fileSize;
	if(myImg.src!=myCtr.value)
		myImg.src=myCtr.value;
	var size=myImg.fileSize;
	size=size*1;
	maxSize=maxSize*1;
    //alert(size);alert(maxSize);alert(myCtr.value);
    if (size==-1)
    	{
        alert('I cannot calculate the size of the picture. Please make sure it is not greater than '+maxSize);
        return -1;
        }	
    if (size>maxSize)
    	{
    	alert('The maximum size allowed is:'+maxSize+' bytes. Your picture is:'+size+' bytes. Please select a smaller picture.');
        return 0;
    	}
    
    return -1;      
}

function showDiv(name)
{
	if (document.all[name].style.display=="block")
        document.all[name].style.display="none";	
    else
    	document.all[name].style.display="block";
}

