/**
 * This function checks that required fields are filled in, then creates the
 * data object to send to the server.  It then makes an AJAX request and
 * sends the data.
 */
function contactUs( isWindow ) {

	var toAddress = "sales@paperhost.com";

	var myIsWindow = true;
	
	if( isWindow == false || isWindow == "false" ) {
		myIsWindow = false;
	}

	var document = this.document;
	var emailFilter = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/

	// if all the required fields have not been filled in, return
	if( document.getElementById( "ContactName" ).value == "" || 
		document.getElementById( "ContactEmailAddress" ).value == "" || 
		document.getElementById( "ContactCompany" ).value == "" || 
		document.getElementById( "ContactPhone" ).value == "" ) {
		alert( "All required fields must be filled in." );
		return;
	}

	// if the email address is not in a valid format, return
	if( !emailFilter.test( document.getElementById( "ContactEmailAddress" ).value.toUpperCase() ) ) {
		alert( "Please enter a valid Email Address." );
		return;
	}

	// if ContactTrial checkbox is checked, send true, otherwise send false
	var contactTrialInterest = "false";
	
	var contactJson = {};
	
	contactJson.ContactUs = {};
	
	contactJson.ContactUs.ToAddress = toAddress;
	contactJson.ContactUs.ContactName = document.getElementById( "ContactName" ).value;
	contactJson.ContactUs.ContactEmailAddress = document.getElementById( "ContactEmailAddress" ).value;
	contactJson.ContactUs.ContactCompany = document.getElementById( "ContactCompany" ).value;
	contactJson.ContactUs.ContactPhone = document.getElementById( "ContactPhone" ).value;
	contactJson.ContactUs.ContactAddress = "";
	contactJson.ContactUs.ContactCityStateZip = "";
	contactJson.ContactUs.HowHearAboutUs = "";
	contactJson.ContactUs.YearBusinessEstablished = "";
	contactJson.ContactUs.ContactComments = document.getElementById( "ContactComments" ).value;
	contactJson.ContactUs.ContactTrialInterest = contactTrialInterest;
	
	var theURL = window.location.protocol + "//" + window.location.host + 
				"/paperhostweb/contactusbyemail?ts=" + new Date().getTime();
				
	sendRequest( theURL, contactJson, myIsWindow );
	
} // eo function contactUs

/**
 * This function checks that required fields are filled in, then creates the
 * data object to send to the server.  It then makes an AJAX request and
 * sends the data.
 */
function contactUsPartnerPact( isWindow ) {

	var toAddress = "sales@paperhost.com";

	var myIsWindow = true;
	
	if( isWindow == false || isWindow == "false" ) {
		myIsWindow = false;
	}

	var document = this.document;
	var emailFilter = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/

	// if all the required fields have not been filled in, return
	if( document.getElementById( "ContactName" ).value == "" || 
		document.getElementById( "ContactEmailAddress" ).value == "" || 
		document.getElementById( "ContactCompany" ).value == "" || 
		document.getElementById( "ContactPhone" ).value == "" || 
		document.getElementById( "ContactAddress" ).value == "" || 
		document.getElementById( "ContactCityStateZip" ).value == "" || 
		document.getElementById( "HowHearAboutUs" ).value == "" || 
		document.getElementById( "YearBusinessEstablished" ).value == "" ) {
		alert( "All required fields must be filled in." );
		return;
	}

	// if the email address is not in a valid format, return
	if( !emailFilter.test( document.getElementById( "ContactEmailAddress" ).value.toUpperCase() ) ) {
		alert( "Please enter a valid Email Address." );
		return;
	}

	// if ContactTrial checkbox is checked, send true, otherwise send false
	var contactTrialInterest = "false";
	
	var contactJson = {};
	
	contactJson.ContactUs = {};
	
	contactJson.ContactUs.ToAddress = toAddress;
	contactJson.ContactUs.ContactName = document.getElementById( "ContactName" ).value;
	contactJson.ContactUs.ContactEmailAddress = document.getElementById( "ContactEmailAddress" ).value;
	contactJson.ContactUs.ContactCompany = document.getElementById( "ContactCompany" ).value;
	contactJson.ContactUs.ContactPhone = document.getElementById( "ContactPhone" ).value;
	contactJson.ContactUs.ContactAddress = document.getElementById( "ContactAddress" ).value;
	contactJson.ContactUs.ContactCityStateZip = document.getElementById( "ContactCityStateZip" ).value;
	contactJson.ContactUs.HowHearAboutUs = document.getElementById( "HowHearAboutUs" ).value;
	contactJson.ContactUs.YearBusinessEstablished = document.getElementById( "YearBusinessEstablished" ).value;
	contactJson.ContactUs.ContactComments = document.getElementById( "ContactComments" ).value;
	contactJson.ContactUs.ContactTrialInterest = contactTrialInterest;
	
	var theURL = window.location.protocol + "//" + window.location.host + 
				"/paperhostweb/contactusbyemail?ts=" + new Date().getTime();
	
	sendRequest( theURL, contactJson, myIsWindow );
	
} // eo function contactUsPartnerPact

/**
 * This function handles the sending of AJAX request and data
 */
function sendRequest( URL, jsonData, isWindow ) {

	var http_request;
	
	if( window.XMLHttpRequest ) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if( http_request.overrideMimeType ) {
			http_request.overrideMimeType( 'text/xml' );
		}
	} 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 an XMLHTTP instance' );
		return false;
	}

	try {
		http_request.onreadystatechange = function() { processRequestChanges( http_request, isWindow ); };
		http_request.open( "POST", URL, true );
		http_request.send( JSON.stringify( jsonData ) );
	} catch( err ) {
		alert( err.message );
	}
	
} // eo function sendRequest

/**
 * This function is where any request changes (Responses) are processed
 */
function processRequestChanges( http_request, isWindow ) {

	try {
		// only if http_request shows "complete"
		if( http_request.readyState == 4 ) {
			// only if status is "OK"
			if( http_request.status == 200 ) {
				// ...processing statements go here...
				var jsonText = http_request.responseText;
				var json = jsonParse( jsonText );
				if( json.HttpResponse.error == "true" ) {
					//alert( json.HttpResponse.message );
					alert( "Your inquiry could not be completed at this time.  You can try again later or contact " + 
						"us by phone.  We apologize for the inconvenience." );
				} else {
					alert( "Thank you for your interest in PaperHost.  We will route your inquiry to the appropriate person" +
						" and someone will contact you within one business day." );
					if( !isWindow ) {
						window.location = window.location.protocol + "//" + window.location.host + "/about_us_contact_us_thankyou.html";
					} else {
						this.parent.tb_remove();
					}
				}
			} else {
				//alert( http_request.status + ": [" + http_request.statusText + "]" );
				alert( "An error occurred while processing your request.  If you do not receive an email from us you can " + 
					"try again later or contact us by phone.  We apologize for the inconvenience." );
			}
		}
	} catch( err ) {
        //alert( err.name + " [" + err.lineNumber + "]: " + err.message );
		alert( "Your request could not be completed at this time.  You can try again later or contact " + 
			"us by phone.  We apologize for the inconvenience." );
	}
	
} // eo function processRequestChanges
