// JavaScript Document
/** INI :: Funciones para el manejo de Cookies **/
function JS_createCookie(sName, mValue, iExpires, sPath){var sExpires = "";if (iExpires){var oDate = new Date();oDate.setTime(oDate.getTime() + iExpires);sExpires = "; expires=" + oDate.toGMTString();}document.cookie = sName + "=" + mValue + sExpires + "; path=/" + (sPath ? sPath + '/' : '');};
/** END :: Funciones para el manejo de Cookies **/


/** INI :: Funciones validación cliente **/
function JS_isset(v){return ((typeof(v)=='undefined' || v.length==0) ? false : true);}
function JS_inArray(mValue, aDatos){ for(var iIndex in aDatos) if (aDatos[iIndex] == mValue) return true;}
/** END :: Funciones validación cliente **/

/** INI :: Funciones para formatear elementos **/
function JS_formatAsMoney(iNumero){if (iNumero == 0) return 0.00; iNumero -= 0; iNumero = (Math.round(iNumero * 100)) / 100; return (iNumero == Math.floor(iNumero)) ? iNumero + '.00' : ( (iNumero * 10 == Math.floor(iNumero * 10)) ? iNumero + '0' : iNumero);}
function JS_formatUserDate(oDate){var iMonth = oDate.getMonth()+1;var iDay = oDate.getDate();return (iDay<10 ? '0' : '') + iDay + '/' + (iMonth<10 ? '0' : '') + iMonth  + '/' + oDate.getFullYear();}
/** END :: Funciones para formatear elementos **/



/**
 * function JS_hacerSublista()
 * @param {Object} selParent => ID del combo padre
 * @param {Object} selChild => ID del combo hijo
 * @param {Object} isSubselectOptional => si es TRUE: muestra un "-- seleccione --" en el combo hijo
 * 													si es FALSE: muestra directamente el primer elemento del combo hijo
 * @param {Object} selChildVal => Valor del elemento seleccionado por defecto
 * @param {Object} sLabelChild => Label para el combo hijo, en primera instancia
 */
function JS_hacerSublista(selParent, selChild, isSubselectOptional, selChildVal, sLabelChild)
{
	$('#' + selParent + selChild).remove();
	$("body").append('<select style="display:none;" id="' + selParent + selChild + '"></select>');
	$('#' + selParent + selChild).html($("#" + selChild + " option"));
	var selParentValue = $('#' + selParent).attr('value');
	$('#'+selChild).html($("#" + selParent + selChild + " .sub_" + selParentValue).clone());
	selChildVal = (typeof selChildVal == "undefined") ? "" : selChildVal;
	$("#"+selChild+' option[@value="'+ selChildVal +'"]').attr('selected', 'selected');
	if (typeof sLabelChild != "undefined")
	{
		$('#' + selChild).prepend("<option value='0'>" + sLabelChild + "</option>");
	}
	$('#'+selParent).change(function()
	{
		var selParentValue = $('#'+selParent).attr('value');
		$('#'+selChild).html($("#"+selParent+selChild+" .sub_"+selParentValue).clone());
		if (isSubselectOptional)
		{
			$('#' + selChild).prepend("<option value=''> -- Seleccione -- </option>");
			$("#" + selChild + ' option[@value="' + selChildVal + '"]').attr('selected', 'selected');
			if (selChildVal != '0')	$('#' + selChild).trigger("change");
		}
	 	//$('#' + selChild).trigger("change");
		$('#' + selChild).focus();
		return false;
	});
}


/* INI  Funciones controladoras de ventanas */
function JS_showSelectBoxes(){var selects = document.getElementsByTagName("select");for (i = 0; i != selects.length; i++){selects[i].style.visibility = "visible";}};
function JS_hideSelectBoxes(){var selects = document.getElementsByTagName("select");for (i = 0; i != selects.length; i++) {selects[i].style.visibility = "hidden";}};
function JS_showFlash(){var flashObjects = document.getElementsByTagName("object");for (i = 0; i < flashObjects.length; i++) {flashObjects[i].style.visibility = "visible";}var flashEmbeds = document.getElementsByTagName("embed");for (i = 0; i < flashEmbeds.length; i++) {flashEmbeds[i].style.visibility = "visible";}};
function JS_hideFlash(){var flashObjects = document.getElementsByTagName("object");for (i = 0; i < flashObjects.length; i++) {flashObjects[i].style.visibility = "hidden";}var flashEmbeds = document.getElementsByTagName("embed");for (i = 0; i < flashEmbeds.length; i++) {flashEmbeds[i].style.visibility = "hidden";}};
function JS_getPageScroll(){var yScroll;if (self.pageYOffset){yScroll = self.pageYOffset;} else if (document.documentElement && document.documentElement.scrollTop){yScroll = document.documentElement.scrollTop;}else if (document.body){yScroll = document.body.scrollTop;}arrayPageScroll = new Array('',yScroll);return arrayPageScroll;};
//Retorna un array con el ancho y alto de la ventana
function JS_getPageSize(){var xScroll, yScroll;if (window.innerHeight && window.scrollMaxY) {xScroll = document.body.scrollWidth;yScroll = window.innerHeight + window.scrollMaxY;} else if (document.body.scrollHeight > document.body.offsetHeight){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;}var windowWidth, windowHeight;if (self.innerHeight) {windowWidth = self.innerWidth;windowHeight = self.innerHeight;} else if (document.documentElement && document.documentElement.clientHeight) {windowWidth = document.documentElement.clientWidth;windowHeight = document.documentElement.clientHeight;} else if (document.body) {windowWidth = document.body.clientWidth;windowHeight = document.body.clientHeight;}if(yScroll < windowHeight){pageHeight = windowHeight;} else {pageHeight = yScroll;}if(xScroll < windowWidth){pageWidth = windowWidth;} else {pageWidth = xScroll;}arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); return arrayPageSize;};
// Retorna un array con los valores x,y del scroll de la pagina
function JS_getPageScroll(){var yScroll;if (self.pageYOffset){yScroll = self.pageYOffset;} else if (document.documentElement && document.documentElement.scrollTop){yScroll = document.documentElement.scrollTop;} else if (document.body){yScroll = document.body.scrollTop;}arrayPageScroll = new Array('',yScroll);return arrayPageScroll;};


function JS_loadingShow(sIdContenido, sText)
{
	//ocultamos el contenido mientras mostramos el mensaje
	sImg = "<div id='loading' style='padding:120px 20px; text-align:center;'><p style='font-weight:bolder; margin:0px;'>" + sText + "</p><img src='share/img/ajax-loader.gif' /></div>";
	$("#"+sIdContenido).hide("slow").after(sImg);
}

function JS_loadingHide(sIdContenido)
{
	//ocultamos el contenido mientras mostramos el mensaje
	sBox = "#"+sIdContenido;
	$(sBox).show("slow");
	
	//removemos la imagen
	$("#loading").remove();
}

/** END :: Funciones controladoras de ventanas **/

/** INI :: MESAGES **/
function JS_setMesage(sAction, sMsg, bShow)
{
	if (bShow && bShow == true)
	{
		$("#cntMensagePage").removeClass().addClass(sAction).html('<p style="width:620px; float:left; display:block; margin:0px;">' + sMsg + '</p><a href="javascript:void(0);" style="float:right;" onclick="$(\'#cntMensagePage\').slideToggle(\'normal\');">Ocultar mensaje</a><div class="clear"></div>').slideToggle("normal");
	}
	else
	{
		$("#cntMensagePage").removeClass().addClass(sAction).html(sMsg).slideToggle("normal").animate({opacity: 1.0}, 6000).slideToggle(1500);
	}
}

function JS_setMesageForm(sMsg)
{
	$("#cntCapaConfirm").html(sMsg).slideDown("normal");
}
/** END :: MESAGES **/


/** INI :: BOX **/
function JS_closeBox()
{
	JS_hideCargando();
	JS_showFlash();
	$("#lightBG").hide();
	$("#lightBOX").hide().html('');
}
function JS_openBox(sElementId, iWidth)
{
	var arrayPageScroll = JS_getPageScroll();
	var arrayPageSize 	= JS_getPageSize();
	JS_hideFlash();
	if (!iWidth) iWidth = 300;
	$("#lightBG").css('height', arrayPageSize[1] + 'px').show();
	$("#lightBOX").html($("#" + sElementId).html())
								.css({top: 		arrayPageScroll[1] + (arrayPageSize[3] / 10) + 'px',
											left: 	Math.round((arrayPageSize[2] - iWidth) / 2) + 'px',
											width: 	(iWidth != 0 ? iWidth : 300)+'px'
										 })
								.show();
}
/** END :: BOX **/


/** INI :: GRID **/
function JS_initGrid()
{
	if ($("#cntBodyList > tr").length == 0)
	{
		$("#cntBodyList").html('<tr align="center"><td colspan="50" class="ajax-load"><div>Recuperando datos</div></td></tr>');
	}

	if ($("table#cntListHeader tr").get(0))
	{
		$("table#cntListHeader tr").mouseover(function(){
			$(this).children().each(function(i){$(this).addClass("hover");});
		});
	}
	$("table#cntListHeader tr").mouseout(function(){
		$(this).children().each(function(i){$(this).removeClass("hover");});
	});


	/** Coloco cada fila a una altura de 70px **/
	/*
	//$("table#cntListHeader tbody tr").css({'height': '20px'});

	$("table#cntListHeader tbody tr").click(function (){
		agrandarTR(this);
 });
 */
	
	if ($(".wgtTooltip").get(0))
	{
		$(".wgtTooltip").Tooltip({ delay: 0, track: true, showBody: " ## ", event: "mouseover"});
	}
};

function JS_showCargando()
{
	$("div.loading").fadeIn();
	$("#cntBodyList").addClass('loading');
}
function JS_hideCargando()
{
	$("div.loading").fadeOut("slow");
	$("#cntBodyList").removeClass();
}
$(document).ready(function(){JS_initGrid();});
/** END :: GRID **/


/*
function agrandarTR(oTr)
{
	$(oTr).css({'height': (parseInt($(oTr).css('height'))+20) + 'px'});
	$(oTr).unbind( "click" ).click(function (){achicarTR(this);});
}
function achicarTR(oTr)
{
	$(oTr).css({'height': (parseInt($(oTr).css('height'))-20) + 'px'});
	$(oTr).unbind( "click" ).click(function (){agrandarTR(this);});
}
*/

/** INI :: FUNCTIONS CONFIRM **/
var sFunctionsConfirm = null;
var aParamsConfirm = null;
function JS_confirm(sTitle, sMsg, sFunction, aParams){sFunctionsConfirm = sFunction;aParamsConfirm = aParams;$.prompt('<h3>' + sTitle + '</h3><p>' + sMsg + '</p>',{buttons:{Aceptar:true, Cancelar:false},prefix:'brownJqi',callback: JS_confirmComplete});};
function JS_confirmComplete(bReturn){if (bReturn){sFunctionsConfirm(aParamsConfirm[0], aParamsConfirm[1]);}}
function JS_alert(sTitle, sText){$.prompt('<h3>' + sTitle + '</h3><p>' + sText + '</p>', {prefix:'brownJqi'});}
/** END :: FUNCTIONS CONFIRM **/


/** INI :: FUNCTIONS IMAGE AJAX **/
function iniInputFile(sInputId)
{
	var oContent = $("#" + sInputId).parent();
	var oForm = oContent.parents("form");
	oForm.prepend('<div class="confirmFile"></div>');

	var sNewCode = '<input type="file" id="upfilexajax_' + sInputId + '" name="upfilexajax_' + sInputId + '" class="cntFileField" />';
	sNewCode += '<input type="hidden" id="' + sInputId + '" name="' + sInputId + '" value="" />';

	$("#" + sInputId).remove();

	oContent.find('img.agregar_imagen').before(sNewCode);
	oContent.find('img.agregar_imagen').after('<img class="imagen_login" id="upfilexajax_' + sInputId + 'igmLoadingFile" src="http://' + SITE_URL + '/img/small_loading.gif">');
	//oContent.before('<span><img id="upfilexajax_' + sInputId + 'igmLoadingFile" src="http://' + SITE_URL + '/img/small_loading.gif"></span>');
	$("#upfilexajax_" + sInputId + "igmLoadingFile").fadeOut(1);
	$("#upfilexajax_" + sInputId).change(function(){return ajaxFileUpload('upfilexajax_' + sInputId, sInputId);});
}

function ajaxFileUpload(sInputId, sRealInputId)
{
	$("#" + sInputId + "igmLoadingFile").ajaxStart(function(){$(this).fadeIn(200);}).ajaxComplete(function(){$(this).fadeOut(1);});
	$.ajaxFileUpload
	(
		{
			url: 'http://' + SITE_URL + '/inc/ajaxfileupload.php?fileImputUpload=' + sInputId, 
			secureuri:false,
			fileElementId: sInputId,
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else{
						$('#' + sRealInputId).val(data.file);
						$('#' + sRealInputId).next('img').attr('src', 'http://' + SITE_URL + '/upload/' + data.file + '?nocache=' + Math.random());
					}
				}
			},
			error: function (data, status, e){alert(e);}
		}
	)
	$("#" + sInputId).change(function(){return ajaxFileUpload(sInputId, sRealInputId);});
	return false;
}

/** END :: FUNCTIONS IMAGE AJAX **/