// JavaScript Document
function fetchComment(url,dataToSend,objectID){
	var pageRequest = false
	if (window.XMLHttpRequest) pageRequest = new XMLHttpRequest();
	else if (window.ActiveXObject) pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
	else return false;
	pageRequest.onreadystatechange = function(){
		filterComment(pageRequest,objectID);
	}
	if (dataToSend) {
		
		if (objectID == 'contact'){
			
			com = document.getElementById('comment').value;
	
	var comm = com.replace(/\+/g, '<plus>');
						var commm = comm.replace(/ /g, '+');
						var commmm = commm.replace(/&/g, '<and>');
						var commmmm = commmm.replace(/\?/g, '<q>');
						var commmmmm = commmmm.replace(/'/g, '<ap>');
						var commmmmmm = commmmmm.replace(/#/g, '<num>');
						var coment = commmmmmm.replace(/(\r\n|\n|\r)/gm,"<e>");
						
			var sendData = 'name=' + document.getElementById('name').value + '&email='+document.getElementById('emailform').value+'&phone='+document.getElementById('phone').value+'&comment='+coment;
			
		}else if (objectID == 'register'){
			var sendData = 'submitted=TRUE&username='+document.getElementById('regusername').value+'&password='+document.getElementById('regpassword').value+'&passwordtoo='+document.getElementById('passwordtoo').value;
		
		}else if (objectID == 'login'){
			var sendData = 'submitted=TRUE&username='+document.getElementById('username').value+'&password='+document.getElementById('password').value;
			
		}else{
		
		var sendData = 'sendData=' + dataToSend;
		}
		pageRequest.open('POST',url,true);
		pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		pageRequest.send(sendData);
	}
	else {
		pageRequest.open('GET',url,true);
		pageRequest.send(null);
	}
}

function filterComment(pageRequest,objectID){
	var object = document.getElementById(objectID);
	if (pageRequest.readyState == 4){
		if (pageRequest.status == 200) { 
		
		
		//do here
		if (objectID == 'contact'){
		if (pageRequest.responseText == 'success'){
			document.getElementById('formsub').innerHTML = 'Thank you. Your form was submitted, we will contact you shortly.';
		}else{
			document.getElementById('formsub').innerHTML = 'ERROR! Please try again later.<br /><br />'+ pageRequest.responseText;
		}
		}
		
		
			
		}else if (pageRequest.status == 404){
			if (objectID == 'submit'){
				document.getElementById('submitbtn').innerHTML = 'There was a problem saving the registration data. Please try again later.';
			}else{
			 object.innerHTML = 'Sorry, that information is not currently available.';
			}
		}else{
			if (objectID == 'submit'){
				document.getElementById('submitbtn').innerHTML = 'There was a problem saving the registration data. Please try again later.';
			}else{
			 object.innerHTML = 'Sorry, there seems to be some kind of problem.<br />Error Code '+pageRequest.status;
			}
		}
	}
	else return;
}
function submitContact(){
	
	if (document.getElementById('name').value == 'Name:'){
		document.getElementById('nameerr').style.display='block';
		document.getElementById('nameerr').innerHTML='*Required';
	}else if(document.getElementById('emailform').value == 'Email:'){
		document.getElementById('emailerr').style.display='block';
		document.getElementById('emailerr').innerHTML='*Required';
	}else if(document.getElementById('phone').value == 'Phone:'){
		document.getElementById('phoneerr').style.display='block';
		document.getElementById('phoneerr').innerHTML='*Required';
	}else if(validateEmail(document.getElementById('emailform').value,0,0) == true){
		
	//everything checks out
	fetchComment('../php/contactform.php',1,'contact');
		
		
	}
}

function validateText(obj, txt, fin){
	if (fin == 1){ //validate
		if (obj == 'name'){
		if (document.getElementById(obj).value == ''){
			document.getElementById(obj).value = txt;
			document.getElementById('nameerr').style.display='block';
			document.getElementById('nameerr').innerHTML='*Required';
		}else{
				document.getElementById('nameerr').style.display='none';
				document.getElementById('nameerr').innerHTML='';
				
		}
		}
		if (obj == 'emailform'){
		if (document.getElementById(obj).value == ''){
			document.getElementById(obj).value = txt;
			document.getElementById('emailerr').style.display='block';
			document.getElementById('emailerr').innerHTML='*Required';
		}else{
			document.getElementById('emailerr').style.display='none';
			document.getElementById('emailerr').innerHTML='';	
			validateEmail(document.getElementById(obj).value,0,0);
		}
		}
		
		if (obj == 'phone'){
		if (document.getElementById(obj).value == ''){
			document.getElementById(obj).value = txt;
			document.getElementById('phoneerr').style.display='block';
			document.getElementById('phoneerr').innerHTML='*Required';
		}else{
			document.getElementById('phoneerr').style.display='none';
			document.getElementById('phoneerr').innerHTML='';	
		}
		}
		if (obj == 'comment'){
		if (document.getElementById(obj).value == ''){
			document.getElementById(obj).value = txt;
		}
		}
		
		
		
	
	}else{ //clear form
		if (document.getElementById(obj).value == txt){
		document.getElementById(obj).value = '';
		}
		
	}
}
function validateEmail(addr,man,db) {
	
	document.getElementById('emailform').value = addr.toLowerCase();
	addr = addr.toLowerCase();

var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) {
		   document.getElementById('emailerr').innerHTML = '*Invalid';
		document.error = 1;
	  }
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) {
		   document.getElementById('emailerr').innerHTML = '*Invalid';
	  
		document.error = 1;
	  }
		 
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) {
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	  
		document.error = 1;
	   
   }
   return false;
}
if (atPos == 0) {
   if (db) {
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	  
		document.error = 1;
   }
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) {
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	  
		document.error = 1;
   }
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db){
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	   
		document.error = 1;
   }
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) {
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	   
		document.error = 1;
   }
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) {
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	  
		document.error = 1;
   }
	
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) {
	    document.getElementById('emailerr').innerHTML = '*Invalid';
	  
		document.error = 1;
   }
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) {
	   document.getElementById('emailerr').innerHTML = '*Invalid';
	 
		document.error = 1;
   }
   return false;
}

document.getElementById('nameerr').innerHTML='';

return true;
}

