function getid(id) {
	return document.getElementById(id);
}

/* ==== DEBUG =====*/
function log(txt){
	if(document.all){
				document.getElementById('log').innerText += "\n" + txt;
	} else{
			document.getElementById('log').textContent += "\n" + txt;
	}
}
/*
function innerHTML(){
	if(document.all){
			document.getElementById('log').innerText += "\n" + txt;
	} else{
			document.getElementById('log').textContent += "\n" + txt;
	}
}*/

function getTotalComment(){
		try {
			var totalComment = window.frames["commentFrame"].document.getElementById("comment_TotalComment").innerHTML;
			//alert(totalComment);
			//log("totalcomment"+totalComment);
			if (totalComment == "undefined" || totalComment == ""){
				totalComment = "0";
			}
			
			window.frames["commentFrame"].document.forms["Comment"].rtp.value = COMMENT_URL + current_guid ;
			
		}catch(e){ 
			//alert(e.description);
			totalComment = "0";
		}
		return totalComment;
}

function adjustIFrameSize (iframe)
{
	if ( iframe.contentDocument ) // firefox
	{
		iframe.width = iframe.contentDocument.documentElement.scrollWidth;
		iframe.height = iframe.contentDocument.documentElement.scrollHeight;
	}
	else // IE
	{
		iframe.style.width = iframe.contentWindow.document.body.scrollWidth;
		iframe.style.height = iframe.contentWindow.document.body.scrollHeight;
	}
}

function callAdjustIFrameSize(){
	var commentFrame = document.getElementById("tagCommentFrame");
	adjustIFrameSize(commentFrame);
}

function recallAdjustIFrameSize(){
	setInterval("callAdjustIFrameSize()", 2000);
}

function onCommentLoaded(){
	try
	{
		var tagCommentFrame = getid("tagCommentFrame");
		var tagTotalCommentFrame = getid("tagTotalComment");

		tagTotalCommentFrame.innerHTML = getTotalComment();
		adjustIFrameSize(tagCommentFrame);
	}
	catch (e)
	{
	}
	
}

function CreateCartForm(){
var tagCartForm = document.getElementById("divcartform");
var frm = "<form name='shoppingcartform' target='_blank' action='http://enabling.truelife.com/shopping/payperitem.jsp' method='post'>";
	 frm = frm + "<input type='hidden' name='token' id='token' value=''>";
	 frm = frm + "<input type='hidden' name='content_id_type' id='content_id_type' value=''>";
	 frm = frm + "<input type='hidden' name='content_type' id='content_type' value=''>";
	 frm = frm + "<input type='hidden' name='product_desc' id='product_desc' value=''>";
	 frm = frm + "<input type='hidden' name='quantity' id='quantity' value=''>";
	 frm = frm + "<input type='hidden' name='price' id='price' value=''>";
	 frm = frm + "<input type='hidden' name='duplicate' id='duplicate' value=''>";
	 frm = frm + "<input type='hidden' name='extendparam' id='extendparam' value=''>";
	 frm = frm + "<input type='hidden' name='cartid' id='cartid' value=''>";
	 frm = frm + "</form>";
	 if (tagCartForm)
	 {
		 tagCartForm.innerHTML = frm;
	 }
}

function addShoppingCart(_token,_content_id_type,_content_type,_product_desc,_quantity,_price,_duplicate,_extendparam,_cartid){
		//alert("_token"+_token+"_content_id_type"+_content_id_type+"_content_type"+_content_type+"_product_desc"+_product_desc+"_quantity"+_quantity+"_price"+_price+"_duplicate"+_duplicate+"_extendparam"+_extendparam+"_cartid"+_cartid);
		//alert("ขออภัยค่ะ ระบบซื้อปิดปรับปรุงชั่วคราวค่ะ");
		//return false;
		if (isLogin()){
			var frmobj = document.shoppingcartform; 
			with( frmobj ){
				token.value=_token;
				content_id_type.value=_content_id_type;
				content_type.value=_content_type;
				product_desc.value=_product_desc;
				quantity.value=_quantity;
				price.value=_price;
				duplicate.value=_duplicate;
				extendparam.value=_extendparam;
				cartid.value=_cartid;
				submit();
			}
		}
		else{
		alert("กรุณา Login ก่อนค่ะ");
		}
}
function addToFavorite()
{
	var strTitle = document.title;
	strTitle = strTitle.substring(0,100);
	window.external.AddFavorite(location.href, strTitle); 
}

function sendToFriends()
{
	var path = window.location;
	var windowprops = "directories=no,scrollbars=no,width=480,height=350,left=200,top=50,resizable=1";
	window.open("http://trueworld.truelife.com/newsletter/send2fr/index.jsp?requestpath=" + escape(path),"",windowprops)
}

function substr(str,n){
		if(str.length > n)
			return str.substring(0,n)+"...";
		else
			return str;
}

function openWin(URL,winName,w,h) {
		aWindow = window.open(URL, winName, 'width='+w+',height='+h+',location=no,resizable=no,scrollbars=no,screenX=20,screenY=20,top=20,left=20');
		aWindow.focus();
}

function ConvRingtoneIVRNumber(getRingtoneIVRNumber){
		var objRingtoneIVRNumber = new RingtoneIVRNumber();
		var postCode = objRingtoneIVRNumber.getPostCode(getRingtoneIVRNumber);
		rtnRingtoneIVRNumber = getRingtoneIVRNumber + postCode;
		return rtnRingtoneIVRNumber;
}

function addtojukebox(content_guid){

	 if (isLogin()){
		 add_to_jukebox(content_guid,central_id);
	}
	else {
		alert("กรุณา Login ก่อนค่ะ");
	}
}


function getCookie(name) {

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(i)==' ') c = c.substring(1,c.length);
		//alert(c + ";" + c.indexOf(nameEQ));
		if (c.indexOf(nameEQ) != -1) return c.substring(nameEQ.length+1,c.length);
	}
	return null;
}





function isLogin(){
	/*var Convergence = getCookie("Convergence");
	var userinfo_arr = new Array();
	var userinfo = "";
	if (Convergence){
		isLogin = 'true';
		userinfo = getCookie("userinfo");
		alert(userinfo);
		userinfo_arr = userinfo.split("|");
		alert(userinfo_arr);
		central_id = userinfo_arr[6];
	}*/
	var central_id = getCookie("CentralID");
	
	var rtn = true;
	try
	{
		if (central_id.length<=5){ rtn= false;}
		
	}
	catch (e)
	{
		rtn = false;
	}
	return rtn;
}

function querySt(ji) {
	try
	{
		hu = window.location.search.substring(1);
	}
	catch (e)
	{
		//alert("s"+e.description);
	}
	
	
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
	return "";
}

function playMVSongGuid(songGuid,winName){
	var mvURL = "http://truemusic.truelife.com/home/player/player_mv.php";
	if (winName != null && winName != "undefined")
			window.open(mvURL+"?songguid="+songGuid, winName);
	else
			window.location.href = mvURL+"?songguid="+songGuid;
}

function redirectSong_toWMPPlayer(guid){
	if(guid!=""){
		var yearofcontent = guid.substring(0,4);
		var monthofcontent = guid.substring(4,2);
		
		if(yearofcontent<=2009){
			redirectURL("wmp",guid);
		}
	}
}

function redirectSong_FlashPlayer(guid){
	if(guid!=""){
		yearofcontent = guid.substring(0,4);
		monthofcontent = guid.substring(4,2);
		//alert(yearofcontent);
		if(yearofcontent>=2009){
			
				redirectURL("flash",guid);
		}
	}
}

function redirectURL(playerType,guid){
	var new_url = "";
	switch(playerType){
	case "wmp":
		new_url = "http://truemusic.truelife.com/home/p/pw_song.php?songguid="+guid;
		break;
	case "flash":
		new_url = "http://truemusic.truelife.com/home/p/song.php?songguid="+guid;
		break;
	default:
		break;
	}
	//alert(new_url);
	window.location = new_url;

}


function CopyToClipBoard(objName,type) 
{
	var obj = document.getElementById(objName);
	obj.focus();
	obj.select();

	if(!document.all) {
		if(type=="button"){alert("กรุณาคลิกที่ Embed Code แล้วกด Ctrl+C เพื่อ Copy ค่ะ"); return false;}
		}
	
	if(type=="button"){
		Copied = obj.createTextRange();
		Copied.execCommand("Copy");
		alert("Copy Embed Code เรียบร้อยแล้วค่ะ");
	}
	
}
