﻿/*-------------------------------------------------------------------------------
	*	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;
	f.action = '';
	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;
		switch(f.search_where.value){
			case '2' :
				f.action = 'news.php';
				return true;
			case '4' :
				f.action = 'service.php';
				return true;
			default :
				f.action = 'search_product.php';
				return true;
		}
	}
	f.action = '';
	return false;
}	
	function setCookie(name,value,day_add){
		var expiredDate = new Date();
		expiredDate.setTime(expiredDate.getTime() + (day_add*24*60*60*1000));
		document.cookie = name + '=' + escape(value) +  ';expires='+expiredDate.toGMTString()+';path=/;domain=netnhac.com';
		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 removeCookie(name){  
	var expiredDate = new Date();  
	expiredDate.setTime (expiredDate.getTime() - 24*60*60*1000);  
	var cval = getCookie(name); 
	document.cookie = name + '=' + escape(cval) + '; expires=' + expiredDate.toGMTString()+';path=/';
}
	
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 '';
	}
	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 arrPixFiles = new Array("jpg","jpeg","gif","png","bmp","tif","jpe");
var arrDocFiles = new Array("doc","xls","txt","xls","pdf","ppt","rtf","zip","rar","gz","bz2","tar","psd","fla");
var arrMovieFiles = new Array("div","mov","wmv","wma","avi","mpg","mpeg","qt","mp4","ram");
var arrFlashFiles = new Array("swf");
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 removeItems(f){
	f.action = removeFavFla();
	var axn;
	var axn_name;
	if(f.order!=null){
		if(f.order.value==1) return true;
	}
	if(f.add_playlist!=null){
		if(f.add_playlist.value==1)	{axn = ' adding  ' ;axn_name=' to your Playlist';}
	}
	if(f.add_favorites!=null){
		if(f.add_favorites.value==1)	{axn = ' adding ' ;axn_name = ' to your Favorites list';}
	}
	if(f.del!=null){
		if (f.del.value==1) axn = ' removing ' ;
	}
	
	if(f.total.value.length>1) {
		var answer=confirm('You are '+ axn +(f.total.value.length-1)+' item(s)'+axn_name+". Are you sure ?");
		if(!answer){
			return false;
		}
		if(f.add_playlist.value==1){
			addnPlaylist(f);
			return false;
		}
		f.submit();
		return true;
	}
	return false;
} 
function removeCarts(f){
	var axn;
	if(f.remove!=null){
		if(f.remove.value==1)	axn = ' removing ' ;
	}
	if(f.update!=null){
		if(f.update.value==1)	axn = ' updating your cart. ' ;
	}
	if(f.empty!=null){
		if (f.empty.value==1) axn = ' removing all items in your cart. ' ;
	}
	var answer = false;
	if((f.remove.value==1)&&(f.total.value.length>1))	answer=confirm('Your are '+ axn +(f.total.value.length-1)+' item(s)'+". Are you sure ?");
	else answer=confirm('You are '+ axn + " Are you sure ?");
	if(!answer){
		return false;
	}
	f.submit();
	return true;
} 
function openBox(winWidth, winHeight, scrollbars,toolbar,top,left,fileSrc) {
	var newParameter = "width=" + winWidth + ",height=" + winHeight ;
	newParameter += ",scrollbars="+scrollbars+",toolbar="+toolbar+",top="+top+",left="+left;
    var newWindow = open(fileSrc,"",newParameter);
}
function removeFavFla(){
	var typeFav = 'favorites';
	var typePla = 'playlist';
		var strUrl = '';
		var wObj = window;
		if(window.parent) wObj = window.parent;
			strUrl = wObj.location.href;
			var pos=strUrl.lastIndexOf('?');
			var strFile = strUrl.substring(0,pos);
			var strQuery = strUrl.substring(pos+1,strUrl.length);
			var arrQuery = strQuery.split('&');
			var strQueryNew = '';
			for(var i=0;i<arrQuery.length;i++){
				if((arrQuery[i]!=null)&&(arrQuery[i]!='')){
					var arrItem = arrQuery[i].split('=');
					if((arrItem[0]!='')){
						if((arrItem[0].indexOf(typeFav)==-1)&&(arrItem[0].indexOf(typePla)==-1)){
							strQueryNew += arrQuery[i] + '&';
						}
					}
				}
			}
	return strFile + '?' + strQueryNew ;		
}
function addnPlaylist(f){
	var arrEs = new Array();
	var strEs = '';
	var j=0;
	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.checked)) {
			arrEs[j] = e.value;
			strEs += e.value + ',';
			j++;
		}
	}
	if(strEs.length) strEs = strEs.substr(0,strEs.length-1);
	var strPlaylist = getCookie('playlist');
	if(strPlaylist==null) {
		setCookie('playlist',strEs,90);
		refeshPlaylist();
		alert(arrEs.length + ' song(s) \'re added to your playlist !');
		return false;
	}
	var arrPlaylist = strPlaylist.split(',');
	if(arrPlaylist.length<100){
		var counter = 0;
		var strInsert = '';
		for(var n in arrEs){
			var m = false;
			for(var e in arrPlaylist){
				if(arrPlaylist[e]!=''){
					if(arrPlaylist[e]==arrEs[n]) {
						m = true;
						break;
					}
				}
			}
			if(!m) {
				counter++;
				strInsert += arrEs[n] + ',';
			}
		}
		if(strInsert.length) {
			strInsert = strInsert.substr(0,strInsert.length-1);
			setCookie('playlist',strPlaylist+','+strInsert,90);
			refeshPlaylist();
		}
		alert(counter + ' song(s) \'re added to your playlist !');
		return false;
	}
}
function add2Playlist(songid){
	var strPlaylist = getCookie('playlist');
	if(strPlaylist==null) {
		setCookie('playlist',songid,90);
		refeshPlaylist();
		alert('The song\'s added to your playlist !');
		return false;
	}
	var arrPlaylist = strPlaylist.split(',');
	if(arrPlaylist.length<100){
		for(var e in arrPlaylist){
			if(arrPlaylist[e]!=''){
				if(arrPlaylist[e]==songid) {
					alert('This song \'s already in your playlist !');
					return false;
				}
			}
		}
		setCookie('playlist',strPlaylist+','+songid,90);
		refeshPlaylist();
		alert('The song\'s added to your playlist !');
		return false;
	}
}
function refeshPlaylist(){
	var strPlaylist = getCookie('playlist');
	if(strPlaylist==null) {
		displayPlaylist();
	}else{
		var arrPlaylist = strPlaylist.split(',');
		var wObj = (window.parent)?window.parent:window;
		var playlistSpan = wObj.document.getElementById('playlist_display_number');
		if(playlistSpan){
			playlistSpan.innerHTML = '<b>' + arrPlaylist.length + ' clips</b>';
		}else{
			displayPlaylist();
		}
	}
}
function displayPlaylist(){
	var wObj = (window.parent)?window.parent:window;
	var boxObj = wObj.document.getElementById('play_list_box');
	if(boxObj){
		var strPlaylist = getCookie('playlist');
		if(strPlaylist!=null){
			var arrPlaylist = strPlaylist.split(',');
			if(arrPlaylist.length){
				var str = '<table width="100%" cellpadding="0" cellspacing="0">';
				str += '<tr><td colspan="2" align="center" height="30"><a href="musics_playlist.php" title="Edit Playlist"><span  class="text_red" id="playlist_display_number"><b>' + arrPlaylist.length + '</b> clips</span></a> available.</td></tr>';
				str += '<tr><td align="center" colspan="2"><a href="" onClick="clearPlaylist();return false;">Clear playlist</a>&nbsp;&nbsp;';
				str += '|&nbsp;&nbsp;' + ((thisUserOk) ? '<a href=\'\' onClick="openBox(365, 600, \'yes\',\'no\',\'100\',\'300\',\'play_music.php?from=playlist\');return false;">Play playlist</a>' : '<a href=\'\' onClick="return false;" title="Your must login to listen playlist.">Play playlist</a>') + '</td>';
				str += '</tr></table>';
				boxObj.innerHTML = str;
				return false;
			}
		}
		var str = '<table width="100%" cellpadding="0" cellspacing="0">';
		str += '<tr><td colspan="2" height="20" align="center">Empty.</td></tr>';
		str += '</table>';
		boxObj.innerHTML = str;
	}
	return false;
}
function clearPlaylist(){
	if(confirm('Are you sure to clear your playlist ?')){
		removeCookie('playlist');
		displayPlaylist();
	}
}
function addOneItem(type,songid){
		var strFav = 'add_one_favorites';
		var strPla = 'add_one_playlist';
		var typeFav = 'favorites';
		var typePla = 'playlist';
		switch(type){
			case typeFav:
				var strItem = strFav;
				break;
			default :
				var strItem = strPla;
				break;
		}
		var strUrl = '';
		var wObj = window;
		if(window.parent) wObj = window.parent;
			strUrl = wObj.location.href;
			var pos=strUrl.lastIndexOf('?');
			var strFile = strUrl.substring(0,pos);
			var strQuery = strUrl.substring(pos+1,strUrl.length);
			var arrQuery = strQuery.split('&');
			var strQueryNew = '';
			for(var i=0;i<arrQuery.length;i++){
				if((arrQuery[i]!=null)&&(arrQuery[i]!='')){
					var arrItem = arrQuery[i].split('=');
					if((arrItem[0]!='')&&(arrItem[0]!='song_id')&&(arrItem[0]!=strFav)&&(arrItem[0]!=strPla)){
						if((arrItem[0].indexOf(typeFav)==-1)&&(arrItem[0].indexOf(typePla)==-1)){
							strQueryNew += arrQuery[i] + '&';
						}
					}
				}
			}
		wObj.location.href = strFile + '?' + strQueryNew + strItem + "=1&song_id="+songid;
	}

function removeList(type){
		var strFav = 'empty_favorites';
		var strPla = 'empty_playlist';
		var typeFav = 'favorites';
		var typePla = 'playlist';

		switch(type){
			case typeFav:
				var strItem = strFav;
				break;
			default :
				var strItem = strPla;
				break;
		}
		var strUrl = '';
		var wObj = window;
		if(window.parent) wObj = window.parent;
			strUrl = wObj.location.href;
			var pos=strUrl.lastIndexOf('?');
			var strFile = strUrl.substring(0,pos);
			var strQuery = strUrl.substring(pos+1,strUrl.length);
			var arrQuery = strQuery.split('&');
			var strQueryNew = '';
			for(var i=0;i<arrQuery.length;i++){
				if((arrQuery[i]!=null)&&(arrQuery[i]!='')){
					var arrItem = arrQuery[i].split('=');
					if((arrItem[0]!='')&&(arrItem[0]!='song_id')&&(arrItem[0]!=strFav)&&(arrItem[0]!=strPla)){
						if((arrItem[0].indexOf(typeFav)==-1)&&(arrItem[0].indexOf(typePla)==-1)){
							strQueryNew += arrQuery[i] + '&';
						}
					}
				}
			}
			if(confirm('Are you sure to clear your '+type+' ?')){
				wObj.location.href = strFile + '?' + strQueryNew + strItem + "=1";
			}
}
function openHref(url){
	var openObj = window;
	if(window.opener) openObj = window.opener;
	openObj.location.href = url;
}
function checkTrue(f){
	f.submit();
	return true;
}
function upper_first_char(str){
	var value_upper = '';
	if((str!=null)&&(str!='')){
		value_upper = str.charAt(0).toUpperCase();
		for(var i=1;i<str.length;i++){
			value_upper += str.charAt(i);
		}
	}
	return value_upper;
}
function upper_first_chars(str){
	var value_upper = '';
	if((str!=null)&&(str!='')){
		var arr_upper = str.split(' ');
		for(var i=0;i<arr_upper.length;i++){
			var arr_i = arr_upper[i];
			var value_upper_i = arr_i.charAt(0).toUpperCase();
			value_upper += value_upper_i;
			for(var j=1;j<arr_i.length;j++){
				value_upper += arr_i.charAt(j);
			}
			value_upper += ' ';
		}
	}
	return Trim(value_upper);
}
function resizeIF(){
	if(window.parent){
		var pcg = document.getElementById('body_top_table');
		if(pcg){
			var ifObj = window.parent.document.getElementById('td_album_pro');
			if(ifObj){
				ifObj.style.height = pcg.offsetHeight + 25;
			}
		}	
	}	
}
function addSpaceN(text){
	var str = '';
	for(var i=0;i<text.length;i++){
		if(text.charAt(i)==' ') str += '&nbsp; &nbsp;';
		else str += text.charAt(i) + ' ';
	}
	return str;
}
//window.defaultStatus = "Netnhac.com - Am nhac khong bien gioi";
