// JavaScript Document
	 var xmlDoc;
	 var mainMenu = new Array();
	 var subMenu = new Array();
	 var channelSynopsis  = new Array();
	 var player;


function loadXML(xmlFile,type) {
	
	try{
		
		var un = document.getElementById("afterlogin").style.display;
		
		//alert('in here'+  un);
		
		if(un == 'none') {
		//	alert('***');
			openLoginPage();				
		} else {
			
		var ut = document.getElementById("usertype").value;
				var pg = document.getElementById("package").value;
		
		//alert("pg:" + pg);
		//var selectedClient = xmlFile;
		
		if(pg.toUpperCase() != '1001' && pg!= "") 
			if (ut.toUpperCase() != 'YOONIC' && type!='Channels' && ut!= ""){
				if(xmlFile.toUpperCase() != ut.toUpperCase() && xmlFile.toUpperCase()!= 'YOONIC'){
					alert('Sorry! You are not authorised to watch this channel!');
					return false;
				}			
		}
		
		
		
		xmlDoc = null;
		
		var name = null;
			
			if (type!= 'Channels')
				name = "xml/" + xmlFile + ".xml";
			else
				name = "xml/channel/" + xmlFile; 
								
//			alert('name:'+ navigator.appVersion);
		if (window.ActiveXObject)
		{// code for IE
			//alert(' in IE');
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false; 
			xmlDoc.load(name); 
		}
		else 
		{// code for Mozilla, Firefox, Opera, etc.
			//xmlDoc = document.implementation.createDocument("", "", null);
		//	alert(' in other than IE');
			var xmlhttp = new window.XMLHttpRequest();
			xmlhttp.open("GET", name, false);
			xmlhttp.send(null);
			xmlDoc = xmlhttp.responseXML.documentElement;
		/*	xmlDoc=document.implementation.createDocument("","",null);
			xmlDoc.async=false; 
			xmlDoc.load(name);*/
		}
		
		if (xmlDoc!=null) 
		{
			xmlDoc.onreadystatechange=verify; 
				
			var arr = new Array();
			var arr2 = new Array();
			var node = null;
			
			
			if(type == 'Main'){
				node = xmlDoc.getElementsByTagName("category");
					
				for(var i=0; i<node.length; i++) 
				{
					//alert(node[i].childNodes[0].firstChild.nodeValue);
					//alert("===" + node[i].childNodes[1].firstChild.nodeValue);
					//alert(node[i].childNodes[1].firstChild.nodeValue);
					if(navigator.appName == "Microsoft Internet Explorer"){
						arr[i] = node[i].childNodes[0].firstChild.nodeValue; //category name
						arr2[i] = node[i].childNodes[1].firstChild.nodeValue; //xmlFile
						
					}
					else{
						
						//alert(node[i].childNodes[1].firstChild.nodeValue);
						//alert(node[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
						//alert(node[i].childNodes[1].nextSibling.nodeValue);
						arr[i] = node[i].childNodes[1].firstChild.nodeValue;
						arr2[i] = node[i].childNodes[3].firstChild.nodeValue;
						
					}
					//alert(arr2[i]);
					mainMenu[i]  = arr2[i]; //xmlFile
				}
				//alert("123");
				addrow('categoryTable', arr,arr2,type);
			} //end if type == main
			else if(type=='subMain')  {
				
				//alert("***");
				var str= xmlFile;
				var clientName = str.toUpperCase();
				document.getElementById("client").innerHTML = clientName;
				
				//document.getElementById("blank20").style.display = "none";
				document.getElementById("dock").style.display = "none";
				document.getElementById("menu").style.display = "block";
				document.getElementById("close").style.display = "block";
				
				//alert("in sub main");
				node = xmlDoc.getElementsByTagName("channel");
				//alert("navigator.appName:" + navigator.appName + ":" + node.length);
				
				for(var i=0; i<node.length; i++) 
				{
					
					if(navigator.appName == "Microsoft Internet Explorer"){
						arr[i] = node[i].childNodes[0].firstChild.nodeValue; //sub category name
						arr2[i] = node[i].childNodes[1].firstChild.nodeValue; //xmlfile name
						channelSynopsis[i]  = node[i].childNodes[2].firstChild.nodeValue;
					}else{
						//alert(node[i].childNodes[1].firstChild.nodeValue);
						arr[i] = node[i].childNodes[1].firstChild.nodeValue; //sub category name
						arr2[i] = node[i].childNodes[3].firstChild.nodeValue; //xmlfile name						
						channelSynopsis[i]  = node[i].childNodes[5].firstChild.nodeValue; //synopsis
					}
					subMenu[i] = arr2[i]; 
					
				}
				
				addrow('subCategoryTable', arr,arr2,type);
				
				//document.getElementById("synopsisTable").innerHTML = null;
				//document.getElementById("channelsTable").innerHTML = null;
				
			} //end of submain
			else if(type=='Channels'){
				
				//alert('in Channels');
				var arr3 = new Array();
				var arr4 = new Array();
				node = xmlDoc.getElementsByTagName("channel");
				//alert('node.length: ' + node.length);
				
				for(var i=0; i<node.length; i++) 
				{
					//alert(node[i].childNodes[7].firstChild.nodeValue);
					if(navigator.appName == "Microsoft Internet Explorer"){
						//alert("image1:" + node[i].childNodes[3].firstChild.nodeValue);
						arr[i] = node[i].childNodes[0].firstChild.nodeValue; //channel name
						arr2[i] = node[i].childNodes[1].firstChild.nodeValue; //image
						arr3[i]  = node[i].childNodes[2].firstChild.nodeValue; //source link
						arr4[i]  = node[i].childNodes[3].firstChild.nodeValue; //source link
					}else{
						//alert("image2:" + node[i].childNodes[7].firstChild.nodeValue);
						arr[i] = node[i].childNodes[1].firstChild.nodeValue; //channel name
						arr2[i] = node[i].childNodes[3].firstChild.nodeValue; //image
						arr3[i]  = node[i].childNodes[5].firstChild.nodeValue; //source link
						arr4[i]  = node[i].childNodes[7].firstChild.nodeValue; //source link
					}
				}
				
				loadChannels(arr,arr2,arr3,arr4);
			}//end of channels
	
		} //end xmlDoc
		}
		}catch(e){
			//alert("Error (11): " + e);	
		}
  }//end of loadXML
  
   function verify() 
{ 
 // 0 Object is not initialized 
 // 1 Loading object is loading data 
 // 2 Loaded object has loaded data 
 // 3 Data from object can be worked with 
 // 4 Object completely initialized 
 //alert("--" + xmlDoc.readyState)
 if (xmlDoc.readyState != 4) 
 { 
   return false; 
 } 
}

function viewVideo(url,title){
	//alert('url:' + url);
	var un = document.getElementById("un").value;
	if(un!="" || un.length > 0 ){
		if(url == "comingsoon")
			alert('Coming soon');	
		else{
			//alert(url);
			var path = "http://bitcast-b.bitgravity.com/syqic/YoonicDemo/";
			//var player = document.getElementById("container");
			jwplayer("container").load(path + url);
		}
	}else{
		 openLoginPage();		
	}
}

/*function viewVideo(url,title,type){
	//alert('url:' + url);
	var un = document.getElementById("un").value;
	//var un = str.replace("username=", "");
	//alert(un.length);
	//alert('Coming soon' + );
	if(un!="" || un.length > 0 ){
		if(url == "comingsoon")
			alert('Coming soon');	
		else{
				sendToActionScript(url,title,type);
		}
	}else{
		 openLoginPage();		
	}
}*/

function doesPopupExist(window_name) {
  var exist = false;
  for (i=0; i<winnames.length; i++) {
    if (winnames[i] == window_name) {
		window.
      exist = true;
      break;
    }
  }
 
  return exist;
}

/*function viewVideo(url){ //for p2p engine
	//alert('url:' + url);
	
	if(url == "comingsoon")
		alert('Coming soon');
	else{
		if(checkJre())
			//if(url == '301') //tutup
				document.myTV.setChannel(''+ url + '');
			//else //tutup
				//document.myTV.setChannel('201'); //tutup
	}
}*/

function addrow(tablename, arr, arr2,type) {
	
	//alert('rowspan:' + totalMainCategory);
	//alert("in addrow: " + type);
		removeRows('subCategoryTable');
		document.getElementById('synopsis').innerHTML =  "";	
		
		
		var tbl = document.getElementById(tablename);
		
		
		for (r = 0; r < arr.length; r++) { 
		  var row = tbl.insertRow(r);
          var cell = row.insertCell(0);
		
		//alert("in addrow: " + type != 'subMain');
		
		if (type != 'subMain'){
			
			
         cell.innerHTML = '<div id="menu' + r + '" onMouseOver="javascript:onMouseOver(this);" onMouseOut="javascript:onMouseOut(this);" onClick="javascript:loadXML(\''+ arr2[r] + '\',\'subMain\');" style="margin-left:10px; margin-top:2px;" >' + arr[r] + '</div>';
		}else{
			//alert("in here");
			cell.innerHTML = '<div id="submenu' + r + '"  onMouseOver="javascript:onMouseOver(this);" onMouseOut="javascript:onMouseOut(this);" onClick="javascript:loadSynopsis(\''+ arr2[r] +'\',\'' + channelSynopsis[r] + '\');" style="margin-left:10px; margin-top:2px;">' + arr[r]+ '</div>';
			 
		}
      }
}

function removeRows(tableName){
	var tbl2 = document.getElementById(tableName);
   
    while(tbl2.lastChild){
         tbl2.removeChild(tbl2.lastChild);
	}	
		
}

function loadSynopsis(xmlFile,synopsisText) {
	
	
	//alert('in here to load synopsis');
	if (synopsisText == "NA") {
		document.getElementById('synopsis').innerHTML =  "";	
	} else {
		document.getElementById('synopsis').innerHTML =  synopsisText;	
	}
	
	//alert('xmlfile:' + xmlFile );
 	loadXML(xmlFile,'Channels');
      //   	alert('xmlfile:' + xmlFile );	 
     
}

function loadChannels(arr,arr2,arr3,arr4){
	
	//alert('in here');
      var imgText = "";
	  var chName = "";
	  var tbl1="";
	  var tbl2="";
	  var tbl3="";
	  
	  clear();
	  
	  var index = 0;
		
		for (r = 0; r < arr.length; r++) { 		
		
	//	alert("r:" + r);
		//alert('in loading channels\' icon : '  + arr2[r] );
		  //  var cell = row.insertCell(r);	
		  tbl = document.getElementById('channelIcon' + r);
		
		tbl.innerHTML = '<img width="80" height="100"  src=' + arr2[r] + ' class="channelsIcon"/>' ;
		
		tbl2 = document.getElementById('channelName' + r);
		
		tbl2.innerHTML ='<div>' + arr[r]+ '</div>';
		
		tbl3 = document.getElementById('detail' + r);
		
		//alert("--" + arr4[r]);
		
		//alert("--//***:" + arr3[r]);
		
		
		if(arr4[r]!= "NA") {
				
			
				tbl3.innerHTML ='<div><table width="100%" border="0"><tr align="center"><td ><label onClick="viewVideo(\''+ arr3[r] + '\',\'' + arr[r] + '\',\'v\');" style="font-size:12px;" id="detail'+ index +'-a" onMouseOver="javascript:onMouseOver2(this);" onMouseOut="javascript:onMouseOut2(this);">' + 'LQ</label></td><td><label onClick="viewVideo(\''+ arr4[r] + '\',\'' + arr[r] + '\',\'v\');" style="font-size:12px;" id="detail'+ index +'-b" onMouseOver="javascript:onMouseOver2(this);" onMouseOut="javascript:onMouseOut2(this);">' + 'HQ</label></td></tr></table></div>';
				
			
		} else {
				tbl3.innerHTML ='<div><table width="100%" border="0"><tr align="center"><td ><label onClick="viewVideo(\''+ arr3[r] + '\',\'' + arr[r] + '\',\'v\');" style="font-size:12px;" id="detail'+ index+'" onMouseOver="javascript:onMouseOver2(this);" onMouseOut="javascript:onMouseOut2(this);">' + 'Play</label></td></tr></table></div>';
				
		}
		
		index += 1;	
		 
		// alert(index);
		//'<div class="mainMenu" onMouseOver="javascript:onMouseOver(this);" onMouseOut="javascript:onMouseOut(this);กต ' + arr[r] + '</div> &nbsp;';
			// cell.innerHTML ='<td width="100" align="center"><img width="80" height="60"  src='../ + arr2[r] + ' onClick="viewVideo(\''+ arr3[r] + '\');" class="channelsIcon"/><div class="channelName">' +  arr[r] + '</div><div  class="moreInfo"><a href="../Archive.html" onclick="showModalBox();"> More video>></a></div></td>';
			//alert("####" + arr.length);
			
				
			
		}
	
		//tbl2.innerHTML = chName;
}

function clear(){	
	for(j=0; j<4; j++){
		document.getElementById('channelIcon' + j).innerHTML = "";
		document.getElementById('channelName' + j).innerHTML = "";
		document.getElementById('detail' + j).innerHTML = "";
	}
}

function getParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function onMouseOver(menu){
	//alert("===" + menu.id);
	document.getElementById(""+  menu.id +"").style.color = "#cc3300";	
	document.getElementById(""+  menu.id +"").style.letterSpacing = "2";
}

function onMouseOut(menu){
	//alert("===**" + menu.id);
	document.getElementById(""+  menu.id +"").style.color = "white";
	document.getElementById(""+  menu.id +"").style.letterSpacing = "1";
	
}

function onMouseOver2(menu){
	//alert("===" + menu.id);
	//document.getElementById(""+  menu.id +"").style.color = "white";	
	//document.getElementById(""+  menu.id +"").style.letterSpacing = "2";
	document.getElementById(""+  menu.id +"").style.textDecoration = "underline";
}

function onMouseOut2(menu){
	//alert("===**" + menu.id);
	document.getElementById(""+  menu.id +"").style.color = "red";
	//document.getElementById(""+  menu.id +"").style.letterSpacing = "1";
	document.getElementById(""+  menu.id +"").style.textDecoration = "none";
	
}

function downloadPlayer(){
//alert('in here to downloadplayer');
//window.location.href ="Yoonic Video Engine.msi";
window.open('download.html','welcome','width=300,height=200,menubar=yes,status=yes,location=no,toolbar=no,scrollbars=no');
//getplayer();
location.reload(true);

}

function getplayer(){
	//alert("in here to get Player");
	window.location.href = "download/Yoonic Video Engine Setup.msi";
}

function viewDetails(id){
alert('Coming Soon!');
//alert('id:' + id);
	//window.open('download.html','_blank','height=200,width=530,resizable=no,scrollbars=no;location=no',true);

}

function goSignUp(){
	//alert("in here");
	window.open('registration.html','_blank',
   'height=520,width=660,resizable=no,scrollbars=no;location=no',true);
	
}


function openLoginPage(){

	window.open('login.html','_blank',
   'height=200,width=530,resizable=no,scrollbars=no;location=no',true);

	//location.reload(true);

}

function forgotPassword(){

	window.open('forgotPwd.html','_blank',
   'height=200,width=530,resizable=no,scrollbars=no;location=no',true);

	//location.reload(true);

}

function checkJre(){
 var versions = deployJava.getJREs();
 //var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
 //alert('versions:' + versions);
 if(versions == ''){
  	alert("You don't have JRE installed in your system. Please download and install it first!");
  	var urlDown = 'http://java.com/en/download/index.jsp';
  window.open(urlDown,'_blank','height=700,width=630,resizable=no,scrollbars=no;location=no',true);
  	return false;
 }else
 	return true;
}

function openChangePassword(){

	window.open('changePassword.html','_blank',
   'height=250,width=530,resizable=no,scrollbars=no;location=no',true);

	//location.reload(true);

}

function openContactUs(){

	window.open('ContactUs.html','_blank',
   'height=410,width=530,resizable=no,scrollbars=no;location=no',true);

	//location.reload(true);

}

/*function sendTextToFlash(username) {
	 //alert('in here:'+ username);
     thisMovie("YoonicIndo").sendTextToFlash(username);
}*/
	 
function toSubscribe() 
{ 
	var fRet; 
	fRet = confirm('Are you sure?'); 
	
	//alert(fRet);
	if(fRet)
		location.href = "package.html";
} 


function alertExpired(){
	var subscribe; 
	subscribe = confirm('Your account has expired. Please subscribe to continue enjoying our services?'); 
	
	if(subscribe)
		location.href = "subscribe.html";
}

 	function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
	 
     function sendToActionScript(location,streamname,type) {
	 	 //alert('in here:'+ type);
         thisMovie("yoonic").sendTextToFlash(location,streamname,type);
     } 
	 
	 function getParam(name) {
		  var regexS = "[\\?&]"+name+"=([^&#]*)";
		  var regex = new RegExp( regexS );
		  var tmpURL = window.location.href;
		  var results = regex.exec( tmpURL );
		  
		  if( results == null )
			return "";
		  else
			return results[1];
	}
	
	function firstLoad(un){
	//alert('url:' + window.location.href);
	//var url = window.location.href;
	//alert(url.indexOf("http://localhost:88/ptitv/"));
	//loadXML('category.xml','Main');
	//loadXML(mainMenu[0],'subMain');
	//loadSynopsis(subMenu[0],channelSynopsis[0]);
		
		//alert(un);
		if(un != null && un!= ""){ //once login
			document.getElementById("afterlogin").style.display ="block";
			document.getElementById("beforelogin").style.display ="none";
		}else{
			var uname = document.getElementById("un");
			uname.value = "";
			document.getElementById("afterlogin").style.display ="none";
			document.getElementById("beforelogin").style.display ="block";
		}
		
	//alert(val);
	//sendTextToFlash(val);
	
	}
	
	function closesubmenu(){
		
		//document.getElementById("blank20").style.display = "block";
		document.getElementById("client").innerHTML = " ";
		document.getElementById("dock").style.display = "block";
		document.getElementById("menu").style.display = "none";
		document.getElementById("close").style.display = "none";
		//document.getElementById("synopsisTable").innerHTML = null;
		//document.getElementById("channelsTable").innerHTML = " ";
		clearchannels();
	}
	
	function clearchannels(){
		
		document.getElementById("channelIcon0").innerHTML = "";
		document.getElementById("channelIcon1").innerHTML = "";
		document.getElementById("channelIcon2").innerHTML = "";
		document.getElementById("channelIcon3").innerHTML = "";
		document.getElementById("channelName0").innerHTML = "";
		document.getElementById("channelName1").innerHTML = "";
		document.getElementById("channelName2").innerHTML = "";
		document.getElementById("channelName3").innerHTML = "";
		document.getElementById("detail0").innerHTML = "";
		document.getElementById("detail1").innerHTML = "";
		document.getElementById("detail2").innerHTML = "";
		document.getElementById("detail3").innerHTML = "";
		
	}

 
function setContent(path) {
	//alert(path);
	var url = "http://bitcast-b.bitgravity.com/syqic/YoonicDemo/";
	//var player = document.getElementById("container");
    jwplayer("container").load(url + path);
 }
 
