var Page = {
	'FindControl' : function(controlID) {
		if (typeof(controlID) == 'object') {
			return controlID;
		}

		var id = controlID;


		if (document.getElementById)
		{
			obj = document.getElementById(id);
		}

		if ( !obj && document.all)
		{
			obj = document.all[id];
		}

		if( !obj && document.forms[0] )
		{
			obj = document.forms[0][id];
		}

		return obj;
	},
	'Download' : function (){
		window.location = "index.php" + window.location.search + "&action=download";
	},
	'Popup' : function (url,w,h, winName, resizable, denyscroll){

		t = (screen.height - h) / 2;
		l = (screen.width - w) / 2;
		
		if(resizable) resizable = ', resizable=yes';
		else var resizable = '';
		
		if (denyscroll) denyscroll = ', scrollbars=no';
		else var denyscroll = '';

		if(winName){
			wind = window.open(url,winName,"width="+w+",height="+h+",top="+t+",left="+l+",scrollbars=yes,status=yes" + resizable);
			return;
		}
		wind = window.open(url,"","width="+w+",height="+h+",top="+t+",left="+l+",scrollbars=yes,status=yes" + resizable + denyscroll);
	},
	'GetAbsolutePosition' : function(control) 
	{
		/*
			Devuelve el rectángulo que ocupa
			el objeto dado, en posición absoluta.
		*/
	
		var ret = { 'top' : 0, 'right' : 0, 'bottom' : 0, 'left' : 0 };
		
		if( control ) 
		{
			ret.top = control.offsetTop;
			ret.left = control.offsetLeft;
			
			var p = control;
			
			while( p.offsetParent ) 
			{
				p = p.offsetParent;
				
				ret.top += p.offsetTop;
				ret.left += p.offsetLeft;
			}
			
			ret.bottom = ret.top + control.offsetHeight;
			ret.right = ret.left + control.offsetWidth;
		}
		
		return ret;
	}
}

var Picker = {
	'Display' : function (entity, classpath, field){
		var url = "index.php?module=admin/picker&field="+field+"&entity=" + entity + "&classpath=" + classpath;
		Page.Popup(url, 500, 300);
	},
	'Display_auto' : function (entity, classpath, field){
		var url = "index.php?module=admin/picker_auto&field="+field+"&entity=" + entity ;
		Page.Popup(url, 500, 300);
	},

	'SetValue' : function (key,value){
		var fieldname = Page.FindControl("fieldname").value;
		opener.Page.FindControl(fieldname+"_hidden").value = key;
		opener.Page.FindControl(fieldname).value = value;

		try {
			opener.Page.FindControl(fieldname+"_hidden").onchange();
		} catch(e){}
	},

	'SetValue_auto' : function (key, value){

		var fieldname = Page.FindControl("fieldname").value;

		opener.Page.FindControl(fieldname+"_ID").value = value;
		opener.Page.FindControl(fieldname).value = key;

		try {
			opener.Page.FindControl(fieldname+"_hidden").onchange();
		} catch(e){}

	},
	'SaveValue' : function (url,params,fnc){
		//prompt('ver',params);
		xmlhttpPost(
			url,
			params,
			fnc
		);
	}


}

var Validator = {

	'OnlyNumbers' : function (obj){
		val = obj.value;
		aux = "";
		if (isNaN(val)){
			for(i=0 ; i < val.length ; i++){
				if(!isNaN(val[i])){
					aux = aux.concat(val[i]);
				}
			}
			var res = parseInt(aux);
			obj.value = isNaN(res)?0:res;
		}

		parseInt(obj.value);
	},
	'OnlyDecimals' : function (obj){
		val = obj.value;
		aux = "";
		hasdot = false;
		if (isNaN(val)){
			for(i=0 ; i < val.length ; i++){
				if ((val[i] == ".") && (i > 0) && !hasdot){
					aux = aux.concat('.');
					hasdot = true;
					continue;
				}
				if(!isNaN(val[i])){
					aux = aux.concat(val[i]);
				}
			}
			obj.value = aux;
		}
	},
	'OnlyPhone' : function (obj){
		val = obj.value;
		valAux = val.replace("-","");
		
		aux = "";
		hasdash = false;
		if (isNaN(valAux)){
			for(i=0 ; i < val.length ; i++){
				if ((val[i] == "-") && (i > 0) && !hasdash){
					aux = aux.concat('-');
					hasdash = true;
					continue;
				}
				if(!isNaN(val[i])){
					aux = aux.concat(val[i]);
				}
			}
			obj.value = aux;
		}
	}

}

var WebControls = {
	'DatePicker' : function (name){

	},
	'ManageImages' : function (entity, id, action, baseurl){

		Page.Popup(baseurl+"index.php?module=admin/imagemanager&entity="+entity+"&id="+id,600,400);
	},

	'ManageImagesAuto' : function (entity, id, action, fkName, baseurl){

		Page.Popup(baseurl+"&entity="+entity+"&idfk="+id+"&action="+action+"&fkName="+fkName,750,500);
	}
}

var LocalizedBox = {

	'Display' : function (imgID,divID, baseurl){

		objDiv = Page.FindControl(divID);

		if (objDiv.style.display == "inline") {
			LocalizedBox.Hide(imgID, divID, baseurl);
			return;
		}

		Page.FindControl(imgID).src = baseurl+"project/images/languages/flags/hideflags.gif";
		objDiv.style.display = "inline";
	},

	'Hide' : function (imgID,divID, baseurl){
		Page.FindControl(imgID).src = baseurl+"project/images/languages/flags/displayflags.gif";
		Page.FindControl(divID).style.display = "none";
	}

}

var StringHelper = {
	'Decode' : function (str){
		str = str.replace("&aacute;","á");
		str = str.replace("&eacute;","é");
		str = str.replace("&iacute;","í");
		str = str.replace("&oacute;","ó");
		str = str.replace("&uacute;","ú");
		str = str.replace("&ntilde;","ñ");
		return (str);
	}
}

function changeProfile(obj){
	window.location = "index.php?module=admin/changeprofile&idProfile=" + obj.value;
}

function saveAndClone(){
	objForm = document.forms[0];
	objForm.clone.value = 1;
	objForm.submit();
}

function saveAndBack(){
	objForm = document.forms[1];
	objForm.back.value = 1;
	objForm.submit();
}


var Panel = {
	'manageDiv' : function(id,self){
		var obj = Page.FindControl(id);
		if(obj.className=='div-show'){
			obj.className='div-hide';
			self.innerHTML = '&#9660;';
		}else{
			obj.className='div-show';
			self.innerHTML = '&#9650;';
		}
	}
}

var MultiSelect = {
	'Display' : function (entity, classpath, idEntity, className, table, fkey){
		var url = "index.php?module=admin/multiselect&idEntity="+idEntity+"&entity="+entity+"&classpath="+classpath+"&class="+className+"&table="+table+"&fkey="+fkey;
		Page.Popup(url, 500, 300);
	}
}

function submitForm(name, action) {
	if(!$(name)) {
		return;
	}
	$(name).action = action;
	
	$(name).submit();
}

function swapDeleteImg(linkDel, name) {

	if(!linkDel.delteImg) {
		linkDel.delteImg = 'no';
	}
	
	if(linkDel.delteImg == 'no') {
		$(name+'_view').oldDilplay = $(name+'_view').style.display;
		$(name+'_view').style.display = 'none'; 
		$(name+'_delete').value = 'yes';
		$(name+'_img').src = _siteURL+'project/images/backend/icons/bin.gif';
		linkDel.delteImg = 'yes';
		
	} else {
		$(name+'_view').style.display = $(name+'_view').oldDilplay ; 
		$(name+'_delete').value = 'no';
		$(name+'_img').src = _siteURL+'project/images/backend/icons/icon_cancel.png';
		linkDel.delteImg = 'no';
		
	}

	
			
}


