/**
 * 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";
	if( document.getElementById( "ContactTrial" ).checked == true ||
		document.getElementById( "ContactTrial" ).value == "true" ) {
		contactTrialInterest = "true";
	}
	
	// Create XML DOM Object to pass values to the server on AJAX request
	var xmlDoc;

	if( window.ActiveXObject ) {
		xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
	} else if( document.implementation && document.implementation.createDocument ) {
		xmlDoc = document.implementation.createDocument( "", "", null );
	} else {
		alert( 'Your browser cannot handle this script' );
	}
	xmlDoc.async = false;

	var root = xmlDoc.createElement( 'DATA' );
	var field;
	
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ToAddress" );
	field.setAttribute( 'value', toAddress );

	root.appendChild( field );
	
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactName" );
	field.setAttribute( 'value', document.getElementById( "ContactName" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactEmailAddress" );
	field.setAttribute( 'value', document.getElementById( "ContactEmailAddress" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactCompany" );
	field.setAttribute( 'value', document.getElementById( "ContactCompany" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactPhone" );
	field.setAttribute( 'value', document.getElementById( "ContactPhone" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactAddress" );
	field.setAttribute( 'value', "" );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactCityStateZip" );
	field.setAttribute( 'value', "" );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "HowHearAboutUs" );
	field.setAttribute( 'value', "" );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "YearBusinessEstablished" );
	field.setAttribute( 'value', "" );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactComments" );
	field.setAttribute( 'value', document.getElementById( "ContactComments" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactTrialInterest" );
	field.setAttribute( 'value', contactTrialInterest );

	root.appendChild( field );
		
	var newXML;

	if( window.ActiveXObject ) {
		newXML = root.xml;
	} else if( document.implementation && document.implementation.createDocument ) {
		xmlDoc.appendChild( root );
		newXML = ( new XMLSerializer() ).serializeToString( xmlDoc );
	}

	var theURL = window.location.protocol + "//" + window.location.host + 
				"/paperhostweb/contactusbyemail?ts=" + new Date().getTime();
	
	sendRequest( theURL, newXML, 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";
	if( document.getElementById( "ContactTrial" ).checked == true ||
		document.getElementById( "ContactTrial" ).value == "true" ) {
		contactTrialInterest = "true";
	}
	
	// Create XML DOM Object to pass values to the server on AJAX request
	var xmlDoc;

	if( window.ActiveXObject ) {
		xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
	} else if( document.implementation && document.implementation.createDocument ) {
		xmlDoc = document.implementation.createDocument( "", "", null );
	} else {
		alert( 'Your browser cannot handle this script' );
	}
	xmlDoc.async = false;

	var root = xmlDoc.createElement( 'DATA' );
	var field;
	
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ToAddress" );
	field.setAttribute( 'value', toAddress );

	root.appendChild( field );
	
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactName" );
	field.setAttribute( 'value', document.getElementById( "ContactName" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactEmailAddress" );
	field.setAttribute( 'value', document.getElementById( "ContactEmailAddress" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactCompany" );
	field.setAttribute( 'value', document.getElementById( "ContactCompany" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactPhone" );
	field.setAttribute( 'value', document.getElementById( "ContactPhone" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactAddress" );
	field.setAttribute( 'value', document.getElementById( "ContactAddress" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactCityStateZip" );
	field.setAttribute( 'value', document.getElementById( "ContactCityStateZip" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "HowHearAboutUs" );
	field.setAttribute( 'value', document.getElementById( "HowHearAboutUs" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "YearBusinessEstablished" );
	field.setAttribute( 'value', document.getElementById( "YearBusinessEstablished" ).value );

	root.appendChild( field );
		
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactComments" );
	field.setAttribute( 'value', document.getElementById( "ContactComments" ).value );

	root.appendChild( field );
	
	field = xmlDoc.createElement( 'FIELD' );
	field.setAttribute( 'name', "ContactTrialInterest" );
	field.setAttribute( 'value', contactTrialInterest );

	root.appendChild( field );
	
	var newXML;

	if( window.ActiveXObject ) {
		newXML = root.xml;
	} else if( document.implementation && document.implementation.createDocument ) {
		xmlDoc.appendChild( root );
		newXML = ( new XMLSerializer() ).serializeToString( xmlDoc );
	}

	var theURL = window.location.protocol + "//" + window.location.host + 
				"/paperhostweb/contactusbyemail?ts=" + new Date().getTime();
	
	sendRequest( theURL, newXML, myIsWindow );
	
} // eo function contactUsPartnerPact

/**
 * This function handles the sending of AJAX request and data
 */
function sendRequest( URL, DOM, 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( DOM );
	} 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