function redireccionar(url) {
	if ( typeof(url) == 'undefined' )
		return false;
		 
	if ( document && document.location ){
		document.location = url;
	} else if ( window && window.location ) {
		window.location = url;
	} else {
		location.href = url;
	}
}

/** Verifica si la fecha es correcta
 *
 * @param string str Fecha en formato (dd-mm-YY)
 * @return bool
 */
function isDate(str) {
	var dateStr = $.trim(str);
	if ( typeof(dateStr) == 'undefined' ) return false;
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat);
	var und = "undefined";
	
	if ( matchArray == null ) return false;
	if ( matchArray.length < 6 ) return false;
	
	var day = parseFloat(matchArray[1]);
	var month = parseFloat(matchArray[3]);
	var year = parseFloat(matchArray[5]);
	
	if ( day < 1 || day > 31 ) return false; // Dia invalido
	
	if ( month < 1 || month > 12 ) return false; // Mes invalido
	
	if ( (month==4 || month==6 || month==9 || month==11) && day==31) return false; // El mes no tiene 31 dias
	
	if (month == 2) { // Verifica si febrero tiene 29 dias
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) return false; // Febrero no tiene n dias
	}
	return true;
}

/** Dinamic PopUp  (@return void) */
function popUp(url, width, height) {
	if ( typeof(url) == 'undefined' || $.trim(url) == '' ) return false;
	var w = ( typeof(width) != 'undefined' )?width:500;
	var h = ( typeof(height) != 'undefined' )?height:450;
	
	var wo = window.open(url, 'wo', 'resizable=1,scrollbars=1,status=0,menubar=0,titlebar=0,toolbar=0,width='+w+',height='+h);
	wo.focus();
}

/** Imprimir */
function print_version() {
	window.print();
}

/** Agregar a favoritos (@return void) */
function add_fav() {
	var url = window.location.href;
	var title = document.title;
	if (window.sidebar) { // For Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	} else if( window.external || document.all) { // For IE Favorite
		window.external.AddFavorite( url, title);
	} else { // for other browsers which does not support
		return false;
	}
}

/** Valida si es numero (@return bool) */
function isNumber(value) {
	return Number(value).toString() != 'NaN';
}

/** Valida si es mail (@return bool) */
function isMail(str) {
	if ( typeof(str) == 'undefined' ) return false;
	return /^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.]+[A-za-z]$/.test(str);
}

/** Acepta solo numeros (@return bool) */
function acceptNum(evt){
	// NOTE: Backspace = 8, Enter = 13, '0' = 48 - '9' = 57, 'up' = 38, 'down' = 40, 'left' = 37, 'right' = 39
	var key = evt.which || evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57) || key == 37 || key == 39 );
}

/** Valida formulario de contacto (@return bool) */
function valCont(frm) {
	var er = '';
	var fc = '';
	
	if ( $.trim(frm.nombre.value) == "" ) {
		er += '\n- Nombre.';
		fc = ( fc == '' )?'nombre':fc;
	}
	
	if ( $.trim(frm.paterno.value) == "" ) {
		er += '\n- Apellido Paterno.';
		fc = ( fc == '' )?'paterno':fc;
	}
	
	if ( $.trim(frm.materno.value) == "" ) {
		er += '\n- Apellido Materno.';
		fc = ( fc == '' )?'materno':fc;
	}
	
	if ( $.trim(frm.edad.value) == "" ) {
		er += '\n- Edad.';
		fc = ( fc == '' )?'edad':fc;
	} else if ( !isNumber(frm.edad.value) ) {
		er += '\n- Edad inválida.';
		fc = ( fc == '' )?'edad':fc;
	}
	
	if ( $.trim(frm.sexo.options[frm.sexo.selectedIndex].value) == "" ) {
		er += '\n- Sexo.';
		fc = ( fc == '' )?'sexo':fc;
	}
	
	if ( $.trim(frm.email.value) == "" ) {
		er += '\n- E-mail.';
		fc = ( fc == '' )?'email':fc;
	} else if ( !isMail(frm.email.value) ) {
		er += '\n- E-mail inválido.';
		fc = ( fc == '' )?'email':fc;
	}
	
	if ( $.trim(frm.comentario.value) == "" ) {
		er += '\n- Comentarios.';
		fc = ( fc == '' )?'comentario':fc;
	}
	
	if ( er != '' ) {
		alert('Favor de llenar los siguientes campos:\n'+er);
		frm.elements[fc].focus();
		return false;
	}
	$('#btn-env').html('<div class="loading">Enviando...</div>');
	
	return true;
}

/** Valida formulario de registro (@return bool) */
function valReg(frm) {
	var ern = '';
	var er = '';
	var fc = '';
	
	if ( $.trim(frm.titulo.options[frm.titulo.selectedIndex].value) == "" ) {
		er += '\n- Titulo.';
		fc = ( fc == '' )?'titulo':fc;
	}
	
	if ( $.trim(frm.nombre.value) == "" ) {
		er += '\n- Nombre.';
		fc = ( fc == '' )?'nombre':fc;
	}
	
	if ( $.trim(frm.paterno.value) == "" ) {
		er += '\n- Apellido Paterno.';
		fc = ( fc == '' )?'paterno':fc;
	}
	
	if ( $.trim(frm.materno.value) == "" ) {
		er += '\n- Apellido Materno.';
		fc = ( fc == '' )?'materno':fc;
	}
	
	if ( $.trim(frm.sexo.options[frm.sexo.selectedIndex].value) == "" ) {
		er += '\n- Sexo.';
		fc = ( fc == '' )?'sexo':fc;
	}
	
	if ( $.trim(frm.updating.value) != 1 ) {
		var fecha = frm.dia.options[frm.dia.selectedIndex].value + '-' + frm.mes.options[frm.mes.selectedIndex].value + '-' + frm.anio.options[frm.anio.selectedIndex].value;
		if ( !isDate(fecha) ) {
			er += '\n- F. de nacimiento.';
			fc = ( fc == '' )?'dia':fc;
		}
	}
	
	if ( $.trim(frm.email.value) == "" ) {
		er += '\n- E-mail.';
		fc = ( fc == '' )?'email':fc;
	} else if ( !isMail(frm.email.value) ) {
		er += '\n- E-mail inválido.';
		fc = ( fc == '' )?'email':fc;
	}
	
	if ( $.trim(frm.updating.value) != 1 ) {
		if ( $.trim(frm.cedula.value) == "" ) {
			er += '\n- Cédula.';
			fc = ( fc == '' )?'cedula':fc;
		}	
	}
	
	if ( $.trim(frm.tipos_pract.options[frm.tipos_pract.selectedIndex].value) == "" ) {
		er += '\n- Tipos de práctica.';
		fc = ( fc == '' )?'tipos_pract':fc;
	}
	
	if ( $.trim(frm.especialidad.options[frm.especialidad.selectedIndex].value) == "" ) {
		er += '\n- Especialidad.';
		fc = ( fc == '' )?'especialidad':fc;
	}
	
	if ( $.trim(frm.hora_de.options[frm.hora_de.selectedIndex].value) == "" ) {
		er += '\n- Horario de Consulta.';
		fc = ( fc == '' )?'hora_de':fc;
	}
	
	if ( $.trim(frm.boletin.options[frm.boletin.selectedIndex].value) == "" ) {
		er += '\n- Deseo recibir información de Bayer de México vía electrónica.';
		fc = ( fc == '' )?'boletin':fc;
	}	
	
	if ( er != '' ) {
		alert('Favor de llenar los siguientes campos:\n'+er);
		frm.elements[fc].focus();
		return false;
	}
	/**validacion de datos numericos (cedula, ladas, telefonos)*/
	if ($.trim(frm.lada_tel.value) != parseFloat($.trim(frm.lada_tel.value)) && $.trim(frm.lada_tel.value) != '') {
		ern += '\n- Lada (Teléfono particular), debe ser númerico.';
		fc = ( fc == '' )?'lada_tel':fc;
	}	
	if ($.trim(frm.telefono.value) != parseFloat($.trim(frm.telefono.value)) && $.trim(frm.telefono.value) != '') {
		ern += '\n- Teléfono (particular), debe ser númerico.';
		fc = ( fc == '' )?'telefono':fc;
	}		
	if ($.trim(frm.lada_cel.value) != parseFloat($.trim(frm.lada_cel.value)) && $.trim(frm.lada_cel.value) != '') {
		ern += '\n- Lada (Teléfono celular), debe ser númerico.';
		fc = ( fc == '' )?'lada_cel':fc;
	}	
	if ($.trim(frm.celular.value) != parseFloat($.trim(frm.celular.value)) && $.trim(frm.celular.value) != '') {
		ern += '\n- Teléfono (celular), debe ser númerico.';
		fc = ( fc == '' )?'celular':fc;
	}		
	if ($.trim(frm.cedula.value) != parseFloat($.trim(frm.cedula.value)) && $.trim(frm.cedula.value) != '') {
		ern += '\n- Cédula, debe ser númerico.';
		fc = ( fc == '' )?'cedula':fc;
	}		
	if ( ern != '' ) {
		alert('Favor de llenar correctamente siguientes campos:\n'+ern);
		frm.elements[fc].focus();
		return false;
	}	
	/** */
	
	$('#btn-env').html('<div class="loading">Enviando...</div>');
	
	return true;
}

/** Valida formulario de modificar registro (@return bool) */
function valModReg(frm) {
	var er = '';
	var fc = '';
	
	if ( $.trim(frm.titulo.options[frm.titulo.selectedIndex].value) == "" ) {
		er += '\n- Titulo.';
		fc = ( fc == '' )?'titulo':fc;
	}
	
	if ( $.trim(frm.nombre.value) == "" ) {
		er += '\n- Nombre.';
		fc = ( fc == '' )?'nombre':fc;
	}
	
	if ( $.trim(frm.paterno.value) == "" ) {
		er += '\n- Apellido Paterno.';
		fc = ( fc == '' )?'paterno':fc;
	}
	
	if ( $.trim(frm.materno.value) == "" ) {
		er += '\n- Apellido Materno.';
		fc = ( fc == '' )?'materno':fc;
	}
	
	if ( $.trim(frm.sexo.options[frm.sexo.selectedIndex].value) == "" ) {
		er += '\n- Sexo.';
		fc = ( fc == '' )?'sexo':fc;
	}
	
	var fecha = frm.dia.options[frm.dia.selectedIndex].value + '-' + frm.mes.options[frm.mes.selectedIndex].value + '-' + frm.anio.options[frm.anio.selectedIndex].value;
	if ( !isDate(fecha) ) {
		er += '\n- F. de nacimiento.';
		fc = ( fc == '' )?'dia':fc;
	}
	
	if ( $.trim(frm.email.value) == "" ) {
		er += '\n- E-mail.';
		fc = ( fc == '' )?'email':fc;
	} else if ( !isMail(frm.email.value) ) {
		er += '\n- E-mail inválido.';
		fc = ( fc == '' )?'email':fc;
	}
	
	if ( $.trim(frm.tipos_pract.options[frm.tipos_pract.selectedIndex].value) == "" ) {
		er += '\n- Tipos de práctica.';
		fc = ( fc == '' )?'tipos_pract':fc;
	}
	
	if ( $.trim(frm.especialidad.options[frm.especialidad.selectedIndex].value) == "" ) {
		er += '\n- Especialidad.';
		fc = ( fc == '' )?'especialidad':fc;
	}
	
	if ( $.trim(frm.hora_de.options[frm.hora_de.selectedIndex].value) == "" ) {
		er += '\n- Horario de Consulta.';
		fc = ( fc == '' )?'hora_de':fc;
	}
	
	if ( er != '' ) {
		alert('Favor de llenar los siguientes campos:\n'+er);
		frm.elements[fc].focus();
		return false;
	}
	$('#btn-env').html('<div class="loading">Enviando...</div>');
	
	return true;
}


var tips = {
	opt: {
		idContenedor: 'tip_container',
		idTitulo: 'tip_title',
		idContenido: 'tip_content',
		zIndex: 100,
		top: 16,
		left: 40
	},
	build: function() {
		$('body').append('<div id="'+this.opt.idContenedor+'"><div id="'+this.opt.idTitulo+'"></div><div id="'+this.opt.idContenido+'"></div></div>');
		
		this.contenedor = $('#'+this.opt.idContenedor);
		this.contenedor.click(function() {
			tips.hide();
		});
		this.contenedor.css({'position': 'absolute', 'z-index': this.opt.zIndex});
		this.titulo = $('#'+this.opt.idTitulo);
		this.contenido = $('#'+this.opt.idContenido);
	},
	position: function() {
		var pos = this.opener.position();
		var top = pos.top + this.opt.top;
		
		var left = pos.left + this.opt.left;
		this.contenedor.css({
			'top': top,
			'left': left
		});
	},
	show: function( opener, titulo, mensaje ) {
		this.opener = $(opener);
		if ( $('#'+this.opt.idContenedor).length < 1 )
			this.build();
		
		this.titulo.html(titulo);
		this.contenido.html(mensaje);
		this.position();
		this.contenedor.show(200);
	},
	hide: function() {
		this.contenedor.hide("fast");
	}
};

function verificaLogin(frm){
	$('#error_php').empty();
	var cedula = frm.credential_0;
	var fecha = frm.credential_1;
	
	var vCedula = $.trim(cedula.value);
	var vFecha = $.trim(fecha.value);
	
	if ( vCedula == "" ){
		cedula.focus();
		tips.show(cedula, 'Cédula', 'Favor proporcionar su cédula.');
		return false;
	}
	if ( vFecha == "" ) {
		cedula.focus();
		tips.show(fecha, 'Fecha de nacimiento', 'Favor proporcionar su fecha de nacimiento.');
		return false;
	}
	
	var divBtnLog = $('#login_btn');
	// var divBtnLogHTML = divBtnLog.html();
	
	divBtnLog.html('<div class="loading">Validando...</div>');
	return true;
	/*
	var dataStr = 'credential_0=' + vCedula + '&credential_1=' + vFecha + '&ajax=1';
	
	$.ajax({
		type: 'POST',
		url: '/register/process_login.php',
		data: dataStr,
		cache: false,
		contentType: 'application/x-www-form-urlencoded; charset=utf-8',
		success: function(resp) {
			if ( resp == 'ok' ) {
				redireccionar('/scripts/pages/es/auth/home.php');
			} else {
				cedula.select();
				tips.show(cedula, 'Datos incorrectos', 'La <strong>cédula profesional</strong> o la <strong>fecha de nacimiento</strong> son inválidos.');
				divBtnLog.html(divBtnLogHTML);
			}
		}
	});
	
	return false*/
}
function selEspecialidad( esp ){
	var sel = esp.options[esp.selectedIndex].value;
	if ( sel == 'Otra' ) {
		$('.otra_esp').show(200);
	} else {
		$('.otra_esp').hide(200);
	}
}

function selVisitado( vis ){
	var sel = vis.options[vis.selectedIndex].value;
	if ( sel.toLowerCase() == 'no' ) {
		$('#vis_desea').show(200);
		if ( $('#desea_visitado').get(0).value.toLowerCase() == 'sí' ) {
			$('#vis_fecha').show(200);
		} else {
			$('#vis_fecha').hide(200);
		}
	} else {
		$('#vis_desea').hide(200);
		$('#vis_fecha').hide(200);
	}
}

function selFecha( des ){
	var sel = des.options[des.selectedIndex].value;
	if ( sel.toLowerCase() == 'sí' ) {
		$('#vis_fecha').show(200);
	} else {
		$('#vis_fecha').hide(200);
	}
}
