
/**
 * Confirm dialog. Returns true if accept, false otherwise
 * @param msg the message or question to ask to the user
 * @return
 */
function stdSiuleConfirm (msg) {
	
	if (confirm(msg)) {
		return true;
	}
	
	return false;
	
}

/**
 * Remove a message from the page
 * @param id the id of the element that contains the message to hide
 * @return
 */
function stdSiuleHideMessage (id) {
	
	var e = document.getElementById (id);
	if (e!=null) {		
		e.parentNode.removeChild(e);
	} 
	
}

/**
 * return a hash representation of the values of a form
 * @param fid the form to get the hash
 * @return
 */
function stdSiuleFormHash (fid) {
	
	var f = document.getElementById (fid);
	if (f==null) {
		return '';
	}
	
	var i;
	var v;
	
	var s = '';
	
	for (i=0; i < f.length; i++) {
		v = f.elements[i].value;
		s = s + v + ';';
	}
		
	
	return s;
}

/**
 * this variable is used for form hash functions
 */
var stdSiule_form_initial_hash='';

/**
 * this method is always executed after a form has been loaded
 * @param fid the form identificator name
 * @return nothing
 */
function stdSiuleFormOnLoad (fid) {
	
	stdSiule_form_initial_hash = stdSiuleFormHash (fid);
	
}

/**
 * this method is called after a cancel button press
 * @param fid the form identificator name
 * @param msg a message to show to the user when asking to confirm cancellation
 * @return true if cancellation is confirmed, false otherwise
 */
function stdSiuleFormOnCancel (fid, msg) {
	
	var form_current_hash = stdSiuleFormHash (fid);
	
	if (form_current_hash != stdSiule_form_initial_hash) {
		
		return stdSiuleConfirm (msg);
	
	} else {
		
		return true;
	}
}

/**
 * ------------------- Modal Window -----------------
 */
/**
 * open a modal window
 */
function stdSiuleOpenModal(id) {
	
	var el = document.getElementById(id);

	if (el==null) {
		return null;
	}

	if (el.style.display=='block') {
		return el;
	}
	
	var lx = sfStd_scrollLeft();
	var ly = sfStd_scrollTop();
	
	el.style.left = lx +'px';
	el.style.top = ly + 'px';		
	
	el.style.display = 'block';
	document.body.style.overflow = "hidden";

	return el;
}

/**
 * close a modal window
 * @param id
 * @return
 */
function stdSiuleCloseModal (id) {

	var el = document.getElementById(id);

	if (el==null) {
		return;
	}

	if (el.style.display=='none') {
		return;
	}
	
	el.style.display = 'none';
	document.body.style.overflow = "auto";
	
}

//scroll browser functions
function sfStd_clientWidth() {
	return sfStd_scrollValueFilter (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function sfStd_clientHeight() {
	return sfStd_scrollValueFilter (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function sfStd_scrollLeft() {
	return sfStd_scrollValueFilter (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function sfStd_scrollTop() {
	return sfStd_scrollValueFilter (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function sfStd_scrollValueFilter(win, doc, body) {
	var n = win ? win : 0;

	if (doc && (!n || (n > doc))) {
		n = doc;
	}
	
	if (body && (!n || (n > body))) {
		n = body;
	}
	
	return n;
}