//© 2009 Darren Dignam - Hosted free at BlocSoft.com

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
//server responds as a refresh
function stateChanged() 
{ 
  if (xmlHttp.readyState==4)
  { 
	window.location.reload();
  }
}

//the server response is shown in an alert
function stateChangedMAN() 
{ 
  if (xmlHttp.readyState==4)
  { 
	alert(xmlHttp.responseText);
	window.location.reload();
  }
}
//server responds by updating a span
function stateChangedComment() 
{ 
  if (xmlHttp.readyState==4)
  { 
	document.getElementById("newcomment").innerHTML = xmlHttp.responseText;
  }
}

//server responds by updating a div
function stateChangedAlert() 
{ 
  if (xmlHttp.readyState==4)
  { 
	alert(xmlHttp.responseText);
	//document.getElementById("result_box").innerHTML = xmlHttp.responseText;
  }
}

//server responds by updating a div
function stateChangedSearch() 
{ 
  if (xmlHttp.readyState==4)
  { 
	document.getElementById("result_box").innerHTML = xmlHttp.responseText;
	//document.getElementById("result_box").style.border="2px solid #000000";
  }
}
//server researches
function stateChangedReSearch() 
{ 
  if (xmlHttp.readyState==4)
  { 
	bbsearch(document.getElementById("s").value);
  }
}

///////////////////////////////////////////////////////////////////////////
//checks form element is not empty
function validateUsername() {
    var error = "";
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (document.newaccount.username.value.length == 0) {
        error = "Please enter your username.\n"
        document.newaccount.username.style.background = '#FFFF99' ; 
	} else if ( document.newaccount.username.value.match(illegalChars) ) {
        error = "Username contains illegal characters.\n"
		document.newaccount.username.style.background = '#FFFF99' ;
    } else {
		document.newaccount.username.style.background = 'White' ;
    }
    return error;  
}

//checks form element is not empty & valid email address
function validateAccountEmail() {
    var error="";
    var tfld = trim(document.newaccount.email.value);    // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (document.newaccount.email.value == "") {
		document.newaccount.email.style.background = '#FFFF99' ;
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
		document.newaccount.email.style.background = '#FFFF99' ;
        error = "Please enter a valid email address.\n";
    } else if ( document.newaccount.email.value.match(illegalChars) ) {
		document.newaccount.email.style.background = '#FFFF99' ;
        error = "The email address contains illegal characters.\n";
    } else {
		document.newaccount.email.style.background = 'White' ;
    }
    return error;
}

//checks the passwords match
function validateAccountPassword() {
    var error = "";
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (document.newaccount.password1.value.length == 0) {
        error = "Please enter a password.\n"
        document.newaccount.password1.style.background = '#FFFF99' ; 
	} else if (document.newaccount.password2.value.length == 0) {
        error = "Please re-enter password.\n"
        document.newaccount.password2.style.background = '#FFFF99' ; 		
	} else if (document.newaccount.password1.value != document.newaccount.password2.value) {
        error = "Passwords do not match.\n"
		document.newaccount.password1.style.background = '#FFFF99' ;
        document.newaccount.password2.style.background = '#FFFF99' ; 		
	} else if ( document.newaccount.password1.value.match(illegalChars) ) {
        error = "Password contains illegal characters.\n"
		document.newaccount.password1.style.background = '#FFFF99' ;
        document.newaccount.password2.style.background = '#FFFF99' ; 
	} else if ( document.newaccount.password1.value.length < 6 ) {
        error = "Password must be 6 characters or more.\n"
		document.newaccount.password1.style.background = '#FFFF99' ;
        document.newaccount.password2.style.background = '#FFFF99' ; 		
    } else {
		document.newaccount.password1.style.background = 'White' ;
		document.newaccount.password2.style.background = 'White' ;
    }
    return error;  
}

//checks form element is not empty
function validateTitle() {
    var error = "";

    if (document.newmovie.mtitle.value.length == 0) {
        error = "Please enter a Title.\n"
        document.newmovie.mtitle.style.background = '#FFFF99' ; 
    } else {
		document.newmovie.mtitle.style.background = 'White' ;
    }
    return error;  
}

//checks form element is not empty
function validateYoutube() {
    var error = "";

    if (document.getElementById("s").value.length == 0) {
        error = "Please enter a Youtube URL, or embed code.\n"
	}
    return error;  
}

//checks form element is not empty
function validateName() {
    var error = "";

    if (document.commentform.t_name.value.length == 0) {
        error = "Please enter your name.\n"
        document.commentform.t_name.style.background = '#FFFF99' ; 
    } else {
		document.commentform.t_name.style.background = 'White' ;
    }
    return error;  
}

//checks form element is not empty
function validateSubject() {
    var error = "";
 
    if (document.commentform.t_subject.value.length == 0) {
        error = "The subject field cannot be empty.\n"
		document.commentform.t_subject.style.background = '#FFFF99' ;
    } else {
		document.commentform.t_subject.style.background = 'White' ;
    }
    return error;  
}
//replace /n w br
function swapBR(s)
{
	return s.replace(/\n/g,"[br/]").replace(/\r/g,"");
}
//replace /n w br
function swapBR2(s)
{
	return s.replace(/\n/g,"<br/>").replace(/\r/g,"");
}
//removes whitespace
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
//valid email??
function validateEmail() {
    var error="";
    var tfld = trim(document.commentform.t_email.value);    // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (document.commentform.t_email.value == "") {
		document.commentform.t_email.style.background = '#FFFF99' ;
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
		document.commentform.t_email.style.background = '#FFFF99' ;
        error = "Please enter a valid email address.\n";
    } else if (document.commentform.t_email.value.match(illegalChars)) {
		document.commentform.t_email.style.background = '#FFFF99' ;
        error = "The email address contains illegal characters.\n";
    } else {
		document.commentform.t_email.style.background = 'White' ;
    }
    return error;
}
////////////////////////////////////////////////////////////////////////////////////////////////

// Rate a movie Movie
function rate(uID,mID,rating)
{
	if (uID != "0")
	{
		var url="rateMovie.asp?mID=" + mID + "&r=" + rating;
    	xmlHttp=GetXmlHttpObject();
    	if (xmlHttp==null)
    	{
       		alert ("Your browser does not support AJAX!");
    	   return;
   		} 
		xmlHttp.onreadystatechange=stateChangedMAN; 
		xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);	
	} else {
		alert ("You need to LOGIN before you can rate!");	
	}
}

//tests the validity of a newMovie form
function MOVIEvalid()
{
var reason = "";

  reason += validateTitle();
  reason += validateYoutube();
  
    if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  } else {
	  
	tmpStr = document.newmovie.mdescription.value;
	tmpStr = swapBR(tmpStr);	
    var url="newMovie.asp"
	var params= "id="+ document.newmovie.userID.value +"&t="+ document.newmovie.mtitle.value + "&y=" + document.getElementById("s").value + "&b=" + tmpStr;
	
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	//server response shown in alert
	xmlHttp.onreadystatechange=stateChangedSearch; 
	
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
  }
}

//tests the validity of a newAccount form
function ACCOUNTvalid()
{
var reason = "";

  reason += validateUsername();
  reason += validateAccountEmail();
  reason += validateAccountPassword();
  
    if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  } else {

	var url="newAccount.asp"; 
	var params = "u="+ document.newaccount.username.value + "&e=" + document.newaccount.email.value + "&p=" + document.newaccount.password1.value;
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	xmlHttp.onreadystatechange=stateChangedSearch; 

	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
  }
}

//tests the validity of a comment form
function valid()
{
var reason = "";

  reason += validateName();
  reason += validateSubject();
  reason += validateEmail();
  
    if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  } else {
	tmpStr = document.commentform.ta_comment.value;
	tmpStr = swapBR(tmpStr);
	
	var url    = "makecomment.asp";
	var params = "aID="+ document.commentform.t_articleID.value + "&n=" + document.commentform.t_name.value + "&e=" + document.commentform.t_email.value + "&s=" + escape(document.commentform.t_subject.value) + "&c=" + escape(tmpStr);
	
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	document.getElementById("newcomment").innerHTML="<h1>Working...<h1><img src='loading.gif' alt='Loading...' border='0'>"
	
	xmlHttp.onreadystatechange=stateChangedComment; 
	
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
  }
}

// Admin Manage User Account
function manageUser(action,ID)
{
	input_box=confirm("Please confirm this action:");
    if (input_box==true)
	{
		var url="manageUser.asp";
		var params = "action="+ action + "&ID=" + ID;
	    xmlHttp=GetXmlHttpObject();
	    if (xmlHttp==null)
	    {
	       alert ("Your browser does not support AJAX!");
	       return;
	    } 
		xmlHttp.onreadystatechange=stateChangedReSearch; 

		xmlHttp.open("POST",url,true);
	    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}

// Admin Delete Movie
function delMovie(aID)
{
	var url="delMovie.asp"; 
	var params = "mID=" + aID;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    }
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

// Admin Delete User Comment
function delComment(aID)
{
	var url="delComment.asp";
	var params = "cID=" + aID;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

//User Login
function login()
{
    var url = "enter.asp";
	var params = "one="+ document.f_login.one.value + "&two=" + document.f_login.two.value;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

//User Logout
function logout()
{
    var url="exit.asp";
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	xmlHttp.onreadystatechange=stateChanged; 
	xmlHttp.open("GET",url,true);
    xmlHttp.send(null);		
}

function clearsearch() {
	document.getElementById("result_box").innerHTML="";
	document.getElementById("result_box").style.border="0px";
}

function bbsearch(s_term)
{
	if (s_term.length==0)
	{ 
		document.getElementById("result_box").innerHTML="";
		// document.getElementById("result_box").style.border="0px";
		return;
	}

	if (s_term.length<2)
	{
		return;
	}
	// no cached copies:
	var url="bbsearch.asp?s_term="+ s_term +"&sid="+Math.random();
	// get a cached copy:
	//var url="bbsearch.asp?s_term="+ s_term;
	
	xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
       alert ("Your browser does not support AJAX!");
       return;
    } 
	
	if (document.getElementById("result_box").innerHTML=="")
	{
		document.getElementById("result_box").innerHTML="<h1>Searching...<h1><img src='loading.gif' alt='Loading...' border='0'>";
	}
	
	xmlHttp.onreadystatechange=stateChangedSearch; 
	xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode 

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return true
	}
	else{
		return false
	}
}
////////////////////////////////  BBD
// Admin watchMovie
function watchMovie(title, thumb, ref, type)
{
		var url="watchMovie.asp";
		var params = "title="+escape(title) + "&thumb="+escape(thumb) + "&ref="+ref + "&type="+type;
	    xmlHttp=GetXmlHttpObject();
	    if (xmlHttp==null)
	    {
	       alert ("Your browser does not support AJAX!");
	       return;
	    } 
		//xmlHttp.onreadystatechange=stateChangedAlert; 

		xmlHttp.open("POST",url,true);
	    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
}