/**************************************************
 * Newill Consulting JS Library    *
 * William V. Newill (bill@newill.com             *
 **************************************************/

function newRow(newRowId, thisObj, newRowPrefix) {
	//check to see if a new Row Id was passed. If it wasn't, use findRowId() to find the numeric id of current row and increment by 1.
	if (newRowId.length==0) {
		//alert('No row ID passed');
		if (thisObj) {
			//alert('Going to have to find one.');
			var oldRowId=findRowId(thisObj, newRowPrefix);
			//alert('Old row was '+oldRowId);
			newRowId=Number(oldRowId)+1;
			//alert('New row will be '+newRowId);
		}
	}
	var table = document.getElementById(newRowPrefix+'_table');
	//alert('New Row ID: '+newRowId);
	// Check to see if the row already exists, and if it doesn't create it.
	if (document.getElementById(newRowPrefix+'_row_'+newRowId)) {
		//alert('The row exists. Do nothing.');
		return false;
	} else {
		//alert('Doesn\'t exist. Proceed!');
		// Object.dpDump(document.getElementById('Row_'+newRowId));
		var newRow = table.insertRow(newRowId+1);
		newRow.id=newRowPrefix+'_row_'+newRowId;
		//alert('Each row has '+document.getElementById('row_0').cells.length+' cells.');
		// loop over the number of cells in the 0-index sample row
		for (i=0; i<document.getElementById(newRowPrefix+'_row_0').cells.length; i++) {
			//alert('Cell: '+i);
			var cell;
			cell = newRow.insertCell(i);
			cell.innerHTML=document.getElementById(newRowPrefix+'_cell_'+i).innerHTML;
			cell.className=document.getElementById(newRowPrefix+'_cell_'+i).className;
			var cNodes=cell.childNodes;
			//if the cell has child nodes, loop over them and adjust the row numbers
			if (cNodes && cNodes.length) {
				//alert('Cell has '+cNodes.length+' children.');
				// If the cell is an input, textarea, select, checkbox, or radio, the add the row number to it's name
				for(j=0; j<cNodes.length; j++) {
					//alert(cNodes.item(j).type);
					if ((cNodes.item(j).type=="hidden" || cNodes.item(j).type=='text')  && cNodes.item(j).name==newRowPrefix+'_save_rows') {
						//alert('Adjust SAVE ROWS');
						//alert('Old value: '+cNodes.item(j).value);
						cNodes.item(j).value=newRowId;
						//alert('New value: '+cNodes.item(j).value);
					} else if (cNodes.item(j).type=='text' || cNodes.item(j).type=='textarea' || cNodes.item(j).type=='select-one' || cNodes.item(j).type=='checkbox' || cNodes.item(j).type=='hidden') {
						cNodes.item(j).name=cNodes.item(j).name+newRowId;
					}
				}
			}
		}
		document.getElementById(newRowPrefix+'_save_rows').value=newRowId;
	}
}


function findRowId(thisObj, newRowPrefix) {
	var rowId=thisObj.parentNode.id;
	var underscoreIndex=rowId.lastIndexOf('_');
	//alert(underscoreIndex);
	if (underscoreIndex == 4+newRowPrefix.length) {
		var num=rowId.substring(underscoreIndex+1);
		return num;
	} else {
		alert('Cannot find row. Something is not configured correctly. ('+underscoreIndex+' '+rowId+')');
	}
}


function moveAllOption(selectObj, destObj){
	while (selectObj.options.length) {
		destObj.options[destObj.options.length] = new Option(selectObj.options[0].text,selectObj.options[0].value);
		selectObj.options[0]=null;
	}
	DirtyFlg=1;
}


function moveOption(selectObj, destObj){
	if (selectObj.type == "select-one") {
		if(selectObj.selectedIndex >= 0) {
			//alert();
			destObj.options[destObj.options.length] = new Option(selectObj.options[selectObj.selectedIndex].text,selectObj.options[selectObj.selectedIndex].value);
			selectObj.options[selectObj.selectedIndex]=null;
			DirtyFlg=1;
		}
	} else {
		var selObjs = $A(selectObj.options).findAll(function(opt, index) { return opt.selected; });
		// FF lets you copy options directly, IE wants a new one
		selObjs.each(function(opt) { 
			var newOpt = new Option(opt.text, opt.value); 
			newOpt.selected=true; 
			destObj.options[destObj.options.length] = newOpt; 
		});
		for (var i=selectObj.options.length-1; i>=0; i-- )
			if (selectObj.options[i].selected)
				selectObj.options[i] = null;
		DirtyFlg=1;
	}
}


function updateSelectionList(selectObj, listObj){
	// selectObj is the form field that contains the selections you want put into a list
	// listObj is the form field that will contain the list of selections
	listObj.value='';
	for(i=0;i<selectObj.options.length;i++){
		if(i==0){
			listObj.value=selectObj.options[i].value;
		}else{
			listObj.value=listObj.value+','+selectObj.options[i].value;
		}
	}
}


function tab_hide(tabs, tabclass) {
	// hide all tabs
	for(i=1; i<=tabs; i++)
		{
			$('Tab'+i).className='hidden';
			$('tab_'+i).className=tabclass;
		}
}


function dirtyCheck() {
	if (dirtyFlg!=0) {
		if (!confirm('Are you sure you wish to continue? Your changes will not be saved.')) {
			return false;
		}
	}
	return true;
}
var dirtyFlg=0;


function background_color_save(obj) {
	background_color_store = obj.style.backgroundColor;
}

function background_color_restore(obj) {
	obj.style.backgroundColor = background_color_store;
}
var background_color_store = "";

function radio_button_setCheckedValue(radioObj, newValue) {
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
	if(!radioObj)  return; 
	var radioLength = radioObj.length; 
	if(radioLength == undefined) 
	{  
		radioObj.checked = (radioObj.value == newValue.toString());  
		return; 
	} 
	for(var i = 0; i < radioLength; i++) 
	{  
		radioObj[i].checked = false;  
		if(radioObj[i].value == newValue.toString()) { radioObj[i].checked = true; } 
	}
}

function checkall(formObj) {
	for(i=0; i<formObj.elements.length; i++)
	{
		if(formObj.elements[i].type=="checkbox")
		{
			formObj.elements[i].checked=true;
		}
	}
}

function uncheckall(formObj) {
	for(i=0; i<formObj.elements.length; i++)
	{
		if(formObj.elements[i].type=="checkbox")
		{
			formObj.elements[i].checked=false;
		}
	}
}

function getListFromSelection(selectObj){
	var result = '';
	return $A(selectObj.options).collect(
		function(elem) { return elem.value; }
	).join(',');
}

