var imagenAjax = 'graphics/loading.gif';

function getCookie(name){
	var cname = name + "=";               
	var dc = document.cookie;             
	if (dc.length > 0) {        
		begin = dc.indexOf(cname);       
		if (begin != -1) {           
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1) end = dc.length;
			return unescape(dc.substring(begin, end));
		} 
	}
	return null;
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) expires = expires * 1000 * 60 * 60 * 24;
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function eraseCookie(name){ Set_Cookie(name,"",-1); }

function isdefined(objToTest) {
	if (null == objToTest) return false;
	if ("undefined" == typeof(objToTest)) return false;
	return true;
}

function isFunction(a){ return typeof a == 'function'; }

function hss(objToProcess, status){ 
	try { document.getElementById(objToProcess).style.display=status;} 
	catch(err){	alert("EXHSS: "+objToProcess);}
}

function gvv(objToProcess){ 
	try { return document.getElementById(objToProcess).value;} 
	catch(err){	alert("EXGVV: "+objToProcess);}
}

function goo(objToProcess){ 
	try { return document.getElementById(objToProcess);} 
	catch(err){	alert("EXGOO: "+objToProcess);}
}

function svv(objToProcess, strValue){ 
	try { document.getElementById(objToProcess).value = strValue;} 
	catch(err){	alert("EXSVV: "+objToProcess);}
}

function sii(objToProcess, strValue){ 
	try { document.getElementById(objToProcess).innerHTML = strValue; } 
	catch(err){	alert("EXSII: "+objToProcess);}
}

function gii(objToProcess){ 
	try { return document.getElementById(objToProcess).innerHTML; } 
	catch(err){	alert("EXGII: "+objToProcess);}
}

function pm(name, enlace){
	if (!isdefined(enlace)) enlace='marketext.com';
	document.write('<a href=mailto:' + name + '@' + enlace + '>' + name + '@' + enlace + '</a>');
}

function modalClose (dialog) {
	dialog.data.hide('fast', function () {
		dialog.container.hide('fast', function () {
			dialog.overlay.fadeOut('slow', function () {
				closeModal();
			});
		});
	});
}

function closeModal(){
	$.modal.close();
}

function goURL(url, params){
	if (!isdefined(params)) params='';
	location.href= url + '?access=' + $("#access").attr("value") + params;
}

function IsNumeric(sText, ValidChars){
	if (!isdefined(ValidChars)) ValidChars = "0123456789.";
	var IsNumber=true;
	if (sText.length == 0) return false;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		var Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}

Array.prototype.inArray = function (value) {
  var i = this.length;
  if (i > 0) {
	 do {
		if (this[i] === value) return true;
	 } while (i--);
  }
  return false;
}

function emailCheck (emailStr) { 
	var emailPat=/^(.+)@(.+)$/; 
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; 
	var validChars="\[^\\s" + specialChars + "\]"; 
	var quotedUser="(\"[^\"]*\")"; 
	var ipDomainPat=/^[(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})]$/; 
	var atom=validChars + '+'; 
	var word="(" + atom + "|" + quotedUser + ")"; 
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); 
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 
	var matchArray=emailStr.match(emailPat); 
	if (matchArray==null) return false;
	var user=matchArray[1] ;
	var domain=matchArray[2] ;
	if (user.match(userPat)==null) return false ;
	var IPArray=domain.match(ipDomainPat) 
	if (IPArray!=null) { 
	    for (var i=1;i<=4;i++) if (IPArray[i]>255) return false ;
	    return true ;
	} 
	var domainArray=domain.match(domainPat) ;
	if (domainArray==null) return false; 
	var atomPat=new RegExp(atom,"g") ;
	var domArr=domain.match(atomPat) ;
	var len=domArr.length ;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) return false ;
	if (len<2) return false; 
	return true; 
} 

function valalfa(e){
	tecla = (document.all) ? e.keyCode : e.which;  if (tecla == 8) return true;
    patron = /[A-Za-z0-9 ]/; te = String.fromCharCode(tecla);
    return patron.test(te);
}

function valnum(e){
    tecla = (document.all) ? e.keyCode : e.which;  if (tecla == 8) return true;
    patron = /[0-9]/;  te = String.fromCharCode(tecla);
    return patron.test(te);
}

function valfloat(e){
    tecla = (document.all) ? e.keyCode : e.which;  if (tecla == 8) return true;
    patron = /[0-9.]/;  te = String.fromCharCode(tecla);
    return patron.test(te);
}

function bl_enter(e) {
    tecla = (document.all) ? e.keyCode : e.which;  
	if (tecla == 13) return false;
}

function bl_space(e) {
    tecla = (document.all) ? e.keyCode : e.which;  
	if (tecla == 32) return false;
}

function getFormData(formName){
	var frmData = "";
	for (i=0; i<goo(formName).elements.length; i++) { 
		objeto = goo(formName).elements[i];
		if (isdefined(objeto.id)){
			if (objeto.type=="checkbox" || objeto.type=="radio" ){
				valor=(objeto.checked)?1:0;
				frmData+="&"+objeto.id+"="+valor;
			}else
				frmData+="&"+objeto.id+"="+objeto.value;
		}
	}
	return frmData;
}

function checkboxes(name) {
	var sel_checks = new Array();
	var elts_a= document.getElementsByName(name);
	var elts_cnt_a  = (typeof(elts_a.length) != 'undefined')?elts_a.length:0;
	if (elts_cnt_a) {
		counter_a = 0;
		for (var i_a = 0; i_a < elts_cnt_a; i_a++) {
			if ( elts_a[i_a].checked) {
				sel_checks[counter_a] = elts_a[i_a].value;
				counter_a++;
			};
		};
	};
	return sel_checks;
};

function addOption(id,text,value){
	var list = goo(id); 
	var long = list.length; 
	list[long] = new Option(text, value); 
}

function displayDate(){
	var now = new Date();
	laCookie = getCookie("idioma");
	if (laCookie==null) laCookie = "en";
	
	if (laCookie=="es"){
	  var dias = new Array('Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado');
	  var meses = new Array('Enero',   'Febrero',   'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre',
						  'Octubre', 'Noviembre', 'Diciembre');}
	if (laCookie=="en"){
	  var dias = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	  var meses = new Array('January',   'February',   'March', 'April', 'May', 'June', 'July', 'August', 'September',
						  'October', 'November', 'December');}
	if (laCookie=="de"){
	  var dias = new Array('Sonntag ', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag');
	  var meses = new Array('Januar',   'Februar',   'März', 'April', 'Mag', 'Juni', 'Jule', 'August', 'September',
						  'Oktober', 'November', 'Dezember');}
						  
	var fecha = ((now.getDate() < 10) ? "0" : "") + now.getDate();
	
	function setYear2K(ano){
		return (ano < 1000) ? ano + 1900 : ano;
	}
	
	fechaactual = dias[now.getDay()] + ", " + meses[now.getMonth()] + " " + fecha + " - " + (setYear2K(now.getYear()));
	$("#system_date").html(fechaactual + " - ");
	WorkingClock();
}

function WorkingClock() {
    var digital = new Date();
    var horas = digital.getHours();
    var minutos = digital.getMinutes();
    var segundos = digital.getSeconds();
    var apm = "AM";
    if (horas > 11) apm = "PM";
    if (horas > 12) horas = horas - 12;
    if (horas == 0) horas = 12;
    if (minutos <= 9) minutos = "0" + minutos;
    if (segundos <= 9) segundos = "0" + segundos;
    dispTime = horas + ":" + minutos + ":" + segundos + " " + apm;
	$("#pendule").html(dispTime);
    setTimeout("WorkingClock()", 1000);
}

function capLock(e, obj, text){
	kc = (e.keyCode)?e.keyCode:e.which; sk = (e.shiftKey)?e.shiftKey:((kc==16)?true:false);
	if (((kc>=65&&kc<=90)&&!sk) || ((kc>=97&&kc<=122)&&sk)){
        if($(obj).data("qtip")){
			$(obj).qtip("show");
		}else{
			$(obj).qtip({
				content: { text: "<p align='center'><img src='graphics/attention.png'>&nbsp;<strong>" + text },
				show: { when: false, ready: true },
				hide: false, 
				position: { corner: { target: 'leftTop', tooltip: 'bottomLeft' } },
				style: {  width: 250, padding: 5, 
				border: { width: 2, radius: 5, color: '#FDDB64'},tip: 'bottomLeft' }
			});
		}
	}else{
        if($(obj).data("qtip")) $(obj).qtip("hide");
	}
}

function removeCapTip(obj){
    if ($(obj).data("qtip")) $(obj).qtip("hide");
}

function LoadDoc(section, idioma, obj){
	if ($("#" + section + "Div").css('display')=='block') {
		$("#" + section + "Div").css({'display':'none'});
		$(obj).css({'background-image':'url(graphics/dn_arrow.gif)'});
		return false;
	}
	$("#" + section + "Div").css({'display':'block', 'text-align':'center', 'padding':'10px'});
	$("#" + section + "Div").html("<img src='" + imagenAjax + "' /> ... Loading / Cargando ... <img src='" + imagenAjax + "' />");
	srcAjax = 'documentation-'+idioma+'.php'; 
	$.ajax({
		type: "POST",
		url: srcAjax,
		data: "&section=" + section,
		error: function(objeto, errData1, errdata2){
			alert("Communications error, please try again");
		},
		success: function(datos){
			$(obj).css({'background-image':'url(graphics/up_arrow.gif)'});
			$("#" + section + "Div").css({'text-align':'left', 'padding':'10px'});
			$("#" + section + "Div").html(datos);
		}
	});
	return false;
}

function copy(elemento) {
	elemento = document.getElementById(elemento);
	if (elemento.createTextRange) {
		rango = elemento.createTextRange(); if (rango) rango.execCommand('Copy');
	} else {
		flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(elemento.value)+'" width="0" height="0" type="application/x-shockwave-flash"><\/embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
}
function ut(objn, tid,frm,taid,parameters,cambiacomillas){
	frm = frm.replace(/¬c¬/g,"'");
	p = parameters.split(',');
	for (n=0;n<p.length;n++){
		if (n>=p.length) break;
		eval("frm = frm.replace(/"+p[n]+"/,'"+document.getElementById("it"+tid+p[n]).value+"');");
	}
	if (frm.substring(0,7) == 'require')
		document.getElementById("ti"+tid).value = frm;
	else if(frm.substring(0,8)=='datagrid')
		document.getElementById("ti"+tid).value = objn + " = new "+frm;
	else
		document.getElementById("ti"+tid).value = objn + " -> "+frm;
}

function up(objn, tid,frm,taid,parameters,cambiacomillas){
	if (cambiacomillas){
		bs = frm.replace(/¬c¬/g,'"'); var c='\"';
	}else{
		bs = frm.replace(/¬c¬/g,"'"); var c="\'";
	}
	p = parameters.split(',');
	for (n=0;n<p.length;n++){
		if (n>=p.length) break;
		if (cambiacomillas)
			eval("bs = bs.replace(/"+p[n]+"/,\""+document.getElementById("it"+tid+p[n]).value+"\");");
		else
			eval("bs = bs.replace(/"+p[n]+"/,'"+document.getElementById("it"+tid+p[n]).value+"');");
	}
	bss=bs.substring(0,4);
	if(bss=='true' || bss=='fals' || bss=='arra')
		document.getElementById("ti"+tid).value = objn + " -> "+ frm + " = "+bs+";";
	else{
		document.getElementById("ti"+tid).value = objn + " -> "+ frm + " = "+c+bs+c+";";
	}
}

function ua(id,valor, taid, objname, fnname){
	document.getElementById(taid).value = objname+ " -> " + fnname + "['"+document.getElementById(id+"k").value+"'] = '"+valor+"';";
}

function us(id,valor, taid, objname, fnname){
	vlr = valor.split('::'); document.getElementById(id+"t").value = vlr[1];document.getElementById(id+"k").value = vlr[0];
	document.getElementById(taid).value = objname+ " -> " + fnname + "['"+vlr[0]+"'] = '"+vlr[1]+"';";
}

