/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

var debug = false;
/**
Browser Compatability function.
Returns the correct XMLHttpRequest depending on the current browser.
*/
function GetXmlHttp() {	
	var xmlhttp = false;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
  }
	else if (window.ActiveXObject)// code for IE
	{
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}


/**
<summary>
Gets the response stream from the passed url, and then calls the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="callbackFunction">The function to call after the response has been recieved. the response <b>must</b> always be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters you want to pass to the functions. (Note: only constants/strings/globals can be passed as params, most variables will be out of scope.) </param>
</summary>
<example>
	<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'div1\',\'div2\',\'div3\'');

function FunctionToHandleTheResponse(response, d1, d2, d3){
	var data = response.split(';');
	document.getElementById(d1).innerHTML = data[0];
	document.getElementById(d2).innerHTML = data[1];
	document.getElementById(d3).innerHTML = data[2];
}
	</code>
</example>
*/
function PassAjaxResponseToFunction(url, callbackFunction, params)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
		                                if (xmlhttp.status==200)
		                                {
			                                var response = xmlhttp.responseText;
			                                var functionToCall = callbackFunction+'(response,'+params+')';
			                                if(debug){
				                                alert(response);
				                                alert (functionToCall);
			                                }
			                                eval(functionToCall);
		                                } else if(debug){
			                                document.write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}


/**
///<summary>
///Sets the innerHTML property of obj_id with the response from the passed url./
///</summary>
///<param name="url">The url to make the request to get the response data.</param>
///<param name="obj_id">The object or the id of the object to set the innerHTML for.</param>
*/
function AjaxResponse(url, obj_id)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                document.getElementById(obj_id).innerHTML="<br><div class='DarkText' align='center'>loading...</div>";
									if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
		                                if (xmlhttp.status==200)
		                                {
			                                if(debug){
				                                alert(xmlhttp.responseText);
			                                }
			                                if(typeof obj_id == 'object'){
				                                obj_id.innerHTML = xmlhttp.responseText;
			                                } else {
				                                document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
			                                }
		                                } else if(debug){
			                                document.Write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}



function AjaxResponsePage(url, objLoading_id, obj_id)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                document.getElementById(objLoading_id).innerHTML="<b>Đang tải dữ liệu...</b>";
									if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
		                                if (xmlhttp.status==200)
		                                {
			                                if(debug){
				                                alert(xmlhttp.responseText);
			                                }
			                                if(typeof obj_id == 'object'){
				                                obj_id.innerHTML = xmlhttp.responseText;												
			                                } else {
				                                document.getElementById(obj_id).innerHTML = xmlhttp.responseText;									
			                                }
		                                } else if(debug){
			                                document.Write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

function chuanhoangay(obj,str){
	var keycode
	keycode=window.event.keyCode
	if (isNumber(keycode,str)){
		obj.value=editdate(trim(str))
		
	}
	else {
		if (str.length==1){
			obj.value="";
		}
		else{
			obj.value=str.substring(0,str.length-1);
		}
	}
}

//--------------
function editdate(str)
{
var value;
if (str.substr(str.length-1,1)=="/" && str.substr(str.length-2,1)=="/"){
	return str.substr(0,str.length-1)
}
switch (str.length){
    case 1:
	if (str.substr(0,1)>=4) {
		value="0"+str.substr(0,1)+ "/";
	}
	else {
		value=str;
	}
    break;
    case 2:
	if (str.substr(1,1)=="/" && str.substr(0,1)>=1 && str.substr(0,1)<=9 ) {
		value="0"+str.substr(0,str.length);
	}
	else{
		if (str>=1 && str<=31) {
			value=str+"/";
		}
		else{
			value=str.substr(0,1);
		}
	}
    break;
    case 3:
	if (str.substr(1,1)=="/" && str.substr(0,1)>=1 && str.substr(0,1)<=9 ) {
		value="0"+str.substr(0,str.length);
	}
	else{
		value=str;
	}
    break;
    case 4:
		if (str.substr(3,1)>=2 && str.substr(2,1)== "/" ) {
			value=str.substr(0,3)+"0"+str.substr(3,1)+ "/";
		}
		else{
			value=str.substr(0,4);
		}
    break;
    case 5:
    		if (str.substr(str.length-1,1)=="/") {
			value=str.substr(0,str.length-2) + "0" + str.substr(str.length-2,str.length);
		}
		else {
			if (str.substr(3,2)>=1 && str.substr(3,2)<=12) {
				value=str+"/";
			}
			else{
				value=str.substr(0,str.length-1);
			}
		}
    break;
    case 8:
		if (str.substr(str.length-2,2)>=45) {
			value=str.substr(0,str.length-2)+"19"+str.substr(str.length-2,2);
		}
		else{
			if (str.substr(str.length-2,2)<=18) {
				value=str.substr(0,str.length-2)+"20"+str.substr(str.length-2,2);
			}
			else {
				value=str;
			}			
		}
    break;
    default:
	value=str;
}
    return value;
}
//---------------
function isNumber(keycode,str)
{

if ((keycode>=48 && keycode <=57)|| (keycode>=96 && keycode <=105) || (keycode == 8)){	
	return true;
}
else{
	return false;
}	
}
function trim( s )
{
    var i, sRetVal = "";
    i = s.length-1;
    while ( i>=0 && s.charAt(i) == ' ' )
         i--;
    s = s.substring( 0, i+1 ); // trim blanks on the right
    i = 0;
    while ( i< s.length && s.charAt(i) == ' ')
         i++;
    return s.substring( i );
}
