﻿/*Sky Sports Forum behaviours */

var hiddenSF

// Execute on DOMLoad //

function bodyLoad(){
  setStyle();
  forumVisual();	
  //setAvatar();
  if (document.getElementById('home')){
		  $('.dbx-box h3 div').addClass('handleMinus');
		  reOrder();
		  forumSetView();
	  }
	  if($('#noise')){ 
	    widgInit();
	  };
	  //optOrder();
  	
  }

function setAvatar(){
	if(readCookie('avatar')){
		readAvatar=readCookie('avatar')	
	}
	
	if(document.getElementById('imageBox') && readCookie('avatar')){				
document.getElementById('imageBox').innerHTML="<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='60' height='65' id='avtar_chosen' name='avtar_chosen' align='middle'><param name='allowScriptAccess' value='sameDomain' /><param name='movie' value='avtar_chosen.swf?"+readAvatar+"' /><param name='wmode' value='transparent'><param name='menu' value='false'><embed src='avtar_chosen.swf?"+readAvatar+"' width='60' height='65' align='middle' name='avtar_chosen' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' wmode='transparent' menu='false' /></object>"							   
	}
}

function forumVisual(){

//set styles and behaviours onload
	$('.styles').html("<strong>set stylesheet: </strong><a href='#' onclick=styleSheet('Default') title='Default styles'>Default styles</a> | <a href='#' onclick=styleSheet('high-contrast') title='High contrast view'>High contrast view</a>");
	$(".forumList tr:odd").addClass('zebra');
	$(".postWrap:odd").addClass('zebra');
  if(getQuerystring('mod')== 'yup'){
    $(".hiddenButton").show();
  }
	$(".forumButtons img").hover(function(){
  currentSrc=this.src
  this.src=this.src.split('.gif')[0]+"_on.gif"
},function(){
 this.src=currentSrc
 
});

$("#jumpto").bind('change',function(){if (this.value.charAt(0)!='0') {document.location.href = this.value}});

if(is_saf || is_opera){
	$('div.dbx-box h3 div').css('margin-top','0px')
	}
	
// set events for show hide forums//	
}

function forumSetView(){
// find forum visible status from cookie//
	hSFstatus = readCookie('hiddenSF')
		if(hSFstatus) {
			var j = hSFstatus.split('|')
			$('.dbx-content').each(function(i){
				if (j[i] && document.getElementById(j[i])) {
					document.getElementById(j[i]).style.display = "none";
					document.getElementById(j[i]).parentNode.getElementsByTagName('h3')[0].getElementsByTagName('div')[0].className="handlePlus"
				}
			});
		//set option box order//
		
		}	

// set events for show hide forums//	

	$('.dbx-box h3').bind("click",function(){

		$(this).parent().find('.dbx-content').toggle();
		$(this).parent().find('.dbx-content:hidden').parent().find('h3 div').removeClass('handleMinus').addClass('handlePlus')
		$(this).parent().find('.dbx-content:visible').parent().find('h3 div').addClass('handlePlus').addClass('handleMinus')
		hiddenSF = "";
			$('.dbx-content:hidden').each(function(i){
  				hiddenSF += this.getAttribute('id') + "|";
			});
		createCookie("hiddenSF", hiddenSF, 365);
	})	
	
}	

//browser detection//

var agt=navigator.userAgent.toLowerCase();
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_aol   = (agt.indexOf("aol") != -1);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1) && (agt.indexOf('firefox')==-1));
var is_ffx = (agt.indexOf("firefox") != -1);
var is_saf = (agt.indexOf("safari") != -1) 
var is_opera = (agt.indexOf("opera") != -1);

var is_mac    = (agt.indexOf("mac")!=-1);
var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );


function optOrder(){
// write the forum select box onto page//	
if(document.getElementById('selectOrderCon')){
	if(readCookie('orderSF')){
	var optionOrder=readCookie('orderSF')
	optionOrder=optionOrder.split('|')
	optionHTML=""
	optionLength = optionOrder.length
	
for(i=0;i<optionLength;i++){
	if(optionOrder[i]){
	optionHTML+="<option id="+ optionOrder[i] +">"+optionOrder[i].replace(/^.{3}/,'')+"</option>"
}}
	document.getElementById('selectOrderCon').innerHTML="<SELECT id=selectOrder size="+(optionLength-1)+" name=selectOrder>"+optionHTML+"</select><img alt='Move option up' title='Move option up' src=images/uparrow.gif onclick=changeOpt('-1');return false><img alt='Move option down' title='Move option down' src=images/downarrow.gif onclick=changeOpt('1');return false>"
			}
		}
	}

function changeOpt(vert){
// set new forum order - move options in select box//	
	currentOpt=document.getElementById("selectOrder").options;
	currentOptText=document.getElementById("selectOrder").options[currentOpt.selectedIndex].text;
	currentOptID=document.getElementById("selectOrder").options[currentOpt.selectedIndex].getAttribute('id');
	prevOptText=document.getElementById("selectOrder").options[currentOpt.selectedIndex+parseInt(vert)].text;
	prevOptID=document.getElementById("selectOrder").options[currentOpt.selectedIndex+parseInt(vert)].getAttribute('id');
	document.getElementById("selectOrder").options[currentOpt.selectedIndex+parseInt(vert)].text=currentOptText;
	document.getElementById("selectOrder").options[currentOpt.selectedIndex+parseInt(vert)].setAttribute('id',currentOptID);
	document.getElementById("selectOrder").options[currentOpt.selectedIndex].text=prevOptText;
	document.getElementById("selectOrder").options[currentOpt.selectedIndex].setAttribute('id',prevOptID);
	currentOpt.selectedIndex= currentOpt.selectedIndex+parseInt(vert);
	addOpt();
}

function addOpt(){
// add new forum order to cookie //	
	optbox=""
	for (i=0; i<document.getElementById("selectOrder").options.length; i++) {
		optbox+=document.getElementById("selectOrder").options[i].getAttribute('id')+ "|";
	}
	createCookie('orderSF',optbox,365)
}
		
function reOrder(){
	newOrderHTML=""
	newOrder=readCookie('orderSF')	
	if(newOrder){
		newOrder=newOrder.split('|')
		newOrder.reverse();
		for(i=newOrder.length-1;i>=0;i--){
					if (document.getElementById(newOrder[i])) {
							$('#msgConSub').append(document.getElementById(newOrder[i]).parentNode)
								}
						}
				}
}

function createCookie(name,value,days) {
// generic cookies script //
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


function styleSheet(sheet){
//swaps stylesheet//
createCookie('styleName',sheet)
$('link[@rel]').each(function(i){
	this.disabled=true
	if(this.getAttribute('title')==sheet){
		this.disabled=false
		}
	})
}

function setStyle(){
//sets stylesheet onload//
	currentStyle=readCookie('styleName')
	if(currentStyle){
	$('link[@rel]').each(function(i){
		this.disabled=true
			if(this.getAttribute('title')==currentStyle){
				this.disabled=false
			}
		})
	}
}

function validate(el){
	var regUname=new RegExp(/^[A-Za-z0-9_]{4,12}$/)
	var regEmail=new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)
	
	switch(el.id){
		
		case "terms":
			redirect="signUp2.aspx"
			if(el.agree.checked==true){
			window.location=redirect
				}
			else{
				alert('You must agree to the terms and conditions before you can proceed')
				}
		break;
	
		case "regDetails" :
			//evalulate all required fields//
			if(el.uName.value.match(regUname) && el.EmailAd.value.match(regEmail) && el.Pswd.value.match(regUname) && el.Pswd.value==el.CPswd.value){
					//if true, clear any previous errors//
					formError(el,'clear');
					el.proceed.disabled=true;
					el.proceed.innerHTML="Sending details...";
					ReceiveDataFromFlashMovie();
					getAvatar = ReceiveDataFromFlashMovie();
					el.hair.value = getAvatar[0];
					el.hat.value = getAvatar[1];
					el['body'].value = getAvatar[2];
					el.fac.value = getAvatar[3];
					el.expr.value = getAvatar[4];
					el.strip.value = getAvatar[5];
					el.submit();
				}
			
			//handle errors//
		else{
			(!el.EmailAd.value.match(regEmail)) ? formError(el.EmailAd,'add','Please enter a valid email address') : formError(el.EmailAd,'destroy');
			(!el.uName.value.match(regUname)) ? formError(el.uName,'add','Please enter a valid username. This can be letters, numbers or _') : formError(el.uName,'destroy');
			(!el.Pswd.value.match(regUname)) ? formError(el.Pswd,'add','Please enter a valid password. This can be letters, numbers or _') : formError(el.Pswd,'destroy');
			(el.Pswd.value!=el.CPswd.value) ? formError(el.CPswd,'add','The password doesnt match. Please try again.') : formError(el.CPswd,'destroy');
					
				}
		break;
	
	case "changeDetails" :
	
			if(el.uName.value.match(regUname) && el.EmailAd.value.match(regEmail)){
				//if true, clear any previous errors//
					formError(el,'clear');
					el.proceed.disabled=true;
					el.proceed.innerHTML="Sending details...";
					getAvatar = ReceiveDataFromFlashMovie();
					el.hair.value = getAvatar[0];
					el.hat.value = getAvatar[1];
					el['body'].value = getAvatar[2];
					el.fac.value = getAvatar[3];
					el.expr.value = getAvatar[4];
					el.strip.value = getAvatar[5];
					el.submit();
				}
			
			//handle errors//
		else {
			(!el.EmailAd.value.match(regEmail)) ? formError(el.EmailAd,'add','Please enter a valid email address') : formError(el.EmailAd,'destroy');
			(!el.uName.value.match(regUname)) ? formError(el.uName,'add','Please enter a valid username. This can be letters, numbers or _') : formError(el.uName,'destroy');
		}
		break;
			
		case "changePass" :
	
			if(el.Pswd.value.match(regUname) && el.NewPswd.value.match(regUname) && el.NewPswd.value==el.ConPswd.value){
				//if true, clear any previous errors//
					formError(el,'clear');
					el.proceed.disabled=true;
					el.proceed.innerHTML="Sending changes...";
					el.submit();
				}
			
			//handle errors//
		else {
			(!el.Pswd.value.match(regUname)) ? formError(el.Pswd,'add','Please enter a valid password. This can be letters, numbers or _') : formError(el.Pswd,'destroy');
			(!el.NewPswd.value.match(regUname)) ? formError(el.NewPswd,'add','Please enter a valid password. This can be letters, numbers or _') : formError(el.NewPswd,'destroy');
			(el.NewPswd.value.match(regUname) && el.NewPswd.value!=el.ConPswd.value) ? formError(el.ConPswd,'add','The passwords do not match. Please try again.') : formError(el.ConPswd,'destroy');
		}
			
		break;
		default:
			return false;
	}
}

function formError(errorPoint,actionType,erMessage){
	switch(actionType){
	case 'add':
		$(errorPoint).parent().find('span').remove();
		$(errorPoint).parent().addClass('alertbg').append('<span>'+erMessage+'</span>')
		window.location="#reqTop";
	break;
	case 'destroy':
		$(errorPoint).parent().removeClass('alertbg').find('span').remove();
	case 'clear':
		$(errorPoint).find('.alertbg span').remove();
		$(errorPoint).find('td').removeClass('alertbg');

	break;
	
	default:
		alert('there has been an unknown error');
	break;
		}
}

function ReceiveDataFromFlashMovie()
{	if(window.document['avatar']){
	var flashMovie=window.document['avatar'];
	avatArray = new Array(flashMovie.GetVariable("hair"),flashMovie.GetVariable("hat"),flashMovie.GetVariable("body"),	flashMovie.GetVariable("fac"),flashMovie.GetVariable("expr"),flashMovie.GetVariable("strip"));
	avatarHash = "hair="+avatArray[0]+"&hat="+avatArray[1]+"&body="+avatArray[2]+"&fac="+avatArray[3]+"&expr="+avatArray[4]+"&strip="+avatArray[5];
	//createCookie("avatar", avatarHash, 0);
	return avatArray;
}

else{return false}
}

function jumpTo(url){
	
	if (url.value.charAt(0)!='0') {document.location.href = url.value};

	}
	
function setAvatarReg(el) {
	getAvatar = ReceiveDataFromFlashMovie();
	el.form.hair.value = getAvatar[0];
	el.form.hat.value = getAvatar[1];
	el.form['body'].value = getAvatar[2];
	el.form.fac.value = getAvatar[3];
	el.form.expr.value = getAvatar[4];
	el.form.strip.value = getAvatar[5];
	
}	

function displayAvatarGrid(){
  document.write('' +
  '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="630" height="190" id="avatar">' +
  '  <param name="movie" value="/includes/flash/forum_avatar/avatar_wide.swf?v=1" />' +
  '  <param name="quality" value="high" />' +
  '  <embed src="/includes/flash/forum_avatar/avatar_wide.swf?v=1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="630" height="190" name="avatar"></embed>' +
  '</object>'+
  '<input id="hair" name="hair" type="hidden" value="0" />'+
  '<input id="hat" name="hat" type="hidden" value="0" />'+
  '<input id="body" name="body" type="hidden" value="0" />'+
  '<input id="fac" name="fac" type="hidden" value="0" />'+
  '<input id="expr" name="expr" type="hidden" value="0" />'+
  '<input id="strip" name="strip" type="hidden" value="0" />');
}

function displayForumOrder(){
  document.write('' +
  '<div id="selectOrderCon">'+
    '<select id="selectOrder" size="7" name="selectOrder"><option id="sf-Football">Football</option><option id="sf-Cricket">Cricket</option><option id="sf-Rugby">Rugby</option><option id="sf-Tennis">Tennis</option>'+
    '<option id="sf-Boxing">Boxing</option>'+
    '<option id="sf-Motorracing">Motorracing</option>'+
    '<option id="sf-Fave">Fave</option>'+
    '</select><img alt="Move option up" title="Move option up" src="/images/uparrow.gif" onclick="changeOpt(\'-1\');return false" /><img alt="Move option down" title="Move option down" src="/images/downarrow.gif" onclick="changeOpt(\'1\');return false"/>'+
  '</div>');
}