// JavaScript Document
var xmlHttp;
var rating = 0;
var total_rating =0;
var count_rating =0;
var yellow_star = '';
var white_srar = '';
function createXMLHttpRequest()
{
	
	 if(window.ActiveXObject)
	 {
	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  
	 }
	 else if(window.XMLHttpRequest)
	 {
	  xmlHttp = new XMLHttpRequest();
	 }
}
function check_login()
{
	var username = document.getElementById('username');
	var password = document.getElementById('password');
	var bad = 0;
	var user_value = document.user_login.elements[0].value;//get user name
	var pass_value = document.user_login.elements[1].value;//get password
	if(user_value =='')
	{
		username.innerHTML = "Username Incorrect";
		username.style.color = 'red';
		bad =1;
		return false;
	}
	if(pass_value =='')
	{
		password.innerHTML = "Password Incorrect";
		password.style.color = 'red';
		bad = 1;
		return false;
	}
}
function confirmReset()
{
	
	if(window.confirm('Click Enter To Reset All'))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function check_search_request()//check search char length
{
	var search_request = document.search_result.elements[0].value;
	/*if(search_request =='')
	{
		return false;
	}*/
	if(search_request.length < 4)
	{
		var ss = document.getElementById('search_tips')
		ss.style.color = 'red';
		ss.style.display = 'block';
		ss.innerHTML = 'Keyword No Less Than 4 Characters !';
		return false;
	}
}

function checkRegisterField()
{
	for(var i=0;i<5;i++)
	{
		if(document.register.elements[i].value==0)
		{
			alert('Please Fill In All Filed');
			return false;
		}
	}
}
var checkFiled = '';
function checkFormFiled(filed)//check username and email format
{
	createXMLHttpRequest();
	var filedValue = '';
	filedValue = document.register.elements[filed].value;
	if(filedValue=='')//if input is null return flase
	{
		return false;
	}
	if(filed=='newusername')
	{
		checkFiled = 'newusername';
		var flag = checkUserNameFormat(filedValue);
		if(!flag)//username format invalid
		{
			document.getElementById(checkFiled).style.display = 'block';
			document.getElementById(checkFiled).style.color = 'red';
			document.getElementById(checkFiled).innerHTML = 'Username Format Invalid Or Less Than 5 Characters';
			document.register.elements[checkFiled].value = '';
			//document.register.elements[checkFiled].style.background = 'yellow';
			setTimeout(function (){document.register.elements[checkFiled].focus();},500);
			return false;
		}
	}
	else if(filed=='email')
	{
		checkFiled = 'email';
		var flag = checkEmailFormat(filedValue);
		if(!flag)//email format invalid
		{
			document.getElementById(checkFiled).style.display = 'block';
			document.getElementById(checkFiled).style.color = 'red';
			document.getElementById(checkFiled).innerHTML = 'Email Format Invalid';
			document.register.elements[checkFiled].value = '';
			//document.register.elements[checkFiled].style.background = 'yellow';
			setTimeout(function (){document.register.elements[checkFiled].focus();},500);//set focus
			return false;
		}
	}
	var url = "/checkregister.php?filed="+filed+"&value="+filedValue;
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange = handleRegisterStateChange;
	xmlHttp.send(null);
}

function  handleRegisterStateChange()
{	
	  if(xmlHttp.readyState == 4)
	  {
		 if(xmlHttp.status == 200)
		   {
			 	var response = '';
	 	  		response = xmlHttp.responseText;
				var showTips = document.getElementById(checkFiled);
				showTips.style.display = 'block';
				if(response=='good')
				{
					showTips.style.color = '#88D9F3';
					if(checkFiled=='newusername')
					{
						showTips.innerHTML = 'This User Valid';
					}
					else
					{
						showTips.innerHTML = 'This E-mail Valid';
					}
				}
				else
				{
					showTips.style.color = 'red';
					if(checkFiled=='newusername')
					{
						showTips.innerHTML = 'This User Already Exist';
						document.register.elements[checkFiled].value = '';
						document.register.elements[checkFiled].focus();
					}
					else
					{
						showTips.innerHTML = 'This E-mail Already Exist';
						document.register.elements[checkFiled].value = '';
						document.register.elements[checkFiled].focus();
					}	
				}
		   }
		   
	  }
	 
}

function checkUserNameFormat(str)
{
	var objReg = new RegExp("^[a-zA-Z0-9_]+$","gi");
	var rightFormat = objReg.test(str);
	/* ï¿½ï¿½Ö¤ï¿½Ã»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç·ï¿½ï¿½Ð·Ç·ï¿½ï¿½Ö·ï¿½ï¿½ï¿½ï¿½ï¿½ï¿?1ï¿½ï¿½Ê¾ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ */
	var errorChar = new RegExp("[^a-zA-Z0-9_]","gi");
	var rightChar = (str.search(errorChar)==-1);
	var namelength = (str.length>4);
	return (rightFormat && rightChar && namelength);
}

function checkEmailFormat(strMail)
{
	/* ï¿½ï¿½Ö¤ï¿½Ê¼ï¿½ï¿½ï¿½Ö·ï¿½Ä¸ï¿½Ê½ï¿½Ç·ï¿½Ï·ï¿½ï¿½ï¿½ï¿½ï¿? abc@def.com/netï¿½ï¿½ */
	var objReg = new RegExp("[a-z0-9_]+@[a-z0-9]*[\.]?[a-z0-9]+[\.]{1}(com|net|bta|cn|org|edu|mil|us|uk|tw|jp|hk|eu|cc|info|asia)","gi");
	var IsRightFmt = objReg.test(strMail);
	
	/* ï¿½ï¿½Ö¤ï¿½Ê¼ï¿½ï¿½ï¿½Ö·ï¿½ï¿½ï¿½Ç·ï¿½ï¿½Ð·Ç·ï¿½ï¿½Ö·ï¿½ï¿½ï¿½ï¿½ï¿½ï¿?1ï¿½ï¿½Ê¾ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ */
	var objRegErrChar = new RegExp("[^a-z0-9-._@]","ig");
	var IsRightChar = (strMail.search(objRegErrChar)==-1);
	
	/* ï¿½Ê¼ï¿½ï¿½ï¿½Ö·ï¿½ï¿½ó³¤¶ï¿½Î?0ï¿½Ö·ï¿½ */
	var IsRightLength = strMail.length <= 60;
	
	/* ï¿½Ð¶ï¿½ï¿½Ö·ï¿½'@'ï¿½ï¿½'.'ï¿½ï¿½Î»ï¿½Ã£ï¿½ï¿½ï¿½ï¿½ï¿½Îªï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ */
	var IsRightPos = (strMail.indexOf("@",0) != 0 && strMail.indexOf(".",0) != 0 && strMail.lastIndexOf("@")+1 != strMail.length && strMail.lastIndexOf(".")+1 != strMail.length);
	
	/* ï¿½Ð¶ï¿½ï¿½Ö·ï¿½'@'ï¿½Ç·ï¿½Î¨Ò» */
	var IsNoDupChar = (strMail.indexOf("@",0) == strMail.lastIndexOf("@"));
	
	/* ï¿½ï¿½'ï¿½ï¿½Ê½'ï¿½ï¿½'ï¿½Ö·ï¿½'ï¿½ï¿½'ï¿½ï¿½ï¿½ï¿½'ï¿½ï¿½'@Î¨Ò»'ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ë¡±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ã¶¼ï¿½ï¿½ï¿½ï¿½ï¿½Ø¼ï¿?*/
	return (IsRightFmt && IsRightChar && IsRightLength && IsRightPos && IsNoDupChar) ;
}

function checkPassword()
{
	var pass = document.register.elements['newpassword'].value;
	var confirmpass = document.register.elements['confirmPassword'].value;
	if(pass.length<6)//check passowrd length
	{
		document.getElementById('newpassword').innerHTML = 'Password Length No Less Than Six Characters';
		document.getElementById('confirmPassword').style.display = 'block';
		document.getElementById('newpassword').style.color = 'red';
		document.register.elements['newpassword'].focus();
		return false;
	}
	else if(pass != confirmpass)//confirm password
	{
		document.getElementById('confirmPassword').innerHTML = 'Password Confirm Error,Please Check Again';
		document.getElementById('confirmPassword').style.color = 'red';
		document.getElementById('confirmPassword').style.display = 'block';
		document.register.elements['confirmPassword'].value = '';
		document.register.elements['newpassword'].focus();
		//setTimeout(function (){document.register.elements['newpassword'].focus();},500);
		return false;
	}
	else
	{
		document.getElementById('confirmPassword').innerHTML = '';
		document.getElementById('newpassword').innerHTML = '';
		document.getElementById('confirmPassword').style.display = 'none';
		document.getElementById('confirmPassword').style.display = 'none';
		return true;
	}
}

function checkEmail()
{
	var email = document.register.elements['email'].value;
	var confirmemail = document.register.elements['confirmemail'].value;
	if(email != confirmemail)
	{
		document.getElementById('confirmemail').innerHTML = 'E-mail Confirm Error,Please Check Again';
		document.getElementById('confirmemail').style.color = 'red';
		document.getElementById('confirmemail').style.display = 'block';
		document.register.elements['confirmemail'].value = '';
		document.register.elements['email'].focus();
		return false;
	}
	else
	{
		document.getElementById('confirmemail').innerHTML = '';
		document.getElementById('confirmemail').style.display = 'none';
		return true;
	}
}

var divName = '';
var score = 0;
function appraise(videoid,flag,divKey,cur_score)
{
	createXMLHttpRequest();
	divName =divKey;//need to show appraise div name
	switch (flag)
	{
		case 'work':
			score = cur_score+1;
		break;
		case 'bad':
			score = cur_score-1;
		break;
	}
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf(videoid+"=1");//find the moviename in cookies
	if(cookieflag ==-1)//if cookie not exist allow to rating
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie =videoid+"=1;expires="+availdate.toGMTString()+";path=/";
		var url = "/report.php?videoid="+videoid+'&flag='+flag;
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = reportHandle;
		xmlHttp.send(null);
	}
}

function reportHandle()
{
	if(xmlHttp.readyState == 4)
 	{
		 if(xmlHttp.status == 200 || xmlHttp.status ==0)
		 {
			 if(score<0)
			 {
				 var boxId = document.getElementById(divName);
				 boxId.innerHTML= score;
				  boxId.style.color = 'red';
			 }
			 else
			 {
				 var boxId = document.getElementById(divName);
				 boxId.innerHTML= '+'+score;
				 boxId.style.color = '#3399FF';
			 }
			 divName = '';
			 score = 0;
		 }
 	}
}

/*function showAppraise(count,idName)
{
	if(count<50)
	{
		count = 100-count;
		 var boxId = document.getElementById(idName);
		 boxId.innerHTML= count+'% said bad';
		 boxId.style.color = 'red';
	}
	else
	{
		 var boxId = document.getElementById(idName);
		 boxId.innerHTML= count+'% said good';
		 boxId.style.color = '#3399FF';
	}
}*/

//count = now rating, videoid = video name, total = total rating, rating_count = total votes,yellow_star=yellow star url
//user and title rating with user playlist
var total_rating=0;
var count_rating=0;
var yellow_star='';
function submitrating(count,videoid,total,rating_count,star_yellow,star_white,user,title,cat)
{	
	createXMLHttpRequest();
	rating = count;
	total_rating = total+count;
	count_rating = rating_count+1;
	yellow_star = star_yellow;
	white_star = star_white;
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf("="+videoid);//find the moviename in cookies
	if(cookieflag ==-1)//if cookie not exist allow to rating
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie =videoid+"="+videoid+";expires="+availdate.toGMTString()+";path=/";
		//var url = '/rate/'+total_rating+'/'+count_rating+'/'+videoid+'/';
		if(user != '' && title !='')
		{
			var url='/rate.php?rate='+total_rating+'&vote='+count_rating+'&videoid='+videoid+'&user='+user+'&title='+title;
		}
		else
		{
			var url='/rate.php?rate='+total_rating+'&vote='+count_rating+'&videoid='+videoid+'&cat='+cat;
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = handleStateChangeRat;
		xmlHttp.send(null);
	}
}
var img_src = new Array();//save the default image url to array for restore
function  handleStateChangeRat()
{
	  if(xmlHttp.readyState == 4)
	  {
		  if(xmlHttp.status == 200 || xmlHttp.status ==0)
		   {
			   	var imageaver = total_rating/count_rating;
				var aver = imageaver*2;
				if(aver != parseInt(aver))
				{
					aver = aver.toString();
					aver = aver.substr(0,3);
				}
				var imageaver = parseInt(imageaver);
				
				var rating_obj = document.getElementById('currnet_rate');
				rating_obj.innerHTML = 'current rating: '+aver+'/10 ('+count_rating+' votes)';
				document.getElementById('rate_pec').innerHTML = (aver*10)+'%';
				var hover_image = document.getElementById("hover_image");
			    var child = hover_image.getElementsByTagName("img");
			    var star_pec = document.getElementById("star_pec");
			    var small_star = star_pec.getElementsByTagName("img");
			    for(var i=0;i<5;i++)
			    {
					if(i<imageaver) star = yellow_star;
					else star = white_star;
					child[i].src = star;
					small_star[i].src = star;
					img_src[i] = star;
 			    }
			   
		   }
	  }
}


function mouseOver(key,star_hover,star_leave)
{
	var hover_image = document.getElementById("hover_image");
	var child = hover_image.getElementsByTagName("img");
	for(var i=0;i<key;i++)
	{
		img_src[i] = child[i].src;
		child[i].src = star_hover;
	}
	for(var i=key;i<5;i++)
	{
		img_src[i] = child[i].src;
		child[i].src = star_leave;
	}
	
}

function mouseOut(key)
{
	var hover_image = document.getElementById("hover_image");
	var child = hover_image.getElementsByTagName("img");
	for(var i=0;i<5;i++)
	{
		child[i].src = img_src[i];
	}
}


function checkComment()
{
	if(document.formComment.elements['comment_title'].value =='')
	{
		alert('Comment Title is Null');
		return false;
	}
	var text = document.formComment.elements['comment_text'];
	if( text.value=='')
	{
		alert('Comment  is Null');
		return false;
	}
	else
	{alert('Your comment has been submitted.Comments are moderated, and will not appear until the author has approved them.');
	return true};
}

function postReportBad(divname,comment_id)
{
	createXMLHttpRequest();
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf("com="+comment_id);//find the comment id in cookies
	if(cookieflag ==-1)//if cookie not exist allow to report
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie =comment_id+"com="+comment_id+";expires="+availdate.toGMTString()+";path=/";
		var report_bad = document.getElementById(divname);
		report_bad.innerHTML = 'Thank You';
		report_bad.style.color = '#FF6600';
		var url = "/report_comment.php?id="+comment_id;
		xmlHttp.open("GET",url,true);
		//xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.send(null);
	}
}

function subPostReportBad(divname,comment_id,forum_cat)
{
	createXMLHttpRequest();
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf("com="+comment_id);//find the comment id in cookies
	if(cookieflag ==-1)//if cookie not exist allow to report
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie =comment_id+"com="+comment_id+";expires="+availdate.toGMTString()+";path=/";
		var report_bad = document.getElementById(divname);
		report_bad.innerHTML = 'Thank You';
		report_bad.style.color = '#FF6600';
		var url = "/report_comment.php?id="+comment_id+'&cat='+forum_cat;
		xmlHttp.open("GET",url,true);
		//xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.send(null);
	}
}

function checkForm(contact)
{
	alert(document.form[contact].name);
	return false;
	if(document.form[contact].name == '' || document.form[contact].message == '')
	{
		return false;
	}
	
}

function checkComment_admin(nameKey,flag,id,user,comment,com_cat)
{
	createXMLHttpRequest();
	switch(flag)
	{
		case 'd':
			document.getElementById(nameKey).style.color='red';
		break;
		case 'u':
			document.getElementById(nameKey).style.color='#0099FF';
		break;
		case 'b':
			document.getElementById(nameKey).style.color='red';
		break;
	}
	if(flag=='b')
	{
			if(confirm('Click Enter To Block User And Delete All Comments About This User!'))
			{
				var url = 'approve_comment.php?flag='+flag+'&id='+id+'&user='+user+'&t='+comment+'&ct='+com_cat;
				xmlHttp.open("GET",url,true);
				//xmlHttp.onreadystatechange = commentStateChange;
				xmlHttp.send(null);
			}
	}
	else
	{
		var url = 'approve_comment.php?flag='+flag+'&id='+id+'&user='+user+'&t='+comment+'&ct='+com_cat;
		xmlHttp.open("GET",url,true);
		//xmlHttp.onreadystatechange = commentStateChange;
		xmlHttp.send(null);
	}
}

function reportWork(id,appraise)
{
	createXMLHttpRequest();
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf("flag="+id);//find the comment id in cookies
	if(cookieflag ==-1)//if cookie not exist allow to report
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie =id+"flag="+id+";expires="+availdate.toGMTString()+";path=/";
		var flag = document.getElementById('flag'+id);
		flag.innerHTML = 'Thank You !';
		var url = "/report.php?flagid="+id+'&app='+appraise;
		xmlHttp.open("GET",url,true);
		//xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.send(null);
	}
}


function freply(formId)
{
	var form = document.getElementById(formId);
	if(form.style.display=='none')
	{
		form.style.display='block';
	}
	else
	{
		form.style.display='none';
	}
}

function checkQuickReply(commentArea,quickArea,user)
{
	if(document.getElementById(commentArea).value=='')
	{
		alert('Please Type Some Words!');
		return false;
	}
	if(user=='')
	{
		alert('Please Login For Post Comments !');
		return false;
	}
	alert('Your comment has been submitted.Comments are moderated, and will not appear until the author has approved them.');
	document.getElementById(commentArea).value='';
	document.getElementById(quickArea).style.display='none';
	return false;
}

function quickReportBad(divname,comment_id)
{
	createXMLHttpRequest();
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf("quick="+comment_id);//find the comment id in cookies
	if(cookieflag ==-1)//if cookie not exist allow to report
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie =comment_id+"quick="+comment_id+";expires="+availdate.toGMTString()+";path=/";
		var report_bad = document.getElementById(divname);
		report_bad.innerHTML = 'Thank You';
		report_bad.style.color = '#FF6600';
		var url = "/quick_report_comment.php?id="+comment_id;
		xmlHttp.open("GET",url,true);
		//xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.send(null);
	}
}

function appriseHq(hq_id,hq_flag,hq_divkey,dvd_count,cam_count)
{
	createXMLHttpRequest();
	var cookieflag = '';
	var cookies = document.cookie;//get the cookie contents
	cookieflag = cookies.indexOf("hq="+hq_id);//find the link id in cookies
	if(cookieflag ==-1)
	{
		var availdate = new Date();
		availdate.setDate(availdate.getDate()+parseInt(1));//set the cookie availtime
		document.cookie ='hq'+hq_id+"=hq"+hq_id+";expires="+availdate.toGMTString()+";path=/";
		switch (hq_flag)
		{
			case 'dvd':
				var report_dvd = document.getElementById('hq_dvd'+hq_divkey);
				report_dvd.innerHTML = parseInt(dvd_count+1);
				var report_cam = document.getElementById('hq_cam'+hq_divkey);
				var kkk = 0;
				kkk = parseInt(cam_count-1);
				if(kkk<0)
				{kkk=0;}
				report_cam.innerHTML = kkk;
			break;
			case 'cam':
				var report_dvd = document.getElementById('hq_dvd'+hq_divkey);
				var jjj = 0;
				jjj = parseInt(dvd_count-1);
				if(jjj<0)
				{jjj=0;}
				report_dvd.innerHTML = jjj;
				var report_cam = document.getElementById('hq_cam'+hq_divkey);
				report_cam.innerHTML = parseInt(cam_count+1);
			break;
		}
		var url = "/report_hq.php?id="+hq_id+'&flag='+hq_flag;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
function ckV(ev)
{
	if(!checkEmailFormat(ev))
	{
		document.getElementById("email_error").style.display="";
		return false;
	}
	else
	{
		document.getElementById("email_error").style.display="none";
		return true;
	}
	
}
var chkflag=0;
function chkall()
{
	var trailer_len = document.trailer_form.length;
	if(chkflag==0)
	{
		for(i=0;i<trailer_len;i++)
		{
			document.trailer_form[i].checked='true';
		}
		chkflag=1;
	}
	else
	{
		
		for(i=0;i<trailer_len;i++)
		{
			document.trailer_form[i].checked='';
		}
		chkflag=0;
	}
}
function chk_trail()
{
	var t_len = document.trailer_form.length;
	var t_email = document.getElementById('email').value;
	var ff=0;
	if (checkEmailFormat(t_email)) 
	{
		for (i = 0; i < t_len; i++)
		 {
			if (document.trailer_form[i].checked)
			 {
			 	ff=1;
				return true;
			}
		}
		if(ff==0)
		{
			alert('Please Select a Trailer');
		}
	}
	else
	{
		alert('Email Format Invalid')
	}
	return false;
}

function qT(w){
    var e = document.getElementById("comment_text");
    var s = document.selection;
    e.focus();
    if (s) {
        var r = s.createRange();
        var d = r.duplicate();
        var t = "[" + w + "]" + d.text + "[/" + w + "]";
    }
    else {
        var ss = e.selectionStart;
        var se = e.selectionEnd;
        var t = e.value.substring(0, ss) + "[" + w + "]" + e.value.substring(ss, se) + "[/" + w + "]" + e.value.substring(se);
    }
    t = t.replace(/( )+\[\/(i|b|u|quote)\]/img, "[/$2]$1");
    if (d) 
        d.text = t;
    else {
        e.value = t;
        e.selectionEnd = e.selectionStart = ss;
    }
    return false
}
function switchDisplay(tag)
{
	var tagSta = document.getElementById(tag);
	if(tagSta.style.display=='none')
	{
		tagSta.style.display='block';
	}
	else
	{
		tagSta.style.display='none';
	}
}
function stopflvplay()
{
		document.getElementById('flash_list').style.display='block';
}