var user,domain, regex, _match;
var queryString="";

function submitForm() {
	var name_field = document.inquiry.name;
	var email_field = document.inquiry.email;
	checkName(name_field);
	checkEmail(email_field);
	connect(name_field,email_field);
}
function connect(name_field,email_field) {

		if ((name_field.style.border == '') &&
			(email_field.style.border == '')) {
		 //	
		 // var sUrl="inquiry_check.html";
         // argument formId can be the id or name attribute value of the
         // HTML form, or an HTML form object.
            var formObject = document.getElementById('inquiry');
            YAHOO.util.Connect.setForm(formObject);
            // This example facilitates a POST transaction.  The POST data(HTML form)
            // are initialized when calling setForm(), and it is automatically
            // included when calling asyncRequest.
            var cObj = YAHOO.util.Connect.asyncRequest('POST', 'email_check.html', callback);
		}

}
var handleSuccess = function(o){

	if(o.responseText !== undefined){
	    var div = document.getElementById("column");
//		div.innerHTML = "Transaction id: " + o.tId;
//		div.innerHTML += "HTTP status: " + o.status;
//		div.innerHTML += "Status code message: " + o.statusText;
//		div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";
		div.innerHTML = o.responseText;
//		div.innerHTML += "Argument object: " + o.argument;
	    //div.innerHTML = "thank you <?php print_r $_POST";
//	    	
//	    div.innerHTML = "<div class=\"packages_inquiry_desc\"><div class=\"orb\" style=\"margin-bottom:15px;\">Thank you " +
//		"<?php print $_POST['NAME']?></div><div style=\"margin-top:14px; float:left; width:272px; margin-bottom:25px;\">We have received your inquiry.<br />Thank you.<br /><br />-Either design format for some special offers.<br />-Discuss a little more about the inquiry, reservation process.<br />-You can put literally anything in this entire column.</div></div>";
	    
//	    	"<div class="packages_inquiry_desc">
//	    <div class=\"orb\" style=\"margin-bottom:15px;\">Thank you Walia</div>
//
//	    <div style=\"margin-top:14px; float:left; width:272px; margin-bottom:25px;\">We have received your inquiry.<br />
//	    Thank you.<br />
//	      <br />
//	    -Either design format for some special offers.<br />
//	    -Discuss a little more about the inquiry, reservation process.<br />
//	    -You can put literally anything in this entire column.</div>
//	    
//	    </div>";
	    
		
	}
}

var callback =
{
  success:handleSuccess
  //failure: handleFailure,
  //argument: ['foo','bar']
};

//
//function setQueryString(){
//    queryString="";
//    var frm = document.forms[0];
//    var numberElements =  frm.elements.length;
//    for(var i = 0; i < numberElements; i++)  {
//        if(i < numberElements-1)  {
//            queryString += frm.elements[i].name+"="+
//                           encodeURIComponent(frm.elements[i].value)+"&";
//        } else {
//            queryString += frm.elements[i].name+"="+
//                           encodeURIComponent(frm.elements[i].value);
//        }
//
//    }
//}
//
//function handleResponse(  ){
//	 if ( req.readyState == 4 && req.status == 200 ){
//		 	alert("dd");
//		}
//	 else {
//		 alert("none");
//	 }
//	 }
//



function Name(n) {
	this.nameFld = n;
	this.message = "";
	this.valid = false;
}
function Email(e) {
	this.emailAddr = e;
	this.message = "";
	this.valid = false;
}


var name_errmsg = "Your name is required.";
function validate_name() {
	if (this.nameFld == "") {
		this.message= name_errmsg;
		this.valid = false;
		return;
		}
	this.valid = true;
}
Name.prototype.validate_name = validate_name;

var email_errmsg = "Please enter a valid email address.";
function validate(){
	if (this.emailAddr == null || 
		this.emailAddr.length == 0 ||
		this.emailAddr.indexOf(".") == -1 ||
		this.emailAddr.indexOf("@") == -1 ||
		this.emailAddr.indexOf(" ") !== -1) {
		this.message= email_errmsg;
		this.valid = false;
		return;
	}
    this.valid=true;
}
Email.prototype.validate = validate;



function nMsg(msg,sColor){
    var div = document.getElementById("name_message");
    div.style.color=sColor;
    div.style.fontSize="0.9em";
    //remove old messages
    if(div.hasChildNodes(  )){
        div.removeChild(div.firstChild);
    }
    div.appendChild(document.createTextNode(msg));
}

function eMsg(msg,sColor){
    var div = document.getElementById("email_message");
    div.style.color=sColor;
    div.style.fontSize="0.9em";
    //remove old messages
    if(div.hasChildNodes(  )){
        div.removeChild(div.firstChild);
    }
    div.appendChild(document.createTextNode(msg));
}

function checkName(name_field) {
	var name = new Name(name_field.value);
	name.validate_name();
    if (! name.valid) {
    	nMsg(name.message,"red");
    	name_field.style.border = '1px solid #FF0000';
    	}
    if(name.valid)
    {
    	var div = document.getElementById("name_message");
        if(div.hasChildNodes(  )){
            div.removeChild(div.firstChild);
        }
        name_field.style.border = '';
    }
}

function checkEmail(email_field){
	var eml = new Email(email_field.value);
	//alert(email_field);
    var url;
    eml.validate(  );
    if (! eml.valid) {
    	eMsg(eml.message,"red");
    	email_field.style.border = '1px solid #FF0000';
    	//alert('not valid');
    	}
    
    if(eml.valid)
    {
    	var div = document.getElementById("email_message");
        if(div.hasChildNodes(  )){
            div.removeChild(div.firstChild);
        }
    	email_field.style.border = '';

    }
}

//event handler for XMLHttpRequest
//see Hack #24
function handleResponse(  ){
    //snipped...
}