String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') };
var filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

function FormField (objId) {
    this.objId = objId;
    this.objInst = null;
    
    this.init = function(){
        if (this.objInst==null)
        if (this.objId.trim().length>0 && document.getElementById(this.objId))
        {
            this.objInst = document.getElementById(this.objId);
        }
    };
    
    this.isEmpty = function(){
        this.init();
        if (this.objInst!=null && this.objInst.value.trim() == "")
            return true;
        return false;
    };
    this.getFieldValue = function(){
        this.init();
        return this.objInst.value;
    };
    this.init();
}

function FormularioAdmin(){
    this.msgLlenaForm = "Proporcione su usuario y contraseña.";
    this.fieldArray2 = new Array();
    this.user = new FormField('user');
    this.pass = new FormField('password');
    
    this.flag = true;
    this.init = function()
    {
        var i = 0;
        this.fieldArray2[i++] = this.user;
        this.fieldArray2[i++] = this.pass;
    };
    
    this.fieldsOk = function()
    {
        this.init();
        var retVal = true;
        for (var i=0;i<this.fieldArray2.length;i++)
        {
            if (this.fieldArray2[i].isEmpty())
            {
                retVal = false;
            }
        }
        return retVal;
    };
    
    this.validate = function()
    {
        if (this.fieldsOk())
		{
			return true;
		}
        return false;
    };
    
    this.submit = function()
    {
		if (this.flag)
		if (this.validate())
        {
            document.getElementById('login-form').submit();
        }
        else
        {
            alert(this.msgLlenaForm);
        }
    };
}

function Formulario(){
    this.msgLlenaForm = "Por favor proporciona la información faltante";
    this.sendUrl = "http://enviayreporta.esmas.com/inversionistas/";
	this.submitAction = "upload.php";
    this.fieldArray2 = new Array();
    this.name = new FormField('name-field');
    this.job = new FormField('job-field');
    this.company = new FormField('company-field');
    this.address = new FormField('address-field');
    this.phone = new FormField('phone-field');
    this.email = new FormField('email-field');
    this.flag = true;
    this.init = function()
    {
        var i = 0;
        this.fieldArray2[i++] = this.name;
        this.fieldArray2[i++] = this.job;
        this.fieldArray2[i++] = this.company;
        this.fieldArray2[i++] = this.address;
        this.fieldArray2[i++] = this.phone;
        this.fieldArray2[i++] = this.email;
    };
    
    this.fieldsOk = function()
    {
        this.init();
        var retVal = true;
        for (var i=0;i<this.fieldArray2.length;i++)
        {
            if (this.fieldArray2[i].isEmpty())
            {
                retVal = false;
            }
        }
        return retVal;
    };
    
    this.validate = function()
    {
        if (this.fieldsOk())
		{
			return true;
		}
        return false;
    };
    
    this.submit = function()
    {
		if (this.validate())
        {
            if (!filter.test(this.email.getFieldValue().trim()))
			{
				alert('Correo electrónico inválido');
				return;
			}
			else
			{
				var form = document.getElementById('form-notif');
				
				this.flag = false;
                if (document.getElementById('unsuscribe').checked)
				{
					form.action=this.sendUrl+'unsuscribe.php';
					AIM.submit(form, {'onComplete' : completeCallback2});
				}
				else
				{
					form.action=this.sendUrl+this.submitAction;
					AIM.submit(form, {'onComplete' : completeCallback});
				}
				form.submit();
			}
        }
        else
        {
            alert(this.msgLlenaForm);
        }
    };
}


function completeCallback(response)
{
    var thisPath = location.pathname;
	if (thisPath.search('inversionistas-espanol')!=-1)
		alert('Gracias por suscribirte.');
	else
        alert('Thanks for join us.');
    closeBox();
}
function completeCallback2(response)
{
    alert('Registro eliminado.');
    closeBox();
}

function validaBusqueda()
{
    var fecha1 = document.getElementById('date1').value;
    var fecha2 = document.getElementById('date2').value;
    var d1 = new Date(fecha1);
    var d2 = new Date(fecha2);
    if (d1!=undefined && d2!= undefined)
    {
        if (d1<=d2)
            document.getElementById('search-form').submit();
    }
}
var formularioAdmin = new FormularioAdmin();
var formulario = new Formulario();
var activeBox = "";
function showBox(box)
{
    resizeLayer();
    var width = $(document).width();
    var height = $(document).height();
    var x = eval(width/2);
    var y = eval(height/2);
    $('#trans-layer').css({'display':'block'});
    $(box).css({'display':'block','top':120,'left':x-241});
    activeBox = box;
}
function showSmallBox(box)
{
    resizeLayer();
    var width = $(document).width();
    var height = $(document).height();
    var x = eval(width/2);
    var y = eval(height/2);
    $('#trans-layer').css({'display':'block'});
    $(box).css({'display':'block','top':120,'left':x-241});
    activeBox = box;
}
function showCalendarC(box)
{
    resizeLayer();
    var width = $(document).width();
    var height = $(document).height();
    var x = eval(width/2);
    var y = eval(height/2);
    $('#trans-layer').css({'display':'block'});
    $(box).css({'display':'block','top':120,'left':x-241});
    activeBox = box;
}
function closeBox()
{
    $(activeBox).css({'display':'none'});
    $('#trans-layer').css({'display':'none'});
    activeBox = "";
}
function resizeLayer(){
    $('#trans-layer').width($(document).width());
    $('#trans-layer').height($(document).height());
}

$(window).resize(function() {
    if (activeBox.trim().length != 0)
    {
        showBox(activeBox);
        resizeLayer();
    }
});

$(document).ready(function() {
    $('#trans-layer').click(function() {
        if (activeBox == '#calendar-conf' )
        {
            closeBox();
        }
    });
	var thisPath = location.pathname;
	if (thisPath.search('inversionistas-espanol')!=-1)
		$('.boxVersion > a').attr( 'href','http://www.televisa.com/inversionistas-ingles/');
	else
		$('.boxVersion > a').attr( 'href','http://www.televisa.com/inversionistas-espanol/');
});
