var xmlhttp = false;

function loadXMLDoc() {
	querystring = '?c_name=' + encodeURIComponent( document.getElementById('c_name').value) + '&c_email=' + encodeURIComponent( document.getElementById('c_email').value ) + '&c_message=' + encodeURIComponent( document.getElementById('c_message').value );

	//document.getElementById('url').innerHTML = querystring;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
//Jscript conditional compilation
try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} catch(E) {
		xmlhttp = false;
	}
}
@end @*/

	//Mozilla
	if(!xmlhttp) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			var url = "http://www.metonymie.com/" + querystring + "&action=sendmessage&method=nojax";
			//It didn't work. Try getting the no Ajax page directly.
			document.location.href = url;
		}
	}

	//It worked.
	if( xmlhttp ) {
		var url = "http://www.metonymie.com/" + querystring + "&action=sendmessage&method=ajax";
  		xmlhttp.onreadystatechange=state_Change
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	} else {
		//JIC
		//It didn't work. Try getting the no Ajax page directly.
		var url = "http://www.metonymie.com/" + querystring + "&action=sendmessage&method=nojax";
		document.location.href = url;
	}
}

function state_Change() {
	// state 1
	if (xmlhttp.readyState==1) {
	  document.getElementById('c-response').innerHTML='<img src="http://www.metonymie.com/testeos/images/home/bigrotation.gif" alt="AJAXing" />';
	}

	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
		// if "OK"
		if (xmlhttp.status==200) {
			document.getElementById('c-response').innerHTML=xmlhttp.responseText;
			document.getElementById('c-response').style.display='block';
		} else {
			document.getElementById('c-response').innerHTML='<p class="form-errors">Problem retrieving XML data:' + xmlhttp.statusText + '</p>';
		}
  	}
}

/* Show - Unshow Element */
function changeDisplay(elemID) {
	var tC = document.getElementById(elemID);
	if(tC.style.display == 'none') {
		tC.style.display = 'block';
	} else {
		tC.style.display = 'none';
	}
}
function toggleFormAndNormal() {
	var main = document.getElementById('text-container');
	if( main.style.display != 'none' ) {
		main.style.display = 'none';
		document.getElementById('contact-form').style.display = 'block';
		document.getElementById('goto-form').innerHTML = 'Go Back to Normal View';
	} else {
		main.style.display = 'block';
		document.getElementById('contact-form').style.display = 'none';
		document.getElementById('goto-form').innerHTML = 'Contact Via Email';
	}	
	return false;
}

/* window onload handler */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    	}
  }
}

/* JS trim */
function trim(str){  return str.replace(/^\s*|\s*$/g,"");}

/* Check wether an Email is Valid */
function checkValidEmail(the_value) {
	var filter =/^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
	return filter.test(the_value);
}

/* test form */
function validateForm() {
	var errors = '';
	//Check for values. Simple version.
	if( trim(document.getElementById('c_name').value ) == '' ){
		errors += 'Name is required.<br />';
	}
	if( trim(document.getElementById('c_email').value ) == '' ){
		errors += 'Email is required.<br />';
	} else {
		if( !checkValidEmail( trim(document.getElementById('c_email').value) ) ){
			errors += 'Email must be a valid email address.<br />';
		}
	}


	if( trim(document.getElementById('c_message').value ) == '' ){
		errors += 'Message is required.<br />';
	}
	if(errors != '') {
		document.getElementById('c-response').innerHTML = '<p class="form-errors">Please to send a Message consider that: <br/>' + errors + '</p>';
		location.href='#page';
	} else {
		loadXMLDoc();
	}
	return false;
}

function preparePage() {
	document.getElementById('goto-normal').onclick = toggleFormAndNormal;
	document.getElementById('goto-form').onclick = toggleFormAndNormal;
	document.getElementById('c-submit').onclick = validateForm;
	document.getElementById('c-reset').onclick = function() { document.getElementById('c-response').innerHTML=''; location.href='#page'; return true; };
	/* process fragment */
	if(location.hash == '#contact-form')toggleFormAndNormal();
}

addLoadEvent( preparePage );
