/*-------------------------------------------------------------------------
	-- File: xajax.js
	-- Purpose: JavaScript functions, primarily for AJAX calls
	-- Note:						
	--
	--  History:
			Date			By				Comments
		----------- 		----------		-------------------------------------
	-- 17 JUN 2008			S Martin		Created
	-- 01 FEB 2010			S Martin		Abstracting some of the code
	-- --------------------------------------------------------------------
*/

function makeSublist(parent,child,isSubselectOptional,childVal)
{
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));
	
	var parentValue = $('#'+parent).attr('value');
	$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child+' option[@value="'+ childVal +'"]').attr('selected','selected');
	$('#'+parent).change( 
		function()
		{
		
			var parentValue = $('#'+parent).attr('value');

			$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

			if(isSubselectOptional) 
				$('#'+child).prepend("<option value='none'> -- Select -- </option>");

			$('#'+child).trigger("change"); 
			$('#'+child).focus();

		}
	);
}

/*function createDependentList(parent,child,isSubselectOptional,childVal)
{
	$(document).ready(function()
	{
		makeSublist(parent,child,isSubselectOptional,childVal);	
	});
}*/

function update_sel_options(parent,curr_parent_val,child,curr_child_val){
    /******************************************************************************
    DESCRIPTION: Adds a category using asynchronus JavaScript
    
    PARAMETERS:
        parent 			- id of the parent element whose selected value will drive the change to the child element-
		curr_parent_val - current db val of the parent select item
		child			- id of the child select box to be updated
		curr_child_val	- current db val of the child select item
    
    RETURNS:
        N/A 
    ******************************************************************************/

	//get the currently selected value of the parent item
	p_elem = document.getElementById(parent);
	p_select_val = p_elem.options[p_elem.selectedIndex].value;
	
	//alert("Selected value is "+p_select_val+"; action is upd_"+child);
	//return false;



	//syntax: new Ajax.Updater(container, url[, options])
	var params = 'parent=' + parent + '&curr_parent_val=' + curr_parent_val + '&child=' + child + '&curr_child_val=' + curr_child_val + '&p_select_val=' + p_select_val + '&ajax_action=upd_region';
	var url = 'front-ajax.php';
	
	//jQuery AJAX
	$(document).ready(function(){
		$.ajax({
		   type: "POST",
		   url: url,
		   data: params,
		   success: function(response){
			   //alert("response = "+response);
				$("select#"+child).html(response);
		   }
	   });
	 }); 


}//function update_sel_options


function submit_comment(sid,uid){
    /******************************************************************************
    DESCRIPTION: Submit comment via asynchronus JavaScript
    
    PARAMETERS:
        sid 			- id of the content item to which the comment applies
		uid				- id of the submitting user
    
    RETURNS:
        comment submission message 
    ******************************************************************************/

	//get the currently selected value of the parent item
	text = document.getElementById('comment_text').value;
	//text = escape(text);
	text = encodeURIComponent(text);
	//alert("Submitted text is "+text);
	//return false;



	//syntax: new Ajax.Updater(container, url[, options])
	var params = 'sid=' + sid + '&uid=' + uid + '&comment_text=' + text + '&ajax_action=upd_commentlist';
	var url = "/front-ajax.php";
	
	//jQuery AJAX
	$(document).ready(function(){
		$.ajax({
		   type: "POST",
		   url: url,
		   data: params,
		   success: function(response){
				$("#comment_list").html(response);
		   }
	   });
	 }); 

	return;
}//function submit_comment

function refresh_commentlist(sid){
    /******************************************************************************
    DESCRIPTION: Update comment list via asynchronus JavaScript
    
    PARAMETERS:
        sid 			- id of the content item to which the comment applies
    
    RETURNS:
        updated comment list 
    ******************************************************************************/

	text = '';
	uid = '';

	var params = 'sid=' + sid + '&uid=' + uid + '&comment_text=' + text + '&ajax_action=upd_commentlist';
	var url = "/front-ajax.php";
	
	$(document).ready(function(){
		$.ajax({
		   type: "POST",
		   url: url,
		   data: params,
		   success: function(response){
				$("#comment_list").html(response);
		   }
	   });
	 }); 

	return;
}//function refresh_commentlist


function trackEvent(category,cid){
	//alert("Category = "+category+", cid = "+cid);

	var params = 'cid=' + cid + '&category=' + category + '&ajax_action=track_event';
	var url = "/front-ajax.php";
	
	$(document).ready(function(){
		$.ajax({
		   type: "POST",
		   url: url,
		   data: params,
		   success: function(response){
		   }
	   });
	 }); 

	return;
}//function trackEvent


