// JavaScript Document




//pngfix
/*
 
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.

Use in <HEAD> with DEFER keyword wrapped in conditional comments:
<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

*/
function fixpng()
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])

	if ((version >= 5.5) && (document.body.filters)) 
	{
	   for(var i=0; i<document.images.length; i++)
	   {
	      var img = document.images[i]
	      var imgName = img.src.toUpperCase()
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	         var imgStyle = "display:inline-block;" + img.style.cssText 
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
	         img.outerHTML = strNewHTML
	         i = i-1
	      }
	   }
	}
}
window.onload = function() {fixpng();}

var nErrorCount = 0;
var arErrorMsg 	= new Array();
var bHasFocus 	= false;
arErrorMsg[0] 	= "---- The following errors occured ----" + String.fromCharCode(10);
function checkFieldValue(szType, formField, szMsg)
{	
	var szCheckValue 	= formField.value.replace(/^\s+/, "");
	var szValid;
	var szInvalid;
	var bAllValid 		= true;
	var bFlagDot  		= false;	
	// Blank error
	if (szType == 'noblank')
	{		
		if (szCheckValue == "")
	  	{			
		  	nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.value	= "";
				formField.focus();
			}
  		}
	}
	// Kiem tra dien chi email
	else if(szType == 'email')
	{
		if(!ValidateEmail(szCheckValue))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	// kiem tra co phai la ten ho khong
	else if(szType == 'vnname')
	{
		// special character
		szInvalid = "`1234567890-=\[];',./~!@#$%^&*()_+|{}:\"<>?";
		if(!isLegal(szCheckValue, szInvalid))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	// address
	else if(szType == 'vnaddress')
	{
		// special character
		szInvalid = "`=;'~!@#$%^&*()_+|{}:\"<>?";
		if(!isLegal(szCheckValue, szInvalid))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if(szType == 'integer')
	{
		/* ^\d+$ */
		if (!szCheckValue.match(/^\d+$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
		}
		else if(szType == 'decimal')
		{
			/* ^((\d+\.\d+)|(\d+))$ */
		if (!szCheckValue.match(/^((\d+\.\d+)|(\d+))$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if(szType == 'text')
	{
		/* ^[A-Za-z\s]+$ */
		if (!szCheckValue.match(/^[A-Za-z\s]+$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if(szType == 'alphanumeric')
	{
		/* ^[\w\s\.\+\-\#\,\/\(\)]+$ */
		if (!szCheckValue.match(/^[\w\s\.\+\-\#\,\\/\(\)]+$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if(szType == 'alphanum')
	{
		/* ^[\w\s]+$ */
		if (!szCheckValue.match(/^[\w\s]+$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if(szType == 'username')
	{
		/* ^(([A-Za-z\d][A-Za-z\d_]+[a-zA-Z\d])|([a-zA-Z\d]+))$ */
		// neu co underscore thi khong nam dau hay cuoi username
		if (!szCheckValue.match(/^(([A-Za-z\d][A-Za-z\d_]+[a-zA-Z\d])|([a-zA-Z\d]+))$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;			
				formField.focus();
			}
		}
	}
	else if(szType == 'verificationcode')
	{
		/* ^[A-Z0-9]{5}$ */
		if (!szCheckValue.match(/^[A-Z0-9]{5}$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if(szType == 'phone')
	{
		if (!szCheckValue.match(/^((\(\+?84\)[\-\.\s]?)|(\+?84[\-\.\s]?)|(0))((\d{3}[\-\.\s]?\d{6})|(\d{2}[\-\.\s]?\d{8}))$/))
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}
	}
	else if (szType == 'time')
	{
		var dtest;
		dtest=TestString(szCheckValue);
		if(dtest==false){		
			nErrorCount ++;
			arErrorMsg[nErrorCount] = szMsg + String.fromCharCode(10);
			if(bHasFocus == false)
			{
				bHasFocus 		= true;				
				formField.focus();
			}
		}			
	}
	else
	{
		if(szType == 'full')
		{
			szValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_ ";
		}

		else if(szType == 'URL')
		{
			szValid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.:/\\";
		}
		else if(szType == 'path')
		{
			szValid = "0123456789.+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz () \\ ";
		}
		else if(szType == 'file')
		{
			szValid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.";
		}
		else
		{
			nErrorCount ++;
			arErrorMsg[nErrorCount] = "Error type."  + String.fromCharCode(10);
			return 1;
		}

		// thu tuc kiem tra, duyet chuoi can kiem tra
		for (i = 0;  i < szCheckValue.length;  i++)
  		{
    		ch = szCheckValue.charAt(i);
    		// neu co trong cac ky tu hop le thi bo qua
			for (j = 0;  j < szValid.length;  j++)
			{
	      		if (ch == szValid.charAt(j))
	      		{
					break;
	      		}

				if (j == szValid.length - 1)
				{

					bAllValid = false;
					break;
				}
			}
			if (!bAllValid)
			{
				nErrorCount ++;
				arErrorMsg[nErrorCount] = szMsg  + String.fromCharCode(10);
					
				if(bHasFocus == false)
				{
					bHasFocus 		= true;					
					formField.focus();
				}
				break;
			}
     	}
	}
}
function switchMainTab(index)
{
	tabs = document.getElementById("maintab").getElementsByTagName("li");
	for(var i = 0; i < tabs.length; i++) {
		if(tabs[i].className == "current") {
			/* Deactivate */
			tabs[i].className = "";
		}
	}
	if( index > -1 && index < tabs.length){
		/* Activate */
		tabs[index].className = "current";
	}
}

function switchSubTab(index)
{
	tabs = document.getElementById("subtab").getElementsByTagName("li");
	for(var i = 0; i < tabs.length; i++) {
		if(tabs[i].className == "current") {
			/* Deactivate */
			tabs[i].className = "";
		}
	}
	if( index > -1 && index < tabs.length){
		/* Activate */
		tabs[index].className = "current";
	}
}

function switchUserTab(index)
{
	tabs = document.getElementById("tab_user").getElementsByTagName("li");
	for(var i = 0; i < tabs.length; i++) {
		if(tabs[i].className == "current") {
			/* Deactivate */
			tabs[i].className = "";
		}
	}
	if( index > -1 && index < tabs.length){
		/* Activate */
		tabs[index].className = "current";
	}
}

function ValidateEmail(szEmail)
{
	// chi co 1 ky tu @ o giua, chieu dai username tu 1 den 64 ky tu
	// chieu dai domain tu 1 den 255 ky tu
	if (!szEmail.match(/^[^@]{1,64}@[^@]{1,255}$/))
	{
		return false;
	}

  	// chia 2 phan truoc va sau @
  	szEmailArray = szEmail.split("@");
  	// szEmailArray[0] user, szEmailArray[1] domain

  	// phan username, chieu dai tu max 64 ky tu
  	// valid character la ky tu chu a-zA-Z, ky tu so 0-9 dash _ hyphen - va dot .
  	// neu de trong quote "langxang, at vinacyber dot com dot vn!"@server.com
  	// tach cac phan phan chia boi dot .
  	szLocalArray = szEmailArray[0].split(".");

  	// kiem tra tung phan
  	for(i = 0; i < szLocalArray.length; i ++)
  	{
  		// part 1: neu co dau dash hay hyphen thi khong co o dau va o cuoi
  		// part 2: neu khong do dash hay hyphen thi la A-Za-z0-9 chieu dai tu 1 den 64 ky tu
  		// part 3: neu trong quote, phan trong quote khong chua \" dau quote hay \\ va back slash
		if (!szLocalArray[i].match(/^(([A-Za-z0-9][A-Za-z0-9_-]{0,62}[A-Za-z0-9])|([A-Za-z0-9]{1,64})|(\"[^(\\|\")]{0,62}\"))$/))
		{
      		return false;
    	}
  	}

  	// domain khong phai la IP
  	if (!szEmailArray[1].match(/\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/))
  	{
  		// kiem tra valid domain, tach tung phan domain
    	szDomainArray = szEmailArray[1].split(".");
    	// it nhat co 2 phan cua domain sau @
    	if (szDomainArray.length < 2)
    	{
        	return false;
    	}

    	// kiem tra tung phan
    	for(i = 0; i < szDomainArray.length; i++)
    	{
    		// maxinum la 64, chua A-Za-z, 0-9, co the chua hyphen -
    		// dau hyphen khong o dau va khong o cuoi, domain case in-sensitive
    		// neu khong co hyphen thi la A-Za-z0-9
			if (!szDomainArray[i].match(/^(([A-Za-z0-9][A-Za-z0-9-]{0,62}[A-Za-z0-9])|([A-Za-z0-9]{0,64}))$/))
			{
        		return false;
      		}
    	}

    	// kiem tra top domain
		// szDomainArray[szDomainArray.length - 1]
  	}
  	return true;
}
function isLegal(szCheck, szInvalid) {
	// szInvalid cac ky tu dac biet
	for(i = 0; i < szInvalid.length; i ++) {
		// co ky tu dac biet trong chuoi can check
		if(szCheck.indexOf(szInvalid.charAt(i)) >= 0 )	{
			return false;
		}
    }
	return true;
}

function checkDate( strDate, strMessage1, strMessage2 ) 
{
 /**gia tri nhap vao phai la dd/mm/yyyy*/
	var szCheckValue 	= strDate.value.replace(/^\s+/, "");
	var arrayDATE;
    var dtest;
   	//arrayDATE = strDate.split("/"); 
	arrayDATE= szCheckValue.split("/");
	/** ngay cua he thong la mm/dd/yyyy nen phai doi ra ngay he thong */
	dtest = new Date(arrayDATE[1] + "/" + arrayDATE[0] + "/" + arrayDATE[2]);   
    if (dtest.getDate() != arrayDATE[0] || dtest.getMonth() +1 != arrayDATE[1] || dtest.getFullYear() != arrayDATE[2]) 
	{
    	alert( strMessage1 );   
		strDate.value='';
		return false;
    }
	if (arrayDATE[2]<=1900) 
	{
	   /**neu nam nho hon 1900 thi se ko insert duoc vao sql server*/
		alert(strMessage2);  
		strDate.value='';
		return false;
	}
	return true;
}

function checkTime ( strTime, strMessage )
{
	/* gia tri nhap vao phai la time HH:MM:SS TT*/		
	var dtest;
	dtest=TestString(strTime);
	if(dtest==false){		
		return false;
	}	
	return true;
}

function TestString(S) {
    return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])(:[0-5][0-9])([ ][A-A][M-M]|[ ][a-a][m-m]|[ ][p-p][m-m]|[ ][A-A][M-M])?$/.test(S);
}
/**
* Written by 		: Trương Kim Phụng
* Dated on			: 01/12/2007
* Description		: kiểm tra ký tự nhập vào có là số hay ko
* Function			: function checkKey
*/
function checkKey(e){
	if ((e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode == 8 || (e.keyCode>=96 && e.keyCode<=105) || e.keyCode == 9)
		return true;
	else
		return false;
}
var flagXuLyPoint;
//Kiểm tra gõ key
function checkCommaKey(e){
	var keyCode //= (window.event) ? event.keyCode : e.which;   
	if (window.event) keyCode = event.keyCode   
	else keyCode = e.which   
	// Was key that was pressed a numeric character (0-9) or . or <- or -> or backspace (8)?   
	if ( keyCode > 47 && keyCode < 58  || keyCode == 44 ||  keyCode == 37 || keyCode == 39 || keyCode == 8 ) {
		flagXuLyPoint = true;
		return; // if so, do nothing   else // otherwise, discard character   
	}
	flagXuLyPoint = false;
	if (window.event) //IE       
		window.event.returnValue = null;     
	else //Firefox       
		e.preventDefault(); 
}
function checkPointKey(e){
	var keyCode //= (window.event) ? event.keyCode : e.which;   
	if (window.event) keyCode = event.keyCode   
	else keyCode = e.which   
	// Was key that was pressed a numeric character (0-9) or , or <- or -> or backspace (8)?   
	if ( keyCode > 47 && keyCode < 58 || keyCode == 46 || keyCode == 37 || keyCode == 39 || keyCode < 32 ) {
		flagXuLyPoint = true;
		return; // if so, do nothing   else // otherwise, discard character   
	}
	flagXuLyPoint = false;
	if (window.event) //IE       
		window.event.returnValue = null;     
	else //Firefox       
		e.preventDefault(); 
	 
}
function addSimpleKey(node, strSimple ){
	var strStart 	= node.value;
	if(!flagXuLyPoint)	return;	
	strResult	= document.getElementById('idUnit').value;	
	if(strResult == 'USD') {
		arrSplit = strStart.split('.');
		if(arrSplit.length > 1 ) {
			str = arrSplit[0];
		}
		else {
			str = strStart;
		}
	}
	else {
		arrSplit = strStart.split(',');
		if(arrSplit.length > 1 ) {
			str = arrSplit[0];
		}
		else {
			str = strStart;
		}
	}
	
	str =filter(str ,'0123456789');	
	
	var cont = true;
	while(cont){
		if(str.charAt(0) == '0'){
			str = str.substr(1,str.length);
		}
		else cont = false;
	}
	temp="";
	for(i=str.length-1; i>=0; i--){
		temp=str.charAt(i)+temp;		
		if ( (((i-str.length)%3)==0) && (i!=0)){
			temp= strSimple + temp;
		}
	}	
	if(strResult == 'USD') {
		arrSplit = strStart.split('.');
		if(arrSplit.length > 1 ) {
			node.value = temp + '.' + arrSplit[1];
		}
		else {
			node.value = temp ;
		}
	}
	else {
		arrSplit = strStart.split(',');
		if(arrSplit.length > 1 ) {
			node.value = temp + ',' + arrSplit[1];
		}
		else {
			node.value = temp ;
		}
	}	
}
function filter(inputString, filterString){
	outString = "";	
	for(i=0; i<inputString.length; i++){
		if(filterString.indexOf(inputString.substr(i,1) )!=-1)
			outString = outString + inputString.substr(i,1);
	}	
	return outString;
}
function actionKey(e, idField) {
	strResult		= document.getElementById(idField).value;	
	var isBoolean 	= '';
	if(strResult == 'USD') {
		checkPointKey(e); 
	}
	else if(strResult == 'VND') {
		checkCommaKey(e);
	}
	
}
function keyUp(idField, strValue) {
	strResult	= document.getElementById(idField).value;		
	if(strResult == 'USD') {
		//addSimpleKey(strValue, ',');
	}
	else if(strResult == 'VND') {
		addSimpleKey(strValue, '.');
	}	
}
function onChange(strInput, idField) {	
	strValue = document.getElementById(idField).value;	
	if(strInput.value == 'VND') {
		/**
		* Từ $124,450.20 --> 124.450,20 VND 
		*/
		arrValue = strValue.split('.');		
		if(arrValue.length > 1 ) {
			strResult = replace(arrValue[0], ",", "\.") + ',' + arrValue[1];
		}
		else {
			strResult = replace(strValue, ",", "\.");				
		}
	}
	else {	
		/**
		* Từ 124.450,20 VND --> $124,450.20 
		*/
		arrValue = strValue.split(',');		
		if(arrValue.length > 1 ) {
			strResult = replace(arrValue[0], "\.", ",") + '.' + arrValue[1];
		}
		else {
			strResult = replace(strValue, "\.", ",");
			alert
		}				
	}
	document.getElementById(idField).value = strResult;
}
function replace(string,text,by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
