// --- START --- Reapunta el opener de un popup ------------------------------------------------------------
function redirectOpener(url){
	window.opener.locaton = url;
}

// --- START --- Muestra y/u oculta Items ------------------------------------------------------------
function toggleItem(itemId){
	var itemObj = document.getElementById(itemId);
	itemObj.style.display = (itemObj.style.display == "") ? "none" : "";
}
// --- END ------------------------------------------------------------------------------------------------

//--- START Función que abre un link en una pagina nueva---------------------------------------------------
function openInBlankAll(url){
	var newWindow = window.open(url,"_blank","width=810,height=550,left=1,top=1,location=no,menubar=no,status=no,resizable=yes,toolbar=no,scrollbars=yes");
}
//--- End -------------------------------------------------------------------------------------------------

//--- START Función que abre un link en una pagina nueva---------------------------------------------------
function openInPopUp(url){
	var newWindow = window.open(url,"_blank","width=300,height=370,left=300,top=150,location=no,menubar=no,status=no,resizable=no,toolbar=no,scrollbars=no");
}
//--- End -------------------------------------------------------------------------------------------------

//--Begin PopUp Generic------------------------------------------------------------------------------------
function openPopup(url,wWidth,wHight,wLeftP,wTopP,wResize,scrl){
	var newWindow = window.open(url,"_blank","width="+wWidth+", height="+wHight+", left="+wLeftP+", top="+wTopP+", scrollbars="+scrl+", resizable="+wResize);
}
//--End PopUp Generic--------------------------------------------------------------------------------------

//--- START --- Función que pasa el valor de un elemento a un campo tipo hidden ---------------------------
function setGroupValue(hdnID, valueToSet){

	var hdnToSet = document.getElementById(hdnID);
	hdnToSet.value = valueToSet;
	//alert(hdnToSet.value);
}
// --- END ------------------------------------------------------------------------------------------------

//--- START --- Función para ------------------------------------------------------------------------------
function evaluateToggleItem(itemID, valueToAvaluate){
	// - Campos actores
	var itemToEvaluate = document.getElementById(itemID);
	// - Validamos si el valor de la selección no es vacío -
	if(valueToAvaluate != ""){
		// - Si el valor es  = 138 "México" -
		if(valueToAvaluate != 14){
			// - Campo estados para méxico
			itemToEvaluate.style.display = "";
		}else{
			itemToEvaluate.style.display = "none";
		}

	}else{
		itemToEvaluate.style.display = "none";
	}
	//itemToEvaluate.style.display = (itemToEvaluate.style.display == "") ? "none" : "";
}
// --- END ------------------------------------------------------------------------------------------------

// --- START --- Función para limpiar un formulario -------------------------------------------------------
function formClear(formName){
	if(confirm("¿Estas seguro que deseas borrar tus datos?")){
		document.forms[formName].reset();
	}
}
// --- END ------------------------------------------------------------------------------------------------

//--- START --- Función para mostrar/ocultar items --------------------------------------------------------
function showHideItem(itemID, valueToAvaluate){
	// - Campos actores
	var itemToEvaluate = document.getElementById(itemID);
	// - Validamos si el valor de la selección no es vacío -
	if(valueToAvaluate != ""){
		// - Si el valor es  = 138 "México" -
		if(valueToAvaluate == 1){
			// - Campo estados para méxico
			itemToEvaluate.style.display = "";
		}else{
			itemToEvaluate.style.display = "none";
		}

	}else{
		itemToEvaluate.style.display = "none";
	}
	//itemToEvaluate.style.display = (itemToEvaluate.style.display == "") ? "none" : "";
}
//--- END -------------------------------------------------------------------------------------------------

//--- START --- Función para mostrar ocultar en checkboxes ------------------------------------------------
function enableDisableField(fieldID){

	var itemToEvaluate = document.getElementById(fieldID);
	itemToEvaluate.style.display = (itemToEvaluate.style.display == "") ? "none" : "";

}
//--- END -------------------------------------------------------------------------------------------------

//--- START --- Función para mostrar/ocultar items --------------------------------------------------------
function showHideOtroItem(itemID, valueToAvaluate){
	// - Campos actores
	var itemToEvaluate = document.getElementById(itemID);
	// - Validamos si el valor de la selección no es vacío -
	if(valueToAvaluate != ""){
		// - Si el valor es  = 138 "México" -
		if(valueToAvaluate == 25 || valueToAvaluate == 39 || valueToAvaluate == 43 || valueToAvaluate == 65 || valueToAvaluate == 109 || valueToAvaluate == 113 || valueToAvaluate == 120 || valueToAvaluate == 125 || valueToAvaluate == 137 || valueToAvaluate == 143){
			// - Campo estados para méxico
			itemToEvaluate.style.display = "";
		}else{
			itemToEvaluate.style.display = "none";
		}

	}else{
		itemToEvaluate.style.display = "none";
	}
	//itemToEvaluate.style.display = (itemToEvaluate.style.display == "") ? "none" : "";
}
//--- END -------------------------------------------------------------------------------------------------


// --- START --- Funcion para poner como minimo un numero de caracteres -----------------------------------
function atLeastCharacters(fieldName, characters){

	var fieldContent = fieldName.value;

	if(fieldContent != ""){
		if (fieldContent.length < characters ) {

			alert("Por favor debes de ingresar al menos " + characters + " caracteres");
			fieldName.value = "";
			//fieldName.focus();

		}
	}

}
// --- END ------------------------------------------------------------------------------------------------
// --- START --- Ajax engine -------------------------------------------------------------------------------
function ajaxEngine(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
// --- END ------------------------------------------------------------------------------------------------

// --- START --- Función para hacer el envio de variables a través de ajax --------------------------------
function callAjax(url,variables,content,mist){
	var contenedor = document.getElementById(content);
	var ajax = ajaxEngine();
	var mist;
	var variables;

	if(variables == "null"){
		ajax.open("GET",url,true);
	}else{
		ajax.open("POST",url,true);
	}
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			contenedor.innerHTML = ajax.responseText;
			if(mist == 1){
				//resetMist();
			}
	 	}else{
			if(mist == 1){
				//showMist();
				//showHideLoader('loaderHolder', 'messageHolder');
				contenedor.innerHTML = '<center><img src="images/Genomma_Asepxia_ajax_loader.gif" width="16" height="16" /></center>';
				//alert("cargando");
			}
		}
	}
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
		if(variables == "null"){
		ajax.send(null);
	}else{
		ajax.send(variables);
	}

}
// --- END ------------------------------------------------------------------------------------------------

//-- START Función para traer el código postal ------------------------------------------------------------
function getCP(codigoPostal,comboClass,ajaxHolder){
	var cp = document.getElementById(codigoPostal).value;
	if(cp.length==5){
		document.getElementById("trAsenta").style.display = '';
		callAjax('classes/Genomma_Asepxia_getAsentamiento.php?cp='+cp+'&class='+comboClass,'null',ajaxHolder,1);
	}
}
// --- END ------------------------------------------------------------------------------------------------

// --- START --- Función que verifica un dia valido -------------------------------------------------------
setDay = function (ano,mes,td){
	var ano = document.getElementById(ano);
	var mes = document.getElementById(mes);
	if(ano.value == ""){
		alert('Debes de seleccionar el año')
	}else{

		//alert('includes/Genomma_Asepxia_getDayOfTheMonth.php?month='+mes.value+'&year='+ano.value, td);
		//llamarasincrono('includes/Genomma_Asepxia_getDayOfTheMonth.php?month='+mes.value+'&year='+ano.value, td);

			callAjax('classes/Genomma_Asepxia_getDayOfTheMonth.php?month='+mes.value+'&year='+ano.value,'null',td,1);
	}
}
// --- END ------------------------------------------------------------------------------------------------

// --- START Función para checar si un usuario ya ha sido registrado en el sistema ------------------------
function checkUser(usuario){
	callAjax('classes/Genomma_Asepxia_CheckUser.php?userName='+usuario,'null','userField',0);
	//alert(usuario);
}
// --- END ------------------------------------------------------------------------------------------------

// --- START ----------------------------------------------------------------------------------------------
function setDateValue(year,month,day,field){

	document.getElementById(field).value = year+"-"+month+"-"+day;
}
// --- END ------------------------------------------------------------------------------------------------

// --- START --- Funcion que solo permite caracteres numericos --------------------------------------------
function justVarchar(fieldName, fieldType){

	//Definimos que tipo de caracteres aceptara
	if(fieldType == "num"){
		var allowedKeys = "0123456789";}
	else{
		var allowedKeys = ".0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz@-_";
	}
	//////////////////////////////////////////

	var contentArray = new Array();
	var finalArray = new Array();
	var fieldContent = fieldName.value;
	var cont = 0;
	var filteredContent = "";

	// Creación de un arreglo con los caracteres del campo de texto
	for(i=0; i<=fieldContent.length; i++){
		contentArray[i] = fieldContent.substring(i,i+1);
	}

	for(j=0; j<=contentArray.length; j++){
		for(x=0; x<=allowedKeys.length; x++){
			if(contentArray[j] == allowedKeys.charAt(x)){
				finalArray[cont] = contentArray[j];
				cont++;
			}
		}
	}

	for(z=0; z<=finalArray.length-2; z++){
		filteredContent += finalArray[z];
	}
	fieldName.value = filteredContent;
}
// --- END ------------------------------------------------------------------------------------------------

// --- START --- Rating Stars Images ----------------------------------------------------------------------
function setStarImagesOn(postID,counter){
	//alert("star_"+postID+"_"+counter);
	for(i=1; i <= counter; i++){
		document.getElementById("star_"+postID+"_"+i).src = "images/Genomma_Asepxia_rating_over.gif";
	}
}
function setStarImagesOff(postID,counter,img){
	//alert(counter);
	for(i=1; i <= counter; i++){
		document.getElementById("star_"+postID+"_"+i).src = img;
	}
}
function setStarImagesOff2(postID,counter,imgStars,img){
	//alert(counter+imgStars);
	for(i=1; i <= imgStars; i++){
		document.getElementById("star_"+postID+"_"+i).src = "images/Genomma_Asepxia_rating_on.gif";
		//alert("star_"+postID+"_"+i)
	}
	for(k=i; k <= counter; k++){
		document.getElementById("star_"+postID+"_"+k).src = "images/Genomma_Asepxia_rating_off.gif";
		//alert("star_"+postID+"_"+k)
	}
}

function setArticleRating(postID,rating,table){
	//alert("el post es: "+postID+" y el rating es: "+rating);
	callAjax('classes/Genomma_Asepxia_setRating.php','postID='+postID+"&rating="+rating+"&table="+table,'ratingAjax_'+postID,0);
}
//--- END -------------------------------------------------------------------------------------------------

//--- START Función que valida la si estas logeado o no ---------------------------------------------------
function loginAlert(){

	myLightWindow.activateWindow({href: 'includes/Genomma_Asepxia_login_alert.php', title: 'Alerta', height: '250', width: '300'});

}
//--- End -------------------------------------------------------------------------------------------------

//--- START Función para abrir el lightbox  ---------------------------------------------------------------
function openIMC(){

	myLightWindow.activateWindow({href: 'pages/Genomma_Asepxia_imc_selector.php', title: 'Calcula tu IMC', height: '500', width: '233'});

}
//--- End -------------------------------------------------------------------------------------------------

//--- START Función para Validar el IMC  ------------------------------------------------------------------
function checkImcForm(){
	var flag = 1;

	var messages = new Array();
		messages[0] = "No, indicaste los metros";
		messages[1] = "Olvidaste, indicar los centímetros";
		messages[2] = "Debes indicar tu peso en kilos";

	var fields = new Array();
		fields[0] = document.getElementById('metros');
		fields[1] = document.getElementById('centimetros');
		fields[2] = document.getElementById('peso');

	for(i=0; i<fields.length; i++){
		if(fields[i].value == ""){
			alert(messages[i]);
			fields[i].focus();
			flag = 0;
			break;
		}
	}

	if(flag == 1){
		var estaturaMetros = parseFloat(fields[0].value+"."+fields[1].value);
		var pesoNumber = parseFloat(fields[2].value);
		var imc = pesoNumber/(estaturaMetros*estaturaMetros);
		var imcRound = imc+"";
		document.getElementById('imc').value = imcRound.substring(0,4);
		document.getElementById('imc').focus();
		//alert(imcRound);
	}
}
//--- End ------------------------------------------------------------------------------------------------

//--- START Función para Validar el captcha  ------------------------------------------------------------------
function checkCaptcha(userKey){
	var userKeyCode = document.getElementById(userKey);

	var publicKeyCode = document.getElementById("captchaPublic");
	if(userKeyCode.value.length == 5){
		callAjax("classes/Genomma_Asepxia_checkCaptcha.php","userCode="+userKeyCode.value+"&publicCode="+publicKeyCode.value,"captchaFlag",0);
		//alert(publicKeyCode.value);
	}

}
//--- End ------------------------------------------------------------------------------------------------
//--Traemos los datos del imc resultante
function getImcData(imc){

	var imc = document.getElementById("imc").value;
	callAjax('../includes/Genomma_Asepxia_getImcData.php','imc='+imc,'imc-field',0);

	}
//--fin de traemos los datos del imc resultante

function setCpDataValue(dataValue){
	document.getElementById("cp_2").value = dataValue;
}

//-- START Función para traer delegacion y municipio ------------------------------------------------------------
function getDelegMuni(estado,comboClass,ajaxHolder){

	var estado = document.getElementById(estado).value;
	if(estado.value!=0){
		callAjax('../classes/Genomma_Asepxia_getDelegMuni.php?estado='+estado+'&class='+comboClass,'null',ajaxHolder,1);
	}
}
// --- END ------------------------------------------------------------------------------------------------

//-- START Función para traer asentamiento ------------------------------------------------------------
function getAsentamiento(deleg_muni,comboClass,ajaxHolder){

	var deleg_muni = document.getElementById(deleg_muni).value;

	if(deleg_muni.value!=0){
		callAjax('../classes/Genomma_Asepxia_getAsentamientoReverse.php?','deleg_muni='+deleg_muni+'&class='+comboClass,ajaxHolder,1);
	}
}
// --- END ------------------------------------------------------------------------------------------------
/////////////// START ----------- Envio de Fomrularios Via POST con AJAT's By: Luis R. Perez Huichita Huichita
function formSend(url, formID, contentID){
	var formData = document.getElementById(formID);
    var formDataString = "";
    var fieldSplit;
	var formRequest = false
	fieldSplit = "" ;
    for (var i=0; i <= formData.elements.length-1;i++) {
		formDataString += fieldSplit+formData.elements[i].name+'='+encodeURI(formData.elements[i].value);
	 	fieldSplit="&";
	}
	callAjax(url,formDataString, contentID,1);

}
// --- END ----------------------------------------------------------------------------------------------

function refreshCaptcha(){

	document.getElementById("code").value = "";
	document.getElementById("captchaValidation").value = "0";
	callAjax("classes/Genomma_Asepxia_setCaptchaImage.php",'null',"captchaTD",0);

}
//--- END -------------------------------------------------------------------------------------------------

//--- START Función que abre el tell a friend -------------------------------------------------------------
function openTaf(url){

	myLightWindow.activateWindow({href: 'includes/Genomma_Asepxia_tell_a_friend.php?url='+url, title: 'Enviar a un amigo', height: '250', width: '300'});

}
//--- End -------------------------------------------------------------------------------------------------