/* JavaScript Document
** Build by khanhhuyna@yahoo.com 
** 06-25-2008
** This script support to create a delay with a progress bar 
*/

//Set up general paramaters;
searching_time=4000; //Time for delay in milisecond
interval_time=100;   //Interval time for progress process in milisecond
result_delay=1000;	 // The time for display the search resutl before redirect
minXXXX=1000;		 // min varibale for result
maxXXXX=9999;		 // max variberl for result
timeOut=0;			 //varible for setTimeout method.


/* This funtion display thea search result 
**  and setup a time (wait_time(milisecond)) to wait before make a redirect to url
*/
function searching_result(url,wait_time)
{
	if (wait_time==null) wait_time=result_delay;
	
	XXXX=Math.round(minXXXX+Math.random()*(maxXXXX-minXXXX));
	txtResult='Found '+XXXX + ' matching your search';	
	result_section  ='<div id="result_inside">';
	result_section +=txtResult;
	result_section +='</div>';
	document.getElementById("search_result").innerHTML=result_section;
	
	timeOut=setTimeout("redirect('"+url+"')",wait_time);
}

/* This funtion make a redirect to url
*/
function redirect(url)
{
	
	window.location.href=url;
	
}

/* This function to creat a html code for searching for
** txtCategory
*/
function create_seaching_header(txtCategory, txtStatus)
{
	if (txtStatus==null) txtStatus="Please wait...";
	html='<div id="searching">';
	html+='<div id="header">';
	html+='<div id="title">Searching for <font size="+2" color="red" ><b>'+txtCategory+'</b></font> from Database</div>';
	html+='<div id="status">'+txtStatus+'</div>';
	html+='</div>';
	html+='<div id="progress_bar">';
	html+='<div id="outside_bar">';
	html+='<div id="inside_bar">';
	html+='</div>';
	html+='</div>';
	html+='</div>';
	html+='<div id="search_result"></div>';
	html+='</div>';
	
	return html;
}

/* This function to setup for searching progress
** for node in a time "progress_time"(milisecond)
*/
function searching_progress(node,progress_time)
{
	if (progress_time==null) progress_time=0;
	
	url=node.href;
	
	//A html section for progress bar
	txtCategory=node.innerHTML;
	loading_section=create_seaching_header(txtCategory);
	
	if ((window.timeOut) & (window.timeOut != 0))
		timeOut=clearTimeout(timeOut);
	
	document.getElementById("mainbody").innerHTML=loading_section;
	init_progress(progress_time,url);
	
	
		
}

/* This funtion to init a progress process for node 
** in  progress_time(milisecond)
*/
function init_progress(progress_time,url)
{
	if (window.timeOut !=0)
	{	
		clearTimeout(timeOut);
		
	}
	loop=(progress_time%interval_time)?(Math.round(progress_time/interval_time)+1):(progress_time/interval_time);
	
	timeOut=setTimeout("progress_process(1," + loop + ",'" + url + "')" ,interval_time);
}

/* This function process the progress
** n: the current lopp
** loop: the sum of loop
** url: the href of the anchor tag, passed for redirecting after
*/

function progress_process(n,loop,url)
{
	
	if (n<loop)
	{
		percent=n/loop*100;
		document.getElementById("inside_bar").style.width=percent+"%";
		timeOut=setTimeout("progress_process("+ (n+1) + "," + loop + ",'" + url + "')",interval_time);
	}	
	else 
	{
		document.getElementById("inside_bar").style.width=100+"%";
		document.getElementById("status").innerHTML="Complete!";
		
		searching_result(url);
	}
}
