/*
HARDCODED items....
oForm gets set to the form with the ID = 'vuForm'
Rows get set to ID = 'ROWID<tableID>'

*/

var arrDirty = new Array();
var arrFields = new Array();
var arrIDs = new Array();
var bTest=true;
var bDebug=false;
var w,oForm,oNewForm;

//TMK 5/18/2005 - check if we wrote out a javascript variable to set the debug value
if (typeof vudebug != "undefined") {bDebug = vudebug} 

var bFirst=false;

//muCheckDirty(field) - CheckDirty function.  This function builds an array of the fields that are dirty. so we can put them in the popup.
function muCheckDirty(field) {
//	alert(arrFields.length +'\n'+ field.name);
	if(arrDirty[field.name]==null){
		arrFields.push(field);
		arrDirty[field.name]=arrFields.length-1;
	}
} 

//muBuildForm - Build Form function.  This function creates the hidden window, builds the form and submits it.
function muBuildForm() {
var s='';
var t='';
var i,j,temp,r,oTable,oRow,p,iID,a; 
var arrIDAdded = new Array();
//Create new window and new form.
	muSetupUpdate();
	muAddHiddenFields(false);
	for(i=0;i<arrFields.length;i++){
		o=arrFields[i];
		t=o.name;
		iID=t.substring(2,t.indexOf('_'));
		//if its a delete we need some extra processing
		if((t.substring(0,2)=='ID')&&(t.substring(t.length-27,t.length)=='_vu_mega_delete_this_record')){
			if (o.checked){
				muAddFormField(o.name,o.value,'textarea');
				r=o;
				while(r.nodeName!='TR'){r=r.parentNode;}//get the row that contains this element
				r.id='ROWID'+ iID;//set rows ID to be like... ROWID<recordsTableID>
			}
		}
		else {
			muAddFormField(o.name,o.value,'textarea');
		}
		//see if we've added the ID field yet...
		if(arrIDs[iID]==null){
			muAddFormField('ID'+ iID +'_ID',iID);
			arrIDs.push(iID);
		}
	}
	muAddFormField('vu_last_field_on_form',document.getElementById('vu_last_field_on_form').value);

	oNewForm.submit();
	arrIDs = new Array();
	arrIDAdded = null;
//}
}

function muDelete(iID) {
	if (confirm('Are you sure you want to delete?')) {
		var o;
		o=document.getElementById('ID'+ iID +'_vu_mega_delete_this_record');  //muCheckDirty(this)' id=ID" & intRecordID.ToString & "_vu_mega_delete_this_record
		o.checked=true;
		muCheckDirty(o);
		muBuildForm();
	} 
}

function muAddCopy(field) {
var oRow,iID; 
	muSetupUpdate('Please wait while the record is being added....<br><br>This window will close automatically.<br>');
	muAddHiddenFields(false);
	oNewForm.ownerDocument.getElementById('vu_nextstep').value='addcopy';
	iID=field.id.substring(2,field.id.indexOf('_'))
	muAddFormField('ADDCOPYID',iID);
	oRow=field;
	while(oRow.nodeName!='TR'){oRow=oRow.parentNode;}//get the row that contains this element
	oRow.id='ROWID'+ iID;
	oNewForm.submit();
}

function muAlert(s) {
alert(s);
}

function muClearDirtyFlags(w) {
// NOTE that this function gets called from the popup window... so we have to pass in a reference to the window
//   to make it easier on ourselves.
	w.arrDirty = new Array();
	w.arrFields = new Array();
}

function muCopyRow(iOldID, iNewID) {
var oRow,oTable,oNewRow,regexp;
var o,a,t,s; 
	oRow=document.getElementById('ROWID'+ iOldID);
	oTable=document.getElementById('vu_report');//due to a goofy way IE impliments parentNode we have to do it this way.
	//loop through and change the fields we need to change.
	oNewRow=oRow.cloneNode(true);
//	oRow.insertAdjacentElement('afterEnd',oNewRow);
//	o=oTable.insertRow(oRow.rowIndex);
//	o.replaceNode(oNewRow);
	
	oRow.parentNode.insertBefore(oNewRow,oRow);
	a=oRow.getElementsByTagName('*');
	for(i=0;i<a.length;i++){
		o=a[i];
		t=o.id;
		//rename all fields that start with ID to reflect our new ID
		if(t.substring(0,2)=='ID'){
			o.id='ID'+ iNewID + t.substring(t.indexOf('_'),t.length);
			o.name='ID'+ iNewID + t.substring(t.indexOf('_'),t.length);
			//change the ID<tableID>_ID field's value to be our new ID
			if(t=='ID'+ iOldID +'_ID'){o.value=iNewID};
		}
		//fix links
		if(o.nodeName=='A'){o.href=o.href.replace(iOldID,iNewID);};
		//fix non-editable fields like count and ID...
		switch(o.getAttribute('headers')){
			case 'Count':
				o.innerHTML='&nbsp;';
				break;
			case 'ID':
				o.innerHTML=iNewID;
				break;
	}}
if (bDebug) {alert('Close')};	
w.close();
w=null;
oForm=null;
oNewForm=null;
}

function muLabelAll() {
var a,i,o;
	a=document.getElementsByTagName('*');
	for(i=0;i<a.length;i++){o=a[i];if(bTest){o.title=o.name;}else{o.title=o.id;};}
	bTest=!bTest;
}

function muDebugDocument(field) {
var a,i,o,s;
	s='';
	o=field;
	while(o.nodeName!='#document'){o=o.parentNode;}//get the row that contains this element
	a=o.getElementsByTagName('*');
	for(i=0;i<a.length;i++){s+=muDebugString(a[i])}
	return s;
}

function muDebugChildren(field,sTag) {
var a,i,o,s;
	s='';
	if(sTag==null){sTag='*';};
	o=field;
	a=o.getElementsByTagName(sTag);
	for(i=0;i<a.length;i++){s+=muDebugString(a[i])}
	return s;
}

function muDebugDocumentHidden(field) {
var a,i,o,s;
	s='';
	o=field;
	while(o.nodeName!='#document'){o=o.parentNode;}//get the row that contains this element
	a=o.getElementsByTagName('*');
	for(i=0;i<a.length;i++){if(a[i].type=='hidden'){s+=muDebugString(a[i])}}
	return s;
}

function muDebugRow(field) {
var oRow,a,i,s;
	s='';
	oRow=field;
	while(oRow.nodeName!='TR'){oRow=oRow.parentNode;}//get the row that contains this element
	a=oRow.getElementsByTagName('*');
	for(i=0;i<a.length;i++){s+=muDebugString(a[i])}
	return s;
}

function muDebugString(field) {
var a,i,o,s;
var bDisplay=false;
	s='';
	if((field.id!='')&&(field.id!=null)){bDisplay=true;}
	if((field.name!='')&&(field.name!=null)){bDisplay=true;}
	if((field.nodeName=='TD')||(field.nodeName=='SMALL')||(field.nodeName=='FONT')||(field.nodeName=='TR')||(field.nodeName=='TABLE')){bDisplay=false;}
	if(field.id.substring(0,2)=='ID'){bDisplay=true;}
	if(bDisplay){
		s+='Type['+ field.nodeName +']';
		s+='ID['+ field.id +']';
		if(field.value!=null){s+='Value['+ field.value +']';}
		if(field.id!=field.name){s+='Name['+ field.name +']';}
		if((field.type!='')&&(field.type!=null)){s+='AttributeType['+ field.type +']';}
		if(field.nodeName=='A'){s+='Href['+ field.href +']';}
		if(field.nodeName=='FORM'){s+='Action['+ field.action +']';}
		s+='\n';
	}
	return s;
}

function muAddHiddenFields(bShouldAddIDs) {
	muAddFormField('vu_first_field_on_form',oForm.vu_first_field_on_form.value);
	muAddFormField('vu_nextstep',oForm.vu_nextstep.value);
	muAddFormField('vu_microupdate','y');
}

function muAddHiddenFields_Old(bShouldAddIDs) {
	var i,o,oNew,sName,bIsAnIDField;
	if(bShouldAddIDs==null){bShouldAddIDs=true};
	for(i=0;i<oForm.length;i++){
		o=oForm.elements[i];
		sName=o.name;
		//get the hidden fields but only the ones we want to put on the form...
		if((o.type=='hidden')&&(sName!='vu_last_field_on_form')){
			//are the ID's required for megaupdate code?
			//implement later... check if we should be adding IDs... add Id's by default ?... 
			if((sName.substring(0,2)=='ID')&&(sName.substring(sName.indexOf('_'),sName.length)=='_ID')){bIsAnIDField=true;}
			else{bIsAnIDField=false;};
			if((bShouldAddIDs)||((!bShouldAddIDs)&&(!bIsAnIDField))){
				oNew=oNewForm.ownerDocument.createElement('input');
				oNew.type='text';
				oNew.name=o.name;
				oNew.id=o.id;
				oNew.value=o.value;
				oNew.title=oNew.name;
				oNewForm.appendChild(oNew);
			}
		}
	}
	muAddFormField('vu_microupdate','y');
}

function muAddFormField(sName,sValue,sType) {
	var oNew;
	if(sType==null){oNew=oNewForm.ownerDocument.createElement('input')}
	else{oNew=oNewForm.ownerDocument.createElement(sType)};
	oNew.name=sName;
	oNew.id=sName;
	oNew.title=sName;
	switch(sType){
		case 'textarea' :
			oNew.innerHTML=sValue;
			break;
		default :
			oNew.type='text';
			oNew.value=sValue;
	}
	oNewForm.appendChild(oNew);
}

function muSetupUpdate(sMessage) {
	var sDivAttribute = '';
	w=window.open('','UpdateWindow_'+ String(Math.random()).substring(3),'width=250,height=150,scrollbars');
	oForm = document.getElementById('vuform');
	if(!bDebug){sDivAttribute=' style="visibility: hidden;"'};
	if(sMessage==null){sMessage='Please wait while your fields are being updated....<br><br>This window will close automatically.<br>';};
	//TMK 5/18/2005 - add var bDebug to update window
	w.document.write('<HTML><head><title>Update Window</title><script>var bDebug = '+bDebug+'</script></head><body>'+ sMessage +'<div'+ sDivAttribute +'><form id="form1"></form></div></body></HTML>');
	oNewForm=w.document.getElementById('form1');
	oNewForm.action='..\/..\/wsmNET_bin\/vu1\/GIPN.aspx?gipn=vu_hidden_page';
	//oNewForm.action='..\/..\/wsmnet_bin\/wsm_common_routines\/wsm_hidden_page.aspx';
	//TMK 5/18/2005 - test bDebug functionality
	if(bDebug){alert(oNewForm.action+'')};
	oNewForm.method=oForm.method;
	if(bDebug){w.resizeTo(600,800)};
}

