/***************************************************************************
* GNU/GPL KumbiaForms - PHP Rapid Develpment Framework
* Simple Object Manipulation Base Functions
****************************************************************************
* (c) 2007 Andres Felipe Gutierrez <andresfelipe at vagoogle.net>
****************************************************************************/

var dummy = function(){}

//Obtiene una referencia a un ob
function $O(obj){
	return getObj(obj);
}

//Obtiene el Option de un SELECT que esta seleccionado
function selectedItem(obj){
	for(var i = 0;i<=obj.options.length-1;i++){
		if(obj.options[i].selected) return obj.options[i]
	}
}

function $S(obj){
	return selectedItem($(obj))
}

//Redirecciona la Ventana padre a un accion determinada
function redirect_parent_to_action(url){
	redirect_to_action(url, window.parent);
}

//Redirecciona una ventana a un url definido
function redirect_to_action(url, win){	
	win = win ? win : window;		
	win.location = $Kumbia.path + url
}

// Obtiene una referencia a un objeto del formulario generado
// o un document.getElementById
function getObj(obj){
	if(document.getElementById("flid_"+obj)){
		return document.getElementById("flid_"+obj);
	} else return document.getElementById(obj);
}

//Oculta un Objeto en cuanto a visibilidad
function hideObj(obj){
	if(document.getElementById(obj)){
		document.getElementById(obj).style.visibility = "hidden";
		if(document.getElementById(obj).style.display=='block'){
			document.getElementById(obj).style.display = "none";
		}
	}
}

//Muestra un Objeto en cuanto a visibilidad
function showObj(obj){
	if(document.getElementById(obj)){
		document.getElementById(obj).style.visibility = "visible";
		if(document.getElementById(obj).style.display=='none'){
			document.getElementById(obj).style.display = "block";
		}
	}
}

//Obtiene el Objeto seleccionado en un SELECT
function thisOption(obj){
	return obj.options[obj.selectedIndex];
}

//Crea un objeto con nombre name y tipo TYPE
function newObj(type, name){
	var a =  document.createElement(type);
	if(name) a.id = name;
	return a;
}

//Concatena un objeto a otro (append)
function appendObj(oParent, obj){
	document.getElementById(oParent).appendChild(obj);
}

/****************************************************
* Auth Functions
****************************************************/
//Funcion que envia un formulario via AJAX
function ajaxRemoteForm(action, up, callback, md5){
	params = "";
	action = action+"/"
	if(md5==undefined) md5 = ""
	FormObjects = eval("FormObjects"+md5)
	if(!FormObjects){
		alert("Hay un problema enviando el formulario")
	}
	value = ""
	for(var i=0;i<=FormObjects.length-1;i++){
		if(FormObjects[i]!=""){
			value = $(FormObjects[i]).value.replace("?", "")
			value = value.replace("&", "")
			params+= FormObjects[i]+"="+value+"&";		
		}		
	}		
	AJAX.viewRequest(
	{
		action: action,
		parameters: params,
		container: up,
		callbacks: callback
	}
	);
}

function submitRemoteForm(action, up, components, callback){
	params = "";
	action = action+"/"		
	FormObjects = components
	if(!FormObjects){
		alert("Hay un problema enviando el formulario")
	}
	value = ""
	for(var i=0;i<=FormObjects.length-1;i++){		
		if(FormObjects[i]!=""){
			value = $(FormObjects[i]).value.replace("?", "")
			value = value.replace("&", "")
			params+= FormObjects[i]+"="+value+"&";		
		}		
	}			
	AJAX.viewRequest(
	{
		action: action,
		parameters: params,
		container: up,
		callbacks: callback
	}
	);
}

var AJAX = new Object();

AJAX.base = function(){
	try {
		if(window.XMLHttpRequest) {
			this.response = new XMLHttpRequest();
			// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			if(new ActiveXObject('Msxml2.XMLHTTP')){
				this.response = new ActiveXObject('Msxml2.XMLHTTP')
			} else {
				this.response = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return this.response;
	}
	catch (e){
		alert("KumbiaError: There was a problem while creating XMLHTTP Object");
		return null
	}
}

AJAX.xmlRequest = function(params){
	if(!params.url && params.action) 
		this.url = $Kumbia.path + params.action
	if(params.parameters){
		this.url+= "/&"+params.parameters
	}		
	if(params.debug){
		alert(this.url)
	}
	if(this.action) this.action = params.action;	
	if(params.asynchronous==undefined) {
		this.asynchronous = true
	} else this.asynchronous = params.asynchronous
	this.oncomplete = params.oncomplete;
	this.callbacks = params.callbacks ? params.callbacks : {};
	this.processRequest = function(http){					
		if(http.response.readyState==1){
			if(http.callbacks.before){
				if(typeof  http.callbacks.before != "string"){
					http.callbacks.before.call(this)
				} else {
					eval(http.callbacks.before)
				}
			}
		}
		if(http.response.readyState==4) {
			if (http.response.status==200) {
				if(this.oncomplete){
					if(!document.all&&http.response.responseXML==undefined){
						window.alert('KumbiaError: Se produjo un error al cargar el documento XML')
					} else {												
						this.oncomplete.call(this, http.response.responseXML)
					}
				}
				if(http.callbacks.success){
					if(typeof  http.callbacks.success != "string"){
						http.callbacks.success.call(this)
					} else {
						eval(http.callbacks.success)
					}
				}
			} else {
				alert("KumbiaError: There was a problem retrieving the XML data: " + http.response.statusText);
			}
			if(http.callbacks.complete){
				http.callbacks.complete.call(this)
			}
		}
	}
	try {
		this.response = AJAX.base();
		__function = this;
		this.response.onreadystatechange = function() { 
			__function.processRequest(__function) 
		}		
		this.response.open("GET", this.url, this.asynchronous);
		document.all ? this.response.send() : this.response.send(null);
	}
	catch(e){
		alert("KumbiaError: " + e.message + " [" + e.name + "]");
	}
}


AJAX.viewRequest = function(params){
	var __function;
	if(!params.action){
		alert("KumbiaError: Ajax Action is not set!");
		return;
	}		
	this.url = $Kumbia.path + params.action; 
	if(params.parameters){
		this.url+="&"+params.parameters;
	}			
	this.action = params.action;	
	if(params.debug){
		alert(this.action)
	}
	if(params.asynchronous==undefined) {
		this.asynchronous = true
	} else this.asynchronous = params.asynchronous
	this.container = params.container;
	this.callbacks = params.callbacks ? params.callbacks : {};
	this.scriptFragment = '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)'
	this.oldReady = 0
	__function = this;		
	this.processView = function(http){		
		if(http.response.readyState==1&&__function.oldReady!=http.response.readyState){						
			if(http.callbacks.before){
				if(typeof  http.callbacks.before != "string"){
					http.callbacks.before.call(this)
				} else {
					eval(http.callbacks.before)
				}
			}
			__function.oldReady = http.response.readyState
		}
		if(http.response.readyState==4) {
			if (http.response.status==200){
				if($(http.container)){
					Element.update(http.container, http.response.responseText)
					if(http.callbacks.success){
						if(typeof  http.callbacks.success != "string"){
							http.callbacks.success.call(this)
						} else {
							eval(http.callbacks.success)
						}
					}
				} else {
					window.alert("KumbiaError: Container Ajax Object '"+http.container+"' Not Found")
				}
				if(http.callbacks.complete){
					if(typeof  http.callbacks.complete != "string"){
						http.callbacks.complete.call(this)
					} else {
						eval(http.callbacks.complete)
					}
				}
			} else {
				alert("KumbiaError: There was a problem retrieving the View data: "+http.response.statusText);
			}
			if(http.response.overrideMimeType){
				if(http.response.status==200){
					http.response.setRequestHeader('Connection', 'close')
				}
			}
		}
	}	
	this.response = AJAX.base();				
	this.response.onreadystatechange = function() {
		__function.processView(__function)
	}
	this.response.open("GET", this.url, this.asynchronous);	
	if(document.all){
		this.response.send()
	} else this.response.send("")
}

AJAX.execute = function(params){
	var __function;
	this.onend = params.onend;
	if(!params.action){
		alert("KumbiaError: Ajax Action is not set!");
		return;
	}
	this.url = $Kumbia.path+params.action;
	if(params.debug){
		alert(this.url)
	}
	this.callbacks = params.callbacks ? params.callbacks : {};
	if(params.asynchronous==undefined) {
		this.asynchronous = true
	} else this.asynchronous = params.asynchronous
	if(params.parameters){
		this.url+="/&"+params.parameters;
	}	
	this.action = params.action;
	this.oldReady = 0
	this.processExecute = function(http){
		if(http.response.readyState==1&&__function.oldReady!=http.response.readyState){
			if(http.callbacks.before){
				if(typeof  http.callbacks.before != "string"){
					http.callbacks.before.call(this)
				} else {
					eval(http.callbacks.before)
				}
			}
			__function.oldReady = http.response.readyState
		}
		if(http.response.readyState==4) {
			if (http.response.status!=200){
				window.alert("KumbiaError: There was a problem executing the action: "+http.response.statusText);
			} else {
				if(http.onend){
					http.onend.callee ? http.onend.call() : eval(http.onend);
				}
				if(http.callbacks.success){
					http.callbacks.success.call(this)
				}
			}
			if(http.callbacks.complete){
				http.callbacks.complete.call(this)
			}
		}
	}
	this.response = AJAX.base();
	__function = this;
	this.response.onreadystatechange = function() { __function.processExecute(__function) }
	this.response.open("GET", this.url, this.asynchronous);
	document.all ? this.response.send() : this.response.send(null);
}

AJAX.query = function(qaction){	
	var me;
	new Ajax.Request($Kumbia.path+qaction, {
			asynchronous: false,
			onSuccess: function(resp){
				xml = resp.responseXML
				data = xml.getElementsByTagName("data");
				if(document.all){
					xmlValue = data[0].text	
				} else {
					xmlValue = data[0].textContent
				}
				me = xmlValue  		
			}
		}
	)
	return me
		
}

function enable_upload_file(file){	
	$(file+"_span").show()
	$(file+"_span_pre").hide()	
	if(document.all){
		$(file+"_file").click()
	}
	$(file+"_im").hide()
}

function upload_file(file){		
	$(file+"_file").id = file+"_tmp"
	$(file).id = file+"_file"
	$(file+"_file").name = file+"_file"
	$(file+"_tmp").id = file
	$(file).name = file
}

function cancel_upload_file(file){		
	$(file+"_file").id = file+"_tmp"
	$(file).id = file+"_file"
	$(file+"_file").name = file+"_file"
	$(file+"_tmp").id = file
	$(file).name = file
	$(file).selectedIndex = 0
	$(file+"_span").hide()
	$(file+"_span_pre").show()	
}

function show_upload_image(file){	
	if(file.options[file.selectedIndex].value!='@'){
		$(file.id+"_im").show()
		$(file.id+"_im").src = $Kumbia.path + "/img/" + file.options[file.selectedIndex].value
	} else {
		$(file.id+"_im").hide()
	}
}

