var loaded = false;

if (document.images) {
    img1off = new Image();img1off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_allbenefits_off.gif";
    img2off = new Image();img2off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_games_off.gif";
    img3off = new Image();img3off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_watch_off.gif";
	img4off = new Image();img4off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_newsletters_off.gif";
	img5off = new Image();img5off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_shop_off.gif";
	img6off = new Image();img6off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_win_off.gif";
	img7off = new Image();img7off.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_connect_off.gif";
    img1hov = new Image();img1hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_allbenefits_hov.gif";
    img2hov = new Image();img2hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_games_hov.gif";
    img3hov = new Image();img3hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_watch_hov.gif";
	img4hov = new Image();img4hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_newsletters_hov.gif";
	img5hov = new Image();img5hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_shop_hov.gif";
	img6hov = new Image();img6hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_win_hov.gif";
	img7hov = new Image();img7hov.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_connect_hov.gif";
    img1on = new Image();img1on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_allbenefits_on.gif";
    img2on = new Image();img2on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_games_on.gif";
    img3on = new Image();img3on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_watch_on.gif";
	img4on = new Image();img4on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_newsletters_on.gif";
	img5on = new Image();img5on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_shop_on.gif";
	img6on = new Image();img6on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_win_on.gif";
	img7on = new Image();img7on.src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/aah/nav_connect_on.gif";
	loaded=true;
    }
	
var activehov = new RegExp("_hov");
var activeoff = new RegExp("_off");
var activeon= new RegExp("_on.gif");

function rollOut(href) {
    if (document.images) {
		var imgArr = href.getElementsByTagName("img");
		var img = imgArr[0].src;
		if(activeoff.test(img) || activeon.test(img)){
			return true;
		}	 
		imgArr[0].src = eval(imgArr[0].id+"off.src");
    }
}

function rollOver(href) {
    if (document.images && (loaded == true)) {
		var imgArr = href.getElementsByTagName("img");
		var img = imgArr[0].src;
		if(activehov.test(img) || activeon.test(img)){
			return true;
		}	
		imgArr[0].src = eval(imgArr[0].id+"hov.src");
    }
}

function onState(imgName) {
        document[imgName].src = eval(imgName + "on.src");
}

function goToSecuredURL(href, width, height) {
	if ($chrome.isUserAuthenticated()) {
		goToURL(href, width, height);
	} else {  /*member not logged in*/
		window.status = "Please register to be an official All-Access member";
		gotoRegisterForm();
	}
}

function goToURL(href, width, height){
	javascript:open_window_ex(href, '', 'directories=yes,toolbar=yes,scrollbars=yes,location=yes,status=yes,menubar=yes,resizable=yes,width=' + width + ',height=' + height)
}

/* Generic Open-Window function */
function open_window_ex(url, title, height, width, options) {
    var path = url;
    var opts = '';
	if(options == null || options == "null")
		opts = 'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=yes, resizable=yes';
    else
		opts = options;
		
	opts += ',width=' + width;
    opts += ',height=' + height;
    mywin=window.open(path,title,opts);
}

/* ******* login scripts: ******** */

var formUrl = $chrome.getMSIB() + "/flow/";
/* these next functions are called from various links in the all access pages */
function forgotPassword() {
	location.href = formUrl+ "requestPasswordReset";;
}
function resetPassword() {
	forgotPassword();
}
function gotoLogin() {
	location.href = formUrl + "login";
}
function gotoLoginUrl(url) {
	/* url will be something like '/allaccess/pageName.html' */
	location.href = formUrl + "login?url="+encodeURIComponent("http://" + location.hostname + url);
}
function logout() {
	location.href = formUrl + "logout?url="+encodeURIComponent(window.location);
}
function editProfile() {
	location.href = formUrl + "editUser";
}
function editSubscriptions() {
	location.href = formUrl + "index";
}
function editFanPrefs() {
	location.href = formUrl + "editUser#fanprefs";
}
function gotoRegisterForm() {
	if ($chrome.isUserAuthenticated()) {
		editProfile();
	} else {
		location.href = formUrl + "register";
	}
}
/* ************* */
function windowOnloadScript(){
	/* determine which tab to enable */
	var thisPage = window.location.href;
	if (thisPage.indexOf("index.html") > 0) {
		onState("img1");
	} else if (thisPage.indexOf("games.html") > 0 ) {
		onState("img2");
	} else if (thisPage.indexOf("gamesFantasyNBA.html") > 0) {
		onState("img2");
	} else if (thisPage.indexOf("gamesFantasyWNBA.html") > 0) {
		onState("img2");
	} else if (thisPage.indexOf("gamesOnline.html") > 0) {
		onState("img2");
	} else if (thisPage.indexOf("watchListen.html") > 0) {
		onState("img3");
	} else if (thisPage.indexOf("newsletters.html") > 0) {
		onState("img4");
	} else if (thisPage.indexOf("newslettersNBA.html") > 0) {
		onState("img4");
	} else if (thisPage.indexOf("newslettersWNBA.html") > 0) {
		onState("img4");
	} else if (thisPage.indexOf("shop.html") > 0) {
		onState("img5");
	} else if (thisPage.indexOf("shopNBA.html") > 0) {
		onState("img5");
	} else if (thisPage.indexOf("shopWNBA.html") > 0) {
		onState("img5");
	} else if (thisPage.indexOf("win.html") > 0) {
		onState("img6");
	} else if (thisPage.indexOf("connect.html") > 0) {
		onState("img7");
	} else if ((thisPage.indexOf("/allaccess/") > 0) && (thisPage.indexOf(".html") < 0)) {
		onState("img1");
	}
	/* set up Member Login area */
	var displayName = $chrome.getUserDisplayName();
	if ($chrome.isUserAuthenticated()) {
		$("myaccount_login").style.display="none";
		$("welcomeLogin").innerHTML=displayName;
		$("notLink").innerHTML="Not " + displayName + "?";
		$("myaccount_welcome").style.display="block";
	} else { /*member has not logged in*/
		$("myaccount_login").style.display="block";
		$("myaccount_welcome").style.display="none";	
	}
	/* determine which elements to hide/show depending on whether member signed-in or not */
	/* initial page state, aaSignedOnNo is displayed, aaSignedOnYes will be displayed only if member is signed in */
	var arrInYes = new Array();
	var arrInNo = new Array();
	arrInYes = getElementsByClass('aaSignedInYes');
	arrInNo = getElementsByClass('aaSignedInNo');
	if ($chrome.isUserAuthenticated()) {
		if (arrInNo.length >0 ) {
			for (i=0;i<arrInNo.length;i++) {
				arrInNo[i].style.display="none";
			}
		}
		if (arrInYes.length >0 ) {
			for (i=0;i<arrInYes.length;i++) {
				arrInYes[i].style.display="inline";
			}
		}

	}
}

/* grab Elements from the DOM by className */
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;
}

/*-------------------------------------------------------
   Form validation functions for contactus.html:
  ------------------------------------------------------- */
function redirectToFAQ(){
	   var e = document.getElementById('select_faq');
	   var targetAnchor = e.options[e.selectedIndex].value;
	   var url = 'http://'+window.location.hostname+'/allaccess/faqs.html' + targetAnchor;
	   window.location = url;
	}
	function onSubmit(){
		if(checkForm()){
			var e = document.getElementById('contactusform');
			e.submit();
		}
	}

	function checkForm () {
		try{
			var status = true;
			if(!checkFName()){status = false;}
			if(!checkLName()){status = false;}
			if(!checkEmail()){status = false;}
			if(!checkTopic()){status = false;}
			if(!checkQuestion()){status = false;}
			return status;
		}catch(err){
			alert(err);
			return false;
		}

	}

	function checkFName(){
		var status = true;
		var firstname =	document.getElementById("fname").value;
		if (firstname == "") {
			displayError(document.getElementById("lbl_fname"),'fnameerror');
			status = false;
		}else{
			displayValid('fnameerror');
		}
		return status;
	}

	function checkLName(){
		var status = true;
		var lastname =	document.getElementById("lname").value;
		if (lastname == "") {
			displayError(document.getElementById("lbl_lname"),'lnameerror');
			status = false;
		}else{
			displayValid('lnameerror');
		}
		return status;
	}

	function checkEmail(){
		var status = true;
		var emailRE = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email =	document.getElementById("email").value;
		if (email == "" || !email.match(emailRE)) {
			displayError(document.getElementById("lbl_email"),'emailerror');
			status = false;
		}else{
			displayValid('emailerror');
		}
		return status;
	}
	function checkQuestion(){
	    var status = true;
		var question = document.getElementById("question").value;
		if (question == "" ) {
			displayError(document.getElementById("lbl_question"),'questionerror');
			status = false;
		}else{
			displayValid('questionerror');
		}
	    return status;
	}
	function checkTopic(){
	    var status = true;
	    var e = document.getElementById('select_topic');
		var topic = e.options[e.selectedIndex].value;
		if (topic == "" ) {
			displayError(e,'topicerror');
			status = false;
		}else{
			displayValid('topicerror');
		}
	    return status;
	}

	function hideAllErrors () {
		var arrElements = getElementsByClassName(document,'*','errormark');
	    for(var i=0; i<arrElements.length; i++){
	        arrElements[i].visibility = "hidden";
	    }
	}

	function displayValid (errorElementName) {
		var e = document.getElementById(errorElementName);
		e.className = "validmark";
	}

	function displayError (element, errorElementName) {
		var e = document.getElementById(errorElementName);
		e.className = "errormark";
		try{
			element.select();
		}catch(err){}
		element.focus();
	}

	function getElementsByClassName(oElm, strTagName, strClassName){
		var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }
	    }
	    return (arrReturnElements)
	}

