function getCookieVal(offset){
 var endstr = document.cookie.indexOf(";",offset);
 if (endstr == -1)
 {
  endstr = document.cookie.length;
 }
 return unescape(document.cookie.substring(offset,endstr));
}

function getCookie(name){
 var arg = name + "=";
 var alen = arg.length;
 var clen = document.cookie.length;
 var i= 0;
 while (i<clen)
 {
  var j = i+alen;
   if (document.cookie.substring(i,j) == arg){
    return getCookieVal(j);
   }
  i = document.cookie.indexOf(" ",i)+1;
  if(i==0) break;
 }
 return null;
}

function setCookie(name,value){
 var argv = setCookie.arguments;
 var argc = setCookie.arguments.length;
 var expires = (2<argc)? argv[2]:null;
 var path = (3<argc)?argv[3]:null;
 var domain = (4<argc)?argv[4]:null;
 var secure = (5<argc)?argv[5]:null;
 document.cookie = name+"="+escape(value)+((expires == null)?" ":(";expires ="+expires.toGMTString()))+((path == null)?"  ":(";path = "+path))+((domain == null)?" ":(";domain =" +domain)) +((secure==true)?";secure":" ");
}

var autoad=getCookie("autoad");
if (autoad!=1) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime()+(24*60*60));
//	window.open("/index.php?action=ads","un","width=10px,height=10px;")
	setCookie("autoad",1,expdate,"/");
}

if(!navigator.cookieEnabled)alert('Seems that your browser have not turn on the Cookie,\nIf you want to continue,you should turn on the Cookie first. ')

var expdate = new Date();
expdate.setTime(expdate.getTime()+(24*60*60*1000*365));
language = getCookie("language");
if(language == null){
 if (navigator.appName == 'Netscape')
  language = navigator.language;
 else
  language = navigator.browserLanguage;
 language = language.substring(0,2);
}

function	popWin(theurl) {
 var leftPos = (screen.availWidth-500) / 2;
 var topPos = (screen.availHeight-500) / 2;
 window.open(theurl,'link','scrollbars=yes,width=500,height=500,top=' + topPos + ',left=' + leftPos);
}


function setLanguage(lang){
 setCookie("language",lang,expdate,"/",null,false);
 window.location.reload();
}

function setLanguageN(lang){
 setCookie("language",lang,expdate,"/",null,false);
}

function ShopAuth(name, url){
 ScreenConvert();
 var AJAX_LoginBody="<div id=\"DialogTitle\"><div class='title_bar'><a href='javascript:DialogHide()'><img src='images/close2.gif' ></a></div><div class='tilte3'>You musct login first then to continue!<br/><a href='?act=login&refer="+escape(url)+"'>click here to login</a> or <a href='?act=join&refer="+escape(url)+"'>join us</a> </div></div>";
 DialogShow(AJAX_LoginBody,450,590,500,350);
}

function AddLink(theurl,theid){
 var leftPos = (screen.availWidth-500) / 2;
 var topPos = (screen.availHeight-500) / 2;
 window.open(theurl+theid,'link','scrollbars=yes,width=500,height=500,top=' + topPos + ',left=' + leftPos);
}

function AddRss(){
 var leftPos = (screen.availWidth-500) / 2;
 var topPos = (screen.availHeight-500) / 2;
 window.open('?mod=feed_manage&act=link','','scrollbars=yes,width=500,height=500,top=' + topPos + ',left=' + leftPos);
}

function FeedSetting(act){
 var leftPos = (screen.availWidth-400) / 2;
 var topPos = (screen.availHeight-590) / 2;
 window.open('?mod=feed_manage&act=setting&m='+act,'','scrollbars=yes,width=400,height=590,top=' + topPos + ',left=' + leftPos);
}

function GotoUrl(theurl,theid){
 window.location.href = theurl + theid;
}

function DropAction(theurl,theid){
 if(confirm("Are you sure ? ")){
  window.location.href = theurl + theid;
 }
}

function sort(act){
 window.location.href = "/?action=audio&act=sort&mod="+act;
}

function Drop(theurl,theid){
 if(confirm("Are you sure you want to delete this?")){
  var theurl = theurl+theid;
  var pars = 'Drop successful!';
  var myAjax = new Ajax.Request(
   theurl,{method: 'get', parameters: pars, onComplete: DropResponse});
 }
}


function Recycle(theurl,theid){
 if(confirm("Attension!\nthe file is move to the recycle bin ?\nyou can get it back from there.")){
  var theurl = theurl+theid;
  var pars = '';
  var myAjax = new Ajax.Request(
   theurl,{method: 'get', parameters: pars, onComplete: DropResponse}
  );
 }
}

function Restore(theurl,theid){
 if(confirm("Attension!\nrestore the file ?")){
  var theurl = theurl+theid;
  var pars = '';
  var myAjax = new Ajax.Request(
   theurl,{method: 'get', parameters: pars, onComplete: DropResponse}
  );
 }
}

function DropResponse(originalRequest){

 var result = originalRequest.responseText;
 var success = result.substring(0, 1);
  var theid = result.substring(2);
  if(success == 1){
  new Effect.Shrink($("item_"+theid));
 }
 else if(success == 2)
 {
  alert('It has user in group!');
  new Effect.Shrink($("item_"+theid));
 }
 else
 {
  alert(theid);
 }
}

function Modify(theid,thevalue){
 if(thevalue == 0){
  document.getElementById("title_"+theid).style.display = "none";
  document.getElementById("title_"+theid).style.display = "none";
  document.getElementById("rating_"+theid).style.display = "none";
  document.getElementById("length_"+theid).style.display = "none";
  document.getElementById("language_"+theid).style.display = "none";
  document.getElementById("title_m_"+theid).style.display = "";
  document.getElementById("rating_m_"+theid).style.display = "";
  document.getElementById("length_m_"+theid).style.display = "";
  document.getElementById("language_m_"+theid).style.display = "";
  document.getElementById("modify_"+theid).value = 1;
 }
 else
 {
  document.getElementById("title_"+theid).style.display = "";
  document.getElementById("rating_"+theid).style.display = "";
  document.getElementById("length_"+theid).style.display = "";
  document.getElementById("language_"+theid).style.display = "";
  document.getElementById("title_m_"+theid).style.display = "none";
  document.getElementById("rating_m_"+theid).style.display = "none";
  document.getElementById("length_m_"+theid).style.display = "none";
  document.getElementById("language_m_"+theid).style.display = "none";
  document.getElementById("modify_"+theid).value = 0;
  var title = listForm.elements["title_"+theid].value;
  var rating = listForm.elements["rating_"+theid].value;
  var length = listForm.elements["length_"+theid].value;
  var language = listForm.elements["language_"+theid].value;
  var theurl = "/?act=myhome&do=modify&id="+theid+"&title="+title+"&rating="+rating+"&length="+length+"&language="+language;
  iframe1.location.href = theurl;
 }
}

/* use in menu frame*/
/* start========================== */
function strpos(str, searchstring){
 for (var i = 0; i < str.length; i++)
  if (str.substring(i, i+searchstring.length) == searchstring) return i;

 return -1;
}

function menuClick(menuid, img){
 if (document.getElementById){
  var rmenu = document.getElementById (menuid);
  if (rmenu && rmenu.className){
   rmenu.className = (rmenu.className == 'menuOpen') ? 'menuClosed' : 'menuOpen';
   if (strpos(img.src, '../images/admin/window_minimize.gif') == -1){
    img.src = '../images/admin/window_minimize.gif';
   }
   else
   {
    img.src = '../images/admin/window_maximize.gif';
   }
  }
 }

 return false;
}
function makePopUp(varg1, varg2, varg3){
 dt = new Date();
 windowID = dt.getTime();
 Eval("page" + windowID + " = window.open('"+varg1+"');");
}


function changeFrame(name){
 parent.Main.location=name;
}
/* ==========================end */


function hiddenAction(name){
 new Effect.Fade($(name));
}

function showAction(name){
 new Effect.Appear($(name));
}

function flag(theid){
 var theurl = "/index.php?action=audio";
 var pars = "do=flag&id="+theid;
 document.getElementById("message").className = "slide-show";
 document.getElementById("message").innerHTML = "<img src='/images/loader.gif'>";
 var myAjax = new Ajax.Request(
  theurl,{method: 'get', parameters: pars, onComplete: flagRespone}
 );
}

function flagRespone(originalRequest){
 var result = originalRequest.responseText;
 document.getElementById("message").innerHTML = "<b>Message:</b> "+result;
}

function FlagAction(theid){
  var theurl = "?mod=file_manage";
  var pars = "act=objection&id="+theid;
  var myAjax = new Ajax.Request(
   theurl,
   {method: 'get', parameters: pars, onComplete: FlagActionRespone}
   );
}

function FlagActionRespone(originalRequest){
 var result = originalRequest.responseText;
 var success = result.substring(0, 1);
  var theid = result.substring(2);
  var trname = 'item_'+theid;
 if(success){
  $(trname).style.background = '#ffffff';
 }
}

function PingService(){
  $('ping_result').innerHTML = "<img src='/images/loader.gif'>";
  $('start_to_ping').disabled = true;
  var theurl = "?mod=ping_service";
  var pingid = '';
  for (var i=0;i<document.theForm.elements.length;i++){
   var ee = document.theForm.elements[i];
   if (ee.type == "checkbox" && ee.checked){
    pingid += ee.value+",";
   }
  }
  var pars = "act=ping&pingid="+pingid;
  var myAjax = new Ajax.Request(
   theurl,
   {method: 'get', parameters: pars, onComplete: PingServiceRespone}
   );

}

function PingServiceRespone(originalRequest){
 var result = originalRequest.responseText;
 $('start_to_ping').disabled = false;
 $('ping_result').innerHTML = result;

}

function showMenu(theid){
 Element.show(theid);
}

function hideMenu(theid){
 Element.hide(theid);
}

/*set user power*/
var powerCount = 0;
var powerSet = null;
function addUser(){
 users = document.getElementById("userList");
 selectedUsers = document.getElementById("userList2");
 //in list users, options 0 is all users
 for (i = 1; i < users.length; i ++){
  if ((! users.options[0].selected) && (! users.options[i].selected)){
   continue;
  }
  if ((selectedUsers.length > 0) && (selectedUsers.options[0].value == '-1')){
   selectedUsers.remove(0);
  }
  added = 0;
  for (j = 0; j < selectedUsers.length; j ++){
   if (selectedUsers.options[j].value == users.options[i].value){
    added = 1;
    break;
   }
  }
  if (added == 0){

   oOption =  document.createElement("OPTION");

   oOption.value = users.options[i].value;
   oOption.innerHTML = users.options[i].text;

   selectedUsers.appendChild(oOption);
  }
 }
}

function delUser(){
 selectedUsers = document.all("userList2");
 for (i = 0; i < selectedUsers.length; i ++){
  if (selectedUsers.options[i].selected){
   selectedUsers.remove(i);
   i --;
  }
 }
}

function clearUserPower(){
 powerObj = document.all("powerList");
 if (powerObj == null)
  return;

 for (i = 0; i < powerObj.length; i ++){
   powerObj[i].checked = false;
 }
}

function showPower(){
 selectedUsers = document.all("userList2");
 tmpCount = 0;
 index = 0;
 for (i = 0; i < selectedUsers.length; i ++){
  if (selectedUsers.options[i].selected){
   index = i;
   tmpCount ++;
   if (tmpCount > 1){
    alert("Only show one user once time !");
    break;
   }
  }
 }
 if (tmpCount == 0){
  alert("Please select user!");
 }

 else if (tmpCount == 1){
  $('power_list').innerHTML = "<img src='/images/loader.gif'>";
  currentUser = selectedUsers.options[index].value;
  theForm.elements["groupid"].value = currentUser;
  var theurl = "?mod=admin_user";
  var pars = "act=group_priv&id="+currentUser;
  var myAjax = new Ajax.Request(
   theurl,{method: 'get', parameters: pars, onComplete: showPowerRespone}
  );
 }
}

function showPowerRespone(originalRequest){
 var result = originalRequest.responseText;
 $('power_list').innerHTML = result;
}

function add_power(){
 var count = 0;
 var power = document.getElementsByName("power");
 var newpower = '';
 for(i=0;i<power.length;i++){
  if(power[i].checked == true){
   count++;
   newpower += power[i].value+',';
  }
 }
 if(count == 0){
  alert("Please at least select one power !");
 }
 else if(count > 0){
  Element.show('msg');
  var theurl = "?mod=admin_user";
  var groupid = theForm.elements["groupid"].value;
  if(groupid == ''){
   alert("Please select the group you want to set !");
   exit;
  }
  var pars = "?act=add_group_power&power="+newpower+"&groupid="+groupid;
  var myAjax = new Ajax.Request(
  theurl,{method: 'get', parameters: pars, onComplete:add_powerRespone}
  );
 }
}

function add_powerRespone(originalRequest){
 Element.hide('msg');
 var result = originalRequest.responseText;
 var success = result.substring(0,1);
 var message = result.substring(2);
  alert(message);
}

function add_group(){
 $('group_scape').style.display = "none";
 $('newgroup').style.display = "block";
}

function use_original(){
 $('group_scape').style.display = "block";
 $('newgroup').style.display = "none";
}

function tdw(id){
 return(toggle(id));
}

function toggle(id){
 var div=document.getElementById(id);
 if(div.className=='slide-show'){
  div.className='slide-hide';
 } else {
  div.className='slide-show';
 }
 return(false);
}

function IsExist(url){
 if(window.XMLHttpRequest){
  x = new XMLHttpRequest();
 }
 else if(window.ActiveXObject)
 {
  x = new ActiveXObject("Microsoft.XMLHTTP");
 }
 x.open("HEAD",url,false) ;
 x.send(null) ;
 return x.status==200;
}

function emailed(i){
 var theurl = "?mod=question";
 var id="e"+i
 if($(id).style.display=='none'){
 	$(id).style.display="";
  var pars = "act=emailed&emailed=1&id="+i;
 }else{
 	$(id).style.display="none";
  var pars = "act=emailed&emailed=0&id="+i;
 }
 var myAjax = new Ajax.Request(theurl,{method: 'get', parameters: pars});
}

function showhide(i){
 var theurl = "?mod=question";
 var id="s"+i
 if($(id).src.indexOf('icon_trash')==-1){
 	$(id).src="../images/admin/icon_trash.gif";
 	$(id).alt="Hide";
  var pars = "act=showhide&show=0&id="+i;
 }else{
 	$(id).src="../images/admin/forums_16.png";
 	$(id).alt="Show";
  var pars = "act=showhide&show=1&id="+i;
 }
 var myAjax = new Ajax.Request(theurl,{method: 'get', parameters: pars});
}

function preview(uri){
	var leftPos = (screen.availWidth-534) / 2;
	var topPos = (screen.availHeight-420) / 2;
	window.open(uri,'','width=534,height=420,top=' + topPos + ',left=' + leftPos)
}

function showIframe(name, url){
	var layer = document.createElement('iframe');
	var holder = document.getElementById('iframe-holder');
	var title = document.getElementById('pp-title');
	layer.tabIndex = '-1';
	layer.className = name;
	holder.style.display = 'block';
	title.style.display = 'block'
	holder.appendChild(layer);
	layer.src = url;// keep this last or it won't load in <IE7
}

function hideLayer(id){
	document.getElementById(id).style.display = 'none';
	removeIframe(id);
	if(id == 'iframe-holder'){
		document.getElementById('pp-title').style.display = 'none';
	}
}
function removeIframe(pi){
	var layers = document.getElementById(pi).parentNode.getElementsByTagName('iframe');
	while(layers.length > 0){
		layers[0].parentNode.removeChild(layers[0]);
	}
}

function video_select_file(file){
	var ext=String(/\.([^.\/\\]+)$/.exec(file.value)[1]).toLowerCase();

	if(!ext||typeof video_extensions[ext]=='undefined'){
		video_show_extension_error(video_extensions_info);
			file.value='';
		return ;
	}
	return ;
}

function video_show_extension_error(video_extensions_info){
	alert('Sorry, that\'s not a video file that we support. '+'We currently only support the following extensions: '+video_extensions_info);
}
