function Application(){
	this.contextPath = "/cdc";
	this.ajaxGif = "<img src='"+this.contextPath+"/img/ajax-gif.gif"+"' alt='Carregando informações' style='vertical-align:middle'/>";
}

navegador = navigator.appName;

isIE = (navegador == "Microsoft Internet Explorer");

app = new Application();

function createRequest(){
	var xmlHttp;
	try{
		/*Requisicao Firefox*/
 		xmlHttp=new XMLHttpRequest();
 		return xmlHttp;
 	}catch (e){
		try{
			/*Requisicao IE6*/
   			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
   			return xmlHttp;
   	    }catch (e){
			try{
				/*Requisicao IE7*/
     			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     			return xmlHttp;
     		}catch (e){
     			alert("Your browser does not support AJAX!");
     			return false;
     		}
   		}
	}
}

function Loader(){
	var elemento = document.createElement("div");
	var width = 500;
	var height = 280;
	var top = ((screen.availHeight - height) / 2);
	var left = ((screen.availWidth - width) / 2);
	var position = "fixed";

	elemento.className = "ajax-loader";
	
	elemento.style.position = position;
	
	elemento.style.width = width+"px";
	elemento.style.left = left+"px";
	elemento.style.top = top+"px";
	
	elemento.onclick=function(){
		loader.hide();
	}
	
	var setVisible=function(f){
		if(f){
			elemento.style.display = "block";
		}else{
			elemento.style.display = "none";
		}
	}
	
	this.show=function(){
		if(elemento.parentNode == null){
			document.body.appendChild(elemento);
		}
		setVisible(true);
	}
	this.hide=function(){
		setVisible(false);
	}
	
	this.clear=function(){
		elemento.innerHTML = "";
	}
	
	this.setHTML=function(html){
		elemento.innerHTML = html;
	}
}

loader = new Loader();



function AjaxRequest(option){
	
	if(option == null || typeof option == undefined){
		option = {};
	}
	
	var ajax = createRequest();
	
	var params = typeof option.params == undefined ? null : option.params;
	
	if(ajax != null){
		ajax.open("POST",option.url,true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function(){
			if(ajax.readyState == 1){
				if(option.onload){
					option.onload();
				}
			}
			if(ajax.readyState == 4 && ajax.status == 200){
				if(option.ready){
					option.ready(ajax);
				}
			}
		}
		ajax.send(params);
	}
}

function loadContent(bl,am,options){
	
	if(options == null || typeof options == undefined){
		options = {};
	}
	
	new AjaxRequest({
		url : "Control.php",
		params : "bl="+bl+"&acao="+am,
		onload : function(){
			loader.setHTML(app.ajaxGif + "Carregando página...");
			loader.show();
		},
		ready : function(ajax){
			if(options.ready){
				options.ready(ajax);
			}else{
				document.getElementById("main").innerHTML = ajax.responseText;
				loader.hide();
			}
		}
		
	});
}

function manutencao(page){
	loadContent('Manutencao','alterar&page='+page+'.tpl',{
		ready : function(ajax){
			document.getElementById("main").innerHTML = ajax.responseText;
			
//			$(function(){
//				$('#content').wysiwyg({controls:{insertImage : {visible:false}}});
//			});
			
			loader.hide();
		}
	});
}

function novaOS(){
	loadContent('OS','novaOS',{
		ready : function(ajax){
			document.getElementById("main").innerHTML = ajax.responseText;
			
			$(function(){
			    $('#descricao').wysiwyg({
				    controls : { 
				    	insertImage : { 
			    			visible : false 
			    		} 
			    	}
			    });
			});
			
			loader.hide();
		}
	});
}

function suporte(bl,am){
	new AjaxRequest({
		url : "Control.php",
		params : "bl="+bl+"&am="+am,
		onload : function(){
		loader.setHTML(app.ajaxGif + "Carregando página...");
		loader.show();
	},
	ready : function(ajax){
			document.getElementById("main").innerHTML = ajax.responseText;
			$(function(){
				$('#descricao').wysiwyg({controls:{insertImage : {visible:false}}});
			});
			loader.hide();
		}
	});
}
function configView(){
	_cont = document.getElementById("conteudo");
	_cont.style.left = ((getWidth() - 800) / 2) + "px";
}

function enviarFormulario(frm,options){
	
	if(typeof options == undefined || options == null){
		options = {};
	}
	
	var formulario = null;
	if(typeof frm == "string"){
		formulario = document.forms[frm];
	}else{
		formulario = frm;
	}
	
	arr = formulario.elements;
	
	str = "";
	
	for(i=0;i<arr.length;i++){
		el = arr[i];
		str = str + el.name + "="+ el.value + "&";
	}
	
	str = str.substring(0,str.length - 1);
	
	new AjaxRequest({
		url : "Control.php",
		params : str,
		onload : function(){
			loader.setHTML(app.ajaxGif + "Enviando formulário...");
			loader.show();
		},
		ready : function(req){
			if(options.afterSend){
				options.afterSend(req);
			}else{
				loader.setHTML(req.responseText);
			}
		}
	});
}

function geraCamposRequestPHP(frm,dv){
	
	var formulario = null;
	if(typeof frm == "string"){
		formulario = document.forms[frm];
	}else{
		formulario = frm;
	}
	
	arr = formulario.elements;
	

	var console = document.getElementById(dv);
	
	for(i=0;i<arr.length;i++){
		el = arr[i];
		console.innerHTML +=  "$" + el.name + "= $_REQUEST['"+el.name+"'];\n<br>";
	}
	
	
}


	
function getWidth() {  
   return window.innerWidth ? window.innerWidth : /* For non-IE */  
          document.documentElement ? document.documentElement.clientWidth : /* IE 6+ (Standards Compilant Mode) */  
          document.body ? document.body.clientWidth : /* IE 4 Compatible */  
          window.screen.width; /* Others (It is not browser window size, but screen size) */  
 }  
   
function getHeight(){  
   return window.innerHeight ? window.innerHeight : /* For non-IE */  
          document.documentElement ? document.documentElement.clientHeight : /* IE 6+ (Standards Compilant Mode) */  
          document.body ? document.body.clientHeight : /* IE 4 Compatible */  
          window.screen.height; /* Others (It is not browser window size, but screen size) */  
}


function param_default(param,def){
	if(param == null || typeof param == undefined){
		return def;
	}else{
		return param;
	}
}

function popup(mypage, myname, options) {
	if(options == null || typeof options == undefined){
		options = {};
	}
	
	var w = param_default(options.width, "600")
	var h = param_default(options.height, "600");
	var scroll = param_default(options.scroll, "yes");
	var resizable = param_default(options.resizable, "no");
	
	var winl = (getWidth() - w) / 2;
	var wint = (getHeight() - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resizable;
	win = window.open(mypage, myname, winprops);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function alterarConteudo(){
	enviarFormulario('frmManutencao');
}

function cpfCnpj(val){
	if(val == "0"){
		document.getElementById('tr-cnpj').style.display = 'none'
		document.getElementById('tr-cpf').style.display = 'block'
	}else if(val == "1"){
		document.getElementById('tr-cnpj').style.display = 'block'
		document.getElementById('tr-cpf').style.display = 'none'
	}
}
