var LC_xmlhttp = LC_init();
var LC_docid = "";
var progressbar = '<div align="center" style="background-color: #FFFFFF;">\n'+
								 	'<p><br></p><p><br></p><p><br></p>\n'+
								 	'<img src="images/spinner.gif" align="absmiddle">&nbsp;&nbsp;<b>loading...</b>\n'+
								 	'<p><br></p><p><br></p><p><br></p>\n'+
								 	'</div>';

var progressbarRow = '<div align="center" style="background-color: #FFFFFF; width: 200px;">\n'+
										 '<img src="images/spinner.gif" align="absmiddle">&nbsp;&nbsp;<b>loading...</b>\n'+
										 '</div>';

function LC_init()
{
	/*
	 *	will store the reference to the XMLHttpRequest object
	 */
  var xmlHttp;

  /*
   *	this should work for all browsers except IE6 and older
   */
  try
  {
  	/*
     *	try to create XMLHttpRequest object
     */
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
  	/*
     *	assume IE6 or older
     */
    var XmlHttpIE = new Array( "MSXML2.XMLHTTP.6.0",
                               "MSXML2.XMLHTTP.5.0",
                               "MSXML2.XMLHTTP.4.0",
                               "MSXML2.XMLHTTP.3.0",
                               "MSXML2.XMLHTTP",
                               "Microsoft.XMLHTTP"
		);

    /*
     *	try every prog id until one works
     */
    for ( var i=0; i<XmlHttpIE.length && !xmlHttp; i++ )
    {
      try
      {
      	/*
         *	try to create XMLHttpRequest object
         */
        xmlHttp = new ActiveXObject( XmlHttpIE[i] );
      }
      catch (e) { }
    }
  }

  /*
   *	return the created object or display an error message
   */
  if ( !xmlHttp )
  	alert( "Error creating the XMLHttpRequest object." );
  else
  	return xmlHttp;
}

/*
 * load progress bar while ajax refresh
 * put progress bar images into html
 *
 * @param       string   docid    div layer that need to refresh
 */
function LC_progressbar( docid, type )
{
	LC_docid = docid;

  if(type == "ROW")
  	progressbar = progressbarRow;

	if( document.getElementById( docid ))
		document.getElementById( docid ).innerHTML = progressbar;
	else if( parent && !parent.closed)
		parent.document.getElementById( docid ).innerHTML = progressbar;
}


/*
 * check if XMLHttpRequest is busy or not
 */
function LC_inprogress()
{
	if( LC_xmlhttp )
	{
		switch( LC_xmlhttp.readyState )
		{
			case 1: case 2: case 3:
				return true;
			break;

			default:
				return false;
		}
	}
}

/*
 * make server request using GET
 */
function LC_get( dsn, docid )
{
	if(docid != "")
		LC_progressbar(docid,'PAGE');

	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{

				LC_docid = docid;
				LC_xmlhttp.open( "GET", dsn, true );
				if(docid != "")
					LC_xmlhttp.onreadystatechange = LC_refresh;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_get( '" + dsn + "','" + docid + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}

/*
 * common response handle for the XMLHttpRequest
 */
function LC_refresh()
{
	if( LC_xmlhttp )
	{
		var documentID = "";
			documentID = document.getElementById( LC_docid );

		if( !documentID && parent && !parent.closed )
			documentID = parent.document.getElementById( LC_docid );

		if( !documentID )
			return; // exit if undefined|null|empty

		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			documentID.innerHTML = LC_xmlhttp.responseText;
		}
	}
}

/*
 * make server request using GET
 */
function LC_getProcess( dsn, docid )
{
	if(docid != "")
		LC_progressbar(docid,'PAGE');

	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{

				LC_docid = docid;
				LC_xmlhttp.open( "GET", dsn, true );
				//if(docid != "")
					LC_xmlhttp.onreadystatechange = LC_refreshProcess;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getProcess( '" + dsn + "','" + docid + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}
/*
 * response handle for the XMLHttpRequest
 */
function LC_refreshProcess()
{
	if( LC_xmlhttp )
	{
		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			location.href = location.href;
		}
	}
}

/*
 * make server request using GET
 */
function LC_getProcessDest(dsn)
{
	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{
				LC_xmlhttp.open( "GET", dsn, true );
				LC_xmlhttp.onreadystatechange = LC_refreshProcessDest;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getProcessDest( '" + dsn + "');", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}
/*
 * response handle for the XMLHttpRequest
 */
function LC_refreshProcessDest()
{
	if( LC_xmlhttp )
	{
		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			dest_arr = LC_xmlhttp.responseText.split(":")
			location.href=dest_arr[1];
		}
	}
}

/*
 * make server request using GET
 */
function LC_getSProcess( dsn)
{

	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{
				LC_xmlhttp.open( "GET", dsn, true );
				LC_xmlhttp.onreadystatechange = LC_refreshSProcess;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getSProcess( '" + dsn + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}
/*
 * response handle for the XMLHttpRequest
 */
function LC_refreshSProcess()
{
	if( LC_xmlhttp )
	{
		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			location.href = location.href;
			//self.parent.location.href=self.parent.location.href;
			//self.parent.tb_remove();
		}
	}
}

/*
 * make server request using GET
 */
function LC_getMLogin( dsn, docid )
{
	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{

				LC_docid = docid;
				LC_xmlhttp.open( "GET", dsn, true );
				LC_xmlhttp.onreadystatechange = LC_refreshMLogin;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getMLogin( '" + dsn + "','" + docid + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}

/*
 * response handle for the XMLHttpRequest for login Process
 */
function LC_refreshMLogin()
{
	if( LC_xmlhttp )
	{
		var documentID = "";
		documentID = document.getElementById( LC_docid );

		if( !documentID && parent && !parent.closed )
			documentID = parent.document.getElementById( LC_docid );

		if( !documentID )
			return; // exit if undefined|null|empty

		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			loginstatus_arr = LC_xmlhttp.responseText.split(":")

			documentID.innerHTML = loginstatus_arr[0];
			if(loginstatus_arr[1] == 10)
			{
				document.getElementById('username').value = "";
				document.getElementById('password').value = "";
				setFocus('username');
			}
			else if(loginstatus_arr[1] == 11)
			{
				document.getElementById('password').value = "";
				setFocus('password');
			}
			else if(loginstatus_arr[1] == 0)
			{
				document.getElementById('username').value = "";
				document.getElementById('password').value = "";
				setFocus('username');
			}
			else if(loginstatus_arr[1] == 1)
			{
				document.getElementById('username').disabled = true;
				document.getElementById('password').disabled = true;
				self.parent.location.href=loginstatus_arr[2];
				self.parent.tb_remove();
			}

		}

	}
}

/*
 * make server request using GET
 */
function LC_getMRegister( dsn, docid )
{
	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{

				LC_docid = docid;
				LC_xmlhttp.open( "GET", dsn, true );
				LC_xmlhttp.onreadystatechange = LC_refreshMRegister;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getMRegister( '" + dsn + "','" + docid + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}

/*
 * response handle for the XMLHttpRequest for login Process
 */
function LC_refreshMRegister()
{
	if( LC_xmlhttp )
	{
		var documentID = "";
		documentID = document.getElementById( LC_docid );

		if( !documentID && parent && !parent.closed )
			documentID = parent.document.getElementById( LC_docid );

		if( !documentID )
			return; // exit if undefined|null|empty

		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			loginstatus_arr = LC_xmlhttp.responseText.split(":")

			documentID.innerHTML = loginstatus_arr[0];

			if(loginstatus_arr[1] == 10)
			{
				document.getElementById('fname').value = "";
				setFocus('fname');
			}
			else if(loginstatus_arr[1] == 11)
			{
				document.getElementById('lname').value = "";
				setFocus('lname');
			}
			else if(loginstatus_arr[1] == 12)
			{
				document.getElementById('ic_1').value = "";
				document.getElementById('ic_2').value = "";
				document.getElementById('ic_3').value = "";
				setFocus('ic_1');
			}
			else if(loginstatus_arr[1] == 13)
			{
				document.getElementById('email').value = "";
				setFocus('email');
			}
			else if(loginstatus_arr[1] == 14)
			{
				document.getElementById('mobile').value = "";
				setFocus('mobile');
			}
			else if(loginstatus_arr[1] == 15)
			{
				document.getElementById('state').value = "";
				setFocus('state');
			}
			else if(loginstatus_arr[1] == 16)
			{
				document.getElementById('username').value = "";
				setFocus('username');
			}
			else if(loginstatus_arr[1] == 17)
			{
				document.getElementById('username').value = "";
				setFocus('username');
			}
			else if(loginstatus_arr[1] == 18)
			{
				document.getElementById('password').value = "";
				setFocus('password');
			}
			else if(loginstatus_arr[1] == 19)
			{
				document.getElementById('repeatpassword').value = "";
				setFocus('repeatpassword');
			}
			else if(loginstatus_arr[1] == 20)
			{
				document.getElementById('repeatpassword').value = "";
				document.getElementById('password').value = "";
				setFocus('password');
			}
			else if(loginstatus_arr[1] == 0)
			{
				document.getElementById('fname').value = "";
				document.getElementById('lname').value = "";
				document.getElementById('email').value = "";
				document.getElementById('mobile').value = "";
				//document.getElementById('state').value = "";
				document.getElementById('username').value = "";
				document.getElementById('password').value = "";
				document.getElementById('repeatpassword').value = "";
				setFocus('fname');
			}
			else if(loginstatus_arr[1] == 1)
			{
				document.getElementById('fname').disabled = true;
				document.getElementById('lname').disabled = true;
				document.getElementById('memail').disabled = true;
				document.getElementById('mobile').disabled = true;
				//document.getElementById('state').disabled = true;
				document.getElementById('username').disabled = true;
				document.getElementById('password').disabled = true;
				document.getElementById('repeatpassword').disabled = true;

				setTimeout("self.parent.location.href=loginstatus_arr[2];",1000);
				setTimeout("self.parent.tb_remove();",1000);
			}

		}

	}
}

/*
 * make server request using GET
 */
function LC_getLogin( dsn, docid )
{
	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{

				LC_docid = docid;
				LC_xmlhttp.open( "GET", dsn, true );
				LC_xmlhttp.onreadystatechange = LC_refreshLogin;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getLogin( '" + dsn + "','" + docid + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}

/*
 * response handle for the XMLHttpRequest for login Process
 */
function LC_refreshLogin()
{
	if( LC_xmlhttp )
	{
		var documentID = "";
		documentID = document.getElementById( LC_docid );

		if( !documentID && parent && !parent.closed )
			documentID = parent.document.getElementById( LC_docid );

		if( !documentID )
			return; // exit if undefined|null|empty

		if( LC_xmlhttp.readyState == 4 && LC_xmlhttp.status == 200 )
		{
			loginstatus_arr = LC_xmlhttp.responseText.split(":")

			documentID.innerHTML = loginstatus_arr[0];
			if(loginstatus_arr[1] == 10)
			{
				document.getElementById('username').value = "";
				document.getElementById('password').value = "";
				document.getElementById('sign_in').className = "button";
				document.getElementById('sign_in').disabled = false;
				setFocus('username');
			}
			else if(loginstatus_arr[1] == 11)
			{
				document.getElementById('password').value = "";
				document.getElementById('sign_in').className = "button";
				document.getElementById('sign_in').disabled = false;
				setFocus('password');
			}
			else if(loginstatus_arr[1] == 0)
			{
				document.getElementById('username').value = "";
				document.getElementById('password').value = "";
				document.getElementById('sign_in').className = "button";
				document.getElementById('sign_in').disabled = false;
				setFocus('username');
			}
			else if(loginstatus_arr[1] == 1)
			{
				document.getElementById('username').disabled = true;
				document.getElementById('password').disabled = true;
				location.href=loginstatus_arr[2];
			}

		}

	}
}

/*
 * make server request using GET
 */
function LC_getDelete( dsn, docid )
{

	if( LC_xmlhttp )
	{
		try
		{
			if( !LC_inprogress() )
			{

				LC_docid = docid;
				LC_xmlhttp.open( "GET", dsn, true );
				LC_xmlhttp.onreadystatechange = LC_refresh;
				LC_xmlhttp.send( null );
			}
			else {
				//--------------------
				//	try again later
				//--------------------
				setTimeout( "LC_getDelete( '" + dsn + "','" + docid + "' );", 500 );
			}
		}
		catch(e) {
			alert( e.toString() );
		}
	}
}
/*
 * delete option
 *
 * @param       string   el    				delete items link
 * @param       string   cur_p    		delete items table id
 * @param       string   index_row    delete items row id
 */
function LC_delete( el, cur_p, index_row, redirectpage_action )
{
		if( !confirm( "Are you sure you want to delete this entry?" ))
		{
			return; // exit
		}

		//	delete row
		document.getElementById( cur_p ).deleteRow(index_row)

		//	call ajax's GET to delete	items
		LC_get( el, "" );

		var page_link = document.getElementsByName('page_link')[0].value;

		//	refresh list
		LC_getDelete( page_link+"&no_header=1&no_footer=1", "content_id" );

}

function LC_delete_attachment( el, cur_p, index_row, redirectpage_action, pid, div )
{
		if( !confirm( "Are you sure you want to delete this entry?" ))
		{
			return; // exit
		}

		//	delete row
		document.getElementById( cur_p ).deleteRow(index_row)

		//	call ajax's GET to delete	items
		LC_get( el, "" );

		if (typeof(div) == 'undefined')
			div = "container";

		//	refresh list
		LC_getDelete( "proc.php?page="+cur_p+"&action="+redirectpage_action+"&id="+pid+"&no_header=1&no_footer=1", div );
}

// delete tmp attachment from tmp attachments list, which has not post to attachments table
function LC_delete_attachment_tmp( el, cur_p, index_row, div)
{
	if( !confirm( "Are you sure you want to delete this entry?" ))
	{
		return; // exit
	}

	//	delete row
	document.getElementById( cur_p ).deleteRow(index_row)

	//	call ajax's GET to delete	items
	LC_get( el, "" );

	//	refresh list
	LC_getDelete( cur_p+".php?no_header=1&no_footer=1", div );
}

/*
 * refresh page
 *
 * @param       string   page    				refresh page link
 * @param       string   loadtype    		page loading type
 */
function load_system(page,loadtype)
{
	//call ajax's GET to refresh the page
	LC_get( page+"&no_header=1&no_footer=1", "content_id" );

	return true;
}

/*
 * logout
 *
 * @param       string   encParam    				logout page link
 */
function LC_Logout( encParam )
{
	if( LC_xmlhttp )
	{
		try
		{
			if( LC_xmlhttp.readyState == 4 || LC_xmlhttp.readyState == 0 )
			{
				LC_xmlhttp.open( "GET", encParam, true );
				LC_xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
				LC_xmlhttp.onreadystatechange = handle_Logout;
				LC_xmlhttp.send( null );
			}
			else
			{
				setTimeout( "LC_Logout('" + encParam + "');", 1000 );
			}
		}
		catch(e)
		{
			alert(e.toString());
		}
	}
}

/*
 * common response handle for the logout request
 *
 * @param       string   encParam    				logout page link
 */
function handle_Logout()
{
	if( LC_xmlhttp.readyState == 4 )
	{
		if ( LC_xmlhttp.status == 200 )
		{
			try
			{
				var response = "";

				//----------------
				//	Got Error ?
				//----------------

				response = LC_xmlhttp.responseText;
				if(( response.indexOf( "ERRNO" ) >= 0 ) || ( response.indexOf( "error:" ) >= 0 ) || ( response.length == 0 ))
					throw( response.length == 0 ? "Void Server Response" : response );

				//----------------
				//	logout
				//----------------
				location.href = "index.php";
			}
			catch(e)
			{
				alert(e.toString());
			}
		}
		else
		{
			alert( LC_xmlhttp.statusText );
		}
	}
}

function LC_sort( sort_id, sort_seq, pagelimit, docid )
{

	var page_link = document.getElementsByName('page_link')[0].value;

	LC_get( page_link+"&no_header=1&no_footer=1&issort=1&pagelimit="+pagelimit+"&sort_id="+sort_id+"&sort_seq="+sort_seq, docid );
}

function LC_limit( pagelimit, docid )
{
	var page_link = document.getElementsByName('page_link')[0].value;

	LC_get( page_link+"&no_header=1&no_footer=1&pagelimit="+pagelimit, docid );
}

function LC_paging( fp, lp, page_no, paging_list, issort, sort_id, sort_seq, pagelimit, docid )
{
	var page_link = document.getElementsByName('page_link')[0].value;

	LC_get( page_link+"&no_header=1&no_footer=1&fp="+fp+"&lp="+lp+"&page_no="+page_no+"&paging_list="+paging_list+"&issort="+issort+"&sort_id="+sort_id+"&sort_seq="+sort_seq+"&pagelimit="+pagelimit, docid );
}

function LC_search( search_criteria, docid )
{
	var page_link = document.getElementsByName('page_link')[0].value;

	LC_get( page_link+"&no_header=1&no_footer=1&search_criteria="+search_criteria, docid );
}