var root = '/lerisorse';
function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for (z=0; z<theForm.length;z++){
		if (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
			theForm[z].checked = theElement.checked;
		}
	}
}

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'javascript:;\' onclick=\'removeElement('+  divIdName + ')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}



var upload_number = 2;
function addFileInput() {
 	var d = document.createElement("div");
 	var file = document.createElement("input");
 	file.setAttribute("type", "file");
 	file.setAttribute("name", "attachment"+upload_number);
 	d.appendChild(file);
 	document.getElementById("moreUploads").appendChild(d);
 	upload_number++;
}


function getElementLeft(Elem) {

		var elem;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	
}


function getElementTop(Elem) {

		if(document.getElementById) {	
			var elem = document.getElementById(Elem);
		} else if (document.all) {
			var elem = document.all[Elem];
		}
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;

}


function SetFocus()
{
document.getElementById('PostsComments').focus();
}

function update2divs(thePath) { 

new Ajax.Updater('PostsComments',thePath , 
{ method: 'post',
  insertion: Insertion.Bottom });
// if(document.getElementById("MessageSave")!=undefined) 
//document.getElementById("Go").focus();
//var Value = parseInt(document.getElementById("NumCom").innerHTML) ;
//document.getElementById("NumCom").innerHTML = document.getElementById("PostsComments").getElementsByTagName("h1").length ;
document.getElementById("NumCom").innerHTML ="";
//thisOne = document.getElementById("PostsComments");
//items = thisOne.getElementsByTagName("h1");

//alert(items.length);
//document.getElementById("NumCom").innerHTML = items.length ;
}


/**
 * Permette di spostarsi da un tab ad un altro
 * Si presuppone che siano dei div e che vengano usate le classi tab e act
 */
function changeTab(id_element, div_header) {
	//try {
        var sTabIdentifier = 'tab';
        var sTabSelectedIdentifier = 'act';
		var aDiv = document.getElementById(div_header).getElementsByTagName('div');

        oEreg = new RegExp(sTabSelectedIdentifier);
		for (var i=0, size = aDiv.length; i < size; i++) {
            sClassName = aDiv[i].className.toString();
            sId = aDiv[i].id;

            // Se è uno dei tab
            if (sClassName.indexOf(sTabIdentifier)!=-1) {
                var bodyElement = document.getElementById(sId + '_body');
                if (id_element==sId) {
                    aDiv[i].className = sClassName + ' ' + sTabSelectedIdentifier;
                    // Mostro il corpo
                    if (bodyElement) {
                        bodyElement.style.display = 'block';
                    }
                } 
                else if (sClassName.indexOf(sTabSelectedIdentifier)!=-1) {
                    aDiv[i].className = sClassName.replace(oEreg, '');
                    // Nascondo il corpo
                    if (bodyElement) {
                        bodyElement.style.display = 'none';
                    }
                }
            }
		}
	//} catch (ex) {
	//}
}


function MaxCaratteri(Object, MaxLen)

{

      return (Object.value.length <= MaxLen);

}



function addElementForm(TheForm)
{
//alert (TheForm);
//put into var
var FormElement = document.forms[TheForm];
//alert (FormElement.id);

var NewFileInput = document.createElement('input');
//NewFileInput.name = 'data[Files][Foto][1]' ;
NewFileInput.setAttribute("type","file");
NewFileInput.setAttribute("name",'data[Files][Foto][' +upload_number + ']')


FormElement.appendChild(NewFileInput);
upload_number++;
}

function removeElement(divNum) {

document.getElementById("myDiv").removeChild(divNum);

//var parentElement = document.getElementById('myDiv');

//document.write(parentElement.value);

//var childElement = document.getElementById(divNum);


//parentElement.removeChild(childElement);

}
