/*--------------------------------------------*/
// New object
/*--------------------------------------------*/
function ajax_request()
{
	this.isIE               = false;
	this.allow_use          = true ;
	this.xmlhandler         = null;
	this.error_string       = '';
	this.nocache			= true;
	this.do_request_functon = function() {}
	this.loading_fired		= 0;
	this.centerdiv          = null;
}
/*--------------------------------------------*/
// Initiate
/*--------------------------------------------*/
ajax_request.prototype.xml_init = function()
{
	try
	{
		//------------------------------------------------
		// Moz, Safari, Opera
		//------------------------------------------------
		this.xmlhandler = new XMLHttpRequest();
		this.ie        = false;
		this.allow_use = true;
		return true;
	}
	catch(e)
	{
		try
		{
			//------------------------------------------------
			// IE
			//------------------------------------------------
			this.xmlhandler = new ActiveXObject('Microsoft.XMLHTTP');
			this.ie        = true;
			this.allow_use = true;
			return true;
		}
		catch(e)
		{
			this.ie        = true;
			this.allow_use = false;
			return false;
		}
	}
}
/*--------------------------------------------*/
// Actually send data
/*--------------------------------------------*/
ajax_request.prototype.process = function( url, type, post )
{
	//------------------------------------------------
	// The 'post' variable needs to be in the following format:
	//
	// var=content&var2=content&var3=content
	//
	// All values need to be escaped with encodeURIComponent();
	//------------------------------------------------
	type = type == "POST" ? "POST" : "GET";
	//------------------------------------------------
	// Use nocache where possible...
	//------------------------------------------------
	if ( this.nocache == true  && type == 'GET' )
	{
		url = this.nocache_url( url );
	}
	//------------------------------------------------
	// Make sure we're initialized
	//------------------------------------------------
	if ( ! this.xmlhandler )
	{
		this.xml_init();
	}
	//------------------------------------------------
	// Only go when ready
	//------------------------------------------------
	if ( ! this.readystate_not_ready() )
	{
		this.xmlhandler.open(type, url, true);
		if ( type == "GET" )
		{
			this.xmlhandler.send(null);
		}
		else
		{
			if ( typeof( this.xmlhandler.setRequestHeader ) != "undefined" )
			{
				this.xmlhandler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=' + use_charset);
			}
			this.xmlhandler.send( post );
		}
		if ( this.xmlhandler.readyState == 4 && this.xmlhandler.status == 200 )
		{
			return true;
		}
	}
	return false;
}
/*--------------------------------------------*/
// retrieve text of an XML document element, including
// elements using namespaces
/*--------------------------------------------*/
ajax_request.prototype.get_element_text_ns = function(prefix, local, parentElem, index)
{
    var result = "";
    if ( prefix && this.isIE )
    {
        //-------------------------------
        // IE
        //-------------------------------
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    }
    else
    {
        //-------------------------------
        // Safari, Gecko
        //-------------------------------
        result = parentElem.getElementsByTagName(local)[index];
    }
    if ( result )
    {
        if (result.childNodes.length > 1)
        {
            return result.childNodes[1].nodeValue;
        }
        else
        {
            return result.firstChild.nodeValue;
        }
    }
    else
    {
        return "n/a";
    }
}
/*--------------------------------------------*/
// Make sure URL is not cached
/*--------------------------------------------*/
ajax_request.prototype.nocache_url = function (url)
{
	var sep    = ( -1 < url.indexOf("?") ) ? "&" : "?";
	var mydate = new Date();
	var newurl = url + sep + "__=" + mydate.getTime();
	return newurl;
}
/*--------------------------------------------*/
// Takes an array of:
// array[ field ] = value
// and returns a nice encoded POST string
/*--------------------------------------------*/
ajax_request.prototype.format_for_post = function( arrayfields )
{
	var str = '';
	try
	{
		for( var i in arrayfields )
		{
			str += i + '=' + this.encodeurl(arrayfields[i]) + '&';
		}
	}
	catch(e)
	{
	}
	return str;
}
/*--------------------------------------------*/
// Hand roll encode URL to UTF-8
/*--------------------------------------------*/
ajax_request.prototype.encodeurl = function( url )
{
	//-------------------------------
	// Ensure we have a string
	//-------------------------------
	url = url.toString();
	var regcheck = url.match(/[\x90-\xFF]/g);
	if ( regcheck )
	{
		for (var i = 0; i < i.length; i++)
		{
			url = url.replace(regcheck[i], '%u00' + (regcheck[i].charCodeAt(0) & 0xFF).toString(16).toUpperCase());
		}
	}
	return escape(url).replace(/\+/g, "%2B");
}

/*--------------------------------------------*/
// Check to ensure ready state-ness
/*--------------------------------------------*/
ajax_request.prototype.readystate_not_ready = function()
{
	return ( this.xmlhandler.readyState && ( this.xmlhandler.readyState < 4 ) );
}
/*--------------------------------------------*/
// Check to ensure ready state-ness
/*--------------------------------------------*/
ajax_request.prototype.readystate_ready_and_ok = function()
{
	return ( this.xmlhandler.readyState == 4 && this.xmlhandler.status == 200 ) ? true : false;
}
/*--------------------------------------------*/
// Onready state change event handler
/*--------------------------------------------*/
ajax_request.prototype.onreadystatechange = function( event )
{
	//------------------------------------------------
	// Make sure we're initialized
	//------------------------------------------------
	if ( ! this.xmlhandler )
	{
		this.xml_init();
	}
	//------------------------------------------------
	// Make sure its a function event
	//------------------------------------------------
	if ( typeof(event) == 'function' )
	{
		this.xmlhandler.onreadystatechange = event;
	}
}
/*--------------------------------------------*/
// Show loading layer
/*--------------------------------------------*/
ajax_request.prototype.show_loading = function( message )
{
	if ( ! this.loading_fired )
	{
		this.loading_fired = 1;
		//------------------------------------------------
		// Change text?
		//------------------------------------------------
		if ( message )
		{
			document.getElementById( 'loading-layer-text' ).innerHTML = message;
		}
		this.centerdiv         = new center_div();
		this.centerdiv.divname = 'loading-layer';
		this.centerdiv.move_div();
	}
	return;
}
/*--------------------------------------------*/
// Hide loading layer
/*--------------------------------------------*/
ajax_request.prototype.hide_loading = function()
{
	try
	{
		if ( this.centerdiv && this.centerdiv.divobj )
		{
			this.centerdiv.hide_div();
		}
	}
	catch(e)
	{
	}
	this.loading_fired = 0;
	return;
}
function Tcheck(target, astr, lmin, lmax, targetname)
{
	var i
	var t = target.value
	if (t.length < lmin || t.length > lmax)
	{
        alert(" Vui long nhap so hay ky tu tu "+ lmin + ' ~ ' + lmax + " ky tu");
		return true ;
	}
	if (astr.length > 1)
	{
		for (i=0; i<t.length; i++) {
			if(astr.indexOf(t.substring(i,i+1))<0)
			{
				return true ;
			}
		}
	}
	return false ;
}

//==========================================
// Set up
//==========================================
var input_red      = 'input-warn';
var input_green    = 'input-ok';
var img_blank      = 'spacer.gif';
var img_tick       = 'check.jpg';
var img_cross      = 'fall.jpg';
var reg_field_ids = new Array();
var reg_img_ids   = new Array();
var reg_msg_ids   = new Array();
var Alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
var Digit = '1234567890'
var ucp_dname_illegal_chars = new Array( '[', ']', '|', ',', ';' );
var ucp_dname_illegal_regex = '';
for ( var i in ucp_dname_illegal_chars )
{
	ucp_dname_illegal_regex += '\\' + ucp_dname_illegal_chars[i];
}
var reg_oktogo    = 0;
var error_email  = 0;
var error_code = 0
/*-------------------------------------------------------------------------*/
// INIT Reg form
/*-------------------------------------------------------------------------*/
function init_reg_form( )
{
	//------------------------------------------
	// INIT objects
	//------------------------------------------
	//------------------------------------------
	// Fields...
	//------------------------------------------
	reg_field_ids['username']   	 = document.getElementById( 'user_name' );
	reg_field_ids['email_address']    = document.getElementById( 'email_address' );
	reg_field_ids['pass_word']    		= document.getElementById('pass_word');
	reg_field_ids['re_pass_wordword']    = document.getElementById( 're_pass_wordword' );
	reg_field_ids['full_name']    = document.getElementById( 'full_name' );
	reg_field_ids['security_code']    = document.getElementById( 'security_code' );
	reg_field_ids['h_code']    = document.getElementById( 'h_code' );

	//------------------------------------------
	// Images
	//------------------------------------------
	reg_img_ids['email_address']   = document.getElementById( 'img-email_address' );
	reg_img_ids['username']   = document.getElementById( 'img-username' );
	reg_img_ids['pass_word']    = document.getElementById( 'img-pass_word' );
	reg_img_ids['re_pass_wordword']    = document.getElementById( 'img-re_pass_wordword' );
	reg_img_ids['full_name']    = document.getElementById( 'img-full_name' );
	reg_img_ids['security_code']    = document.getElementById( 'img-security_code' );

	//------------------------------------------
	// Messages
	//------------------------------------------
	reg_msg_ids['email_address']         = document.getElementById( 'msg-email_address' );
	reg_msg_ids['username']         = document.getElementById( 'msg-username' );
	reg_msg_ids['pass_word']    = document.getElementById( 'msg-pass_word' );
	reg_msg_ids['re_pass_wordword']    = document.getElementById( 'msg-re_pass_wordword' );
	reg_msg_ids['full_name']    = document.getElementById( 'msg-full_name' );
	reg_msg_ids['security_code']    = document.getElementById( 'msg-security_code' );

	//------------------------------------------
	// Set up onblur
	//------------------------------------------
	reg_field_ids['username'].onblur     = check_username;
	reg_field_ids['email_address'].onblur     = check_email_address;
	reg_field_ids['pass_word'].onblur    = check_pass_word;
	reg_field_ids['re_pass_wordword'].onblur     = check_re_pass_wordword;
	reg_field_ids['full_name'].onblur     = check_full_name;
	reg_field_ids['security_code'].onblur     = check_security_code;
	//------------------------------------------
	// Already got error messages?
	//------------------------------------------
}
/*-------------------------------------------------------------------------*/
// INIT Reg form
/*-------------------------------------------------------------------------*/
function init_complete_login_form(){
}
/*-------------------------------------------------------------------------*/
// Check email_address
/*-------------------------------------------------------------------------*/
function check_username( event )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var error_found = '';
	//----------------------------------
	// Make sure we have sommat
	//----------------------------------
		if (reg_field_ids['username'].value == '')
		{
			error_found=reg_error_username_empty+"<br>";
		}
		if ( error_found )
		{
			reg_field_ids['username'].className   = input_red;
			reg_img_ids['username'].src           = var_image_url + '/' + img_cross;
			reg_msg_ids['username'].innerHTML     = error_found;
			error_email=1;
		}
		else
		{

			var url = './?check=username&key='+escape( reg_field_ids['username'].value );

			/*--------------------------------------------*/
			// Main function to do on request
			// Must be defined first!!
			/*--------------------------------------------*/
			do_request_function = function()
			{
				//----------------------------------
				// Ignore unless we're ready to go
				//----------------------------------
				if ( ! xmlobj.readystate_ready_and_ok() )
				{
					// Could do a little loading graphic here?
					return;
				}
				//----------------------------------
				// INIT
				//----------------------------------
				var html = escape(xmlobj.xmlhandler.responseText);
				if ( html == 'found' )
				{
					error_found = reg_error_username_taken+"<br />";
					reg_field_ids['username'].className   = input_red;
					reg_img_ids['username'].src           = var_image_url + '/' + img_cross;
					reg_msg_ids['username'].innerHTML     = error_found;
					error_email=1;
				}
				if ( html == 'notfound' )
				{
					error_found ="";
					reg_field_ids['username'].className   = input_green;
					reg_img_ids['username'].src           = var_image_url + '/' + img_tick;
					reg_msg_ids['username'].innerHTML     = '';
					error_email=0;
				}
			}
			//----------------------------------
			// LOAD XML
			//----------------------------------
			xmlobj = new ajax_request();
			xmlobj.onreadystatechange( do_request_function );
			xmlobj.process( url );
		}
		error_found = '';
}

function check_email_address( event )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var error_found = '';
	//----------------------------------
	// Make sure we have sommat
	//----------------------------------
		var re =/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,5})$/gi;
		var email = reg_field_ids['email_address'];
		if (reg_field_ids['email_address'].value == '')
		{
			error_found=reg_error_email_empty+"<br>";
		}
		if (reg_field_ids['email_address'].value != '' && reg_field_ids['email_address'].value.match(re)==null)
		{
			error_found=reg_error_email_invalid+"<br>";
		}
		if ( error_found )
		{
			reg_field_ids['email_address'].className   = input_red;
			reg_img_ids['email_address'].src           = var_image_url + '/' + img_cross;
			reg_msg_ids['email_address'].innerHTML     = error_found;
			error_email=1;
		}
		else
		{

			var url = './?check=email&key='+escape( reg_field_ids['email_address'].value );
			/*--------------------------------------------*/
			// Main function to do on request
			// Must be defined first!!
			/*--------------------------------------------*/
			do_request_function = function()
			{
				//----------------------------------
				// Ignore unless we're ready to go
				//----------------------------------
				if ( ! xmlobj.readystate_ready_and_ok() )
				{
					// Could do a little loading graphic here?
					return;
				}
				//----------------------------------
				// INIT
				//----------------------------------
				var html = escape(xmlobj.xmlhandler.responseText);
				//alert('html='+html)
				if ( html == 'found' )
				{
					error_found = reg_error_email_taken+"<br />";
					reg_field_ids['email_address'].className   = input_red;
					reg_img_ids['email_address'].src           = var_image_url + '/' + img_cross;
					reg_msg_ids['email_address'].innerHTML     = error_found;
					error_email=1;
				}
				if ( html == 'notfound' )
				{
					error_found ="";
					reg_field_ids['email_address'].className   = input_green;
					reg_img_ids['email_address'].src           = var_image_url + '/' + img_tick;
					reg_msg_ids['email_address'].innerHTML     = '';
					error_email=0;
				}
			}
			//----------------------------------
			// LOAD XML
			//----------------------------------
			xmlobj = new ajax_request();
			xmlobj.onreadystatechange( do_request_function );
			xmlobj.process( url );
		}
		error_found = '';
}
/*-------------------------------------------------------------------------*/
// Check pass_wordword
/*-------------------------------------------------------------------------*/
function check_pass_word( event )
{
	var error_found = '';
	if ( ! reg_field_ids['pass_word'].value || reg_field_ids['pass_word'].value.length < 3 || reg_field_ids['pass_word'].value.length > 25 )
	{
		error_found += reg_error_pass_wordword;
	}

		if ( error_found )
		{
			reg_field_ids['pass_word'].className   = input_red;
			reg_img_ids['pass_word'].src           = var_image_url + '/' + img_cross;
			reg_msg_ids['pass_word'].innerHTML     = error_found;
		}
		else
		{
			reg_field_ids['pass_word'].className   = input_green;
			reg_img_ids['pass_word'].src           = var_image_url + '/' + img_tick;
			reg_msg_ids['pass_word'].innerHTML     = '';
		}
		error_found = '';
	if ( reg_field_ids['re_pass_wordword'].value != '')
		check_re_pass_wordword( event );
		
		
}
/*-------------------------------------------------------------------------*/
// Check pass_wordword
/*-------------------------------------------------------------------------*/
function check_re_pass_wordword( event )
{
	var error_found = '';
	if (  reg_field_ids['pass_word'].value != reg_field_ids['re_pass_wordword'].value )
	{
		error_found =reg_error_confirm_pass_wordword+" <br />";
	}
	if (  !reg_field_ids['re_pass_wordword'].value  )
	{
		error_found =reg_empty_confirm_pass_wordword+"<br />";
	}
	if ( error_found )
		{
			reg_field_ids['re_pass_wordword'].className   = input_red;
			reg_img_ids['re_pass_wordword'].src           = var_image_url + '/' + img_cross;
			reg_msg_ids['re_pass_wordword'].innerHTML     = error_found;
		}
		else
		{
			reg_field_ids['re_pass_wordword'].className   = input_green;
			reg_img_ids['re_pass_wordword'].src           = var_image_url + '/' + img_tick;
			reg_msg_ids['re_pass_wordword'].innerHTML     = '';
		}
		error_found = '';
}
/*-------------------------------------------------------------------------*/
// Check full_name
/*-------------------------------------------------------------------------*/
function check_full_name( event )
{
	//----------------------------------
	// INIT
	//----------------------------------
	var error_found = '';
	//----------------------------------
	// Make sure we have sommat
	//----------------------------------
	if ( ! reg_field_ids['full_name'].value )
	{
		error_found +=reg_error_fullname+"<br />";
	}

		if ( error_found )
		{
			reg_field_ids['full_name'].className   = input_red;
			reg_img_ids['full_name'].src           = var_image_url + '/' + img_cross;
			reg_msg_ids['full_name'].innerHTML     = error_found;
		}
		else
		{
			reg_field_ids['full_name'].className   = input_green;
			reg_img_ids['full_name'].src           = var_image_url + '/' + img_tick;
			reg_msg_ids['full_name'].innerHTML     = '';

		}
		error_found = '';
}

/*-------------------------------------------------------------------------*/
// Check Security_code
/*-------------------------------------------------------------------------*/
function check_security_code( event )
{
	var error_found = '';
        if (reg_field_ids['security_code'].value ==''){
    	            	error_found =reg_empty_security_code+"<br />";
            	}
         if ( error_found )
		{
                    	reg_field_ids['security_code'].className   = input_red;
	                	reg_img_ids['security_code'].src           = var_image_url + '/' + img_cross;
	                	reg_msg_ids['security_code'].innerHTML     = error_found;
	                	error_code=1;
		}
		else
		{
    	var url = './?check=sec&key='+escape( reg_field_ids['security_code'].value );

			/*--------------------------------------------*/
			// Main function to do on request
			// Must be defined first!!
			/*--------------------------------------------*/
			do_request_function = function()
			{
				//----------------------------------
				// Ignore unless we're ready to go
				//----------------------------------
				if ( ! xmlobj.readystate_ready_and_ok() )
				{
					// Could do a little loading graphic here?
					return;
				}
				//----------------------------------
				// INIT
				//----------------------------------
				var html = escape(xmlobj.xmlhandler.responseText);
				if ( html == 'no' )
				{
                      	reg_field_ids['security_code'].className   = input_red;
	                	reg_img_ids['security_code'].src           = var_image_url + '/' + img_cross;
	                	reg_msg_ids['security_code'].innerHTML     = error_found;
	                	error_code=1;
				}
				if ( html == 'yes' )
				{
                   	reg_field_ids['security_code'].className   = input_green;
	            	reg_img_ids['security_code'].src           = var_image_url + '/' + img_tick;
	            	reg_msg_ids['security_code'].innerHTML     = reg_ok_security_code;
	            	error_code=0;
				}
			}
			//----------------------------------
			// LOAD XML
			//----------------------------------
			xmlobj = new ajax_request();
			xmlobj.onreadystatechange( do_request_function );
			xmlobj.process( url );
		}


   		error_found = '';
}
/*-------------------------------------------------------------------------*/
// Validate the complete log in form
/*-------------------------------------------------------------------------*/

function validate_complete_login_form( event )
{
	//------------------------------------------
	// Simply run the functions
	//------------------------------------------
	reg_oktogo = 1;
	return reg_oktogo ? true : false;
}
/*-------------------------------------------------------------------------*/
// Validate the registration form
/*-------------------------------------------------------------------------*/
function validate_reg_form( event )
{
	//------------------------------------------
	// Simply run the functions
	//------------------------------------------
	reg_oktogo = 1;
	check_username( event );
	check_email_address( event );
	check_pass_word (event );
	check_re_pass_wordword( event );
	check_full_name( event );
	check_security_code( event );
	//------------------------------------------
	// Got error messages
	//------------------------------------------
	if (error_email){
		reg_oktogo = 0;
	}
	if ( reg_msg_ids['pass_word'].innerHTML ){
		reg_oktogo = 0;
	}
	if ( reg_msg_ids['re_pass_wordword'].innerHTML ){
		reg_oktogo = 0;
	}
	if ( reg_msg_ids['full_name'].innerHTML ){
		reg_oktogo = 0;
	}
	if (error_code){
		reg_oktogo = 0;
	}
	//------------------------------------------
	// Return
	//------------------------------------------
	return reg_oktogo ? true : false;
}

