// DFP ad serving variables
var ord		= Math.random()*10000000000000000;
var ordnum	= Math.round(Math.random() * 1000000000);


/*******************************************************************
 *	Cookie Functions
 *******************************************************************/
// Check for a cookie's existance
function getCookie(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;
}
// Create a new cookie
function setCookie(name, value, expires, path, domain, secure) {
	var expDate		= new Date(expires);
	var curCookie	= name + "=" + escape(value) +
		((expires) ? "; expires=" + expDate.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}
// Delete an existing cookie
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		var curCookie	= name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		document.cookie = curCookie;
	}
}


/*******************************************************************
 *	FireClick Functions
 *******************************************************************/
// Track a click and redirect the page
function flashFire (name, url) {
	window.open(url);
	fc_click(name, 'ad', url);
}
// Track a click without redirecting page
function flashFireNoGo (name, url) {
	fc_click(name, 'ad', url);
}


/*******************************************************************
 *	CNN Video Player Functions
 *******************************************************************/
// Open popup window
function CNN_openPopup( url, name, widgets, openerUrl )
{
	var host = location.hostname;
	try {
		window.top.name = "opener";
	} catch (e) {}
	var popupWin = window.open( url, name, widgets );
	if ( popupWin) {
		popupWin.focus();
	}
}
// Play a PGATOUR.com video
function cnnVideo( mode, arg, expiration ) {
	cnnVideoArgs = 'url=' + arg;
	openURL = '/video/player/player.html';;
	CNN_openPopup( 'http://www.pgatour.com/'+openURL+'?'+cnnVideoArgs, 'CNNVideoPlayer', 'scrollbars=no,resizable=no,width=770,height=570' );
}

/** Update **/
//Gets the current URL and replaces /index.html with /print.html
//Used on the microsite news articles to pop up a printable version of the article
//NAB - 6/02/2008
function GetPrintURL()
{
	var myURL;
	var newURL;
	var f = "/index.html";
	var fileRegEx = /\/index.html$/;
	var prnFile = "/print.html";
	
	myURL = window.location;
	myURL = String(myURL);
	if (myURL.match(fileRegEx)) {
		newURL = myURL.replace(fileRegEx, prnFile);
	}
	else {
		newURL = myURL + prnFile;
	}	
	window.open(newURL, "mywindow","location=1,status=1,scrollbars=1,width=650,height=800");	
	//testwindwo.moveTo(0,0);

}
//Used to assist in closing a window in Firefox.
//TFH - 6/28/2007
function closeWindow() {

window.open('','_parent','');

window.close();

}

function cnnOpen(url) {
    if (url) {
        lowerUrl = url.toLowerCase().replace(" ", "");
		
		if (lowerUrl.indexOf("/video") != -1 || lowerUrl.indexOf("/audio") != -1) {
			if (lowerUrl.indexOf("?") != -1) { 
				openURL = url;
            }
			else {
				openURL = "/video/?" + url
            }
			
			CNN_openPopup( 'http://www.pgatour.com' + openURL, 'CNNVideoPlayer', 'scrollbars=yes,status=yes,resizable=yes,width=1000,height=700' );
        }
		
        else if (lowerUrl.indexOf("/video/video") > -1 ||
            lowerUrl.indexOf("/video/audio") > -1 ||
            lowerUrl.indexOf("/video/") > -1 &&
            lowerUrl.indexOf("/video/liveat/") < 0) {
            window.location = "/video/?" + url;
        } else if (lowerUrl.indexOf("/photos/20") > -1) {
            window.location = "/photos/?" + url;
        } else if (lowerUrl.indexOf("/video/liveat/?") == 0) {
            openLiveAt(url.replace("/video/liveat/?", ""));
        } else if (lowerUrl.indexOf("/swingplex") > -1) {
            CNN_openPopup(url, "780x445", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=780,height=445");
        } else {
            window.location = url;
        }
    }
}
function tourOpen( url ) { cnnOpen(url); }

function MSloadPhotoGallery(_GalleryObj, _link) {
	if (!_GalleryObj || !_link) { 
		return; 
	}
	
	$("MSphotoGalH1").innerHTML = _GalleryObj.getElementsByTagName('h4')[0].innerHTML
	$("MSphotoGalBlurb").innerHTML = _GalleryObj.getElementsByTagName('span')[0].innerHTML;
	
	$("MSphotoGalFlashIF").src = _link;
}

function MSgetPhotoGalleryRef(_LinkObj, _link) {
	if (!_LinkObj || !_link) { 
		return; 
	}
	
	var oParentNodeRef;
	var oGrandParentNodeRef;
	
	try {
		oParentNodeRef = _LinkObj.parentNode;
		oGrandParentNodeRef = oParentNodeRef.parentNode;
		MSloadPhotoGallery(oGrandParentNodeRef, _link);
	} catch(e) {}
}


// Handle Email Alert Modules
function checkSubmit(_formObj) {
	var emailCheckerRegEx = /^.+@.+$/;
	if (!_formObj.email.value || !emailCheckerRegEx.test(_formObj.email.value)) {		
		alert("Enter a valid Email Address.");
		return false; 
	}
	
	setCookie("EMAILSUBMITTED", _formObj.email.value);
	return true;
}

// Handle Email Alert onLoad
function checkForSubmittal() {
	var cookieName	= "EMAILSUBMITTED";
	var cookieVal	= getCookie(cookieName);
	
	if (cookieVal) {
		Element.hide('email_message');
		Element.hide('email_form');
		Element.show('email_thankyou');
	} else {
		Element.show('email_message');
		Element.show('email_form');
		Element.hide('email_thankyou');
	}
}

// ********************************************************************************
// 7.0 WINDOW OPEN FUNCTION
//*********************************************************************************
function PGAopenWindow(_windowID) {
	if ( !_windowID ) {
		return;
	}
	else if (_windowID == "travelers") {
		window.open('http://www.pga.com/promos/travelers/index.html','promo_huber','status=no,toolbar=no,resizable=no,scrollbars=no,menubar=no,location=no,width=800,height=623');
	}
	else {
		window.open('/promos/'+_windowID+'/','promo_'+_windowID+'','status=no,toolbar=no,resizable=no,scrollbars=no,menubar=no,location=no,width=800,height=623');
	}
}
