/* VARIABLES */
var rateOptions = new Array();
var rating;

function updateActualPage(page,pageOf)
{
	if (pageOf=="comments")
	{
	document.getElementById('pageCommentHidden').value=page;
	}
	else
	{
		document.getElementById('pageEpisodeHidden').value=page;
	}
}
function processPaging(direction,numberOfpages,pagingType)
{
	var intGoToPage;
	var intNumberOfPages;

	switch (pagingType)
	{
		case 'episodes':
									
									intNumberOfPages=parseInt(numberOfpages)-1;
									intGoToPage=parseInt(document.getElementById('pageEpisodeHidden').value);

									if (direction=="foward")
									{
										if (intGoToPage<intNumberOfPages)
										{
											intGoToPage=intGoToPage+1;
											showHideDIV('Episode','show','show');
											
											if ((intGoToPage)==intNumberOfPages)
											{
												showHideDIV('Episode','show','hide');
											}
										}
										else
										{
											intGoToPage=intNumberOfPages;
										}
										showHideLnk(intGoToPage,intNumberOfPages+1,'0','1','0');
									}
									else
									{
										if (intGoToPage>=intNumberOfPages || intGoToPage>=1)
										{
											intGoToPage=intGoToPage-1;
											showHideDIV('Episode','show','show');

											if(intGoToPage==0)
											{
												showHideDIV('Episode','hide','show');
											};
										}
										else
										{
											intGoToPage=0;
											showHideDIV('Episode','hide','show');
										}
										
										showHideLnk(intGoToPage,intNumberOfPages+1,'0','1','0');
									}									
									SmartyAjax.update('episodeScroller', 'web/episodeslist.php', 'get', 'episodesPage='+intGoToPage+'&series='+document.getElementById("intIdSerie").value+'&f=');
									document.getElementById('pageEpisodeHidden').value=intGoToPage;
									return false;
		
		case 'series':
									intNumberOfPages=parseInt(numberOfpages)-1;
									intGoToPage=parseInt(document.getElementById('pageSerieHidden').value);
									if (direction=="foward")
									{
										if (intGoToPage<intNumberOfPages)
										{
											intGoToPage=intGoToPage+1;
											showHideDIV('Serie','show','show');
											
											if ((intGoToPage)==intNumberOfPages)
											{
												showHideDIV('Serie','show','hide');
											}
											
										}
										else
										{
											intGoToPage=intNumberOfPages;
										}
										showHideLnk(intGoToPage,intNumberOfPages+1,'1','0','0');
									}
									else
									{
										if (intGoToPage>=intNumberOfPages || intGoToPage>=1)
										{
											intGoToPage=intGoToPage-1;
											showHideDIV('Serie','show','show');
											
											if(intGoToPage==0)
											{
												showHideDIV('Serie','hide','show');
											};
										}
										else
										{
											intGoToPage=0;
											showHideDIV('Serie','hide','show');
										}

										showHideLnk(intGoToPage,intNumberOfPages+1,'1','0','0');
									}
									
									SmartyAjax.update('seriesScroller', 'web/serieslist.php', 'get', 'seriesPage='+intGoToPage+'&f=');
									document.getElementById('pageSerieHidden').value=intGoToPage;
									return false;
	
		case 'comments':
									intNumberOfPages=parseInt(numberOfpages)-1;
									intGoToPage=parseInt(document.getElementById('pageCommentHidden').value);

									if (direction=="foward")
									{
										if (intGoToPage<intNumberOfPages)
										{
											intGoToPage=intGoToPage+1;
											showHideDIV('Comment','show','show');
											
											if ((intGoToPage)==intNumberOfPages)
											{
												showHideDIV('Comment','show','hide');
											}
										}
										else
										{
											intGoToPage=intNumberOfPages;
										}
										showHideLnk(intGoToPage,intNumberOfPages+1,'0','0','1');
									}
									else
									{
										if (intGoToPage>=intNumberOfPages || intGoToPage>=1)
										{
											intGoToPage=intGoToPage-1;
											showHideDIV('Comment','show','show');

											if(intGoToPage==0)
											{
												showHideDIV('Comment','hide','show');
											};
										}
										else
										{
											intGoToPage=0;
											showHideDIV('Comment','hide','show');
										}
										
										showHideLnk(intGoToPage,intNumberOfPages+1,'0','0','1');
									}
									
									SmartyAjax.update('commentScroller', 'web/commentslist.php', 'get','commentsPage='+intGoToPage+'&IdEpisode='+document.getElementById("intIdEpisode").value+'&f=');
									document.getElementById('pageCommentHidden').value=intGoToPage;
									return false;
	}

}
/* ---- UTILITY FUNCTIONS ---- */
function showHideLnk(n,t,channel,episode,comment)
{
	if(channel == 1)
	{
		document.getElementById('pageSerieHidden').value = n;
		for (i=0; i<t; i++) {
		if (i==n) {
			
				document.getElementById('lnkPage'+i).className = 'current';
				document.getElementById('lnkPage'+i).title = 'current page';
				document.getElementById('lnk2Page'+i).className = 'current';
				document.getElementById('lnk2Page'+i).title = 'current page';
				document.getElementById('lnkPage'+i).onmouseover ="javascript: alert('hola');"
		   }
		else
			{
				document.getElementById('lnkPage'+i).className = '';
				document.getElementById('lnkPage'+i).title = '';
				document.getElementById('lnk2Page'+i).className = '';
				document.getElementById('lnk2Page'+i).title = '';
				}
		}
		//alert(document.getElementById('pageSerieHidden').value);
	}
	if(episode == 1)
	{
		for (i=0; i<t; i++) 
		{
			if (i==n)
			{
				document.getElementById('lnkEp'+i).className = 'current';
				document.getElementById('lnkEp'+i).title = 'current page';
		  }
			else
			{
				document.getElementById('lnkEp'+i).className = '';
				document.getElementById('lnkEp'+i).title = '';
			}
		}
	}
	if(comment == 1)
	{
		for (i=0; i<t; i++) 
		{
			if (i==n)
			{
				document.getElementById('lnkComm'+i).className = 'current';
				document.getElementById('lnkComm'+i).title = 'current page';
		  }
			else
			{
				document.getElementById('lnkComm'+i).className = '';
				document.getElementById('lnkComm'+i).title = '';
			}
		}
	}
	
}
function showHideDIV(strType,strFirst,strLast)
{
	if (strFirst=="hide")
	{
		document.getElementById('firstPage'+strType).style.visibility = 'hidden';
	
		if (strType=="Serie")
		{
			document.getElementById('firstPage'+strType+'2').style.visibility = 'hidden';
		}
	}
	else
	{
		document.getElementById('firstPage'+strType).style.visibility = 'visible';
	
		if (strType=="Serie")
		{
			document.getElementById('firstPage'+strType+'2').style.visibility = 'visible';
		}
	}

	if (strLast=="hide")
	{
		document.getElementById('lastPage'+strType).style.visibility = 'hidden';
		if (strType=="Serie")
		{
			document.getElementById('lastPage'+strType+'2').style.visibility = 'hidden';
		}
	}
	else
	{
		document.getElementById('lastPage'+strType).style.visibility = 'visible';
		if (strType=="Serie")
		{
			document.getElementById('lastPage'+strType+'2').style.visibility = 'visible';
		}	
	}
}
function changeSerieImage(strImgSerieID,imgON,imgOFF,strFrontEndTemplatesImagesPath,strONOFF)
{

	if (strONOFF=="on")
	{
		document.getElementById(strImgSerieID).src=strFrontEndTemplatesImagesPath+"series/"+imgON;
	
	}
	else
	{
			document.getElementById(strImgSerieID).src=strFrontEndTemplatesImagesPath+"series/"+imgOFF;
	}
}

function emailToFriend()
{
	var from= $("strEmailFrom");
	var to= $("strEmailTo");

	if ( emailCheck(from.value) == true) 
	{
		if( emailCheck(to.value) == true)
		{
			
			var from2= $("strFrom");
			var to2= $("strTo");

			from2.value =  from.value;
			to2.value =  to.value;

			from.value = '';
			to.value = '';
			
			SmartyAjax.submit($("sendToFriend_Form"), '')
			return true;
			
		}
		else
		{
			return false;
		}
	}	
	else
	{
		return false;
		
	}
}
function emailCheck (emailStr)
	{

		/* The following variable tells the rest of the function whether or not
		to verify that the address ends in a two-letter country or well-known
		TLD.  1 means check it, 0 means don't. */

		var checkTLD=1;

		/* The following is the list of known TLDs that an e-mail address must end with. */

		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

		/* The following pattern is used to check if the entered e-mail address
		fits the user@domain format.  It also is used to separate the username
		from the domain. */

		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);

		if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		alert("Ths username contains invalid characters.");
		return false;
		   }
		}
		for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		alert("Ths domain name contains invalid characters.");
		return false;
		   }
		}


		if (user.match(userPat)==null) {

		alert("The username doesn't seem to be valid.");
		return false;
		}

		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {

		// this is an IP address

		for (var i=1;i<=4;i++) {
		if (IPArray[i]>255) {
		alert("Destination IP address is invalid!");
		return false;
		   }
		}
		return true;
		}


		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
		alert("The domain name does not seem to be valid.");
		return false;
		   }
		}


		if (checkTLD && domArr[domArr.length-1].length!=2 &&
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
		}

		if (len<2) {
		alert("This address is missing a hostname!");
		return false;
		}

		return true;
	}

function updateRating() {
	if (rating != undefined) {
		var vot= $("intVote");
		vot.value =  rating;
		SmartyAjax.submit($("rateItForm"), '');
		return true;
		}
	else {
		alert("Please choose a rating");
		return false;
		}
	}
	
/* addLoadEvent() by Simon Willison  http://simon.incutio.com/  */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
	oldonload();
      }
      func();
    }
  }
}

/* getElementsByClass() by Dustin Diaz  http://www.dustindiaz.com/getelementsbyclass/ */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* RATING FORM FUNCTIONS */
function setRating(ind) {
	rating = ind+1;

	for (var i = 0; i < rateOptions.length; i++) {
		if (i <= ind) 
			rateOptions[i].src = "webtemplates/images/rating/on.png";
		else
			rateOptions[i].src = "webtemplates/images/rating/off.png";
		}
	}


/* SHOW AND HIDE FUNCTIONS */
function showDesc() {
	$("topDesc").className = "topBrowser";
	$("rateForm").className = "topBrowser hidden";
	$("rateConf").className = "topBrowser hidden";
	$("sendForm").className = "topBrowser hidden";
	$("sendConf").className = "topBrowser hidden";

	$("descItem").className = "on";
	$("rateItem").className = "";
	$("sendItem").className = "";
	}

function showRateForm() {
	$("topDesc").className = "topBrowser hidden";
	$("rateForm").className = "topBrowser";
	$("rateConf").className = "topBrowser hidden";
	$("sendForm").className = "topBrowser hidden";
	$("sendConf").className = "topBrowser hidden";

	$("descItem").className = "";
	$("rateItem").className = "on";
	$("sendItem").className = "";
	}
	
function showRateConf() {
	$("topDesc").className = "topBrowser hidden";
	$("rateForm").className = "topBrowser hidden";
	$("rateConf").className = "topBrowser";
	$("sendForm").className = "topBrowser hidden";
	$("sendConf").className = "topBrowser hidden";
	
	$("descItem").className = "";
	$("rateItem").className = "on";
	$("sendItem").className = "";
	
	//Once you've rated an epsiode you can't rate it again
	$("rateLink").onclick = function() {
		showRateConf(); return false;
		};
	}
	
function showSendForm() {
	$("topDesc").className = "topBrowser hidden";
	$("rateForm").className = "topBrowser hidden";
	$("rateConf").className = "topBrowser hidden";
	$("sendForm").className = "topBrowser";
	$("sendConf").className = "topBrowser hidden";

	$("descItem").className = "";
	$("rateItem").className = "";
	$("sendItem").className = "on";
	}
	
function showSendConf() {
	$("topDesc").className = "topBrowser hidden";
	$("rateForm").className = "topBrowser hidden";
	$("rateConf").className = "topBrowser hidden";
	$("sendForm").className = "topBrowser hidden";
	$("sendConf").className = "topBrowser";

	$("descItem").className = "";
	$("rateItem").className = "";
	$("sendItem").className = "on";
	}

function showCommentList() {
	$("commentListTab").className = "tabOn";
	$("commentPager").className = "pageControl";
	$("commentFormTab").className = "currentSeries";

	$("commentScroller").className = "";
	$("commentForm").className = "hidden";
	$("commentFormConf").className = "hidden";
	}
	
function showCommentForm() {
	$("commentListTab").className = "currentSeries";
	$("commentPager").className = "pageControl hidden";
	$("commentFormTab").className = "tabOn";
	
	$("commentScroller").className = "hidden";
	$("commentForm").className = "";
	$("commentFormConf").className = "hidden";
	}
	
function showCommentConf() {
	$("commentListTab").className = "currentSeries";
	$("commentPager").className = "pageControl hidden";
	$("commentFormTab").className = "tabOn";

	$("commentScroller").className = "hidden";
	$("commentForm").className = "hidden";
	$("commentFormConf").className = "";
	}

var cnt = 0;	
/* ADD HANDLERS ON window.load */
addLoadEvent(function() {
	if (document.getElementById) {

		rateOptions = [$("rate1"),
			$("rate2"),
			$("rate3"),
			$("rate4"),
			$("rate5")];
		$("rate1").onclick = function() {
			setRating(0); return false;
		};
		$("rate2").onclick = function() {
			setRating(1); return false;
		};
		$("rate3").onclick = function() {
			setRating(2); return false;
		};
		$("rate4").onclick = function() {
			setRating(3); return false;
		};
		$("rate5").onclick = function() {
			setRating(4); return false;
		};
	
		$("descLink").onclick = function() {
			showDesc(); return false;
		};
		$("rateLink").onclick = function() {
			showRateForm(); return false;
		};
		$("rateSubmit").onclick = function() {
			if (updateRating()) {   //if validation passed
				showRateConf(); 
				}
			return false;
		};
		$("sendLink").onclick = function() {
			showSendForm(); return false;
		};
		$("sendSubmit").onclick = function() {
			//add AJAX processing
			if (emailToFriend()) {   //if validation passed
				showSendConf();
				$("strEmailTo").value = "";  //reset friend's email field
				}
			return false;
		};
		$("sendAgain").onclick = function() {
			showSendForm(); return false;
		};
		$("commentLink").onclick = function() {
			showCommentList(); return false;
		};
		$("commentListReturn").onclick = function() {
			showCommentList(); return false;
		};
		$("commentFormLink").onclick = function() {
			showCommentForm(); return false;
		};
		$("commentAgain").onclick = function() {
			showCommentForm(); return false;
		};
		$("form_comments").onsubmit = function() {
			//reset validation divs to hidden
			if(this.commentName.value=="")
			{
			 $("commentNameVal").className = '';
			 return false;
			}
			else
			{
				if(this.commentText.value=="")
				{
					$("commentNameVal").className = 'hidden';
					$("commentComVal").className = '';
					return false;
				}
				else
				{ 
			 		$("commentNameVal").className = 'hidden';
					$("commentComVal").className = 'hidden';
					SmartyAjax.submit(this, '', showCommentConf());
					this.commentText.value="";
					return false;
				}
			}
		};
		$("favLink").onclick = function() {
			bookIt('http://www.mondominishows.com/|Mondo Mini Shows'); return false;
		};
		if($("favLinkMast")) {
			$("favLinkMast").onclick = function() {
			bookIt('http://www.mondominishows.com/|Mondo Mini Shows'); return false;
			};
		}
		//create 600x345 popup links
		var popups = document.getElementsByClassName("popLink");
		for (var p=0; p < popups.length; p++) {
			var theHref = popups[p].href;
			popups[p].onclick = new Function("window.open('"+theHref+"', 'popped', 'width=600,height=345,menubar=no,toolbar=no,scrollbars=yes');return false;");
		}
		//create full window popup links
		if (document.getElementsByTagName) {
			var anchors = document.getElementsByTagName("a");
			for (var i=0; i<anchors.length; i++) {
				var anchor = anchors[i];
				if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
					anchor.target = "_blank";
			}
 		}
 		//thumbnail image not found handlers
		var thumbs = document.getElementsByClassName("thumb",$("episodeScroller"),"img");
		for (var i=0; i < thumbs.length; i++) {
			var thumb = thumbs[i];
			thumb.onerror = new Function("this.onerror=null;this.title='No thumbnail available';this.alt='No thumbnail available';this.src='webtemplates/images/thumbs/thumb_generic.gif';");
		}
	}
});

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}