/**
 * @author edu
 */

 var msgGlobalWait = '';
 
/**
 * fa el submit d'un formulari
 * @param {Object} aForm
 */
function formSubmit( aForm ) {
	if (!verifyMandatoryFields(aForm)) {
		openAlert( str_MANDATORY_FIELDS );
	}
	else {
		el = document.getElementById(aForm);
		if (el != null) {
			el.submit();
		}
		else {
			els = document.getElementsByTagName('form');
			for (i = 0; i < els.length; i++) {
				if (els[i].name == aForm) 
					els[i].submit();
			}
		}
	}
}

/**
 * Envia un formulari d'un diāleg via POST-AJAX
 * @param {String} aForm Nom o ID del formulari
 * @param {String} anURL URL on fer el POST
 * @param {String} msg missatge que volem que aparegui amb la imatge de loading
 */
function formSubmitAjax( aForm, anURL, msg ) {
	if (!verifyMandatoryFields(aForm)) {
		openAlert( str_MANDATORY_FIELDS );
	}
	else {
		frm = document.getElementById(aForm);
		if (frm == null) {
			els = document.getElementsByTagName('form');
			for (i=0; i<els.length; i++) {
				if (els[i].name==aForm) {
					frm = els[i];
					break;
				}
			}
		}
	 
	    if (frm != null) {
			if ((typeof(anURL) == 'undefined') || (anURL == '')) {
				anURL = frm.action;
			}
			YAHOO.util.Connect.setForm(frm, true);
			var callback = {
				success: function(o) {
					if (o.responseText.indexOf('<!-- RELOAD -->') > -1) {
						var defaultReload = true;
					    
						if(typeof(reloadIntercept)!='undefined') {
							if(reloadIntercept!=null) {
								defaultReload = false;
								//reloadIntercept();
								eval(reloadIntercept);
								closeDialog();
							}
						}
						if(defaultReload) window.location.reload(true);
					}
					else {
						if (o.responseText.indexOf('<!-- AUTOHIDE -->') > -1) {
							closeDialog();
						} else {
							if (o.responseText.indexOf('<!-- TIMEDAUTOHIDE -->') > -1) {
								window.setTimeout('closeDialog()',3000);
							}
							document.getElementById('divJSDialogContent').innerHTML = o.responseText;
						}
					}
				},
				upload: function(o) {
					this.success(o);
				}
			}
			var cObj = YAHOO.util.Connect.asyncRequest('POST', anURL, callback );
            
			if(typeof(msg) == 'undefined'){
				msgGlobalWait = '';
			}else{
                msgGlobalWait= msg;
            }
			
            //Si es fa directe en Firefox, no funciona, elimina el contingut del div, abans de que arribi a executar el connect, degut a que es fa asincronament
			setTimeout("waitAjaxResponse()",100);
		}
	}
}

function waitAjaxResponse(){
    $('#divJSDialogContent').html('<img src="./templates/noe/img/loading.gif" title="loading"></img>'+msgGlobalWait);
}

/**
 * Comproba el camp obligatori o no
 * @param {Object} element
 */
function checkMandatory( element ) {
	if( element.value=='' ) {
		//cal omplir el camp (es marca)
		element.className = "InputTextMandatory";
	} else {
		//Ja hi ha algun valor
		if(element.className!="InputText") element.className = "InputText";
	}
}

function checkMandatorySelect(element) {
	if( element.options[element.selectedIndex].value=='' ) {
		//cal omplir el camp (es marca)
		element.className = "InputTextMandatory";
	} else {
		//Ja hi ha algun valor
		if(element.className!="InputText") element.className = "InputText";
	}
}

/**
 * Fa una comprobaciķ inicial dels camps obligatoris
 */
function checkMandatoryStartup() {
	els = document.getElementsByTagName('input');
	for(i=0;i<els.length;i++) {
		if(els[i].className.indexOf("InputTextMandatory") > -1) {
			checkMandatory(els[i]);
			els[i].onkeyup = function() { checkMandatory(this); };
		}
	}
	
	els = document.getElementsByTagName('textarea');
	for(i=0;i<els.length;i++) {
		if(els[i].className.indexOf("InputTextMandatory") > -1) {
			checkMandatory(els[i]);
			els[i].onkeyup = function() { checkMandatory(this); };
		}
	}
	
	els = document.getElementsByTagName('select');
	for(i=0;i<els.length;i++) {
		if(els[i].className.indexOf("InputTextMandatory") > -1) {
			checkMandatorySelect(els[i]);
			els[i].onchange = function() { checkMandatorySelect(this); };
		}
	}
}

/**
 * Verifica els camps marcats com a obligatoris
 */
function verifyMandatoryField(field) {
	return field.value!='';	
}

function verifyMandatoryFieldSelect(field) {
	return field.options[field.selectedIndex].value!='';	
}

function verifyMandatoryFields(form) {
	if (form=='') {
		frm = document;		
	} else {
		frm = document.getElementById(form);
		if(frm==null) {
			els = document.getElementsByTagName('form');
			for (i=0; i<els.length; i++) {
				if (els[i].name == form) {
					frm = els[i];
					break;
				}
			}
		}
	}
	
	els = frm.getElementsByTagName('input');
	for(i=0;i<els.length;i++) {
		if(els[i].className.indexOf("InputTextMandatory") > -1) {
			if( !verifyMandatoryField(els[i]) ) return false;
		}
	}
	
	els = frm.getElementsByTagName('textarea');
	for(i=0;i<els.length;i++) {
		if(els[i].className.indexOf("InputTextMandatory") > -1) {
			if( !verifyMandatoryField(els[i]) ) return false;
		}
	}
	
	els = frm.getElementsByTagName('select');
	for(i=0;i<els.length;i++) {
		if(els[i].className.indexOf("InputTextMandatory") > -1) {
			if( !verifyMandatoryFieldSelects(els[i]) ) return false;
		}
	}
	
	return true;
}

/* No va eliminar
var loader = new YAHOO.util.YUILoader();
loader.insert({
    onSuccess: function(loader) {
            YAHOO.util.Event.addListener(YAHOO.util.Dom.get("loglink"), "click", function(e) {
                YAHOO.util.Event.stopEvent(e);
                YAHOO.log("This is a simple log message.");
            });

            // Put a LogReader on your page
            this.myLogReader = new YAHOO.widget.LogReader();
    }
});
*/
