// JavaScript Document
function echeck(str) {
		var at="@"
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Invalid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert("Invalid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert("Invalid email address")
		    return false
		 }

 		 return true					
	}

function validateForm(){
	var emailID=document.getElementById('formEmail');
	var nameID=document.getElementById('formName');
	var topicID=document.getElementById('formTopic');
	var commID=document.getElementById('formComment');
	var listID=document.getElementById('formList');
	var listReq=(listID.checked==true || listID.value=="yes")?"yes":"no";
	var sendMail=document.getElementById('formSendMail');
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	if(sendMail==1){
		if ((nameID.value==null)||(nameID.value=="")){
			alert("Please enter your name")
			nameID.focus()
			return false
		}
		if ((topicID.value==null)||(topicID.value=="")){
			alert("Please enter a topic")
			topicID.focus()
			return false
		}
		
		if ((commID.value==null)||(commID.value=="")){
			alert("Please enter a comment")
			commID.focus()
			return false
		}
	} 
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	var myPage="scripts/processForm.php";
	request.open("POST", myPage, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	request.onreadystatechange = function(){
		if (request.readyState == 4 && request.status == 200) {
			if(sendMail.value==1){
				document.getElementById("contactFormDiv").style.display="none";
				document.getElementById("contactFormThanks").style.display="block";
			} else {
				document.getElementById("formEmail").value="";
				alert("Thank you! / Merci!");
				
			}
			
		}
	};
	//alert("name="+encodeURIComponent(nameID.value)+"&email="+encodeURIComponent(emailID.value)+"&topic="+encodeURIComponent(topicID.value)+"&comment="+encodeURIComponent(commID.value)+"&list="+listReq+"&sendMail="+encodeURIComponent(sendMail.value));
	request.send("name="+encodeURIComponent(nameID.value)+"&email="+encodeURIComponent(emailID.value)+"&topic="+encodeURIComponent(topicID.value)+"&comment="+encodeURIComponent(commID.value)+"&list="+listReq+"&sendMail="+encodeURIComponent(sendMail.value));
 }

function showForm(){
	var emailID=document.getElementById('formEmail');
	var nameID=document.getElementById('formName');
	var topicID=document.getElementById('formTopic');
	var commID=document.getElementById('formComment');
	var listID=document.getElementById('formList'); 
	emailID.value='';
	nameID.value='';
	topicID.value='';
	commID.value='';
	listID.checked=false;
	document.getElementById("contactFormDiv").style.display="block";
	document.getElementById("contactFormThanks").style.display="none";	
}

function toggleTag(tag){
  currTags=document.getElementById("categories").value;
  if(currTags.indexOf(tag)>0){
	  currTags=currTags.replace(tag,"");
  } else {
	  currTags+=" "+tag;
	  currTags=currTags.replace(/\s\s/g," ");
	  currTags=currTags.replace(/^\s+/,"");
  }
  document.getElementById("categories").value=currTags;
  
}

function postMe(pic){
	document.photoform.filename.value=pic;
	document.photoform.submit();
}

function pickapic(pic,me){

	if(document.getElementById('image').value!=pic){

		document.getElementById('image').value=pic;
		pics=document.getElementById('photopicker').getElementsByTagName('div');
		for(p=0;p<pics.length;p++){
			pics[p].className="thumbnail";
		}
		me.className="thumbnail2";

	} else {
		document.getElementById('image').value="";
		me.className="thumbnail";
	}
}


function showForm(formtype){
	if(formtype=="image"){
		document.getElementById("imagedata").style.display="block";
		document.getElementById("newsdata").style.display="none";
	} else {
		document.getElementById("imagedata").style.display="none";
		document.getElementById("newsdata").style.display="block";
	}
}

