// 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 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 pop_comments(vendor_info) {

	
	update = vendor_info.split('|');
	//var temp = vendor[0].replace(/&#146;/, "");
	document.MapForm.resource_name.value = update[0].replace(/_/g, " ");
	document.MapForm.vendorID.value = update[1];
	
	document.MapForm.mod_antispam.focus();
	setTimeout('scrolldown()',100); 
	


}

function scrolldown() {
	window.scroll(0,1200); // horizontal and vertical scroll targets
}

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;
	} else if (validateEmail(document.MapForm.com_email.value)) {
		//valid email	
			}
		else {
			
			document.getElementById('com_email_lbl').innerHTML = " Invalid 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 validateEmail(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(address) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   else {
	return true;   
   }
}

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 vendorID = document.MapForm.vendorID.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;
	var mapName = document.MapForm.map.value;
	
	document.getElementById('mail_results').innerHTML = 'Sending....';

    http.open('get', 'http://www.thenaturalresource.com/ajax/map_formsV2.php?msg='+msg+'&type='+type+'&relation='+relation+'&vendor='+vendor+'&zip='+zip+'&email='+email+'&name='+name+'&vendorID='+vendorID+'&mapname='+mapName+'&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();
			document.MapForm.vendorID.value = "NA";
			window.setTimeout("vanish_div('mail_results')", 6000);
			}
         
        }
    }
}

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

function charter_addition() {
	document.MapForm.comment_type.selectedIndex = 1;	
	document.MapForm.mod_antispam.focus();
	setTimeout('scrolldown()',100); 
	


}