/**
 * @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 left = document.getElementById('leftform');
	right.style.display = "block";
	left.style.display = "block";
	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] - 95 - parseInt(div.style.height)) / 2 +(30)) + 'px');	
	div.style.left = (((pagesize[0] - 20 - parseInt(div.style.width)) / 2) + 'px');	
}

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


function showProcess(){
	var div = document.getElementById('sending');
	var form = document.getElementById('rightform');
	var left = document.getElementById('leftform');
	var thanks = document.getElementById('formsended')
	
	//if (Ajax.readyState == 4 && (Ajax.status==200 || window.location.href.indexOf("http")==-1)){
	
	if(Ajax.readyState==4){		
		if(Ajax.status==200){
			xml = Ajax.responseXML;
			if(xml.hasChildNodes()){
				var myresp = xml.getElementsByTagName('title').item(0).firstChild.data;
				
				if (myresp == 'Sending'){
					div.style.display = 'none';
					left.style.display = 'none';
					thanks.style.display = "block";
				} else {
					var p = document.createElement('p');
					if(myresp == 'no_data') {
						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);
					}
				}
				Close();
			} else {
				alert ('XML: NO Nodes')
			}
		} else {
			alert('Error: '+Ajax.status);
		}
	} else {
		div.style.display = 'block';
		form.style.display = "none";
	}	
}


function envia(nameto, email, bodytext, url){
	AjaxRequest();
	strData = "nameto="+escape(nameto)+
		"&email="+escape(email) +
		"&bodytext="+escape(bodytext);
	if(!Ajax){
		alert('ajax its no working');
		return;
	}	
	Ajax.open('GET', url+'sendemail2007.php?'+strData, true);
	Ajax.onreadystatechange = showProcess;
	//Ajax.onreadystatechange = alertMe("ok Ajax");

	Ajax.send(null);
	//alert(strData);
}

function alertMe(mytext){
	alert(mytext);
}

function valida(url){
	var d = document.frmcontact;
	if(d.nameto.value == "" || d.email.value == "" ||  d.bodytext.value == "") {
		alert('Todos los campos son necesarios. | All fields are required.\nPor Favor, intentalo de nuevo. | Please, try again.');
	} else {
		envia(d.nameto.value, d.email.value, d.bodytext.value, url);
	}
}

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()', 65);
}

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";
			}				
		}	
	
}