
// this function starts each periodic executer function when the page loads and keeps divs automatically updating without the need for a page refresh
function startRefreshers() {
	
	//periodic_entries = new Ajax.PeriodicalUpdater('entries','/ajax_functions.php', {evalScripts: true, parameters: { action: 'refresh_entries'}, frequency: 60, decay: 2});
	new PeriodicalExecuter(refresh_funfact, 150);
	new PeriodicalExecuter(refresh_comments, 60);
	new PeriodicalExecuter(refresh_poll, 150);
	new PeriodicalExecuter(changeheader, 100);	
	
}

// this function refreshes the fun facts
function refresh_funfact() {
	new Ajax.Updater('funfact','/fun_facts.php');
}

// this function refreshes the entries div
function refresh_entries() {
	new Ajax.Updater('entries','/ajax_functions.php', {evalScripts: true, parameters: { action: 'refresh_entries'}});
}

// this function refreshes the comments div
function refresh_comments() {
	new Ajax.Updater('comments','/ajax_functions.php', {evalScripts: true, parameters: { action: 'refresh_comments'}});
}

// this function refreshes the poll
function refresh_poll() {
	new Ajax.Updater('poll','/ajax_functions.php', {evalScripts: true, parameters: { action: 'refresh_poll'}});
}

// this function refreshes the commenter form
function refresh_commentor(){
		var cookievalue = GetCookie('PHPSESSID');
		new Ajax.Updater('commentor_div','/commentor.php', {evalScripts: true, parameters: { action: 'use_global',cookie: cookievalue}});
}

// this function adds new entries
function add_entry() {
	if(document.getElementById('entry').value != "") {
		new Ajax.Updater('entries','/ajax_functions.php', {evalScripts: true, parameters: { action: 'add_entry',entry: document.getElementById('entry').value}});
		document.getElementById('entry').value = "";
		//new Effect.BlindUp(document.getElementById("entry_form_inside_div"), {duration: .4}),
		//document.getElementById("hide_entry_button").style.display = "none",
		//document.getElementById("show_entry_button").style.display = "block";		
	}
}


function pause_entries_for_video_viewing() {
	periodic_entries.stop();
	setTimeout('periodic_entries.start()', 300000);
}

// this function shows the entry form
function edit_entry(id) {
	periodic_entries.stop();	
	document.getElementById(id).style.display = 'block';
}

// this function pauses the automatic updates for the entry div while a new entry is being written
function cancel_update(id){
	periodic_entries.start();
	document.getElementById(id).style.display = 'none';
}

// this function updates entries
function update_entry(id) {
	if(document.getElementById('entry' + id).value != "") {
		new Ajax.Updater('entries','/ajax_functions.php', {evalScripts: true, parameters: { action: 'update_entry',id: id, entry: document.getElementById('entry' + id).value}});
		periodic_entries.start();		
	}
}

// this function deletes entries
function delete_entry(id) {

	if(id) {
		var answer = confirm("Do you really want to delete this entry?");
		if(answer) {	
			new Ajax.Updater('entries','/ajax_functions.php', {parameters: { action: 'delete_entry',id: id}});		
		}
	}
}

// this function adds comments
function add_comment() {
	if(document.getElementById('comment').value != "" && document.getElementById('commentor').value != "") {
		new Ajax.Updater('comments','/ajax_functions.php', {evalScripts: true, parameters: { action: 'add_comment',comment: document.getElementById('comment').value,commentor: document.getElementById('commentor').value}});
		setTimeout("refresh_commentor()",200);
		document.getElementById('comment').value = "";
	} else {
	
		if(document.getElementById('comment').value == "") {
			document.getElementById('comment').style.backgroundColor = "#FFFFAA";
			document.getElementById('comment').focus();
		} else {
			document.getElementById('comment').style.backgroundColor = "#FFFFFF";
		}
	
		if(document.getElementById('commentor').value == "") {
			document.getElementById('commentor').style.backgroundColor = "#FFFFAA";
			if(document.getElementById('comment').value != "") {
				document.getElementById('commentor').focus();
			}
		} else {
			document.getElementById('commentor').style.backgroundColor = "#FFFFFF";
		}
	}
	
}

// this function deletes comments
function delete_comment(id) {

	if(id) {
		var answer = confirm("Do you really want to delete this comment?");
		if(answer) {	
			new Ajax.Updater('comments','/ajax_functions.php', {parameters: { action: 'delete_comment',id: id}});		
		}
	}
}

// this function handles saved name changes in the comment form
function changename() {
		new Ajax.Request('/ajax_functions.php', {parameters: { action: 'change_name'}});
		setTimeout("refresh_commentor()",200);		
}

//this function refreshes the thumbs in the div and is called from a few other functions
function refresh_thumb_div() {
	new Ajax.Updater('uploaded_thumbs','/ajax_functions.php', {parameters: { action: 'reload_upload_thumbs'}});
}

//this function gets called from Flash after each photo finishes loading
function photoUploadOneComplete() {
	refresh_thumb_div();
}

// this function handles poll submissions
function submitPoll(pollid	) {

	//get selected radio button value
	for (var i=0; i < document.pollform.pollvote.length; i++) {
	   if (document.pollform.pollvote[i].checked) {
		  var pollvote = document.pollform.pollvote[i].value;
	   }
	}
	
	if(!pollvote || !pollid) {
		alert("Awe man!  You've gotta select something if you want to vote.");
	} else {
		new Ajax.Updater('poll','/ajax_functions.php', {evalScripts: true,parameters: { action: 'add_vote',pollid: pollid, vote: pollvote}});
	}

}

//this function will clear the php $_SESSION variable for photos
function clearphotos(){
	new Ajax.Updater('uploaded_thumbs','/ajax_functions.php', {parameters: { action: 'clear_photo_session'}});
}

//this function refreshes the thumbs in the div and is called from a few other functions
function viewphotos() {
	new Effect.Appear(document.getElementById('thumbs_overlay'), { duration: .3,from: 0.0001, to: 0.8} );
	new Effect.Appear(document.getElementById('smalloverlay'), { duration: .4,from: 0.0001, to: 1.0} );
	new Ajax.Updater('all_thumbs','/ajax_functions.php', {parameters: { action: 'view_all_thumbs'}});
}

//this function with swap out the header once the baby has arrived and show a new header with a photo of the full family
function changeheader() {
	if(document.getElementById("header_image")) {
		if(document.getElementById("header_image").value != "") {
			//alert("url(" + document.getElementById("header_image").value + ")");
			document.getElementById("header").style.background = "url(/images/" + document.getElementById("header_image").value + ")";
		}
	}
}




// the next two functions are used to insert the [img] tag into an entry
function setCaretTo(obj, pos) { 
    if(obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move("character", pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
} 


function insertAtCaret(obj, text) {
	if(document.selection) {
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n");
		var range = document.selection.createRange();

		if(range.parentElement() != obj) {
			return false;
		}

		range.text = text;
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		for(var index = 0, start = 0; 
			tmp.match(text) 
				&& (tmp = tmp.replace(text, "")) 
				&& index <= diff; 
			index = start + text.length
		) {
			start = actual.indexOf(text, index);
		}
	} else if(obj.selectionStart) {
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}
	
	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {
		obj.value += text;
	}
}


//this function gets the cookie for Flash
function GetCookie(sName) {
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}