// JavaScript Document
function chLang(lang) {
	var link = parent.location.href;
	var endLink = link.length;
	var i;
	var n;
	var found = 0;
	
	for(i = 0; i <= endLink; i++) {
		n = i + 3;
		if (link.substring(i, n) == "eng") {
			chLink = link.substring(0, i) + lang + link.substring(n, endLink);
			break;
		}
				else if (link.substring(i, n) == "chi") {
			chLink = link.substring(0, i) + lang + link.substring(n, endLink);
			break;
		}				
	}
	//alert(lang);
	parent.location.href = chLink;
}


function chk_username(v_username){
	var browser = navigator.userAgent.toLowerCase();
	var xmlDoc;
	
	//Check for IE
	if (browser.indexOf("msie") != -1) { 
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";		
		xmlDoc.load("check_login.asp?username=" + v_username);
		xmlObj=xmlDoc.documentElement;
		chk = xmlObj.childNodes(0).getAttribute("chk");
		return chk;
	//Check for non-IE browser (Firefox and Netscape)
	}else{
		xmlDoc = document.implementation.createDocument("","",null);
		xmlDoc.async=false;
		xmlDoc.load("check_login.asp?username=" + v_username);
		chk_user = xmlDoc.getElementsByTagName("chk_detail");
		chk = chk_user[0].getElementsByTagName("username")[0].getAttribute("chk");
		return chk;
	}
	
}


function isEmail(email) {

	invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return false;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;

}

//  check for valid numeric strings	

function isNumeric(strString)
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

//Check for English characters
function isChr (text)  {
	var loAZ = "abcdefghijklmnopqrstuvwxyz";
	var symbols = "-_1234567890";
	var loc; 
	
//	symbols+= loAZ.toUpperCase();
	symbols+= loAZ;
	
    loc = symbols.indexOf(text);
    if (loc >-1)
    {
		return true
    }
	
    return false; 
}

function isEng(str){
	var i;

	for(i=0; i<=str.length; i++){
		if ( !isChr( str.charAt(i) ) ){
			return false;
		}
	}
	return true;
}


function isDate (year, month, day) {
	// month argument must be in the range 1 - 12
	
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	
	if ( (year == tempDate.getYear()) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
		return true;
	else
		return false;
}

function dateDiff( start, end, interval, rounding ) {

    var iOut = 0;
    

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        return null ;
    }
    
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        return null ;
    }
    
    return iOut ;
}



