var arr_pagelist = new Array() ;

var handleSuccess_ChangePage = function(o){
	if(o.responseText !== undefined){
		/*
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
		div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";
		div.innerHTML += "<li>Server response: " + o.responseText + "</li>";
		div.innerHTML += "<li>Argument object: Object ( [foo] => " + o.argument.foo +
						 " [bar] => " + o.argument.bar +" )</li>";
		*/
		var loadingarea_div = document.getElementById("LOADINGICONAREA");
		if(loadingarea_div!=null){ 
			loadingarea_div.innerHTML = " &nbsp; ";	
		}


		if(o.argument.MyTargetDIV == "DIV_MAINCONTENT"){
			window.scrollBy(-1024,-9999); 
		}
		
		if(o.argument.MyTargetDIV == "popuppane_header"){

			window.scrollBy(1,1); 
			window.scrollBy(-1,-1); 

		}
		

		var maincontent_div = document.getElementById(o.argument.MyTargetDIV);
		maincontent_div.innerHTML = o.responseText;
	}
	progresstonestpage();
}

var handleFailure_ChangePage = function(o){
	if(o.responseText !== undefined){
		/*
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
		*/
		var loadingarea_div = document.getElementById("LOADINGICONAREA");
		if(loadingarea_div!=null){ 
			loadingarea_div.innerHTML = " &nbsp; ";	
		}

		var maincontent_div = document.getElementById(o.argument.MyTargetDIV);
		maincontent_div.innerHTML = o.responseText;
	}

	progresstonestpage();
}

function progresstonestpage()
{
	if(arr_pagelist.length > 0){
		var contentparam = arr_pagelist.shift();
		if(contentparam == "1stpagemark"){
			arr_pagelist.shift();
			if(arr_pagelist.length > 0){
				contentparam = arr_pagelist.shift();
				YAHOO.util.History.navigate("suwcontent", contentparam);
			}
		}else{
			YAHOO.util.History.navigate("suwcontent", contentparam);
		}
	}
}

function MyChangePage(TargetModule, TargetFile, TargetDIV, UserParam01)
{
	
	var callback_ChangePage =
	{
	  success: handleSuccess_ChangePage,
	  failure: handleFailure_ChangePage,
	  argument: {MyTargetDIV: TargetDIV}
	};
	
	var DispLoadIcon = true;
	if(UserParam01){
		if(UserParam01.length>11){

			if(UserParam01.substring(0,11) == "NOLOADICON="){
				DispLoadIcon = false;
			}
		}
	}

	var maincontent_div = document.getElementById(TargetDIV);
	var loadingarea_div = document.getElementById("LOADINGICONAREA");
	
	if(loadingarea_div!=null){ 
		// loadingarea_div.innerHTML = "<TABLE WIDTH=\"100\" CELLSPACING=\"0\" CELLPADDING=\"0\" BORDER=\"0\" STYLE=\"font-family:verdana;font-size:8pt;color:#666666\" BGCOLOR=\"#FFFFFF\"><TR><TD ALIGN=\"left\"><IMG SRC=\"images/icons/loading.gif\" WIDTH=\"20\"/> Loading ...</TD></TR></TABLE>";
		if(DispLoadIcon){
			maincontent_div.innerHTML = "<TABLE WIDTH=\"100\" CELLSPACING=\"0\" CELLPADDING=\"0\" BORDER=\"0\" STYLE=\"font-family:verdana;font-size:8pt;color:#666666\" BGCOLOR=\"#FFFFFF\"><TR><TD ALIGN=\"left\"><IMG SRC=\"images/icons/loading.gif\" WIDTH=\"20\"/> Loading ...</TD></TR></TABLE>";
		}
	}else{
		return;
	}

	var TargetURL = 'subrender/' + TargetModule + '/' + TargetFile + '.php';
	TargetURL=TargetURL+"?sid="+Math.random()
	TargetURL=TargetURL+"&"+UserParam01

	var request = YAHOO.util.Connect.asyncRequest('GET', TargetURL, callback_ChangePage); 
}

function ChangePage(TargetModule, TargetFile, TargetDIV, UserParam01)
{
	

	var curdate = new Date()
	var xnum = curdate.getTime();
	var contentparam = ""+TargetModule+"suwcnt"+TargetFile+"suwcnt"+TargetDIV+"suwcnt"+UserParam01+"suwcnt"+xnum;

	if(arr_pagelist.length <= 0){
		// ONLY 1 //
		arr_pagelist.push("1stpagemark");
		arr_pagelist.push(contentparam);
	}else{
		arr_pagelist.push(contentparam);
	}

	if(TargetDIV != "DIV_MAINCONTENT"){
		MyChangePage(TargetModule, TargetFile, TargetDIV, UserParam01);return;
	}else{
		YAHOO.util.History.navigate("suwcontent", contentparam);
	}
	

	
	//loadSection(contentparam);
	//alert('a');
}



function loadSection(state) {
	// do cut string //
	//alert(state);

	var pageinfoarray=state.split("suwcnt");
	MyChangePage(pageinfoarray[0], pageinfoarray[1], pageinfoarray[2], pageinfoarray[3]);
	//alert(pageinfoarray[0]+" "+pageinfoarray[1]+" "+pageinfoarray[2]+" "+pageinfoarray[3]);

	
}

function CallFunction(FunctionFile, TargetDIV, UserParam01)
{
	var callback_ChangePage =
	{
	  success: handleSuccess_ChangePage,
	  failure: handleFailure_ChangePage,
	  argument: {MyTargetDIV: TargetDIV}
	};

	var maincontent_div = document.getElementById(TargetDIV);
	//maincontent_div.innerHTML = "Wait!";

	var TargetURL = FunctionFile;
	TargetURL=TargetURL+"?sid="+Math.random()
	TargetURL=TargetURL+"&"+UserParam01

	var request = YAHOO.util.Connect.asyncRequest('GET', TargetURL, callback_ChangePage); 
}


 

function initializeNavigationBar()
{

	currentSection = YAHOO.util.History.getCurrentState("suwcontent");
	//alert(currentSection);

	var pageinfoarray=currentSection.split("suwcnt");
	/*
	if(pageinfoarray[2]=="DIV_WORKINGPANE"){
		ChangePage('content','firstpage','DIV_MAINCONTENT','NOAUTOREDIRECT=TRUE');
	}*/
	ChangePage(pageinfoarray[0], pageinfoarray[1], pageinfoarray[2], pageinfoarray[3]);
	//loadSection(currentSection);
}


bookmarkedSection = YAHOO.util.History.getBookmarkedState("suwcontent");
querySection = YAHOO.util.History.getQueryStringParameter("suwcontent");
initSection = bookmarkedSection || querySection || "contentsuwcntfrontpagesuwcntDIV_MAINCONTENTsuwcntundefined";

// Register our only module. Module registration MUST take place
// BEFORE calling initializing the browser history management library!
YAHOO.util.History.register("suwcontent", initSection, function (state) {
        // This is called after calling YAHOO.util.History.navigate,
        // or after the user has trigerred the back/forward button.
        // We cannot distinguish between these two situations.
        loadSection(state);
    });

YAHOO.util.History.onReady(function () {   
	initializeNavigationBar();
}); 

// Initialize the browser history management library.
try {
	YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
} catch (e) {
	// The only exception that gets thrown here is when the browser is
	// not supported (Opera, or not A-grade) Degrade gracefully.
	// Note that we have two options here to degrade gracefully:
	//   1) Call initializeNavigationBar. The page will use Ajax/DHTML,
	//      but the back/forward buttons will not work.
	//   2) Initialize our module. The page will not use Ajax/DHTML,
	//      but the back/forward buttons will work. This is what we
	//      chose to do here:
	alert('Error history initialization');
	//loadSection("Error");
}

function ClearDIV(DIVID)
{
		document.getElementById(DIVID).innerHTML = "";
}

