/////////////////////////////////////////////////////
//criacao xmlhttp request
/////////////////////////////////////////////////////
var xmlhttp = false;
  //Check if we are using IE.
  try {
    //If the Javascript version is greater than 5.
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    //If not, then use the older active x object.
    try {
      //If we are using Internet Explorer.
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      //Else we must be using a non-IE browser.
      xmlhttp = false;
    }
  }
  //If we are using a non-IE browser, create a javascript instance of the object.
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
  }

//////////////////////////////////////

function makerequest(serverPage, objID){

		var obj = document.getElementById(objID);
		obj.innerHTML = "<img src='images/upload/indicator.gif'>";
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
		  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		  }
		}
		xmlhttp.send(null);
}// makerequest
  
//////////////////////////////////////

function saiba_mais(img,id,idSM,tabela){
	var imgsrc = document.getElementById(img).src.split("/images/upload/");	
	
	if(imgsrc[1] == "38.gif"){
		document.getElementById(img).src = "images/upload/77.gif";
		var page = 'modules/ajaxSM.php?id=' + idSM + '&tabela=' + tabela;
		makerequest(page, id);
	}else if(imgsrc[1] = "77.gif"){
		document.getElementById(img).src = "images/upload/38.gif";
		document.getElementById(id).innerHTML = '';
	}
	
}// function

//////////////////////////////////////

// calendar 3 campos

    YAHOO.util.Event.onDOMReady(function () {

        function onButtonClick() {

            /*
                 Create an empty body element for the Overlay instance in order 
                 to reserve space to render the Calendar instance into.
            */

            oCalendarMenu.setBody("&#32;");

            oCalendarMenu.body.id = "calendarcontainer";


            // Render the Overlay instance into the Button's parent element

            oCalendarMenu.render(this.get("container"));
            

            // Align the Overlay to the Button instance

            oCalendarMenu.align();
            
            
            /*
                 Create a Calendar instance and render it into the body 
                 element of the Overlay.
            */

            var oCalendar = new YAHOO.widget.Calendar("buttoncalendar", oCalendarMenu.body.id);

            oCalendar.render();


            /* 
                Subscribe to the Calendar instance's "changePage" event to 
                keep the Overlay visible when either the previous or next page
                controls are clicked.
            */

            oCalendar.changePageEvent.subscribe(function () {
                
                window.setTimeout(function () {

                    oCalendarMenu.show();
                
                }, 0);
            
            });


            /*
                Subscribe to the Calendar instance's "select" event to 
                update the month, day, year form fields when the user
                selects a date.
            */

            oCalendar.selectEvent.subscribe(function (p_sType, p_aArgs) {

                var aDate;

                if (p_aArgs) {
                        
                    aDate = p_aArgs[0][0];
                        
                    YAHOO.util.Dom.get("month-field").value = aDate[1];
                    YAHOO.util.Dom.get("day-field").value = aDate[2];
                    YAHOO.util.Dom.get("year-field").value = aDate[0];

                }
                
                oCalendarMenu.hide();
            
            });
            

            /*
                 Unsubscribe from the "click" event so that this code is 
                 only executed once
            */

            this.unsubscribe("click", onButtonClick);
        
        }


        // Create an Overlay instance to house the Calendar instance

        var oCalendarMenu = new YAHOO.widget.Overlay("calendarmenu");


        // Create a Button instance of type "menu"

        var oButton = new YAHOO.widget.Button({ 
                                            type: "menu", 
                                            id: "calendarpicker", 
                                            label: "Choose A Date", 
                                            menu: oCalendarMenu, 
                                            container: "datefields" });
        

        /*
            Add a "click" event listener that will render the Overlay, and 
            instantiate the Calendar the first time the Button instance is 
            clicked.
        */

        oButton.on("click", onButtonClick);
    
    });

////////////////////////////////////

function evento_tr(id){
	document.getElementById(id).style.background='#72bf44';	
	document.getElementById(id).style.color = "#ffffff";
	document.getElementById(id).style.fontWeight = "bold";
}

////////////////////////////////////

function evento_tr_out(id,out){
	document.getElementById(id).style.background = out;	
	document.getElementById(id).style.color = "#000000";
	document.getElementById(id).style.fontWeight = "normal";	
}

////////////////////////////////////

function isValidEmail(email)
    {
         var regex = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i;
         return regex.test(email);
    }
    function validateForm(frm)
    {
         if (!isValidEmail(frm.email.value)) {
             return false;
         }
        return true;
    }
	
	/////////////////////////////////////

	function HabilitaCampos(tel2){

		if (tel2.value == ""){
				document.getElementById("Manha").disabled = true;
				document.getElementById("Tarde").disabled = true;    				        
				document.getElementById("Noite").disabled = true;                         
		}else{
				document.getElementById("Manha").disabled = false;
				document.getElementById("Tarde").disabled = false;    				        
				document.getElementById("Noite").disabled = false;      
		}
	
	}
	
	/////////////////////////////////////

          function mostrarCampoTurma() {
		  
		  var layer = aluno;            
            if (document.fale.rdaluno[0].checked) {
               eval(aluno).style.display = "";
            }
            else {
            	 eval(aluno).style.display = "none";   
            }
          }   
		
	
	///////////////////////////////////////////////
	
	function check(fale){
	
		var erro = false;
		if(fale.unidade.selectedIndex == 0){
			erro = true;
			document.getElementById("erro_unidade").style.background = '#9eb0b9'
			document.getElementById("erro_unidade").innerHTML = "<p id='padding0' class='comentFale'>Selecione uma unidade abaixo</p>";
		}else{
			erro = false;
			document.getElementById("erro_unidade").style.background = ''
			document.getElementById("erro_unidade").innerHTML = "";
		}
		
		if(fale.area.selectedIndex == 0){
			erro = true;
			document.getElementById("erro_area").style.background = '#9eb0b9'
			document.getElementById("erro_area").innerHTML = "<p id='padding0' class='comentFale'>Selecione uma área abaixo</p>";
		}else{
			erro = false;
			document.getElementById("erro_area").style.background = ''
			document.getElementById("erro_area").innerHTML = "";
		}
		
		if(fale.nome.value == "" || fale.nome.value == "Digite aqui seu nome"){
			erro = true;
			document.getElementById("erro_nome").style.background = '#9eb0b9'
			document.getElementById("erro_nome").innerHTML = "<p id='padding0' class='comentFale'> Digite seu nome</p>";
		}else{
			erro = false;
			document.getElementById("erro_nome").style.background = ''
			document.getElementById("erro_nome").innerHTML = "";
		}
		
		var email = isValidEmail(fale.email.value);
		
		if(email == false){
			erro = true;
			document.getElementById("erro_email").style.background = '#9eb0b9'
			document.getElementById("erro_email").innerHTML = "<p id='padding0' class='comentFale'> E-mail inválido</p>";
		}else{
			erro = false;
			document.getElementById("erro_email").style.background = ''
			document.getElementById("erro_email").innerHTML = "";
		}
		
		if(fale.msg.value == "Digite sua mensagem" || fale.msg.value == ""){
			erro = true;
			document.getElementById("erro_msg").style.background = '#9eb0b9'
			document.getElementById("erro_msg").innerHTML = "<p id='padding0' class='comentFale'> Digite a mensagem</p>";
		}else{
			erro = false;
			document.getElementById("erro_msg").style.background = ''
			document.getElementById("erro_msg").innerHTML = "";
		}
		
		if(erro == false){
			document.getElementById("fale").submit();
		}
		

	}
				
/////////////////////////////////////////

function apply_add_ajax_search(table) {

		// Find the search box in the DOM
		var theElement = document.getElementById('search');
		
		// Create a function for when the user presses a key
		theElement.onkeyup = function(){ search_delay(this,table); };
}

	function search_delay(element,table) {
		var page = 'modules/ajax_faq_grad.php?query=' + element.value + '&table=' + table;;
		// Create a function to get the search results
		var func = function() { makerequest(page, 'REPLY'); };

		// Check to see if there is already a timeout and if so...
		// ...cancel it and create a new one
		if ( element.zid ) {
			clearTimeout(element.zid);
		}
		element.zid = setTimeout(func,1000);
	
	}
	
///////////////////////////////////////

function chama3(table){
	makerequest('modules/ajax_faq_grad.php?table='+table, 'REPLY');
}

////////////////////////////////////// autocomplete

  function findPosX(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;
}

///////////////////////

function findPosY(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;
}


////////////////////////
function timeouT(){
	acObject = document.getElementById("autocompletediv");
    acObject.style.visibility = "hidden";
}

function autocomplete (thevalue, e){
	
	var func = function() { timeouT();}
	if ( thevalue.zid ) {
            clearTimeout(thevalue.zid);
        }
		
     thevalue.zid = setTimeout(func,8000);
	 
	theObject = document.getElementById("autocompletediv");
	theObject.style.visibility = "visible";
	theObject.style.width = "150px";
	var posx = 0;
	var posy = 0;
	posx = (findPosX (document.getElementById("campo_busca")) + 1);
	posy = (findPosY (document.getElementById("campo_busca")) + 23);
	theObject.style.left = posx + "px";
	theObject.style.top = posy + "px";
	var theextrachar = e.which;
	
	if (theextrachar == undefined){
		theextrachar = e.keyCode;
	}

//The location we are loading the page into.
var objID = "autocompletediv";

    //Take into account the backspace.
    if (theextrachar == 8){
      if (thevalue.length == 1){
        var serverPage = "modules/autocomp.php";
      } else {
        var serverPage = "modules/autocomp.php" + "?sstring=" + 
thevalue.substr (0, (thevalue.length -1));
      }
    } else {
      var serverPage = "modules/autocomp.php" + "?sstring=" + 
thevalue + String.fromCharCode (theextrachar);
    }
    var obj = document.getElementById(objID);
    xmlhttp.open("GET", serverPage);
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        obj.innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.send(null);
  }


//////////////////////


  function setvalue (thevalue){
    acObject = document.getElementById("autocompletediv");
    acObject.style.visibility = "hidden";
    acObject.style.height = "0px";
    acObject.style.width = "0px";
    document.getElementById("campo_busca").value = thevalue;
  }

//////////////////////
