var uri = new Object();
var locat = window.location.href;
locate = (locat.charAt(7) == 'l') ? 'http://localhost/infra7' :  'http://www.infranetworking.com';

uri.base  = locate;
uri.stat  = locate+'/static';
uri.js    = locate+'/static/js';
uri.img   = locate+'/static/img';

if (document.images)
{
	ajax_loading = new Element('img',{'src' : uri.img+'/ajax-loader.gif', 'width' : '16px', 'height' : '16px'});
}

/* Menu cabezal */
if ($('horizontal-menu'))
{
	$('horizontal-menu').select('li[class="menu-option"]').each(function(item)
	{
		item.onmouseover = function (){if (this.firstDescendant())	this.addClassName('menu-option-active');};
		item.onmouseout = function (){if (this.firstDescendant() && this.hasClassName('menu-option-active')) this.removeClassName('menu-option-active');};
	});

	if ($('login-target'))
	{
		var li = $('login-option');
		$('login-target').onclick = function()
		{
			if (li.hasClassName('menu-option-active')) li.removeClassName('menu-option-active');
			else {
				li.addClassName('menu-option-active');
				li.setStyle({'z-index' : '1500'});
			}

			return false;
		};

		//		li.onmouseover = function(){
		//			this.addClassName('menu-option-active');
		//		};
		//
		//		li.onmouseout = function(){
		//			if (this.hasClassName('menu-option-active')) this.removeClassName('menu-option-active');
		//		};
	}

	if ($('logued-target'))
	{
		var li2 = $('logued-option');
		$('logued-target').onclick = function()
		{
			if (li2.hasClassName('menu-option-active')) li2.removeClassName('menu-option-active');
			else li2.addClassName('menu-option-active');

			return false;
		};
	}
}

if ($('login_submit'))
{

	Event.observe('login_submit', 'submit', login);

	//	$('login_submit').onsubmit = function(e)
	//	{
	//		Event.stop(e);
	//		login();
	//	};
}

function removeSelectedChildElements(elem, type)
{
	$(elem).select(type).each(function(item) {
		item.remove();
	});
}

function removeAllChildsElements(elem)
{
	$(elem).descendants().each(function(item) {
		item.remove();
	});
}

function abre_ventana_chat(url_abre,ancho,alto)
{
	window.open(url_abre,'Infra_Chat',"scrollbars=yes,toolbar=no,status=no,menubar=no,location=no,resizable=no,width="+ancho+",height="+alto,'true');
}

function cambiar_leido(obj_id, control){
	
	var url = uri.base+'/clientes/tacciones.php';
	var pars = 'id='+obj_id;
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: function(){
		if (control.checked) $(obj_id).removeClassName('ticket-unread');
		else $(obj_id).addClassName('ticket-unread');
	}})
}

function logout()
{
	var url = uri.base+'/ax/logout.php';
	var pars = '';
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: function(){
		$('login-option').setStyle({'display':'block'});
		$('user-name').update('');
		if ($('login-option').hasClassName('menu-option-active')) $('login-option').removeClassName('menu-option-active');
		$('logued-option').setStyle({'display':'none'});
	}})
}

function login(event)
{
	Event.stop(event);

	removeAllChildsElements($('login-message'));
	$('login-message').insert(ajax_loading);

	var url = uri.base+'/ax/login.php';
	var pars = 'user='+escape($F('login-user'))+'&pass='+escape($F('login-pass'));
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: login_response})
}

function login_response(o)
{
	var data = o.responseText;

	removeAllChildsElements($('login-message'));

	if (data!=0)
	{
		$('login-option').setStyle({'display':'none'});
		$('user-name').update(data);
		$('logued-option').setStyle({'display':'block'});
		$('logued-option').addClassName('menu-option-active');
	} else {
		$('login-message').update('<span>Datos incorrectos, intente nuevamente!</span>')
	}
}

function center_object(obj)
{
	if(obj != null)
	{
		if (typeof window.innerHeight != 'undefined')
		var screen_size = window.innerHeight;
		else
		var screen_size = document.documentElement.clientHeight;

		var scroll = obj.cumulativeScrollOffset();
		var top = Math.round(scroll[1] + ((screen_size - obj.getHeight())) / 2);

		var margin = obj.getWidth() / 2;

		obj.setStyle({'position' : 'absolute', 'top' : top+'px', 'left' : '50%', 'marginLeft' : '-'+margin+'px'});
	}
}

/* scripts viejos */

function checkValidWhois(control)
{
	var Dominio =control.value;
	if(!checkValidDomain(Dominio,false))
	{
		alert(Dominio+' No es un Dominio válido');
		control.focus();
		return false;
	}else{
		var aux=new String(Dominio);
		var puntos=aux.split(".");
		if(puntos.length>2)
		{
			alert('Formato de dominio no válido. Solo dominio de primer nivel');
			return false;
		}
	}
	return true;
}

function checkValidDomain(campo_dominio,control_punto)
{
	var specialChars="/\\(\\)><@,;:\\\\\\\"\\\\[\\]_*?¿'!¡{|}ºª<>Ññ?#&%¬=%`´";
	var validChars="\[^\\s" + specialChars + "\]";
	var atom=validChars + '+';
	var atomPat=new RegExp("^" + atom + "$");
	var valor_campo = campo_dominio;

	// 1 No vacio
	if ( valor_campo == "" ) 			     {if(control_punto)alert("Falta nombre de Dominio");  return false;}

	// 2 Sin Metacaracteres solo de 0 - 9 a - z todo miniuscula

	if ( valor_campo.search(atomPat)==-1)    {if(control_punto)alert("El Nombre de Dominio tiene caracteres no permitidos");  return false;}

	// No empezar con -
	if ( valor_campo.substr(0,1) == "-" ) {if(control_punto)alert("Nombre Dominio no puede comenzar con - ");  return false;}

	// No terminar con -
	b = valor_campo.length - 1;
	// var.substring ( start, stop )
	// arranca en el numero mas chico y termina en el mas grande
	//a = valor_campo.substring( b, b-1);

	if ( valor_campo.substr( b , 1 ) == "-" ){if(control_punto)alert("Nombre Dominio no puede terminar con - ");  return false;}

	// No puede tener .
	if (control_punto && valor_campo.search(".")==-1) 	{if(control_punto)alert("Nombre Dominio no puede tener . "); return false;}

	for ( i = 0 ; i < valor_campo.length ; i++)
	{
		var n;                                  //Declara la variable.
		n = valor_campo.charCodeAt(i);

		var numero = false;
		var letra  = false;
		var menos  = false;

		numero = esnumero(valor_campo.substr(i,1));
		letra  = esletra(valor_campo.substr(i,1));

		if( n == 45 || (n==46 && control_punto==false))//guion y punto
		{
			menos = true;
		}

		if( numero == false && letra == false && menos == false )
		{
			if(control_punto)alert("Hay caracteres no permitidos en el nombre del Dominio");
			return false;
		}
	}

	return true;
}

function esnumero(digito)
{
	var e = digito.charCodeAt(0);
	if( ( e >= 48 )  && ( e <= 57 ) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function esletra(digito_l)
{
	var e = digito_l.charCodeAt(0);
	if((e>=65 && e<=90) || ( e >= 97   &&  e <= 122 ) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function OpenBrWindow(theURL,winName,features, myWidth, myHeight, isCenter)
{ //v3.0
	if(window.screen)if(isCenter)if(isCenter=="true")	{
		var myLeft = (screen.width-myWidth)/2;
		var myTop = (screen.height-myHeight)/2;
		features+=(features!='')?',':'';
		features+=',left='+myLeft+',top='+myTop;
	}
	msgWindow=open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);

	if (msgWindow.opener == null) msgWindow.opener = self;
}


/* fin scripts viejos */