/**
 * @author divonei macedo
 */

var Ajax = false;

var testObj;

function AjaxRequest(){  
  if(window.XMLHttpRequest){
       Ajax = new XMLHttpRequest();
  }else if(window.ActiveXObject){
       try{
               Ajax = new ActiveXObject("Msxml.XMLHTTP");        
       }catch(e){
         try{
               Ajax = new ActiveXObject("Msxml.XMLHTTP");        
           }catch(e){
               try{
                       Ajax = new ActiveXObject("Microsoft.XMLHTTP");                
               }catch(E){
           Ajax = false;
               }
           }
     }
   }
}

function getPageScroll(){

  var yScroll;

  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){   // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  }  
  arrayPageScroll = new Array('',yScroll) 
  return arrayPageScroll;
}

function getPageSize(){
  
  var xScroll, yScroll;
  
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  
  var windowWidth, windowHeight;
  if (self.innerHeight) {  // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }  
  
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){  
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

    
  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}

function initForm(){
  var pagesize = getPageSize();
  var pagescroll = getPageScroll();  
  var a = document.createElement('a');
  var thanks = document.getElementById('formsended');
  var div = document.getElementById('contform');
  var right = document.getElementById('rightform');
  var sending = document.getElementById('sending');
  //var left = document.getElementById('leftform');
  right.style.display = "block";
  sending.style.display = "none";
  thanks.style.display = "none";
  div.style.opacity = "0";
  div.style.filter = 'alpha(opacity=' + 0 + ')';
  div.style.display = "block";  
  fadeIn('contform')  
  //div.style.opacity = "1";
  //div.style.filter = 'alpha(opacity=' + 100 + ')';
  div.style.top = (pagescroll[1] + ((pagesize[3] - 35 - parseInt(div.style.height)) / 2 +(90)) + 'px');  
  div.style.left = (((pagesize[0] - 20 - parseInt(div.style.width)) / 2) + 'px');  
}

function Close(){  
  fadeOut('contform');
}


function show(){
  var div = document.getElementById('sending');
  var form = document.getElementById('rightform');
  var thanks = document.getElementById('formsended')
  if(Ajax.readyState==4){    
    if(Ajax.status==200){
      xml = Ajax.responseXML;
      if(xml.hasChildNodes){
        var ERRO = xml.getElementsByTagName('erro')[0].firstChild.nodeValue;
        if(ERRO != 'not email' && ERRO != 'ERRO'){
          div.style.display = 'none';
          thanks.style.display = 'block';
        }
        else{
          var p = document.createElement('p');
          if(ERRO=='not email')
            var txt = document.createTextNode('*This is not a valid email address.');
          else
            var txt = document.createTextNode('*The server is too busy please try again later.')
          div.style.display = "none";
          form.style.display = "block";
          p.style.color = "red";
          p.appendChild(txt);          
          form.appendChild(p);
        }  
      }
            
    }
    else{
      alert('Error: '+Ajax.status);
    }
  }
  else{
    div.style.display = 'block';
    form.style.display = "none";
  }  
}


function envia(name, email, subject, texto, url){
  AjaxRequest();
  strData = "name="+escape(name)+
    "&email="+escape(email) +
    "&subject="+escape(subject)+
    "&text="+escape(texto);
  if(!Ajax){
    alert('ajax its no working');
    return;
  }
  link = url + "/contact/"+'send.php?'+strData;
  Ajax.open('GET', link, true);
  Ajax.onreadystatechange = show;
  Ajax.send(null);
  //alert(strData);
}


function valida(url){
  var d = document.frmcontact;
  if(d.name.value == "" || d.email.value == "" || d.subject.value == "" ||  d.text.value == "")
    alert('Sorry, your message has not been sent. All fields are required. Please, check back if none has been left in blank, and try again.');
  else{
    envia(d.name.value, d.email.value, d.subject.value, d.text.value, url);
  }
	
	return false;    
}

var fadein=0, fadeout=100, opac;
var callFunc, Value;
var callFadeout

function fadeIn(fadeObj) {
  //alert(fadeObj);  
  testObj = document.getElementById(fadeObj);
  Value=0;
  fadeout=100;          
  callFunc = setInterval('setOpacity()', 50);  
}

function fadeOut(fadeObj){
  testObj = document.getElementById(fadeObj);
  Value=100;          
  callFadeout = setInterval('setOpacity()', 50);
}

function setOpacity()
{    
  //alert(Value);  
  if(Value==0 && fadein<100){    
    fadein+=10;    
    opac = fadein*0.01;
    if(fadein<100){
      testObj.style.opacity = opac;
      testObj.style.filter = 'alpha(opacity=' + (fadein-10) + ')';
    }
    else{
      clearInterval(callFunc);
      fadein=0;
    }              
  }
  else if(Value==100 && fadeout>0){    
      fadeout-=15;    
      opac = fadeout*0.01;
      //alert(opac);
      if(fadeout>0){
        //alert(opac);
        testObj.style.opacity = opac;
        testObj.style.filter = 'alpha(opacity=' + (fadeout-10) + ')';
      }
      else{
        //alert(fadeout);
        clearInterval(callFadeout);
        //fadeout=100;
        testObj.style.display = "none";
      }        
    }  
  
}
