/*******************XMLHTTP OBJECT VARIABLES********************/
/***************************************************************/

var url = "inc/engine2.php?a="; // The server-side script for GET procedures
var url2 = 'inc/engine2.php'; // The server-side script for POST procedures
var isWorking = false;
var http = getHTTPObject(); // We create the HTTP Object

function checkMail() //Checks the validity of submitted email address
{
	var x = document.forms.mform.email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(x)) {
		return 0;
	}
	return 1;
	
}

function formhandler() {
	var status = false;
	var nonreq = "submitform apt1 apt2 aptfloor full twin queen king cking items comments"; //String of Optional Fields
	var radio_opts = "bulk stairs packaging"; //String of radio buttons
	var command = 'a=cform';	
	if (!checkMail()) {
		alert("Please Enter A Valid Email Address!");
		return status;
	}	
	for(i=0; i<document.mform.elements.length; i++) { //Loop through all form elements		
		if (nonreq.indexOf(document.mform.elements[i].name,0) == -1) { //Ensure the current form element is required
			if (document.mform.elements[i].value == "") { //Is the required form element empty?
				alert('Please Ensure All Starred (*) Fields Are Filled Out!');
				return status;
			}
		}
		
		if (radio_opts.indexOf(document.mform.elements[i].name,0) == -1) { //Ignore Radio Buttons
			command += '&'+document.mform.elements[i].name+'='+document.mform.elements[i].value; //Build the POST string for the form handler
		} else {
			if (document.mform.elements[i].checked==true) //Append values from checked radio buttons and checkboxes ONLY
				command += '&'+document.mform.elements[i].name+'='+document.mform.elements[i].value;
		}		
	}
	
	if (http) {
	   //var command = 'a=cform&name='+document.forms.mform.name.value+'&email='+document.forms.mform.email.value+'&phone='+document.forms.mform.phone.value+'&message='+document.forms.mform.message.value+'&security_code='+document.forms.mform.security_code.value;

	  
	  //For File Upload, include: &pic='+document.forms.form1.browsepic.value

	  http.open("POST", url2, false);
	  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  http.send(command);

		
	  if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			//document.getElementById('actionstatus').innerHTML = http.responseText;
	  		//document.getElementById('actionstatus').style.display='block';
			if (http.responseText != '') {
				alert(http.responseText+" (Please try re-sending your message)");
				return status;
			} else {
				status = true;
			}
			http = getHTTPObject();
			command='';
	  	}
	  }
	}
	return status;
	
}

function getHTTPObject() {
  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;
    }
  }
  return xmlhttp;
}


