var releaseIds=new Array();
var techIds=new Array();

function appendReleaseIds(el, appendStr) {
	allEls=el.getElementsByTagName('*');
	if ((typeof allEls == "undefined") || (allEls==null)) {
		alert("No elements found in appendReleaseIds for "+el.id);
		return false;
	}
	// Process the children
	for (i=0; i<allEls.length; i++) {
		if ( (typeof allEls[i].id != "undefined") && (allEls[i].id != null) && (allEls[i].id!="") ) {
			allEls[i].id=allEls[i].id.toString()+appendStr;
		}
		if ( (typeof allEls[i].htmlFor != "undefined") && (allEls[i].htmlFor != null) && (allEls[i].htmlFor!="") ) {
			allEls[i].htmlFor=allEls[i].htmlFor.toString()+appendStr;
		}
		if ( (typeof allEls[i].name != "undefined") && (allEls[i].name != null) && (allEls[i].name!="") ) {
			allEls[i].name=allEls[i].name.toString()+appendStr;
		}
	}
	
	// Process the node itself
	if ( (typeof el.id != "undefined") && (el.id != null) && (el.id!="") ) {
		el.id=el.id.toString()+appendStr;
	}
}

function getReleaseAryPos(releaseId) {
	releaseAryPos=0;
	found=false;
	while (releaseAryPos<releaseIds.length && !found) {
		if (releaseIds[releaseAryPos]==releaseId) {
			return releaseAryPos;
		}
		releaseAryPos++;
	}
}

function aryRemove(ary, pos) {
	if (pos==0) {
		ary.shift(); // Delete item from front
		return ary;
	} else if (pos==ary.length-1) {
		ary.pop(); // Delete item from end
		return ary;
	} else {
		aryStart=ary.slice(0, pos);
		aryEnd=ary.slice(pos+1);
		return aryStart.concat(aryEnd);
	}
}

function addRelease() {
	if(document.getElementById('source_release_info')) {
		if (releaseIds.length==0) {
			releaseIds[0]=0;
		} else {
			releaseIds[releaseIds.length]=releaseIds[releaseIds.length-1]+1;
		}
		
		newId=releaseIds[releaseIds.length-1];
		
		techIds[techIds.length]=new Array();
		
		newNode=document.getElementById('source_release_info').cloneNode(true);
		appendReleaseIds(newNode, "_"+newId);
		
		document.getElementById('releaseOutput').appendChild(newNode);
		
		removeBtn=document.getElementById("format_remove_"+newId);
		removeBtn.parentRelease=document.getElementById("source_release_info_"+newId);
		removeBtn.onclick=function() {
			removeRelease(this.parentRelease);
			return false;
		}
		
		addTechInfoBtn=document.getElementById("add_tech_info_"+newId);
		addTechInfoBtn.releaseId=newId;
		addTechInfoBtn.onclick=function() {
			addTechInfo(this.releaseId);
			return false;
		}
		
		document.getElementById("hiddenReleaseIds").value=releaseIds.join(",");
	
		addTechInfo(newId);
	}
}

function addTechInfo(releaseId) {
	i=0;
	found=false;
	
	releaseAryPos=getReleaseAryPos(releaseId);
		
	thisTechIdSet=techIds[releaseAryPos]
	if (thisTechIdSet.length==0) {
		thisTechIdSet[0]=0;
	} else {
		
		thisTechIdSet[thisTechIdSet.length]=thisTechIdSet[thisTechIdSet.length-1]+1;
	}
	newTechId=thisTechIdSet[thisTechIdSet.length-1];
	
	newTechNode=document.getElementById('source_tech_info').cloneNode(true);
	
	appendReleaseIds(newTechNode, "_"+releaseId+"_"+newTechId);
	
	document.getElementById('tech_info_container_'+releaseId).appendChild(newTechNode);
	
	updateHiddenTechIds();
	
	removeTechBtn=document.getElementById("tech_remove_"+releaseId+"_"+newTechId);
	removeTechBtn.parentTechInfo=document.getElementById('source_tech_info_'+releaseId+"_"+newTechId);
	removeTechBtn.onclick=function() {
		removeTech(this.parentTechInfo);
		return false;
	}
}

function updateHiddenTechIds() {
	techIdArrays=new Array();
	for (i=0; i<techIds.length; i++) {
		techIdArrays[i]=techIds[i].join(",");
	}
	document.getElementById("hiddenTechIds").value=techIdArrays.join(";");
}

function removeRelease(elementRef) {
	if (releaseIds.length==1) {
		alert("You must have at least one Format.");
		return;
	}
	
	releaseId=parseInt(elementRef.id.toString().substring(elementRef.id.toString().lastIndexOf("_")+1));
	
	releaseAryPos=getReleaseAryPos(releaseId);
	releaseIds=aryRemove(releaseIds, releaseAryPos);
	techIds=aryRemove(techIds, releaseAryPos);
	
	elementRef.parentNode.removeChild(elementRef);
	
	document.getElementById("hiddenReleaseIds").value=releaseIds.join(",");
	updateHiddenTechIds();
}

function removeTech(elementRef) {
	erId=elementRef.id.toString()
	releaseSection=erId.substring(0, erId.lastIndexOf("_"));
	releaseId=parseInt(releaseSection.substring(releaseSection.lastIndexOf("_")+1));
	techId=parseInt(erId.substring(erId.lastIndexOf("_")+1));
	
	// Find the position of the releaseId in the releaseIds array
	releaseAryPos=getReleaseAryPos(releaseId);
	
	// Using that, find the position of the techId in the techIds array at that position.
	techAryPos=0;
	found=false;
	while (techAryPos<techIds[releaseAryPos].length && !found) {
		if (techIds[releaseAryPos][techAryPos]==techId) {
			found=true;
		}
		if (!found) techAryPos++;
	}
	
	if (techIds[releaseAryPos].length==1) {
		alert("You must have at least one Audio Format.");
		return;
	}
	
	techIds[releaseAryPos]=aryRemove(techIds[releaseAryPos], techAryPos);
	
	elementRef.parentNode.removeChild(elementRef);
	
	updateHiddenTechIds();
}
	
function initAddRelease() {
		if (document.getElementById("add_format_button")) {
			document.getElementById("add_format_button").onclick=function() {
			addRelease();
			return false;
		}
	}
}

//Register Content Form
	function validateContent(){
		var contentForm = document.forms.registerContent
		errorFields=Array();
		
		if(""==contentForm.title.value){
			document.getElementById('title').className="required";
			errorFields[errorFields.length]="Title";
		} else {
			document.getElementById('title').className="";
		}
		
		if(""==contentForm.provider.value){
			document.getElementById('provider').className="required";
			errorFields[errorFields.length]="Provider";
		} else {
			document.getElementById('provider').className="";
		}
		for (i=0; i<releaseIds.length; i++) {
			if(""==document.getElementById('format_'+releaseIds[i]).value){
				document.getElementById('format_label_'+releaseIds[i]).className="required";
				errorFields[errorFields.length]="Format";
			} else {
				document.getElementById('format_label_'+releaseIds[i]).className="";
			}
			for(j=0;j<techIds[i].length;j++) {
				if(""==document.getElementById('audioformat_'+releaseIds[i]+"_"+techIds[i][j]).value){
					document.getElementById('audioformat_label_'+releaseIds[i]+"_"+techIds[i][j]).className="required";
					errorFields[errorFields.length]="Audio Format";
				} else {
					document.getElementById('audioformat_label_'+releaseIds[i]+"_"+techIds[i][j]).className="";
				}
			}
		}
		
		if (errorFields.length==0) return true;
		alert("Please make sure you fill in the following fields: "+errorFields.join(", "));
		return false;
	}
	