// JavaScript Document

<!-- Begin form validation-->

// word count
function cnt(w,x) {
	var y=w.value;
	var r = 0;
	a=y.replace(/\s/g,' ');
	a=a.split(' ');
	for (z=0; z<a.length; z++) {
		if (a[z].length > 0) r++;
	}
	x.value=r;
} 

// email validation function
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 E-mail ID")
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		//alert("Invalid E-mail ID")
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false;
	}
	if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false;
	}
	return true          
}

function ValidateStory(){
	missinginfo = "";
	// verify fields are filled out
	if (document.submit_story.first_name.value==""){
    	missinginfo += "\n     -  First Name";
  	}
	if (document.submit_story.last_name.value==""){
    	missinginfo += "\n     -  Last Name";
  	}
	var relationshipID=document.submit_story.relationship;
	var otherID=document.submit_story.other;
	if ((relationshipID.value==null)||(relationshipID.value=="")||((relationshipID.value=="other")&&((otherID.value=="")||(otherID.value=="null")))){
    	missinginfo += "\n     -  Relationship to Sinai";
  	}
	// this section validates the email address
	var emailID=document.submit_story.email_address;
	if ((emailID.value==null)||(emailID.value=="")||(echeck(emailID.value)==false)){
    	missinginfo += "\n     -  Email Address";
  	}
	if (document.submit_story.address.value==""){
    	missinginfo += "\n     -  Address";
  	}
	if (document.submit_story.city.value==""){
    	missinginfo += "\n     -  City";
  	}
	if (document.submit_story.state.value==""){
    	missinginfo += "\n     -  State";
  	}
	if(document.submit_story.zip_code.value.search(/(^\d{5}$)|(^\d{5}-\d{4}$)/)==-1){ 
		missinginfo += "\n     -  Zip Code: Please enter a 5 or 9 digit zip code with the format xxxxx or xxxxx-xxxx.";
	} 
  	if (document.submit_story.story_title.value==""){
    	missinginfo += "\n     -  Story Title";
  	}
	if (document.submit_story.story_txt.value==""){
    	missinginfo += "\n     -  Story";
  	}  
	if (document.submit_story.c.value > 150){
		missinginfo += "\n     -  Please limit your story to 150 words or less";
  	}
	if(!document.submit_story.agree.checked){
		missinginfo += "\n     -  Agree to terms";
  	} 
	if (missinginfo != "") {
		missinginfo ="Please include " + missinginfo;
		alert(missinginfo);
		return false;
	}
else return true;
}

function ValidatePicture(){
	missinginfo = "";
	// verify fields are filled out
	if (document.submit_picture.first_name.value==""){
    	missinginfo += "\n     -  First Name";
  	}
	if (document.submit_picture.last_name.value==""){
    	missinginfo += "\n     -  Last Name";
  	}
	// this section validates the email address
	var emailID=document.submit_picture.email_address;
	if ((emailID.value==null)||(emailID.value=="")){
    	missinginfo += "\n     -  Email Address";
  	}
	if (echeck(emailID.value)==false){
    	missinginfo += "\n     -  Email Address";
  	}
	if (document.submit_picture.address.value==""){
    	missinginfo += "\n     -  Address";
  	}
	if (document.submit_picture.city.value==""){
    	missinginfo += "\n     -  City";
  	}
	if (document.submit_picture.state.value==""){
    	missinginfo += "\n     -  State";
  	}
	if(document.submit_picture.zip_code.value.search(/(^\d{5}$)|(^\d{5}-\d{4}$)/)==-1){ 
		missinginfo += "\n     -  Zip Code: Please enter a 5 or 9 digit zip code with the format xxxxx or xxxxx-xxxx.";
	} 
  	if (document.submit_picture.story_title.value==""){
    	missinginfo += "\n     -  Picture Title";
  	}
	if(!document.submit_picture.agree.checked){
		missinginfo += "\n     -  Agree to terms";
  	} 
	if (missinginfo != "") {
		missinginfo ="Please include " + missinginfo;
		alert(missinginfo);
		return false;
	}
else return true;
}

function ValidateEntry(){
	missinginfo = "";
	// verify fields are filled out
	if (document.submit_entry.first_name.value==""){
    	missinginfo += "\n     -  First Name";
  	}
	if (document.submit_entry.last_name.value==""){
    	missinginfo += "\n     -  Last Name";
  	}
	// this section validates the email address
	var emailID=document.submit_entry.email_address;
	if ((emailID.value==null)||(emailID.value=="")||(echeck(emailID.value)==false)){
    	missinginfo += "\n     -  Email Address";
  	}
	if (document.submit_entry.address.value==""){
    	missinginfo += "\n     -  Address";
  	}
	if (document.submit_entry.city.value==""){
    	missinginfo += "\n     -  City";
  	}
	if (document.submit_entry.state.value==""){
    	missinginfo += "\n     -  State";
  	}
	if(document.submit_entry.zip_code.value.search(/(^\d{5}$)|(^\d{5}-\d{4}$)/)==-1){ 
		missinginfo += "\n     -  Zip Code: Please enter a 5 or 9 digit zip code with the format xxxxx or xxxxx-xxxx.";
	} 
  	if(!document.submit_entry.agree.checked){
		missinginfo += "\n     -  Agree to terms";
  	} 
	if (missinginfo != "") {
		missinginfo ="Please include " + missinginfo;
		alert(missinginfo);
		return false;
	}
else return true;
}


// double checking that deleting the record is the right thing to do
function confirmdel () {
	var answer = confirm("Are you sure you want to delete this record?\nTHERE IS NO GOING BACK\nIf you intend to contact the author please do so before you delete the record- afterwards all information will be gone.\nTo delete this record click Okay, then Submit.\nTo cancel this action click Cancel.");
	if(answer == false) {
		var elems = document.getElementsByName('action');
		for(i=0;i<elems.length;i++)
		elems[i].checked=false;
	}
}

// double checking that demoting the record is the right thing to do
function confirmdem () {
	var answer = confirm("Are you sure you want to demote this record?\nTo demote this record click Okay, then Submit.\nTo cancel this option click Cancel.");
	if(answer == false) {
		var elems = document.getElementsByName('action');
		for(i=0;i<elems.length;i++)
		elems[i].checked=false;
	}
}