var viewingForm = false;

// Function that will swap the display/no display for
// all content within span tags
function toggleme(itemid) {
	if(item = document.getElementById(itemid))
	{
	
		if (item){
			alert('item found');
		} else {
			alert('item not found');
		}
	
		if (item.style.display == "none") {
			item.style.display = "";
		} else {
			item.style.display = "none";
		}
	}
}

function hideme(itemid) {
	/*
	if(item = document.getElementById(itemid))
	{
		item.style.display = "none";
	}
*/
var item = document.getElementById(itemid);
	if (item){
		item.style.display = "none";
	}
}

function showme(itemid) {

	//alert(itemid);	
	/*
	if(item = document.getElementById(itemid))
	{
		item.style.display = "";
	}
*/
var item = document.getElementById(itemid);
	if (item){
		item.style.display = "";
	}

}

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(){

	if(viewingForm === false){
		viewingForm = true;
		
		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] - 35 - parseInt(div.style.height)) / 2 +(90)) + 'px');
		div.style.top = '140px';
		div.style.left = (((pagesize[0] - 20 - parseInt(div.style.width)) / 2) + 'px');	

	} else {	

	}
}

function Close(){	
	fadeOut('contform');
	viewingForm = false;
}


function show(){
	var div = document.getElementById('sending');
	var form = document.getElementById('rightform');
	var left = document.getElementById('leftform');
	var thanks = document.getElementById('formsended');
	if(Ajax.readyState==4){		
		if(Ajax.status==200){
			 
			 //notice = Ajax.responseText;
			 //alert(notice);
			 
			xml = Ajax.responseXML;
			if(xml.hasChildNodes){
				var ERRO = xml.getElementsByTagName('error')[0].firstChild.nodeValue;
				//alert(ERRO);
				if(ERRO == 'ok'){
					div.style.display = 'none';
					left.style.display = 'none';
					thanks.style.display = "block";
				}
				else{
				var notice = xml.getElementsByTagName('error')[0].firstChild.nodeValue;
				/*
					var p = document.createElement('p');
					var txt = document.createTextNode(ERRO);
					div.style.display = "none";
					form.style.display = "block";
					p.style.color = "red";
					p.appendChild(txt);					
					form.appendChild(p);
				*/
				
				
				//alert(notice);
			errorContactobj = document.getElementById('errorContact');
			errorContactobj.innerHTML = notice;
			div.style.display = "none";
					form.style.display = "block";
				}	
			}
		
	
	/*
			if (Ajax.responseText ==1){
				div.style.display = 'none';
				left.style.display = 'none';
				thanks.style.display = "block";
			} else {
			//var p = document.createElement('p');
			//var txt = document.createTextNode('*Their was a problem with your submission please try again.');
			
			notice = Ajax.responseText;
			errorContactobj = document.getElementById('errorContact');
			errorContactobj.innerHTML = notice;
					div.style.display = "none";
					form.style.display = "block";
					//p.style.color = "red";
					//p.style.wieght = 'bold';
					//p.appendChild(txt);					
					//form.appendChild(p);
			}
			*/
			//alert(Ajax.responseText);
		
							
		}
		else {
			alert('Error: '+Ajax.status);
		}
	}
	else{
		div.style.display = 'block';
		form.style.display = "none";
	}	
}


function envia(name, email, phone, subject, texto, url){
	AjaxRequest();
	strData = "data[Contacts][name]="+escape(name)+
		"&data[Contacts][email]="+escape(email) +
		"&data[Contacts][phone]="+escape(phone) +
		"&data[Contacts][subject]="+escape(subject)+
		"&data[Contacts][comment]="+escape(texto);
	if(!Ajax){
		alert('ajax its no working');
		return;
	}	
	//alert(strData);

	Ajax.open('POST', url, true);
	
	//Send the proper header information along with the request
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", strData.length);
	Ajax.setRequestHeader("Connection", "close");
	
	Ajax.onreadystatechange = show;
	Ajax.send(strData);
}


function valida(url){
	//var d = document.frmcontact;
	
	var ContactName = document.getElementById('ContactName').value;
	var ContactEmail = document.getElementById('ContactEmail').value;
	var ContactPhone = document.getElementById('ContactPhone').value;
	var ContactSubject = document.getElementById('ContactSubject').value;
	var ContactComment = document.getElementById('ContactComment').value;

envia(ContactName, ContactEmail, ContactPhone, ContactSubject, ContactComment, url);
	/*
	if(ContactName == "" || ContactEmail == "" || ContactSubject == "" ||  ContactComment == "")
		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(ContactName, ContactEmail, ContactSubject, ContactComment, url);
		*/
}

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

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

function fadeOut(fadeObj){
	showme('flashcontent');
	//showme('searchform');
	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";
			}				
		}	
	
}

