var form_submission_array = new Array();

var browser = "fire";
if ( navigator.appVersion.indexOf("MSIE") != -1 ) {
	browser = "ie"
}

function pushForm() {
	form_submission_array.push( document.forms[document.forms.length-1].id );
}

function popForm() {
	return form_submission_array.pop();
}
function getNextForm() {
	var formid = form_submission_array[form_submission_array.length-1];
	return document.forms[formid];
}

function generic_submitAllForms( dopop ) {
	if ( dopop ) {	
		popForm();
	}
	var form = getNextForm();	
	if ( form ) {
		// loop through the elements, and only submit if one of them has been modified.
		// the main form should be submitted first
		//if ( formIsModified( form ) ) {
			// a value is different
			var onOK = 'generic_submitAllForms( 1 )';
			var onError = 'submitFormError("'+form.id+'")';
			return submitForm( form, onOK, onError);
		//}				
	}
}

// used to get the last key, for after a submission (last insert id, basically).
var _lastSubmittedKey = null;
function submitForm( form, submitOK, submitFailure ) {
	if ( submitOK ) {
		form.elements['submitOK'].value = submitOK;
	}
	if ( submitFailure ) {
		form.elements['submitFailure'].value = submitFailure;
	}		
	form.submit();
}

function submitFormError( formid ) {
	var form = document.getElementById( formid );
	if ( form && form.parentElement ) {
		form.parentElement.style.background = '#F5E1B8';
	}
}

function getView(divID, component, id, view, parent, parentKey, values) {
	var thisDiv = document.getElementById( divID );
    if ( thisDiv ) {
		//thisDiv.innerHTML = "<div style='padding: 10px; background-color: white;'>Loading...</div>";
        //show( divID );
		var rand = Math.random();
        var action = 'index.php5?rr=' + rand + '&component=' + component + '&id=' + id + '&view=' + view
		if ( parent ) action += '&parent=' + parent;
		if ( parentKey ) action += '&parentKey=' + parentKey
		if ( values ) action +='&viewValues=' + values;
		var xmlhttp = getHTTPObject();
		xmlhttp.open("GET", action, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {                       
				//alert(xmlhttp.responseText);                              
				thisDiv.innerHTML = xmlhttp.responseText;
				//show( divID );
				doFooterCode();
			}
		}
		xmlhttp.send(null);
	}
	else {
		alert( 'Unknown Div: ' + divID );
	}
	//return false;
}

function show( divID ) {
	var thisDiv = document.getElementById( divID );
	if( thisDiv ) {
		thisDiv.style.visibility = 'visible'
	}
}

function getHTTPObject() {
  	var xmlhttp;
  
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}	
	return xmlhttp;
}

function displayConfirmation(component,id,view) {
	goView(component, id, view,'','','','');
}

function go( url ) {
	var rand = Math.random()
	window.location.href = url + '&xr=' + rand;
}
function goView( component, id, view, parent, parentKey, viewValues, crumb ) {
	var url  = 'index.php5?component='+component+'&id='+id+'&view='+view+'&parent='+parent;
	    url += '&parentKey='+parentKey+'&viewValues='+viewValues+'&crumb='+crumb;
	go( url );
}

