var UrlPath = "";
for (var i=1; i<location.pathname.split("/").length - 1; i++){
	UrlPath = UrlPath + '/' + location.pathname.split("/")[i];
}

String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function convertTags(strInputEntry){
	strInputEntry = strInputEntry.replace(/'/g, "\'");
	strInputEntry = strInputEntry.replace(/ /g, "%20");
	strInputEntry = strInputEntry.replace(/&/g, "%26");
	
	return strInputEntry;
}

//Top offset
function getOffsetTop(elm){
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

//Left offset
function getOffsetLeft(elm){
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

//Jump Scroll
function jumpScroll(elemenID){
	var objLocation = document.getElementById(elemenID);
	var pos = getOffsetTop(objLocation) - 100;
	window.scroll(0,pos);
}

function SubmitForm(){
	var keyword = document.getElementById('keyword').value;
	var genre = document.getElementById('genre')[document.getElementById('genre').selectedIndex].value;
	
	for (var i=0; i<document.getElementsByName('by').length; i++) {
		if (document.getElementsByName('by')[i].checked == true){
			var by = document.getElementsByName('by')[i].value;
		}
	}
	if (keyword != ""){
		if (by == "" || by == undefined || by == 1)
			if (genre == "" || genre == undefined)
				window.location.href = UrlPath + '/#key.' + encodeURIComponent(keyword);
			else
				window.location.href = UrlPath + '/#type.' + genre + ',key.' + encodeURIComponent(keyword);
		else
			if (genre == "" || genre == undefined)
				window.location.href = UrlPath + '/#by.' + by + ',key.' + encodeURIComponent(keyword);
			else
				window.location.href = UrlPath + '/#type.' + genre + ',by.' + by + ',key.' + encodeURIComponent(keyword);
	}
	
	postAjaxData_Song('get_song_aj.asp','t=' + genre + '&b=' + by + '&k=' + convertTags(keyword),'song');
	jumpScroll('song');
}

function SubmitLoginForm(){
	var name = document.getElementById('name').value;
	var password = document.getElementById('password').value;
	var autologin = document.getElementsByName('autologin')[0].checked;
	//var NS = document.getElementById('NS').value;
	if (document.getElementsByName('securityCode').length > 0){
		var securityCode = document.getElementById('securityCode').value;
		postAjaxDataNoLoadingAlert('login_user.asp','name=' + name + '&password=' + password + '&autologin=' + autologin + '&NS=true&securityCode=' + securityCode,'login');
	}else{
		postAjaxDataNoLoadingAlert('login_user.asp','name=' + name + '&password=' + password + '&autologin=' + autologin + '&NS=true','login');
	}
	jumpScroll('login');
}

function LogoutUser(){
	getAjaxDataNoLoading('logout_user.asp?rnd=' + Math.random(),'login');
	jumpScroll('login');
}

function checktab(elementName,order){
	for (var i=0; i<document.getElementsByName(elementName).length; i++){
		if (order == i){
			document.getElementsByName(elementName)[i].checked = true;
			document.getElementById('tab'+order).style.background = 'beige url(styles/bg_tab_checked.gif) repeat-x bottom left'; 
			document.getElementById('tab'+order).style.color = '#F9F900'; 
			
			for (i=0; i<document.getElementsByName(elementName).length; i++){
				if (i != order){
					document.getElementById('tab'+i).style.background = 'beige url(styles/bg_tab_nocheck.gif) repeat-x bottom left';
					document.getElementById('tab'+i).style.color = '#000000';
				}
			}
		}
	}
}

function checkoption(elementName,order){
	for (var i=0; i<document.getElementById(elementName).options.length; i++){
		if (order == i){
			document.getElementById(elementName).options[i].selected = true;
			
		}
	}

}


function SendComment(){
	var playlist = document.getElementById('playlist').value;
	var message = encodeURI(document.getElementById('message').value.trim());
	
	if (message == ""){
		document.frmMessage.message.value = '';
		document.frmMessage.message.focus();
	}else{
		message = message.replace(/&/g, "amp;");
		message = message.replace(/\+/, "@dd");
		//document.getElementById('emoticons').style.display = "none";
   		getAjaxData('get_msg_comment.asp?playlist=' + playlist + '&message=' + message + '&rnd=' + Math.random(), 'comment');
   		document.frmMessage.message.value = '';
	}
}

function toggleEmo(objDiv)
{
	var objBox = document.getElementById(objDiv);
	var objLocation = document.getElementById('emo')
	
	objBox.style.left = (getOffsetLeft(objLocation) - 280) + 'px';
	objBox.style.top = (getOffsetTop(objLocation) - 58) + 'px';
	
	if (objBox.style.display == "none")
		objBox.style.display = "";
	else
		objBox.style.display = "none";

}

function AddEmo(iconCode){
	var touser = document.frmMessage.ToUser;
 	var txtarea = document.frmMessage.message;
	
	if (txtarea.value.length + iconCode.length <= 200){
	txtarea.value  += iconCode;
	}
	txtarea.focus();
	
	document.getElementById('emoticons').style.display = "none";
}

//PlayType => 1:Play Song | 2:Play List
//RunMode => 0:An | 1:Binh thuong | 2:Tu chay
//WidthORHeight => Width khi ModePlay=
function GetCode(ID, PlayType, RunMode, Pixel){
	var codeinput;
	
	switch (RunMode){
		case 0:
			if (PlayType == 1){
				codeinput = '<embed src="http://nhac.koquen.com/player.swf?file=http://nhac.koquen.com/file.asp?i=' + ID + '" width="0" height="0" hidden type="application/x-shockwave-flash" quality="high" wmode="transparent"></embed>';
			}else{
				codeinput = '<embed src="http://nhac.koquen.com/player.swf?file=http://nhac.koquen.com/playlist_file.asp?i=' + ID + '" width="0" height="0" hidden type="application/x-shockwave-flash" quality="high" wmode="transparent"></embed>';
			}
			break;
		case 1:
			if (PlayType == 1){
				codeinput = '<embed src="http://nhac.koquen.com/player.swf?file=http://nhac.koquen.com/file.asp?i=' + ID + '&autostart=false" width="380" height="' + Pixel + '" type="application/x-shockwave-flash" quality="high" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed>';
			}else{
				codeinput = '<embed src="http://nhac.koquen.com/player.swf?file=http://nhac.koquen.com/playlist_file.asp?i=' + ID + '&autostart=false&displaywidth=' + Pixel + '" width="580" height="280" type="application/x-shockwave-flash" quality="high" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed>';
			}
			break;
		case 2:
			if (PlayType == 1){
				codeinput = '<embed src="http://nhac.koquen.com/player.swf?file=http://nhac.koquen.com/file.asp?i=' + ID + '" width="380" height="' + Pixel + '" type="application/x-shockwave-flash" quality="high" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed>';
			}else{
				codeinput = '<embed src="http://nhac.koquen.com/player.swf?file=http://nhac.koquen.com/playlist_file.asp?i=' + ID + '&displaywidth=' + Pixel + '" width="580" height="280" type="application/x-shockwave-flash" quality="high" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed>';
			}
			break;
	}
	document.getElementById('htmlcode').value = codeinput;
}

