
var config_temp = "" ;
var config_type = "" ;
var config_sys = "" ;
$(document).ready(function()
{
	//if ($.browser.webkit) {
		$('input').css('marginTop', 0);
		$('input').css('marginBottom',0);
		$('select').css('marginTop',0) ;
		$('select').css('marginBottom',0) ;
	//}
	$('#config-tabs').tabs() ;
	$('#txtConfigServer').focus() ;
	$('#btnConfigSave').button().bind('click',saveConfig) ;
	$('#btnConfigTest').button().bind('click',testConfig) ;
	$('#btnConfigTestNotif').button().bind('click',testConfigNotif) ;
	$('#btnConfigCreateNotif').button().bind('click',createConfigNotif) ;
	$('#btnConfigUpdateDatabase').button().bind('click',updateDatabase) ;
	$('#btnConfigClearTransaction').button().bind('click',clearTransaction) ;
	$('#btnConfigCreate').button().bind('click',createConfig) ;
	$('#txtConfigDbPort').keypress(function() { numericInput(0,'',0,0) ; })

	$('#btnConfigProcessBackup').button().bind('click',processBackup) ;
	$('#btnConfigProcessRestore').button().bind('click',processRestore) ;
}) ;
function processRestore(){
	if ($('#cobConfigRestoreFile').val() == '0') {
		$('#cobConfigRestoreFile').focus();
		alert('Please select the file first!')
		return false;
	}

	config_type = "process_restore" ;
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfig()) {
			var data = { "type": "PROCESS_RESTORE",
				"server": $('#txtConfigServer').val(),
				"dbname": $('#txtConfigDbName').val(),
				"dbuser": $('#txtConfigDbUser').val(),
				"dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwd').val(),config_temp)) ,
				"dbport": $('#txtConfigDbPort').val(),
				"dbtype": $('#cobConfigDbType').val(),
				"dbbu": $('#buName').val(),
				"restorefilename": $('#cobConfigRestoreFile').val() } ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
			config_type = "" ;
            config_sys = "";
			callServer(url,"json",data,onConfigRestoreResponse,null) ;
		}
	}
}
function processBackup(){
	if ($('#txtConfigBackupFilename').blank()) {
		$().focus();
		alert('Please fill in the backup filename first!')
		return false;
	}

	config_type = "process_backup" ;
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfig()) {
			var data = { "type": "PROCESS_BACKUP",
				"server": $('#txtConfigServer').val(),
				"dbname": $('#txtConfigDbName').val(),
				"dbuser": $('#txtConfigDbUser').val(),
				"dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwd').val(),config_temp)) ,
				"dbport": $('#txtConfigDbPort').val(),
				"dbtype": $('#cobConfigDbType').val(),
				"dbbu": $('#buName').val(),
				"backupfilename": $('#txtConfigBackupFilename').val() } ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
			config_type = "" ;
            config_sys = "";
			callServer(url,"json",data,onConfigBackupResponse,null) ;
		}
	}
}
function saveConfig() {
	config_type = "u" ;
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfigAll()) {
			var data = { "type": "u", "server": $('#txtConfigServer').val(),
				"dbname": $('#txtConfigDbName').val(), "dbuser": $('#txtConfigDbUser').val(),
				"dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwd').val(),config_temp)) ,
				"dbport": $('#txtConfigDbPort').val(), "dbtype": $('#cobConfigDbType').val(),
                "dbtypenotif": $('#cobConfigDbTypeNotif').val() ,
                "servernotif": $('#txtConfigServerNotif').val(),
                "dbnamenotif": $('#txtConfigDbNameNotif').val(),
                "dbusernotif": $('#txtConfigDbUserNotif').val(),
                "dbpwdnotif": strToHex(xxtea_encrypt($('#txtConfigDbPwdNotif').val(),config_temp)) ,
                "dbportnotif": $('#txtConfigPortNotif').val(),
                "dbtypenotif": $('#cobConfigDbTypeNotif').val(),
                "backupfilename": $('#txtConfigBackupFilename').val()
                 } ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
            config_sys = "";
			callServer(url,"json",data,onConfigResponse,null) ;
		}
	}
}
function testConfig() {
	config_type = "q" ;
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfig()) {
			var data = { "type": "q", "server": $('#txtConfigServer').val(),
				"dbname": $('#txtConfigDbName').val(), "dbuser": $('#txtConfigDbUser').val(),
				"dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwd').val(),config_temp)) ,
				"dbport": $('#txtConfigDbPort').val(), "dbtype": $('#cobConfigDbType').val() } ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
			config_type = "" ;
            config_sys = "";
			callServer(url,"text",data,onConfigTestResponse,null) ;
		}
	}
}

function testConfigNotif() {
	config_type = "q" ;
    config_sys = "notif";
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfigNotif()) {
			var data = { "type": "q",
                        "systemtype" : "notif",
                        "server": $('#txtConfigServerNotif').val(),
                        "dbname": $('#txtConfigDbNameNotif').val(),
                        "dbuser": $('#txtConfigDbUserNotif').val(),
                        "dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwdNotif').val(),config_temp)) ,
                        "dbport": $('#txtConfigDbPortNotif').val(),
                        "dbtype": $('#cobConfigDbTypeNotif').val() } ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
			config_type = "" ;
			callServer(url,"text",data,onConfigTestResponse,null) ;
		}
	}
}
function createConfigNotif() {
}
function updateDatabase() {
	config_type = "update_database" ;
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfig()) {
			var data = { "type": "UPDATE_DATABASE", "server": $('#txtConfigServer').val(),
				"dbname": $('#txtConfigDbName').val(), "dbuser": $('#txtConfigDbUser').val(),
				"dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwd').val(),config_temp)) ,
				"dbport": $('#txtConfigDbPort').val(), "dbtype": $('#cobConfigDbType').val() } ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
			config_type = "" ;
            config_sys = "";
			callServer(url,"text",data,onConfigTestResponse,null) ;
		}
	}
}
function clearTransaction() {
	config_type = "clear_transaction" ;
	if (config_temp == "") {
		var data = { "type": "g" } ;
		var url = "admin.pzx?&d=" + new Date().getTime() ;
		callServer(url,"json",data,onConfigResponse,null) ;
	} else {
		if (validateConfig()) {
			var data = { "type": "CLEAR_TRANSACTION", "server": $('#txtConfigServer').val(),
				"dbname": $('#txtConfigDbName').val(), "dbuser": $('#txtConfigDbUser').val(),
				"dbpwd": strToHex(xxtea_encrypt($('#txtConfigDbPwd').val(),config_temp)) ,
				"dbport": $('#txtConfigDbPort').val(), "dbtype": $('#cobConfigDbType').val() ,
				"dbbu": $('#buName').val(),} ;
			var url = "admin.pzx?d=" + new Date().getTime() ;

			clearConfig() ;
			config_temp = "" ;
			config_type = "" ;
            config_sys = "";
			callServer(url,"text",data,onConfigTestResponse,null) ;
		}
	}
}
function createConfig() {
}
function clearConfig() {
	$('#config_err_mesg').text("") ;
}
function onConfigResponse(obj,resp) {
	if (resp.status == "0") {
		if (resp.type == "g") {
			config_temp = resp.data ;
			if (config_type == "q"){
                if(config_sys == "notif"){
                    testConfigNotif() ;
				} else {
                    testConfig() ;
                }
            } else if (config_type == "update_database")
				updateDatabase() ;
			else if (config_type == "clear_transaction")
				clearTransaction() ;
			else if (config_type == "process_backup")
				processBackup() ;
			else if (config_type == "process_restore")
				processRestore() ;
			else
				saveConfig() ;
		} else  {
			alert(resp.mesg) ;
			clearConfig() ;
		}
	}
	else{
		alert(resp.mesg) ;
	}
}
function onConfigRestoreResponse(obj, resp){
	alert(resp.mesg) ;
}

function onConfigBackupResponse(obj, resp){
	$('#cobConfigRestoreFile').empty();
	for(i=0;i<resp.data.length;i++){
		if(resp.data[i] != "." && resp.data[i] != ".."){

			$('#cobConfigRestoreFile').append($('<option>', {
				value: resp.data[i],
				text: resp.data[i]
			}));
		}
	}
	alert(resp.mesg);
}
function onConfigTestResponse(obj,resp) {
	alert(resp) ;
	//$('#sbg-progress1').hide() ;
}
function validateConfig() {
	$('#config_err_mesg').text('') ;
	if ($('#txtConfigServer').blank())
	{
		$('#config_err_server').show() ;
		$('#txtConfigServer').focus() ;
		$('#config_err_mesg').text("Server name can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_server').hide() ;

	if ($('#txtConfigDbName').blank())
	{
		$('#config_err_name').show() ;
		$('#txtConfigDbName').focus() ;
		$('#config_err_mesg').text("Database name can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_name').hide() ;

	if (!$('#txtConfigDbPort').blank())
	{
		if (!isInt($('#txtConfigDbPort').val())) {
			$('#config_err_port').show() ;
			$('#txtConfigDbPort').focus() ;
			$('#config_err_mesg').text("Invalid Port no. Port no must be integer.") ;
			return false ;
		} else
			$('#config_err_port').hide() ;
	}
	else
		$('#config_err_port').hide() ;

	if ($('#txtConfigDbUser').blank())
	{
		$('#config_err_user').show() ;
		$('#txtConfigDbUser').focus() ;
		$('#config_err_mesg').text("Database login user name can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_user').hide() ;

	if ($('#txtConfigDbPwd').blank())
	{
		$('#config_err_pwd').show() ;
		$('#txtConfigDbPwd').focus() ;
		$('#config_err_mesg').text("Database login password can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_pwd').hide() ;

	return true ;
}

function validateConfigNotif() {
	$('#config_err_mesg').text('') ;
	if ($('#txtConfigServerNotif').blank())
	{
		$('#config_err_server').show() ;
		$('#txtConfigServerNotif').focus() ;
		$('#config_err_mesg').text("Notification Centre server name can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_server').hide() ;

	if ($('#txtConfigDbNameNotif').blank())
	{
		$('#config_err_name').show() ;
		$('#txtConfigDbNameNotif').focus() ;
		$('#config_err_mesg').text("Notification Centre database name can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_name').hide() ;

	if (!$('#txtConfigDbPortNotif').blank())
	{
		if (!isInt($('#txtConfigDbPortNotif').val())) {
			$('#config_err_port').show() ;
			$('#txtConfigDbPortNotif').focus() ;
			$('#config_err_mesg').text("Invalid Port no. Port no must be integer.") ;
			return false ;
		} else
			$('#config_err_port').hide() ;
	}
	else
		$('#config_err_port').hide() ;

	if ($('#txtConfigDbUserNotif').blank())
	{
		$('#config_err_user').show() ;
		$('#txtConfigDbUserNotif').focus() ;
		$('#config_err_mesg').text("Notification Centre database login user name can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_user').hide() ;

	if ($('#txtConfigDbPwdNotif').blank())
	{
		$('#config_err_pwd').show() ;
		$('#txtConfigDbPwdNotif').focus() ;
		$('#config_err_mesg').text("Notification Centre database login password can not be blank.") ;
		return false ;
	}
	else
		$('#config_err_pwd').hide() ;

	return true ;
}

function validateConfigAll(){
    if (!validateConfig()){
        return false;
    }
    return true;
	// without nofication centre first
    //return validateConfigNotif();
}
function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 }
