// form.js

/**
 * submit the form
 * a message should be shown
 */
function submitWithMessage(fMsg,fName,command){
	if(!fMsg){
		fMsg = msgSave;
	}
	return submitForm(fName,command,fMsg);
}

/**
 * submit the form
 * set the right command
 */
function submitWithCommand(command,fName,fMsg){
	if(!command){
		fMsg = 'update';
	}
	return submitForm(fName,command,fMsg);
}

/**
 * Reload the filter or detailForm
 * Set the right command for reloading on changed data
 *
 */
function reloadForm(fName,command){
	var objForm = fName;

	if(!command){
		// preload the form
		// to set the right command
		objForm = tryForm(fName);
		if(!objForm){
			// we hebben geen command
			// en geen formuliernaam kunnen bepalen
			// dus zet op true
			// zodat de normale werking doorgaat
			return true;
		}

		if(typeof objForm.commandreload == 'object'){
			command = objForm.commandreload.value;
		}

		if(!command){
			command = 'detail';
			if(objForm.name.substring(0,10) != 'formDetail'){
				command = 'list';
			}
		}
	}

	return submitForm(objForm,command);

}

function submitForm(fName,command,fMsg){

	var oForm = getForm(fName);

	if(!oForm){
		saveStatus = 'noform';
		return true;
	}
	
	if(fMsg){
		$isOk = window.confirm(fMsg);
		if(!$isOk){
			saveStatus = 'nosave';
			return false;
		}
	}
//	debugShow('submit form',window.event.srcElement);
	if(command){
		oForm.command.value = command;
	}
	try{
		oForm.submit();
		saveStatus = 'done';
	}catch(ex){
		alert('Error on submit');
		saveStatus = 'error';
	}
	
	return false;	// send false back, so form is not submitted twice
}


/**
 * submit the form
 * set the right button
 */
function submitWithButton(fName,button,fMsg){
	if(!button){
		// er is geen aparte button naam meegestuurd
		// kijk of we deze kunnne achterhalen
		if(typeof fName == 'object'){
			button = fName.name;
		}
	}
	if(button){
		var oForm = getForm(fName);
	//alert(oForm.name);
		if(typeof oForm.buttonclicked != 'undefined'){
			oForm.buttonclicked.value = button;
		}
		if(!fMsg){
			// als we geen message hebben, dan niet moeilijk doen en gewoon submitten
			return oForm.submit();
		}
	}
	
	if(fMsg){
		return submitWithMessage(fMsg, fName);
	}
	return submitForm(fName);
}



