/*
Page:           rating.js
Created:        Aug 2006
Handles actions and requests for rating bars.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
--------------------------------------------------------- */

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function validateForm() {
	var theDiv = document.getElementById("error-notice"); // the UL
	var email  = document.contactForm.Email.value;//.value;
	var message = document.contactForm.Message.value;
	var code = document.contactForm.Code.value;
	with(document.contactForm) {
		if(email == "") {
			document.getElementById("error-notice").innerHTML = "Please Enter Your Email";
			document.contactForm.Email.style.borderColor = '#8d1a1a';
			Email.focus();
			return false;
		}
		var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
		var regex = new RegExp(emailReg);
    	email_valid = (regex.test(email)) ? 1 : 0;
        if(!email_valid)
			{
				document.getElementById("error-notice").innerHTML = "Please Enter Valid Email";
				document.contactForm.Email.style.borderColor = '#8d1a1a';
				Email.focus();
				return false;
			}
		if(message == "") {
			document.contactForm.Email.style.borderColor = '#d3d2d2';
			document.getElementById("error-notice").innerHTML = "Please Enter Your Message";
			document.contactForm.Message.style.borderColor = '#8d1a1a';
			Message.focus();
			return false;
		}
		if(message.length < 10 ) {
			document.contactForm.Email.style.borderColor = '#d3d2d2';
			document.getElementById("error-notice").innerHTML = "Your message should be at least 10 characters.";
			document.contactForm.Message.style.borderColor = '#8d1a1a';
			Message.focus();
			return false;
		}
		
		if(code == "") {
			document.contactForm.Email.style.borderColor = '#d3d2d2';
			document.contactForm.Message.style.borderColor = '#d3d2d2';
			document.getElementById("error-notice").innerHTML = "Please Enter Your Answer";
			document.contactForm.Code.style.borderColor = '#8d1a1a';
			Code.focus();
			return false;
		}
	}
// switch UL with a loading div
	document.getElementById("error-notice").innerHTML = 'Sending Email..';
	document.contactForm.Email.style.borderColor = '#d3d2d2';
	document.contactForm.Message.style.borderColor = '#d3d2d2';
	document.contactForm.Code.style.borderColor = '#d3d2d2';
	xmlhttp.open('get','ajax_contact.php?email='+email+'&message='+message+'&code='+code);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}


function handleResponse() {

  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}
