/*@date:Dic 2006
@copyright:2006 visual knock out
@site:http://www.visualko.com.ar
@email:info@visualko.com.ar*/
js = {
	init:function(){
		this.call('content',1,'page.php?');	
		suff = this.gParam("suff");	
		idsec = this.gParam("idsec");
		idsecparent = this.gParam("idsecparent");
		lang = this.gParam("lang");
		js.flash.call("menu0","flash/menu[0].swf?suff="	+	suff	+	"&idsecparent=" + idsecparent + "&lang=" + lang,750,24,"#FFED9C");
		js.flash.call("menu1","flash/menu[1].swf?suff="	+	suff	+	"&idsec=" + idsec + "&idsecparent=" + idsecparent + "&lang=" + lang,162,235,"#FFED9C");
		window.onscroll = this.handler.scroll;
	},
	reload : function(){
		return location.href = '?idsec='+this.gParam("idsec");
	},
	sub:{
		page:function(id){js.sParam("idsec",id);js.call("content",2,"page.php?");},
		call:function(id){js.sParam("idsec",id);js.sParam("idsecparent",id);suff = js.gParam("suff");	lang = js.gParam("lang");js.flash.call("menu1","flash/menu[1].swf?suff=" + suff + "&idsecparent=" + id + "&lang=" + lang,162,235,"#FFED9C");js.call('content',2,'page.php?');}},
	lang:{
		active:"",
		set:function(lang){
			if(js.gParam("lang") == lang)
				return;
			js.gByFormName("lang").value = lang; 	
			js.sParam("lang",lang);
			js.init()
		}
	},
	param:[],
	sParam:function(a,b){js.param[a] = b},
	gParam:function(a){return js.param[a];},
	gAllParam:function(){s='';a=js.param;for(var i in a){s+= '&'+i+'='+a[i];}return s;},
	gById:function(a){return document.getElementById(a);},
	gByName:function(a){return document.getElementsByName(a);},
	gByTagName:function(a){return document.getElementsByTagName(a);},
	gByFormName:function(a){return document.forms[0].elements[a];},
	keycapture:function(evt){var c = document.layers ? evt.which:document.all ? event.keyCode:evt.keyCode;this.keycode = c;this.keyvalue = String.fromCharCode(c);return true;},
	servername:function(){return window.location.protocol + '//' + window.location.hostname;},
	status:{},
	elem:{},
	chkParam:function(idcm){if(idcm)this.sParam("idcm",idcm);	this.sParam("idsrch",js.gById('search') != null ? 1:0);},
	xmlhttp:{},
	call:function(target,idcm,url){	this.chkParam(idcm);this.tab.active = {};	this.status = this.gById("status"); this.elem = this.gById(target); try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch(e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e2) { xmlhttp = false; }}if(!xmlhttp && typeof XMLHttpRequest != 'undefined') { 	xmlhttp = new XMLHttpRequest();	}	this.xmlhttp = xmlhttp; this.status.style.top = parseInt(document.documentElement.scrollTop + 1,10); this.status.style.display = "inline"; url += 	this.gAllParam();	this.xmlhttp.open("GET",url, true);	xmlhttp.onreadystatechange = this.onreadystate;	this.xmlhttp.send(null);},
	onreadystate:function(){ if(js.xmlhttp.readyState == 4){ js.elem.innerHTML = js.xmlhttp.responseText; js.status.style.display = "none";}},	
	list:{
		search:function(id){var e = js.gByName("srch");	wher = [];for(var i=0;i<e.length;i++){if(e[i].value.length){wher.push(e[i].id+","+e[i].value);}}js.sParam("idls",id);js.sParam("wher",wher.join(";"));js.sParam("exac",js.gByFormName("exac").checked);if(!wher.length){return this.pag(1);}return js.call('list',3,'page.php?&pg=1');},
		pag:function(pg){	js.call("list",0,"page.php?pg="+pg);},
		active :{},
		setting:{
			col:{mouseover:"#006600",mouseout:"black",click:"white"},
			bg:{mouseover:"#B5EDBC",mouseout:"#F1F1F1",	click:"#003300"}
		},
		mouseover:function(row){if(this.active == row && row.opened){	return;}row.style.color = this.setting.col.mouseover;row.style.background = this.setting.bg.mouseover;},
		mouseout:function(row){if((this.active == row && row.opened) ||(row.opened)){return;}row.style.color = this.setting.col.mouseout;row.style.background = this.setting.bg.mouseout;},
		click:function(row){target = "frm_"+row.id;row.style.color = this.setting.col.click;row.style.background = this.setting.bg.click;	if(this.active.id != undefined && this.active != row){if(this.active.opened){	this.active.style.color = this.setting.col.mouseover;	this.active.style.background = this.setting.bg.mouseover;}else {this.active.style.color = this.setting.col.mouseout;this.active.style.background = this.setting.bg.mouseout;}}if(typeof row.opened == "undefined"){	js.gById(target).style.display = "block";	js.sParam("id",row.id);	js.call(target,0,"page.php?view=form");	row.opened = true;}	else if(row.opened == true){js.gById(target).style.display = "none";row.opened = false;	}	else {js.gById(target).style.display = "block";row.opened = true;}this.active = row;}
	},
	flash:{
		call:function(target,src,w,h,bg){	var so = new SWFObject(src, js.gParam("suff"), w, h, "7", bg);so.addVariable("ie", "1");so.addVariable("app", "");so.addVariable("id", "");	so.addParam("scale", "noscale");so.addParam("swliveconnect", "true");so.write(target);}
	},
	tab:{
		active:{},
		swap:function(tab){if(this.active == tab){return;}if(this.active.id != undefined){this.active.className = "off";idlayact = this.active.id.replace('tab','lay');}else{js.gById('tab0').className = "off";idlayact = 'lay0';}layact = js.gById(idlayact);layact.style.display = 'none';tab.className = "on";idlay = tab.id.replace('tab','lay');lay = js.gById(idlay);lay.style.display = 'inline';this.active = tab;}
	},
	handler:{
		scroll:function(){js.gById("status").style.top = parseInt(document.documentElement.scrollTop + 1,10);}
	},
	util:{
		vswap:function(id,div,name,restore){a = js.gById(id);	if(a.style.display == 'none'){display = 'block';div.innerHTML = restore;}	else {display = 'none';	div.innerHTML = name;	}	a.style.display = display;},
		screenWidth:function(){w = null;if(self.innerWidth){	w = self.innerWidth;}else if(document.documentElement && document.documentElement.clientWidth){w = document.documentElement.clientWidth;}else if(document.body){w = document.body.clientWidth;}return w;},
		screenHeight:function(){h = null;if(self.innerWidth){h = self.innerHeight;}else if(document.documentElement && document.documentElement.clientWidth){	h = document.documentElement.clientHeight;}else if(document.body){h = document.body.clientHeight;}return h;},
		posX:function(obj){var curleft = 0;if(obj.offsetParent){while(obj.offsetParent){curleft += obj.offsetLeft;obj = obj.offsetParent;}}else if(obj.x){curleft += obj.x;	}return curleft;},
		posY:function(obj){var curtop = 0;if(obj.offsetParent){while(obj.offsetParent){curtop += obj.offsetTop;obj = obj.offsetParent;}}	else if(obj.y){curtop += obj.y;}	return curtop;}
	},
	validator : {
		fields : ['checkbox','text','select-one','textarea','file','radio'],
		LANG_MAP : ['es','en'],
		FIELD_MUST_HAVE_VALUE : ['Esta solicitud no puede tener campos vacios. Por favor, complete el siguiente campo.','This form cannot have empty fields. Please, check this field for a value.'],
		RADIO_MUST_BE_CHECKED : ['Por favor, elija una opcion.','Please, choose an option.'],
		getIdLang : function(){
			var lang = js.gParam('lang');
			for(var e=0; e < this.LANG_MAP.length;e++){
				if(this.LANG_MAP[e] == lang){
					return e;
				}
			}
		},
		getCheckedValue : function (radioObj) {
			if(!radioObj)
				return "";
			var radioLength = radioObj.length;
			if(radioLength == undefined)
				if(radioObj.checked)
					return radioObj.value;
				else
					return "";
			for(var i = 0; i < radioLength; i++) {
				if(radioObj[i].checked) {
					return radioObj[i].value;
				}
			}
			return "";
		},
		chkNonEmpty : function(form){
			if(typeof form.elements != 'object'){
				return;
			}
			
			var empty = false;

			var idlang = this.getIdLang();
			
			for(i=0; i < form.elements.length; i++){

				var field = form.elements[i];
				
				for(var j in this.fields){

					var type = this.fields[j];

					if(type == field.type){

						if(type == 'radio'){
							
							var check = this.getCheckedValue(js.gByName(field.name));
							
							if(!check.length){
								alert(this.RADIO_MUST_BE_CHECKED[idlang]);
								return field.focus();
							}

						} else {
							
							if(!field.value.length){
								
								if(typeof field.opcional != "undefined"){
									continue;
								}
	
								empty = true;
								alert(this.FIELD_MUST_HAVE_VALUE[idlang]);
								return field.focus();
							}
						}
					}
				}
			}

			if(empty == false){
				form.submit();
			}
		}
	}
};
