// 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 test() {
		document.getElementById('mail_results').innerHTML = 'Something Happened!!';
}

function clear_mod() {
	
	clear_mod_form();
	clear_mod_results();

}

function clear_mod_form() {
	document.MapForm.resource_name.value = "";
document.MapForm.mod_antispam.value ="";
document.MapForm.comments.value = "";
document.MapForm.comment_type.value = "";
document.MapForm.relationship.value = "";
document.MapForm.com_zip.value = "";
document.MapForm.com_name.value = "";
document.MapForm.com_email.value = "";
document.getElementById('antispam_lbl').innerHTML = "";
document.getElementById('resource_name_lbl').innerHTML = "";
document.getElementById('drop_lbl').innerHTML = "";
document.getElementById('comments_lbl').innerHTML = "";
document.getElementById('comment_type_lbl').innerHTML = "";
document.getElementById('com_name_lbl').innerHTML = "";
document.getElementById('com_email_lbl').innerHTML = "";
document.getElementById('com_zip_lbl').innerHTML = "";
document.getElementById('relationship_lbl').innerHTML = "";
	
}
function clear_mod_results() {
	document.getElementById('mail_results').innerHTML = "";	
}

function clear_comments() {
		clear_comments_form();
		clear_comments_results();

}

function clear_comments_form() {
	document.ContactForm.contact_antispam.value = "";
document.ContactForm.contact_comment_type.value ="";
document.ContactForm.contact_comments.value = "";
document.ContactForm.contact_name.value = "";
document.ContactForm.contact_email.value = "";
document.ContactForm.contact_zip.value = "";
document.getElementById('contact_antispam_lbl').innerHTML = "";
document.getElementById('contact_comment_type_lbl').innerHTML = "";
document.getElementById('contact_name_lbl').innerHTML = "";
document.getElementById('contact_comments_lbl').innerHTML = "";
document.getElementById('contact_email_lbl').innerHTML = "";
document.getElementById('contact_zip_lbl').innerHTML = "";


	
}
function clear_comments_results() {
	document.getElementById('contact_results').innerHTML = "";	
}


function OnChange(dropdown)
{
	if (document.MapForm.comment_type.value == "") {
		document.getElementById('drop_com_lbl').innerHTML = "";
		valid = 0;
	}

	if (document.MapForm.comment_type.value == "Resource Correction") {
		document.getElementById('drop_com_lbl').innerHTML = " *Please tell TNR what information is not accurate about this resource.";
		valid = 0;
	}
	if (document.MapForm.comment_type.value == "Resource Addition") {
		document.getElementById('drop_com_lbl').innerHTML = " *Please supply full resource name, street address, and phone number.";
		valid = 0;
	}
	if (document.MapForm.comment_type.value == "Resource Deletion") {
		document.getElementById('drop_com_lbl').innerHTML = " *Please explain why TNR should remove this resource from this map.";
		valid = 0;
	}
	if (document.MapForm.comment_type.value == "General Comment") {
		document.getElementById('drop_com_lbl').innerHTML = " *Please tell TNR what's unique about this location.";
		valid = 0;
	}
}


function contact_validate() {

	
	//clear error msgs
	document.getElementById('contact_antispam_lbl').innerHTML = "";
document.getElementById('contact_comment_type_lbl').innerHTML = "";
document.getElementById('contact_name_lbl').innerHTML = "";
document.getElementById('contact_zip_lbl').innerHTML = "";
document.getElementById('contact_email_lbl').innerHTML = "";
document.getElementById('contact_comments_lbl').innerHTML = "";
document.getElementById('contact_results').innerHTML = "";

	var valid = 1;
	
	//validate inputs
	if(document.ContactForm.contact_antispam.value != "TNR") {
	//display error and exit
		document.getElementById('contact_antispam_lbl').innerHTML = " *Required Field (Must be TNR)";
		valid = 0;
		//document.MapForm.vendor_err.innerHTML = "*Required Field";
	}
	if (document.ContactForm.contact_comment_type.value == "") {
		document.getElementById('contact_comment_type_lbl').innerHTML = " *Please choose a comment type.";
		valid = 0;
	}
	if (document.ContactForm.contact_comments.value == "") {
		document.getElementById('contact_comments_lbl').innerHTML = " *Please add your comments.";
		valid = 0;
	}
	
	if (document.ContactForm.contact_name.value == "") {
		document.getElementById('contact_name_lbl').innerHTML = " *Please include your name.";
		valid = 0;
	}
	
	if (document.ContactForm.contact_zip.value == "") {
		document.getElementById('contact_zip_lbl').innerHTML = " *Please include your zip code.";
		valid = 0;
	}
	
	if (document.ContactForm.contact_email.value == "") {
		document.getElementById('contact_email_lbl').innerHTML = " *Please include your email address.";
		valid = 0;
	}
	
	
	if(valid == 1) {
	document.getElementById('contact_results').innerHTML = 'Checking....';
	contact_sendemail();
	}
	else {
		document.getElementById('contact_results').innerHTML = '<font color="#660000">Comment not sent.  Please check above for errors.</font>';
	}
	
	

}



function validate() {

	
	//clear error msgs
	document.getElementById('antispam_lbl').innerHTML = "";
document.getElementById('resource_name_lbl').innerHTML = "";
document.getElementById('drop_lbl').innerHTML = "";
document.getElementById('comment_type_lbl').innerHTML = "";
document.getElementById('com_name_lbl').innerHTML = "";
document.getElementById('com_email_lbl').innerHTML = "";
document.getElementById('com_zip_lbl').innerHTML = "";
document.getElementById('relationship_lbl').innerHTML = "";
document.getElementById('comments_lbl').innerHTML = "";
document.getElementById('mail_results').innerHTML = "";

	var valid = 1;
	
	//validate inputs
	if(document.MapForm.mod_antispam.value != "TNR") {
	//display error and exit
		document.getElementById('antispam_lbl').innerHTML = " *Required Field (Must be TNR)";
		valid = 0;
		//document.MapForm.vendor_err.innerHTML = "*Required Field";
	}
	if (document.MapForm.comment_type.value == "") {
		document.getElementById('comment_type_lbl').innerHTML = " *Please choose a comment type.";
		valid = 0;
	}
	
	if (document.MapForm.resource_name.value == "") {
		document.getElementById('resource_name_lbl').innerHTML = " *Please enter the name of the resource.";
		valid = 0;
	}
	if (document.MapForm.comments.value == "") {
		document.getElementById('comments_lbl').innerHTML = " *Please include details of Correction/Addition/Deletion.";
		valid = 0;
	}
	
	if (document.MapForm.com_name.value == "") {
		document.getElementById('com_name_lbl').innerHTML = " *Please include your name.";
		valid = 0;
	}
	
	if (document.MapForm.com_zip.value == "") {
		document.getElementById('com_zip_lbl').innerHTML = " *Please include your zip code.";
		valid = 0;
	}
	
	if (document.MapForm.com_email.value == "") {
		document.getElementById('com_email_lbl').innerHTML = " *Please include your email address.";
		valid = 0;
	}
	
	if (document.MapForm.relationship.value == "") {
		document.getElementById('relationship_lbl').innerHTML = " *Please provide your relationship with this resource.";
		valid = 0;
	}
	
	
	if(valid == 1) {
	document.getElementById('mail_results').innerHTML = 'Checking....';
	sendemail();
	}
	else {
		document.getElementById('mail_results').innerHTML = '<font color="#660000">Comment not sent.  Please check above for errors.</font>';
	}
	

}


function contact_sendemail() {
	
	//get values
	var anti = document.ContactForm.contact_antispam.value;
	var type = document.ContactForm.contact_comment_type.value;
	var comment = escape(document.ContactForm.contact_comments.value);
	var name = document.ContactForm.contact_name.value;
	var email = document.ContactForm.contact_email.value;
	var zip = document.ContactForm.contact_zip.value;

	document.getElementById('contact_results').innerHTML = 'Sending....';

    http.open('get', 'ajax/contact.php?msg='+comment+'&anti='+anti+'&type='+type+'&name='+name+'&email='+email+'&zip='+zip+'&action=contact');
    http.onreadystatechange = handleResponseContact;
    http.send(null);
}

function sendemail() {
	//get values
	var msg = escape(document.MapForm.comments.value);
	var type = document.MapForm.comment_type.value;
	var vendor = escape(document.MapForm.resource_name.value);
	var name = document.MapForm.com_name.value;
	var zip = document.MapForm.com_zip.value;
	var email = document.MapForm.com_email.value;
	var relation = document.MapForm.relationship.value;
	
	document.getElementById('mail_results').innerHTML = 'Sending....';

    http.open('get', 'ajax/contact.php?msg='+msg+'&type='+type+'&relation='+relation+'&vendor='+vendor+'&zip='+zip+'&email='+email+'&name='+name+'&action=resource');
    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 Error';
			}
			else {
            document.getElementById(update[0]).innerHTML = update[1];
			clear_mod_form();
			window.setTimeout("vanish_div('mail_results')", 6000);
			}
         
        }
    }
}

function handleResponseContact() {
    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('contact_results').innerHTML = 'Generic Error';
			}
			else {
            document.getElementById(update[0]).innerHTML = update[1];
			clear_comments_form();
			window.setTimeout("vanish_div('contact_results')", 6000);
			}
         
        }
    }
}

function vanish_div(divname) {
	var elem = document.getElementById(divname);
	elem.innerHTML = "";
}