﻿/*-------------------------------------------------------------------------------
	*	FileName ; $common.js
	* Function : commmon utilies functions
	* CreatedDate : 01/02/2003
	* LastUpdate : 01/03/2003
-------------------------------------------------------------------------------*/
function stoperror(){ 
return true 
} 
//window.onerror=stoperror 

//------------------------------------------------
// Function Name : LTrim 
// Actions : Remove left string.
//------------------------------------------------
function LTrim(Str) {
	return Str.replace(/^\s+/, '');
}

//------------------------------------------------
// Function Name : RTrim 
// Actions : Remove right space.
//------------------------------------------------
function RTrim(Str) {
	return Str.replace(/\s+$/, '');
}

//------------------------------------------------
// Function Name : Trim 
// Actions : Remove left&right space.
//------------------------------------------------
function Trim(Str) {
	return RTrim(LTrim(Str));
}

//------------------------------------------------
// Function Name : RemoveSpace 
// Actions : Remove left, right & all unwanted spaces inside a Str
//------------------------------------------------
function RemoveSpace(Str) {
	var str = Trim(Str).replace(/\s+/g, ' ');
	str = str.replace(/\s+[,]/g, ',');
	str = str.replace(/\s+[;]/g, ' ;');
	str = str.replace(/\s+[:]/g, ' :');
	return str.replace(/\s+[.]/g, '.');
}

//------------------------------------------------
// Function Name : AddSpace 
// Actions : Add some space to correct sentense
//------------------------------------------------
function AddSpace(Str) {
	var str = Str.replace(/\,/g, ', ');
	str = str.replace(/;/g, '; ');
	str = str.replace(/;/g, ' ;');
	str = str.replace(/\./g, '. ');
	str = str.replace(/\:/g, ' :');
	str = str.replace(/\:/g, ': ');
	str = RemoveSpace(str);
	return str;
}

//------------------------------------------------
// Function Name : TypingCheck 
// Actions : Check spell
//------------------------------------------------
function TypingCheck(Str) {
	var arrayStr = new Array();
	Str = AddSpace(Str);
	for (var i = 0; i < Str.length; i++) {
			arrayStr[i] = Str.charAt(i);
	}
	for (var i = 0; i < Str.length; i++) {
		if ( ( (isPeriod = (arrayStr[i] == '.')) || (arrayStr[i] == ',') || (arrayStr[i] == ';') )  && (i != Str.length - 1) ) {
			arrayStr[i+2] = isPeriod ? arrayStr[i+2].toUpperCase() : arrayStr[i+2].toLowerCase();
		}
	}
	arrayStr[0] = arrayStr[0].toUpperCase();
	var str = "";
	for (var i = 0; i < Str.length; i++) {
			str += arrayStr[i];
	}
	return str;
}

function checkSearch(f) {
	var keys;
	var keys_lower;
	var keys_upper;
	keys = Trim(f.k.value);
	if (keys!='') {
	 	f.k.value = keys;
		keys_lower = keys.toLowerCase();
		f.l.value = keys_lower;
		keys_upper = keys.toUpperCase();
		f.u.value = keys_upper;
	}
	return true;
}	
function checkNewsletter(f){
	if(isEmail(f.email.value)){
		window.open('newsletter.php?email='+f.email.value,'PopNewsLetter','width=350,height=60');
	}
	else{
		alert('Email không hợp lệ. Vui lòng kiểm tra lại !');
		f.email.focus();
	}
	return false;
}
function checkMail(f){
	if(isEmail(f.email.value)){
		window.open('mail2friend.php?email='+f.email.value,'PopEmail','width=350,height=60');
	}
	else{
		alert('Email không hợp lệ. Vui lòng kiểm tra lại !');
		f.email.focus();
	}
	return false;
}
	function setCookie(name,value){
		document.cookie = name + '=' + escape(value) +  ';path=/';
		return ;
	}
	function getCookie(name){
		var cname = name + '=';
		var dc = document.cookie;
		if (dc.length > 0) {
			var begin = dc.indexOf(cname);
			if (begin != -1) {
			begin += cname.length;
			var end = dc.indexOf(';', begin);
			if (end == -1) end = dc.length;
				return unescape(dc.substring(begin, end));
			}
		}
		return null;
	}
	
function GetYear() {
	var mydate=new Date()
	var year=mydate.getYear()
	if (year < 1000)
		year+=1900
	document.write (year)
}

function DateDisplay() {
	var mydate=new Date()
	var year=mydate.getYear()
	if (year < 1000)
		year+=1900
	var day=mydate.getDay()
	var month=mydate.getMonth()
	var daym=mydate.getDate()
	if (daym<10) daym="0"+daym
		var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
		var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
		document.write(dayarray[day]+", "+montharray[month]+" "+daym+", "+year)
}
	function getCategoryName(id){
		var i;
		for(i=0;i<Tree.length;i++){
			var arrElement = Tree[i].split('|');
			if(arrElement[0]==id){
				return arrElement[2];
			}
		}
		return 'Products';
	}
	function getCategoryName4member(id){
		var i;
		for(i=0;i<Tree4member.length;i++){
			var arrElement = Tree4member[i].split('|');
			if(arrElement[0]==id){
				return arrElement[2];
			}
		}
		return '';
	}
	function instantStr(str){
		var imax = str.length;
		var str_new = '';
		for(var i=0;i<str.length;i++){
			str_new += str.charAt(i)+'&nbsp;'; 
		}
		return str_new;
	}
	function getTopicName(id){
		var i;
		for(i=0;i<arr_document_topic.length;i++){
			if(arr_document_topic[i][0]==id) return arr_document_topic[i][1];
		}
		return 'N/A';
	}

var _fileName;
var marked_row = new Array;
function CheckAll_bottom(f)
{
	if(f.allbox_up.checked) {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				if(!e.checked) {
					e.click();
				}
			}
		}
	}else {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				e.click();
			}
		}
	}
}
function CheckAll_bottom_v2(f)
{
	if(f.allbox_up.checked) {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				if(!e.checked) {
					e.click();
				}
			}
		}
	}else {
		for(var i=0;i<f.elements.length;i++)
		{
			var e=f.elements[i];
			if((e.name!='allbox_up')&&(e.type=='checkbox')&&(e.disabled==false)) {
				e.click();
			}
		}
	}
}
 function pasteClass(e,classSelect,classNormal)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
		r.className = e.checked ? classSelect : classNormal;
	}
    }

 function nothing(e,bg,row_number,mark_color)
    {
	var r = null;

	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
		var  theCells = null;
		//r.style.backgroundColor=bg;
		if (typeof(document.getElementsByTagName) != 'undefined') {
			theCells = r.getElementsByTagName('td');
		}
		else if (typeof(r.cells) != 'undefined') {
			theCells = r.cells;
		}
		else {
			return false;
		}
		 var rowCellsCnt  = theCells.length;
         for (var c = 0; c < rowCellsCnt; c++) {
              //theCells[c].style.backgroundColor = bg;
			  theCells[c].setAttribute('bgcolor', bg, 0);
         }
		 if(bg.toLowerCase()==mark_color.toLowerCase()) {
			 marked_row[row_number] = true;
		 }
		 else marked_row[row_number] = false;
	}
    }
	
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
		
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0)
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // Garvin: deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function
function writeSelectOrder (sPage, seName, choice, lang,typeDisplay) {	
	var l= lang==null ? 0 : lang; 
	var sT='';
	sT = '<select size="1" name="' + seName + '"'
	if (sPage=='pageList') sT += ' onchange=this.form.submit()';
	sT +='>';
	if (choice == null || choice=='') {
		choice = 0
	}
	sT += '<option value=0' 
		if (choice ==0) sT += ' selected ' 
		if (!lang) sT +='>Kh&ocirc;ng hi&#7875;n th&#7883;</option>'; else sT +='>Hide</option>';
	sT += '<option value=1' 
		if (choice ==1) sT += ' selected ' 		
		if (!lang) sT +='>Hi&#7875;n th&#7883; &#7903; trang ch&#7911;</option>'; else sT += 'Display on Homepage</option>';
	if(typeDisplay==1){
	sT += '<option value=2' 
		if (choice ==2) sT += ' selected ' 		
		if (!lang) sT +='>Hi&#7875;n th&#7883;</option>'; else sT += 'Display</option>';		
	}
	else{
	sT += '<option value=2' 
		if (choice ==2) sT += ' selected ' 		
		if (!lang) sT +='>&#431;u ti&ecirc;n 1 (Cao nh&#7845;t)</option>'; else sT += 'First (highest)</option>';
	sT += '<option value=3' 
		if (choice ==3) sT += ' selected ' 
		if (!lang) sT +='>&#431;u ti&ecirc;n 2 </option>'; else sT +='>Second</option>';		
	sT += '<option value=4' 
		if (choice ==4) sT += ' selected ' 
		if (!lang) sT +='>&#431;u ti&ecirc;n 3</option>'; else sT +='>Third</option>';
	sT += '<option value=5' 
		if (choice ==5) sT += ' selected ' 
		if (!lang) sT +='>&#431;u ti&ecirc;n 4 (Th&#7845;p nh&#7845;t)</option>'; else sT +='>Fourth (lowest)</option>';
	sT += '</select>'	
	}
	document.write (sT);
}
function previewFile(value,display){
	if((value=='')||(value==null)||(value==sMediaPathGlobal)){
		document.all(display).innerHTML = '';
		return ;
	}
	else if(isPix(value)){
		document.all(display).innerHTML = '<img src="'+value+'">';
		return ;	
	}
	else if(isFlash(value)){
		document.all(display).innerHTML ='<embed src="'+value+'" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash"></embed>';
		return ;
	}
	return ;
}
function brickChoice(n,display){
	var str;
	str = '<table width="100%" border="0" bgcolor="#FFFFFF" cellpadding="1" cellspacing="1">';
	for(i=1;i<=n;i++){
		str += '<tr>';
		str += '<td class=wsAddLabel align="right" width="20%">Choice '+i+'</td>';
		str += '<td class=wsAddLabel><textarea name="choice_'+i+'" cols="50" rows="3" id="choice_'+i+'"></textarea></td>';
  		str += '</tr>';
	}
	str += '</table>';
	document.all(display).innerHTML = str;
}
function checkSearch(f) {
	var keys;
	var keys_lower;
	var keys_upper;
	keys = Trim(f.k.value);
	if (keys=='') {
	 alert (msg_search_empty);
	 f.k.value = keys;
	 f.k.focus();
	 return false; 
	}
	f.k.value = keys;
	keys_lower = keys.toLowerCase();
	f.l.value = keys_lower;
	keys_upper = keys.toUpperCase();
	f.u.value = keys_upper;
	return true;
}