
/**
 * 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";
	
}

// font size functions
function stdSiuleIncreaseFontSize() {
	
   var p = document.getElementsByTagName('p');
   var i = 0;
   
   for (i=0; i < p.length; i++) {
	   
	   var e = p[i];
	   
	   var fs = stdSiuleGetStyleObject (e, 'font-size');
	   
	   if (fs=='') {
		   fs = '12px';
			
	   } else {
		   fs = fs.replace("px", "");
	   
		   var n = parseInt (fs);
	   
		   n++;
	   
		   fs = n + "px";
	   }
	   
	   e.style.fontSize = fs;
   }
}

function stdSiuleDecreaseFontSize() {
	
	   var p = document.getElementsByTagName('p');
	   var i = 0;
	   
	   for (i=0; i < p.length; i++) {
		   
		   var e = p[i];

		   var fs = stdSiuleGetStyleObject (e, 'font-size');
	   
		   if (fs=='') {
			   fs = '12px';
				
		   } else {
			   fs = fs.replace("px", "");
		   
			   var n = parseInt (fs);
		   
			   n--;
		   
			   fs = n + "px";
		   }
		   
		   e.style.fontSize = fs;
	   }
   
	}

// css functions

function stdSiuleGetStyle(name, styleProp)
{
			
	var x = document.getElementById(name);
	
	return stdSiuleGetStyleObject (x, styleProp);
}

function stdSiuleGetStyleObject (x, styleProp) {
	
	if (x==null) return;
	
	if (x.currentStyle)
		return x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		return document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	
}

//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;
}


// Java Server Faces Ajax support functions
function sfStd_AjaxSubmitForm(formName, linkId, handler, data) {
	
	var clearFn = 'clearFormHiddenParams_'+formName.replace(/-/g, '\$:').replace(/:/g,'_');
	if(typeof window[clearFn] =='function')
	{
		window[clearFn](formName);
	}
	
	var form = document.forms[formName];
			
	
	sfStd_AjaxOamSetHiddenInput(formName,formName +':'+'_idcl',linkId);
	
	if(form.onsubmit)
	{
		var result=form.onsubmit();
		if((typeof result=='undefined')||result)
		{
			try
			{
				var sa = new SiuleAjax(); 
				
				sa.onTimeOut = sfStd_AjaxOnTimeout;
				sa.onFailed = sfStd_AjaxOnFailed;
				
				sa.onComplete = handler;
				
				sa.doFormRequest (form, data);
				//form.submit();
			}
			catch(e){}
		}
		
	}
	else 
	{
		try
		{
			var sa = new SiuleAjax(); 
			
			sa.onTimeOut = sfStd_AjaxOnTimeout;
			sa.onFailed = sfStd_AjaxOnFailed;
			
			sa.onComplete = handler;			
			
			sa.doFormRequest (form, data);
			//form.submit();
		}
		catch(e){}
	}
			
	
	sfStd_AjaxOamClearHiddenInput(formName,formName +':'+'_idcl',linkId);
	return false;
}


function sfStd_AjaxOamSetHiddenInput(formname, name, value)
{
	var form = document.forms[formname];
	if(typeof form.elements[name]!='undefined' && form.elements[name].nodeName=='INPUT')
	{
		form.elements[name].value=value;
	}
	else
	{
		var newInput = document.createElement('input');
		newInput.setAttribute('type','hidden');
		newInput.setAttribute('id',name);
		newInput.setAttribute('name',name);
		newInput.setAttribute('value',value);
		form.appendChild(newInput);
	}
	
}


function sfStd_AjaxOamClearHiddenInput(formname, name, value)
{
	var form = document.forms[formname];
	var hInput = form.elements[name];
	if(typeof hInput !='undefined')
	{
		form.removeChild(hInput);
	}
	
}

function sfStd_AjaxOnTimeout (ajax, data) {
	alert ("Se perdió la conexión con el servidor");
}

function sfStd_AjaxOnFailed (ajax, data) {
	
	alert ("Error al recibir los datos");
}

function sfStd_AjaxReplaceContent (ajax, data) {
	// data is the id of the element which content 
	// will be replaced
	var e = document.getElementById (data);
	if (e==null) return;
	
	var t = ajax.getTextResponse();
	
	if (t==null) return;
	
	e.innerHTML = t;
}

