function ValidaFormularios(controles)
{
	for ( i = 0, n = controles.length; i < n; i++ )
 	{
	var iControl = controles[i];
  	var e = document.getElementById(iControl[0]);
  		if (e)
  		{
   			if ( e.value.length == 0 )
   			{
    			alert("El campo " + iControl[1] + " está vacío.");
    			e.focus();
    			return 0;
   			}
  		}
 	}
        return 1;
}

function ValidaMails(mails)
{
	var filtro = /^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
	
	for ( i = 0, n = mails.length; i < n; i++ )
 	{
		var iMail = mails[i];
  		var e = document.getElementById(iMail[0]);
		if (e)
  		{
   			if ( e.value.length > 0 )
   			{
				var s = e.value;
				if (!filtro.test(s))
				{
					alert("El campo " + iMail[1] + " no es válido.");
    				e.focus();
    				return 0;
				}
			}			
   		}
  	}

        return 1;
}

function ValidaURL(url) {
var re=/^(http:|https:|ftp:)\/\/\w+(\.\w+)*(\-\w+)?\.\w{2,3}(\:\d{2,6})?(\/{1,2}(\:|\-|\w|\.|\?|\/|\=|\&|\%|\@|\\|\,)*)?$/;
return re.test(url);
}


function Numeros(e)
{
	var key = (document.all) ? e.keyCode : e.which;
	if (key < 48 || key > 57)
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function PaginacionSubmit(i,por,order)
{
	document.paginar.page.value=i;
	document.paginar.por.value=por;
	document.paginar.order.value=order;
	document.paginar.submit();
	
}

function SubmitID(id, command)
{
	var myForm = document.getElementById('formid');	
	myForm.command.value = command;
	myForm.id.value = id;
	myForm.submit();
}

/* DIVS DESPLEGABLE */

function mostrar(blo,off,on)
{
	OCULTO="none";
	VISIBLE="block";
	if(blo != "")
	{
		document.getElementById(blo).style.display=VISIBLE;
	}
	if(off != "")
	{
		document.getElementById(off).style.display=VISIBLE;
	}
	if(on != "")
	{
		document.getElementById(on).style.display=OCULTO;
	}
}
function ocultar(blo,off,on)
{
	OCULTO="none";
	VISIBLE="block";
	if(blo != "")
	{
		document.getElementById(blo).style.display=OCULTO;
	}
	if(off != "")
	{
		document.getElementById(off).style.display=OCULTO;
	}
	if(on != "")
	{
		document.getElementById(on).style.display=VISIBLE;
	}
}


/////////////////Login/////////////////////////////




function AgregarFavoritos(url,titulo)
{
	
	if ((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4)) 
	{
		window.external.AddFavorite(url,titulo);
	}
	else 
	{
		if(navigator.appName == "Netscape")
		window.sidebar.addPanel(titulo, url,"");
	}
}

function popUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=660,height=420,left = 262,top = 259');");
}



/*DIV MOVIBLE*/

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }


  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();


var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",	dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }


  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",	dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,	true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function LightboxDelegate(url,caption) 
{
	var objLink = document.createElement('a');
	objLink.setAttribute('href',url);
	objLink.setAttribute('rel','lightbox');
	objLink.setAttribute('title',caption);
	Lightbox.prototype.start(objLink);
}


$(document).ready(function(){

	
	$("#btn_login").click(function() {
  	if(document.frmlogin.usuario.value!='' && document.frmlogin.password.value!='')
	{
            $('#msg_loginflotante').text("");
		var obj = document.getElementById('frmlogin');
		obj.submit();
	}else
	{
            if(document.frmlogin.usuario.value=='')
            {
                document.frmlogin.usuario.className='error_lf';
            }
            else
            {
                 document.frmlogin.usuario.className='form_campo_contenido_chic1';
            }
            if(document.frmlogin.password.value=='')
            {
                document.frmlogin.password.className='error_lf';
            }
            else
            {
                 document.frmlogin.password.className='form_campo_contenido_chic1';
            }
            $('#msg_loginflotante').text("Favor de llenar los campos obligatorios");
               
            return false;
	}
	});

        $("#img_login").click(function() {
  	if(document.frmlogin.usuario.value!='' && document.frmlogin.password.value!='')
	{       $('#msg_loginflotante').text("");
		var obj = document.getElementById('frmlogin');
		obj.submit();
	}else
	{
		 if(document.frmlogin.usuario.value=='')
            {
                document.frmlogin.usuario.className='error_lf';
            }
            else
            {
                 document.frmlogin.usuario.className='form_campo_contenido_chic1';
            }
            if(document.frmlogin.password.value=='')
            {
                document.frmlogin.password.className='error_lf';
            }
            else
            {
                 document.frmlogin.password.className='form_campo_contenido_chic1';
            }
                 $('#msg_loginflotante').text("Favor de llenar los campos obligatorios");
                 return false;
	}
	});


        var validator1 = $("#contactanos").validate();
	$("#btn_contactanos").click(function() {
  	if($("#contactanos").valid())
	{
		var obj = document.getElementById('contactanos');
		obj.submit();
	}else
	{
		return false;
	}
	});

        var validator2 = $("#anunciate").validate();
	$("#btn_anunciate").click(function() {
  	if($("#anunciate").valid())
	{
		var obj = document.getElementById('anunciate');
		obj.submit();
	}else
	{
		return false;
	}
	});

        var validator3 = $("#comentarios").validate();
	$("#btn_comentarios").click(function() {
  	if($("#comentarios").valid())
	{
		var obj = document.getElementById('comentarios');
		obj.submit();
	}else
	{
		return false;
	}
	});

        var validator4 = $("#actualizanos").validate();
	$("#btn_actualizanos").click(function() {
  	if($("#actualizanos").valid())
	{
		var obj = document.getElementById('actualizanos');
		obj.submit();
	}else
	{
		return false;
	}
	});

        var validator5 = $("#forgot").validate();
	$("#btn_forgot").click(function() {
  	if($("#forgot").valid())
	{
		var obj = document.getElementById('forgot');
		obj.submit();
	}else
	{
		return false;
	}
	});


        var validator6 = $("#frmlogin2").validate();
	$("#btn_frmlogin2").click(function() {
  	if($("#frmlogin2").valid())
	{
		var obj = document.getElementById('frmlogin2');
		obj.submit();
	}else
	{
		return false;
	}
	});


        var validator7 = $("#newlester").validate();
	$("#btn_newlester").click(function() {

        var obj = document.getElementById('newlester');
  	if($("#newlester").valid())
	{
            /*
            if(obj.nextel.value == "" && obj.nextel1.value == "" && obj.nextel2.value == "")
            {
                $('#msn_nextel').text("Favor de llenar el numero nextel");
                return false;
            }
            $('#msn_nextel').text("");*/
		
            obj.submit();
	}else
	{
            /*
            if(obj.nextel.value == "" && obj.nextel1.value == "" && obj.nextel2.value == "")
            {
                $('#msn_nextel').text("Favor de llenar el numero nextel");
            }
            */
            return false;	}
	});

        var validator8 = $("#patrocinador").validate();
	$("#btn_patrocinador").click(function() {
  	if($("#patrocinador").valid())
	{
		var obj = document.getElementById('patrocinador');
		obj.submit();
	}else
	{
		return false;
	}
	});

        var validator9 = $("#frmfranquicias").validate();
	$("#btn_franquicias").click(function() {
  	if($("#frmfranquicias").valid())
	{
		var obj = document.getElementById('frmfranquicias');
		obj.submit();
	}else
	{
		return false;
	}
	});

        
});


function validaReporta()
{
        var validator7 = $("#frmreporta").validate();
	
  	if($("#frmreporta").valid())
	{
		var obj = document.getElementById('frmreporta');
                var opinion = obj.id_opinion.value;
                var mail = obj.mail.value;
                var comentario = obj.comentario.value;
		xajax_GuardaReporte(opinion, mail,comentario);
	}else
	{
		return false;
	}


}

function limiteCaracteres(frm,field,field1,total)
{
	count = total;
	var tex = document[frm][field].value;
	var len = tex.length;
	if(len > count)
	{
            tex = tex.substring(0,count);
            document[frm][field].value =tex;
    	return false;
	}
	document[frm][field1].value = count-len;
}

function ClickAdbox(id, friendly, position)
{
	xajax_AddClickAdbox(id, friendly, position);

}
