/*	*****************************************************
	Email Us Script
	***************************************************** */
function vEmail(val) {
	var apos = val.indexOf("@");
	var dotpos = val.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) {
		return false;
	} else {
		return true;
	}
}
function vRequired(val) {
	if (val==null||val=="") {
		return false;
	} else {
		return true;
	}
}
function sendForm(_formId) {
	var formDocRef = document.getElementById(_formId);
	var vPass = true;
	var vErrorString = "You must have a valid:\n";
	// prepare to validate fields
	var emailTo = "live@mail.pgatour.com";
	var emailEmail = formDocRef.email.value
	var emailName = formDocRef.lastname.value
	var emailSubject = formDocRef.subject2.value
	var emailMessage = formDocRef.comment.value
	
	// validate form before sending
	if(!vRequired(emailEmail)) {
		vErrorString += "- email address\n";
		vPass=false;
	} else {
		if(!vEmail(formDocRef.email.value)) {
			vErrorString += "- email address\n";
			vPass=false;
		}
	}
	if(!vRequired(emailName)) {vErrorString += "- name\n"; vPass=false;}
	if(!vRequired(emailSubject)) {vErrorString += "- subject\n"; vPass=false;}
	if(!vRequired(emailMessage)) {vErrorString += "- message\n"; vPass=false;}
	if (!vPass) alert(vErrorString+"\nPlease correct these field(s) and try again. Thank you.");
	
	// make AJAX call to server, we are assuming everything works, we will
	// not know if the request fails because there is nothing setup
	// to send back a proper response.
	if(vPass) {
		var formAction = "http://dyn.pga.com/liveat/2007/r023/pipeline/email.cfm";
		var querystring = "to=" + emailTo;
		querystring += "&from="+ emailEmail;
		querystring += "&name=" + encodeUrl(emailName);
		querystring += "&subject=" + encodeUrl(emailSubject);
		querystring += "&message=" + encodeUrl(emailMessage);
		querystring += "&video="
		querystring += "&title=" + encodeUrl(emailSubject);
		querystring += "&type=fb";
		iCall(formAction+"?"+querystring);
		return true; //shows the thank you page
	} else {
		return false; //remains on the email us page
	}
}
function iCall(URL) {
	iFrameRefId = CreateIFrame(URL);
}
function IFrameLoaded(iFrameRefId) {
	document.getElementById(iFrameRefId+"notLoaded").id = iFrameRefId;
	var ifr = document.getElementById(iFrameRefId);
    // fixes bug if iframe page has loaded element not directly linked to it: i.e. ads
	ifr.src="";
	ifr.parentNode.removeChild(ifr);
}
function GetIFrameDoc(iFrameId) {
	
	var iFrameObj = document.getElementById(iFrameId);
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return false;
	}
	return IFrameDoc;
}
function CreateIFrame(URL) {
	// create a new instance of a iframe
	var IdToUse;
	for(var i=0; i<1000; i++) {
		if (!document.getElementById("RSIFrame"+i)) {
			IdToUse = "RSIFrame"+i;
			i = 1001;
		}
	}
	if (document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// iCall() is called
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id',IdToUse+"notLoaded");
			tempIFrame.setAttribute('src',URL);
			tempIFrame.setAttribute('onload', "IFrameLoaded('"+IdToUse+"')");
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames[IdToUse];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='\<iframe ';
			iframeHTML+='id="'+IdToUse+'notLoaded" ';
			iframeHTML+='src="'+URL+'" ';
			iframeHTML+='onload=\'IFrameLoaded("+IdToUse+")\' ';
			iframeHTML+='style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById(IdToUse);
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	return IdToUse;
}
function encodeUrl(str) {
	var result = "";
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) == " ") result += "+";
		else result += str.charAt(i);
	}
	return escape(result);
}
function decodeUrl(str) {
 	var result = str.replace(/\+/g, " ");
	return unescape(result);
}
/*	*****************************************************
	Utility Functions
	***************************************************** */
function hideFocusBorders(){
	var theahrefs = document.getElementsByTagName("a");
	if (!theahrefs){return;}
	for(var x=0;x!=theahrefs.length;x++){
		theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
	}
}
/*	*****************************************************
	Cutomize Functions
	***************************************************** */
function CreateNewOption (id, text, value) {
	var layer = document.getElementById(id); 
	layer.options[layer.options.length] = new Option(text, value); // works in all browsers, createElement has problems
}
function swapOptions(selectboxFrom, selectboxTo, limit, selected) {
	var i;
	var optn;
	var selectboxToId = selectboxTo;
	selectboxFrom = document.getElementById(selectboxFrom);
	selectboxTo = document.getElementById(selectboxTo);
	selectboxFromLen = selectboxFrom.options.length;
	for(i=0; i<selectboxFromLen; i++) {
		if(selectboxFrom.options[i]) {
			if(selectboxFrom.options[i].selected || selected == false) {
				if (selectboxTo.options.length < limit || limit == 0) {
					CreateNewOption(selectboxToId, selectboxFrom.options[i].text, selectboxFrom.options[i].value);
					selectboxFrom.remove(i);
					i--;
				}
			}
		}
	}
}
function removeOption(selectboxFrom, selectboxTo) {
	if($(selectboxFrom).options.selectedIndex>-1) {
		CreateNewOption(selectboxTo, $(selectboxFrom).options[$(selectboxFrom).options.selectedIndex].text, $(selectboxFrom).options[$(selectboxFrom).options.selectedIndex].value)
		$(selectboxFrom).remove($(selectboxFrom).options.selectedIndex);
	}
}
function removeAllOptions(selectboxFrom, selectboxTo) {
	while($(selectboxFrom).options.length > 0) {
		CreateNewOption(selectboxTo, $(selectboxFrom).options[0].text, $(selectboxFrom).options[0].value)
		$(selectboxFrom).remove(0);
	}
}
function removeOptions(selectbox) {
	var i;
	selectbox = document.getElementById(selectbox);
	for(i=selectbox.options.length-1;i>=0;i--)	{
		if(selectbox.options[i].selected)
			selectbox.remove(i);
	}
}
function tourSaveCustomPlayers(selectBoxId) {
	if (tourCustomPlayersObject != 0) {
		var tourSelectedBox = document.getElementById(selectBoxId);
		var tourPlayers = new Array();
		for(i=0; i<tourSelectedBox.options.length; i++){
			tourPlayers.push(tourSelectedBox.options[i].value);
		}
		createLiveAtCookie(tourCustomPlayersObject.id, tourPlayers, 365);
		tlac.HandleRefreshCustomLeaderBoard(4, tourPlayers);
	}
}
function tourSortCustomPlayers(playerListId, playerCookieListId, objArrayOfPlayers) {
	var i, n, optn;
	var tourCookiePlayers = new Array();
	var tourInCookie = false;
	tourCookiePlayers = readLiveAtCookie(objArrayOfPlayers.id);
	document.getElementById(playerCookieListId).innerHTML = '';
	document.getElementById(playerListId).innerHTML = '';
	for (var k=0; k<tourCookiePlayers.length; k++) {
		for(var i=0; i<objArrayOfPlayers.players.length; i++) {
			if (tourCookiePlayers[k] == objArrayOfPlayers.players[i].id) {
				CreateNewOption(playerCookieListId, objArrayOfPlayers.players[i].name, objArrayOfPlayers.players[i].id);
			}		
		}
	}
	for (var m=0; m<objArrayOfPlayers.players.length; m++) {
		tourInCookie = false;
		if (tourCookiePlayers.length > 0) {
			for (var n=0; n<tourCookiePlayers.length; n++) {
				if (objArrayOfPlayers.players[m].id == tourCookiePlayers[n]) { 
					tourInCookie = true;
					break;
				}
			}
			if (tourInCookie == true) { continue; }
		}
		CreateNewOption(playerListId, objArrayOfPlayers.players[m].name, objArrayOfPlayers.players[m].id);
	}
}
function tourCreateObject(){
	tourMetaData = {
	    id:4, 
	    name:"Buick Invitational", 
	    players:[
	        { id:1, name:"T. Woods" },
	        { id:2, name:"B. Hampton" },
			{ id:3, name:"C. Jones" },
			{ id:4, name:"H. Howard" },
			{ id:5, name:"J. Lewis" },
			{ id:6, name:"T. Nelson" }
	    ]
	};
	//tourCustomPlayersObject = eval(tourMetaData);
	tourCustomPlayersObject = tourMetaData;
}
/*	*****************************************************
	Cookies
	***************************************************** */
 function printLiveAtCookie(id, tourId) {
	var i;
	var tourCustomPlayers = readLiveAtCookie(tourId);
	document.getElementById(id).innerHTML = "";
	for(i=0; i<tourCustomPlayers.length; i++) {
		document.getElementById(id).innerHTML += "Player"+(i+1)+": "+tourCustomPlayers[i]+"<br/>";
	}
}
function createLiveAtCookie(tourId, arrayValues, days) {
	var pipedCookieValue = "";
	for (var i=0; i<arrayValues.length; i++) {
		pipedCookieValue += arrayValues[i];
		if ((i+1)<arrayValues.length) { pipedCookieValue += "|"; }
	}
	createCookie("tourLiveAtCustomPlayers_"+tourId,pipedCookieValue, days)
}
function createPipedCookie(_cookieName, _cookieValuesAsArray, days) {
	var pipedCookieValue = "";
	for (var i=0; i<_cookieValuesAsArray.length; i++) {
		pipedCookieValue += _cookieValuesAsArray[i];
		if ((i+1) < _cookieValuesAsArray.length) { pipedCookieValue += "|"; }
	}
	createCookie(_cookieName, pipedCookieValue, days);
}
function readLiveAtCookie(tourId) {
	var tourCustomPlayers = new Array();
	var tourCookieValue = readCookie("tourLiveAtCustomPlayers_"+tourId);
	if (tourCookieValue)
		tourCustomPlayers = tourCookieValue.split("|");
	return tourCustomPlayers;
}
function eraseLiveAtCookie(tourId) {
	eraseCookie("tourLiveAtCustomPlayers_" + tourId);
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var 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 eraseCookie(name) {
	createCookie(name,"",-1);
	return true;
}
function loadPropertiesJSON(_propertiesFile) {
    var htmlDOC = document.getElementsByTagName('head').item(0);
    var scriptTag = document.createElement('script');
    
    scriptTag.setAttribute('language', 'javascript');
    scriptTag.setAttribute('type', 'text/javascript');
    scriptTag.setAttribute('src', _propertiesFile);
    htmlDOC.appendChild(scriptTag);
    return false;
}

var adVars;
function setADVars() {
    adVars = readCookie("adSegVars");    
    if (adVars == null) {
        adVars = "NGUserID=" + readCookie("NGUserID") + "&adDEmas=" + readCookie("adDEmas");
    }
}

function init() {
    hideFocusBorders();
    setADVars();
    loadPropertiesJSON(TOURNAMENT_LIVEAT_PROPERTIES_FILE);
}



/*	*****************************************************
	Document Load
	***************************************************** */
Event.observe(window, 'load', init, false);