//find form
$(document).ready (
	function() {
		$('#contactform').attr("action", "javascript:get(document.getElementById('contactform'));");
		$('#contactform').removeAttr("method");
		$('#status').hide();
		$('#submit').click(function(){
			$('#status').fadeOut('fast');
		})
	}
)


var http_request = false;
var success = null;
   
   function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            var result = http_request.responseText;
            $('#status').html(result);
            $('#status').show("def");
            if(result.match(success)) { //if message sent
            	$('#submit').attr("disabled", "disabled");
            }
                      
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
    var getstr = "?";
    var children = $("form input, form select, form textarea");
	var child;
    for(i=0; i<children.length; i++) {
    child = children[i];      	
      	if (child.tagName == "INPUT") {
            if (child.type == "text" || child.type =="hidden") {
            	if(child.name == "success") success = child.value;
               getstr += child.name + "=" + child.value + "&";
            }
            if (child.type == "checkbox") {
               if (child.checked) {
                  getstr += child.name + "=" + child.value + "&";
               } else {
                  getstr += child.name + "=&";
               }
            }
            if (child.type == "radio") {
               if (child.checked) {
                  getstr += child.name + "=" + child.value + "&";
               }
            }
         }   
         if (child.tagName == "SELECT") {
            var sel = child;
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         if (child.tagName == "TEXTAREA") {
            getstr += child.name + "=" + child.value + "&";
         }
      }
      getstr += "ajax=true";
      makeRequest('http://cooperhong.com/Jon/UPG/site/sendmessage.php', getstr);
   }
