var max = 0;				// maximum # of attachments allowed
var currentUploads = 0;		// current # of attachment sections on the web page
var currentUploads2 = 0;	
var nameDesc = '';			// Name property for the Description Input field
var nameFile = '';			// Name property for the File Input field
var scrollPosVert = 0;		// stores the current scroll position on the form

// for some reason when a div is taken out, the form
// will scroll to the top on both Firefox and IE

// SCROLL FUNCTIONS
function saveScrollPos(offset){
	scrollPosVert=(document.all)?document.body.scrollTop:window.pageYOffset-offset;
}

function setScrollPos(){
	window.scrollTo(0, scrollPosVert);
	setTimeout('window.scrollTo(0, scrollPosVert)',1);
}


function addUploadLoc(fileFieldName, div, idn, where, val){
	nameFile=fileFieldName;
	currentUploads++;
	if (currentUploads>0)
		document.getElementById(idn).childNodes[0].data='Aggiungi locale';
	// First, clone the hidden attachment section
	var newFields = document.getElementById(div).cloneNode(true);
	newFields.id = '';
	// Make the new attachments section visible
	newFields.style.display = 'block'; 

	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++){
		if (newField[i].name==nameFile){
			newField[i].id=nameFile+currentUploads;
			newField[i].name=nameFile+'[]';
			//newField[i].name=nameFile+currentUploads;
		}
	}
	// Insert our new Attachment section into the Attachments Div
	// on the form...
	var insertHere = document.getElementById(where);
	insertHere.parentNode.insertBefore(newFields,insertHere);
	if (currentUploads > val) {
		newFields.style.display = 'none'; 
		currentUploads--;
		alert("Non puoi aggiungere piu' di 4 locali!!");
	}
}



// This function removes an attachment from the form
// and updates the ID and Name properties of all other
// Attachment sections 
function removeLoc(container, item){
	// get the ID number of the upload section to remove
	var tmp = item.getElementsByTagName('input')[0];
	var basefieldname = '';
	basefieldname = nameFile;
	var iRemove=Number(tmp.id.substring(basefieldname.length, tmp.id.length));
	// Shift all INPUT field IDs and NAMEs down by one (for fields with a
	// higher ID than the one being removed)
	var x = document.getElementById('attachments').getElementsByTagName('input');
	for (i=0;i<x.length;i++){
		basefieldname=nameFile;
		var iEdit = Number(x[i].id.substring(basefieldname.length, x[i].id.length));
		if (iEdit>iRemove){
			x[i].id=basefieldname+(iEdit-1);
			x[i].name=basefieldname+(iEdit-1);
		}
	}

	// Run through all the DropCap divs (the number to the right of the attachment
	// section) and update that number...
	x=document.getElementById('attachments').getElementsByTagName('div');
	for (i=0;i<x.length;i++){
		// Verify this is actually the "dropcap" div
		if (x[i].id.substring(0, String('dropcap').length)=='dropcap'){
			ID = Number(x[i].id.substring(String('dropcap').length, x[i].id.length));
			// check to see if current attachment had a higher ID than the one we're
			// removing (and thus needs to have its ID dropped)
			if (ID>iRemove){
				x[i].id='dropcap'+(ID-1);
				x[i].childNodes[0].data=(ID-1);
			}
		}
	}

	currentUploads--;
	saveScrollPos(0);
	container.removeChild(item);
	setScrollPos();
	document.getElementById(idn).style.visibility='visible';
	if (currentUploads==0)
		document.getElementById(idn).childNodes[0].data='Aggiungi locale';
}

function addUploadLoc2(fileFieldName, div, idn, where, val){
	nameFile=fileFieldName;
	currentUploads2++;
	if (currentUploads2>0)
		document.getElementById(idn).childNodes[0].data='Aggiungi locale';
	// First, clone the hidden attachment section
	var newFields = document.getElementById(div).cloneNode(true);
	newFields.id = '';
	// Make the new attachments section visible
	newFields.style.display = 'block'; 

	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++){
		if (newField[i].name==nameFile){
			newField[i].id=nameFile+currentUploads;
			newField[i].name=nameFile+'[]';
			//newField[i].name=nameFile+currentUploads;
		}
	}
	// Insert our new Attachment section into the Attachments Div
	// on the form...
	var insertHere = document.getElementById(where);
	insertHere.parentNode.insertBefore(newFields,insertHere);
	if (currentUploads2 > val) {
		newFields.style.display = 'none'; 
		currentUploads2--;
		alert("Non puoi aggiungere piu' di 4 locali!!");
	}
}



// This function removes an attachment from the form
// and updates the ID and Name properties of all other
// Attachment sections 
function removeLoc2(container, item){
	// get the ID number of the upload section to remove
	var tmp = item.getElementsByTagName('input')[0];
	var basefieldname = '';
	basefieldname = nameFile;
	var iRemove=Number(tmp.id.substring(basefieldname.length, tmp.id.length));
	// Shift all INPUT field IDs and NAMEs down by one (for fields with a
	// higher ID than the one being removed)
	var x = document.getElementById('attachments').getElementsByTagName('input');
	for (i=0;i<x.length;i++){
		basefieldname=nameFile;
		var iEdit = Number(x[i].id.substring(basefieldname.length, x[i].id.length));
		if (iEdit>iRemove){
			x[i].id=basefieldname+(iEdit-1);
			x[i].name=basefieldname+(iEdit-1);
		}
	}

	// Run through all the DropCap divs (the number to the right of the attachment
	// section) and update that number...
	x=document.getElementById('attachments').getElementsByTagName('div');
	for (i=0;i<x.length;i++){
		// Verify this is actually the "dropcap" div
		if (x[i].id.substring(0, String('dropcap').length)=='dropcap'){
			ID = Number(x[i].id.substring(String('dropcap').length, x[i].id.length));
			// check to see if current attachment had a higher ID than the one we're
			// removing (and thus needs to have its ID dropped)
			if (ID>iRemove){
				x[i].id='dropcap'+(ID-1);
				x[i].childNodes[0].data=(ID-1);
			}
		}
	}

	currentUploads2--;
	saveScrollPos(0);
	container.removeChild(item);
	setScrollPos();
	document.getElementById(idn).style.visibility='visible';
	if (currentUploads==0)
		document.getElementById(idn).childNodes[0].data='Aggiungi locale';
}