// JavaScript Document

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function contact(num) {
	//clear all spans and pritn form to appropriate span
	clear_spans();
		print_form(num);
}

function clear_spans() {
	document.getElementById('request_1').innerHTML = '';
	document.getElementById('request_2').innerHTML = '';
	document.getElementById('request_3').innerHTML = '';
	document.getElementById('request_4').innerHTML = '';
}

function print_form(a) {
	
	form_html = '<form name="myForm" id="myForm" style="width:370px; padding:4px; background-color:#FDFED8; border:1px solid #000000;">';
	form_html += '<input type="hidden" name="sendto" id="sendto" value="'+a+'">';
	form_html += 'Please include your email or phone, and a short message.  A response will be requested on your behalf.<br>';
	form_html += '<table width="365px"><tr><td width="55px;" align="left" valign="top">';
	form_html += '<strong>Email:</strong></td><td align="left" valign="top"><input type="text" id="email" name="email" style="width:200px;" value=""></td></tr>';
	form_html += '<tr><td width="55px;" align="left" valign="top"><strong>Phone:</strong></td>';
	form_html += '<td align="left" valign="top"><input type="text" id="phone" name="phone" style="width:200px;" value=""></td>';
	form_html += '</tr><tr><td colspan="2" align="left" valign="top">';
	form_html += '<strong>Comment:</strong><br>';
	form_html += '<textarea rows="3" id="comment" name="comment" style="width:275px;"></textarea>';
	form_html += '</td></tr></table>';
		form_html += '<span id="val_err" class="err"></span>';
	form_html += '<a href="javascript:validate();" class="content-link">Submit</a>&nbsp;&nbsp;';
	form_html += '<a href="javascript:clear_form();" class="content-link">Clear</a><br>';
	form_html += '<span id="mail_results"></span>';
	form_html += '</form>';
	
	document.getElementById('request_'+a).innerHTML = form_html;
}
function clear_form() {
	
document.myForm.email.value = "";
document.myForm.phone.value ="";
document.myForm.comment.value = "";
document.getElementById('mail_results').innerHTML = "";
document.getElementById('val_err').innerHTML = "";





}



function validate() {

	
	//clear error msgs
	document.getElementById('mail_results').innerHTML = "";
	document.getElementById('val_err').innerHTML = "";

	var valid = 1;
	
	//validate inputs
	if(document.myForm.email.value == "") {
	//display error and exit
		if (document.myForm.phone.value =="") {
		document.getElementById('val_err').innerHTML = " *Please include your Email or Phone<br>";
		valid = 0;
		}
		else {
			if (document.myForm.comment.value == "") {
			document.getElementById('val_err').innerHTML = " *Please include a short message.<br>";
			valid = 0;
			}	
		}
	}
	else {
		if (document.myForm.comment.value == "") {
		document.getElementById('val_err').innerHTML = " *Please include a short message.<br>";
		valid = 0;
		}	
	}
	
	
	
	
	if(valid == 1) {
	document.getElementById('mail_results').innerHTML = 'Checking....';
	sendemail();
	//alert('sending mail');
	}
	
	

}


function sendemail() {
	
	//get values
	var email = document.myForm.email.value;
	var phone = document.myForm.phone.value;
	var comment = document.myForm.comment.value;
	var sendto = document.myForm.sendto.value;
	
	document.getElementById('mail_results').innerHTML = 'Sending....';

    http.open('get', 'ajax/inter.php?email='+email+'&phone='+phone+'&sendto='+sendto+'&comment='+comment+'&action=send');
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
			//var display = document.getElementById(update[0]).innerHTML;
			if(update[0] == 'Loading...') {
			document.getElementById('mail_results').innerHTML = 'Generic Message';
			}
			else {
            document.getElementById('mail_results').innerHTML = update[1];
			}
         
        }
    }
}