﻿function writeSource(div) 
{
	if (!document.getElementById) { return; }
	var o = document.getElementById(div);
	if (typeof(o) == "undefined" || o==null) { return; }
	var s = o.innerHTML;
	if (s==null || s.length==0) { 
		return;
		}
	else {
		var i;
		for(i=0;s.charAt(i)==" "||s.charAt(i)=="\n"||s.charAt(i)=="\r"||s.charAt(i)=="\t";i++) {}
		s = s.substring(i);
		for (i = s.length; i>0; i--) {
			if (s.charAt(i)=="<") {
				s = s.substring(0,i) + "&lt;" + s.substring(i+1) ;
				}
			}
		for (i = s.length; i>0; i--) {
			if (s.charAt(i)==">") {
				s = s.substring(0,i) + "&gt;" + s.substring(i+1) ;
				}
			}
		for (i = s.length; i>0; i--) {
			if (s.charAt(i)=="\t") {
				s = s.substring(0,i) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + s.substring(i+1) ;
				}
			}
		for (i = s.length; i>0; i--) {
			if (s.charAt(i)=="\n") {
				s = s.substring(0,i) + "<BR>" + s.substring(i+1) ;
				}
			}
		s = s + "<BR>";
		}
	document.write('<A STYLE="font-family:arial; font-size:x-small; text-decoration:none;" HREF="#" onClick="var d=document.getElementById(\'jssource'+div+'\').style; if(d.display==\'block\'){d.display=\'none\';this.innerText=\'+ Show Source\';}else{d.display=\'block\';this.innerText=\'- Hide Source\';} return false;">+ Show Source</A><BR>');
	document.write('<SPAN ID="jssource'+div+'" STYLE="display:none;background-color:#EEEEEE"><TT>'+s+'</TT></SPAN>');
}
	
function trimAll(sString) 
{
	var strTemp = sString;
	
	while (strTemp.substring(0, 1) == ' ')
	{
		strTemp = strTemp.substring(1, strTemp.length);
	}
	while (strTemp.substring(strTemp.length - 1, strTemp.length) == ' ')
	{
		strTemp = strTemp.substring(0, strTemp.length - 1);
	}
	return strTemp;
}

function is_Date(y,m,d){
    if (y.length!=4 || m.length>2 || d.length>2) return false;
    tmpDate = new Date(y,m-1,d);
    
    if (m != tmpDate.getMonth()+1 || d != tmpDate.getDate()) return false;
    return true;
}

function is_StyleDate(st)
{
    var arrDate;
    
    if(st.search(/[^0-9\/]/) != -1) return false;
    else
    {
        if(st.search(/[\/]/) == -1) return false;
        else
        {
            arrDate = st.split("/");
            if (arrDate.length != 3) return false;
            else return is_Date(arrDate[2], arrDate[1], arrDate[0])
        }
    }
}

/**
==========================================================
//Function  : Chỉ cho nhập số trên textbox
//Value     : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
//input     : event
//Use       : <input type="Text" name="iNumber" onKeyPress="return fncInputNumberCode(event);" >
==========================================================
**/
function fncInputNumberCode(e){
	if (window.event){	 //IE
		if (e.keyCode < 48 || e.keyCode > 57) e.returnValue = false;
	}else if (e.which){		//Netscape
		if ((e.which < 48 || e.which > 57) && ( e.which != 8)) return false;
	}else{		
		return true;
	}	
}

/**
==========================================================
//Function  : Chỉ cho nhập số và ký tự / trên textbox
//value     : 0 ,1,2,3,4,5,6,7,8,9,/
//input     :   event
// Use      : <input type="Text" name="iDate" onKeypress='return fncInputDate(event);' >
//Example   : 20/02/2005
==========================================================
**/
function fncInputDate(e){	//IE
	if (window.event){	
		if (e.keyCode < 47 || e.keyCode > 57) e.returnValue = false;
	}else if (e.which){		//Netscape
		if ((e.which < 47 || e.which > 57)&& ( e.which != 8)) return false;
	}else{		
		return true;
	}
}

function rightMouse(e) 
{     
    if (navigator.appName == 'Netscape' && e.which == 3) return false; 
    else if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) return false; 
    return true; 
}

/**
==========================================================
//Function  : Chỉ cho nhập số và ký tự . trên textbox
//value     : 0 ,1,2,3,4,5,6,7,8,9, .
//input     :   event
// Use      : <input type="Text" name="iDate" onKeypress='return fncInputCurrency(event);' >
//Example   : 20/02/2005
==========================================================
**/
function fncInputCurrency(e){	//IE    
    //44: ,
    //45: -
    
	if (window.event){	 //IE
		if ((e.keyCode < 46 || e.keyCode > 57 || e.keyCode == 47) && (e.keyCode != 44)) e.returnValue = false;
	}
	else if (e.which)
	{		//Netscape	    
		if ((e.which < 46 || e.which > 57 || e.which == 47) && ( e.which != 8) && (e.which != 44)) return false;
	}else{		
		return true;
	}	
}

/**
==========================================================
//Function  : Chỉ cho nhập số và ký tự ., - trên textbox
//value     : 0 ,1,2,3,4,5,6,7,8,9, ., -
//input     :   event
// Use      : <input type="Text" name="iDate" onKeypress='return fncInputPhoneNumber(event);' >
//Example   : 20/02/2005
==========================================================
**/
function fncInputPhoneNumber(e){	//IE
    //47: /
    //44: ,
    //46: .
    //42: *
    //43: +
    //45: -

	if (window.event){	 //IE
		if ((e.keyCode < 40 || e.keyCode > 57 || e.keyCode == 47 || e.keyCode == 44 || e.keyCode == 42) &&
		        (e.keyCode != 32)) e.returnValue = false;
	}
	else if (e.which)
	{		//Netscape	    
		if ( (e.which < 40 || e.which > 57 || e.which == 47 || e.which == 44 || e.which == 42) && 
		        ( e.which != 8) && ( e.which != 32) ) return false;
	}else{		
		return true;
	}	    
}
//mm/dd/yyyy
function compareDate(date1, date2)
{
    var arrDate1 = date1.split('/');
    var strStartDate = arrDate1[2] + arrDate1[1] + arrDate1[0];
    //var dteStartDate = new Date(arrDate1[2], arrDate1[1], arrDate1[0]);
    var arrDate2 = date2.split('/');
    var strEndDate = arrDate2[2] + arrDate2[1] + arrDate2[0];
    //var dteEndDate = new Date(arrDate2[2], arrDate2[1], arrDate2[0]);
    
    //if (dteStartDate == dteEndDate) return 0;
    //if (dteStartDate > dteEndDate) return 1;
    //if (dteStartDate < dteEndDate) return -1;    
    if (strStartDate == strEndDate) return 0;
    if (strStartDate > strEndDate) return 1;
    if (strStartDate < strEndDate) return -1;        
}


/**
==========================================================
//Function :  Passed a string, finds Replace string "Bad" by String "Good"
//input : String
//output : String
// Examlpe : str="aa %  %" => fncStrReplace( str, "%", "%%" ) => str="aa %%  %%"
==========================================================
**/
function fncStrReplace( strValue, strBad, strGood ) {
	temp = "" + strValue; // temporary holder
	while( temp.indexOf(strBad) > -1 ) {
		pos= temp.indexOf( strBad );
		temp = "" + ( temp.substring(0, pos) + strGood + 	temp.substring( (pos + strBad.length), temp.length) );
	}
	return temp;
}

/**
==========================================================
//Function : Kiểm tra chuỗi dữ liệu có phải la kiểu số không
//input : String
//output : bool
// Examlpe : str="ffefefe" => fncIsNumber(str) => true
==========================================================
**/
function fncIsNumber(strValue) {
    var intValue = parseInt(strValue);
    if (intValue) return true;
    else return false;
}

var INPUT_INVALID = ' kiếu dữ liệu không hợp lệ.';
