// usatCommon.js

Event.observe(window, 'load', function(event) {

	try {
		_initPage(null, event);
	}
	catch (e) {
		// todo: handle exception
	}
	
	Event.observe(window, 'unload', function(event) {
		_unloadPage(null, event);
	}); 

	Event.observe(window, 'beforeunload', function(event) {
		_onBeforeUnloadPage(null, event);
	}); 
	
});

// Page loading/unloading functions
var chatWindowDivElement;
var chatWindowGuestDivElement;

// Page loading/unloading functions
function _initPage(thisObj, thisEvent) {
	// insert site-wide code here 
	
	try {
		chatWindowDivElement= $('templateChatWindowDiv');

		new Draggable(chatWindowDivElement);
	
		setTimeout('templateClickToChatMouseOver()', 180000);
	}	
	catch (e) {
		; // ignore
	}
	
	try {
		chatWindowGuestDivElement = $('templateChatWindowGuestDiv');

		new Draggable(chatWindowDivGuestElement);
	
		setTimeout('templateClickToChatMouseOverGuest()', 180000);
	}	
	catch (e) {
		; // ignore
	}
	
	
	
	// you typically would run this after all site-wide
	// execution has processed, but it's really up to
	// the design of your site to when you may want to run this
	if( typeof window.initPage == "function" ) {
		window.initPage(thisObj, thisEvent);
	}

}

function _unloadPage(thisObj, thisEvent) {
	// insert site-wide code here


	// you typically would run this after all site-wide
	// execution has processed, but it's really up to
	// the design of your site to when you may want to run this
	if( typeof window.unloadPage == "function" ) {
		window.unloadPage(thisObj, thisEvent);
	}

}

function _onBeforeUnloadPage(thisObj, thisEvent) {
	// insert site-wide code here


	// you typically would run this after all site-wide
	// execution has processed, but it's really up to
	// the design of your site to when you may want to run this
	if( typeof window.onBeforeUnloadPage == "function" ) {
		return window.onBeforeUnloadPage(thisObj, thisEvent);
	}
	return "";
}

function _subscribeToPub() {
	if( typeof window.subscribeToPub == "function" ) {
		window.subscribeToPub();
	}
	else {
		document.location = "/subscriptions/order/checkout.faces";
	}
}

function _subscribeToBW() {
	document.location = "/subscriptions/order/checkout.faces?pub=BW";
}

function _giveAGift() {
	document.location = "/subscriptions/order/checkout.faces";
}

function _subscribeByMail() {
	if( typeof window.subscribeByMail == "function" ) {
		window.subscribeByMail();
	}
	else {
	// usatoday redirect
		document.location = "/subscriptions/subscribebymail.html";
	}
}

function _subscribeToElectronicEdition() {

	if( typeof window.subscribeToElectronicEdition == "function" ) {
		window.subscribeToElectronicEdition();
	}
	else {
		document.location = "/subscriptions/electronic/orderEntry.faces";
	}
}
	
function _subscribeToElectronicEditionOrderPage() {

	if( typeof window.subscribeToElectronicEditionOrderPage == "function" ) {
		window.subscribeToElectronicEditionOrderPage();
	}
	else {
		document.location = "/subscriptions/order/checkout.faces?pub=EE";
	}
}
	
function _subscribeToInternationalEdition() {
	if( typeof window.subscribeToInternationalEdition == "function" ) {
		window.subscribeToInternationalEdition();
	}
	else {
		document.location = "/international/welcomeint.jsp";
	}
}

function _shopUSAT() {
	if( typeof window.shopUSAT == "function" ) {
		window.shopUSAT();
	}
	else {
		document.location = "http://onlinestore.usatoday.com";
	}
}

function _loadFAQ() {
	if( typeof window.loadFAQ == "function" ) {
		window.loadFAQ();
	}
	else {
		window.open('/faq/index.jsp','FAQ', 'width=825,height=800,menubar=no,titlebar=yes,scrollbars=yes,status=no,location=no,directories=no');
	}
}

function func_Logout(thisObj, thisEvent) {
	document.location = '/index.jsp?logsessobj=rem';
}

function respondToFlashClickEvent( whereToGo ) { 
	if (typeof whereToGo == 'string' && whereToGo.length > 0) { 
		document.location = whereToGo; 
	} 
}

function templateLogoClick() {
	document.location = "/welcome.jsp";
}

var templateShowChatOnMouseOver = true;
var checkoutGuaranteeOnMouseOver = true;
var usat_lastTimeStamp = 0;
// call so following require scriptaculous

function templateClickToChatClicked(thisObj, thisEvent) {


	try {
		
		if (chatWindowDivElement.visible() || chatWindowGuestDivElement.visible()) {
			// hide them if already visible.
			if ((chatWindowDivElement.visible())) {
				setTimeout('templateClickToChatHide()', 1000);
			}
			if ((chatWindowGuestDivElement.visible())) {
				setTimeout('templateClickToChatHideGuest()', 1000);
			}
		}
		else {
			// otherwise shot it
			Effect.Grow('templateChatWindowDiv', {duration:0.75});
			chatWindowDivElement.setOpacity(1.0);
			
		}
		
	}
	catch (e) {
	}
	return false;
}

function checkoutGuarantee(thisObj, thisEvent) {

	
	if ( (thisEvent.timeStamp - usat_lastTimeStamp) < 1000)  {
		return false;
	}
	
	usat_lastTimeStamp = thisEvent.timeStamp;
	
	try {
		if (!(checkoutGuaranteeDivElement.visible())) {
			Effect.Grow('checkoutGuaranteeDiv', {duration:0.75});
			checkoutGuaranteeDivElement.setOpacity(1.0);
				
		}
		else {
			if ((checkoutGuaranteeDivElement.visible())) {
				setTimeout('checkoutGuarantee()', 1000);
			}
		}
	}
	catch (e) {
	}

	return false;
}

function templateClickToChatClickedGuest(thisObj, thisEvent) {

	try {
		if (chatWindowDivElement.visible() || chatWindowGuestDivElement.visible()) {
			// hide them if already visible.
			if ((chatWindowDivElement.visible())) {
				setTimeout('templateClickToChatHide()', 1000);
			}
			if ((chatWindowGuestDivElement.visible())) {
				setTimeout('templateClickToChatHideGuest()', 1000);
			}
		}
		else {
			// otherwise shot it
			Effect.Grow('templateChatWindowGuestDiv', {duration:0.75});
			chatWindowGuestDivElement.setOpacity(1.0);			
		}
		
	}
	catch (e) {
	}

	return false;
}

function templateClickToChatMouseOver(thisObj, thisEvent) {
	
	try {  
		if ( !(chatWindowDivElement.visible()) &&  templateShowChatOnMouseOver) {
			usat_lastTimeStamp = thisEvent.timeStamp;
			Element.CenterOnPage(chatWindowDivElement);
			Effect.Grow('templateChatWindowDiv', {duration:0.75});
			chatWindowDivElement.setOpacity(1.0);
		}
		return false;
	}
	catch (e) {
	}
	return false;
}

function templateClickToChatMouseOverGuest(thisObj, thisEvent) {
	
	try {  
		if ( !(chatWindowDivElement.visible()) &&  templateShowChatOnMouseOver) {
			usat_lastTimeStamp = thisEvent.timeStamp;
			Element.CenterOnPage(chatWindowDivElement);
			Effect.Grow('templateChatWindowGuestDiv', {duration:0.75});
			chatWindowGuestDivElement.setOpacity(1.0);
		}
		return false;
	}
	catch (e) {
	}
	return false;
}
function checkoutGuaranteeHide() {
	try {  
		checkoutGuaranteeOnMouseOver = false;
		Effect.Shrink('checkoutGuaranteeDiv');
		return false;
	}
	catch (e) {
	        return false;
	}
}
function templateClickToChatHideGuest() {
	try {  
		templateShowChatOnMouseOver = false;
		Effect.Shrink('templateChatWindowGuestDiv');
		return false;
	}
	catch (e) {
	        return false;
	}
}

function templateClickToChatHide() {
	try {  
		templateShowChatOnMouseOver = false;
		Effect.Shrink('templateChatWindowDiv');
		return false;
	}
	catch (e) {
	        return false;
	}
}

function mobileNavChanged(thisObj, thisEvent) {

	if (thisObj.value != "noop"){
		document.location = thisObj.value;
	}
}
function mobileNavChangedV2(thisObj, thisEvent, url) {

	if (url != "noop"){
		document.location = url;
	}
}

Element.CenterOnPage = function(element, parent) {

    var w, h, pw, ph;
    var d = Element.getDimensions(element);
    w = d.width;
    h = d.height;

    if (!parent) {
          pw = document.viewport.getWidth();
          ph = document.viewport.getHeight();
            
    } else {
            pw = parent.offsetWidth;
            ph = parent.offsetHeight;
    }
    
   var scrollVariance = document.viewport.getScrollOffsets();
	
   var top = ((ph/2) - (h/2)) + scrollVariance[1];
   //var left = ((pw/2) - (w/2)) + scrollVariance[0];
   var left = ((pw/2) - (w/2));

   $(element).style.top = top + 'px';      
   $(element).style.left = left + 'px';           
};

//////////////////////////////////////////////////////////////////////////
// original
// newFunction
var selectedRow;
function usat_highlight(item) {
	if (item != selectedRow) {
		var pItem = $(item);
		if (pItem.hasClassName('termsNormal')) {
			pItem.removeClassName('termsNormal');
		}
		pItem.addClassName('termsHighlight');
	}
}

function usat_cancelHighlight(item) {
	if (item != selectedRow) {
		item.className =  "termsNormal";
	}
}

function usat_selectRow(theid) {
	var row = document.getElementById(theid);
	
	if (selectedRow) {
		selectedRow.className = "termsNormal";
		var columns = selectedRow.getElementsByTagName("td");   
		for(var i = 0; i < columns.length; i++){
			columns[i].className = "usatTermsFontNormal";
		}
	}
	row.className = "termsSelected";
	var columns2 = row.getElementsByTagName("td");
	for(i = 0; i < columns2.length; i++){
		columns2[i].className = "usatTermsFontSelected";
	}
	selectedRow = row;
}

function usat_selectRadioAndHighlight(row) {
	
	var radio = row.getElementsByTagName('input');
	if (radio[0]) {
		radio[0].checked = true;
		usat_selectRow(row.id);
	}
}

function usat_selectRadio(row) {
	
	var radio = row.getElementsByTagName('input');
	if (radio[0]) {
		radio[0].checked = true;
	}
}

function usat_selectInitialRow(table) {
	var rows = table.getElementsByTagName('tr');
	for(var i = 0; i < rows.length; i++){
		var radio = rows[i].getElementsByTagName('input');
		if (radio && radio[0] && radio[0].checked) {
			usat_selectRadioAndHighlight(rows[i]);
			break;
		}
	}
	
}

function getCurrentYear() {
	today=new Date();
    y0=today.getFullYear();
    return y0;
}


//newFunction
//following method is used to verify domain.
function usat_gup( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    
		return "";  
	else    
		return results[1];
}

var usat_verifyDomains = true;

function usat_verifyDomain() {

	try {
		if (usat_verifyDomains) {
			var host = window.location.hostname;

			if (host != "service.usatoday.com") {
				if (host.startsWith("usat-") || host == "localhost") {
					;
				}
				else {
					usat_forceRedirect();
				}
			}
		}
	}
	catch (e) {
		// 
	}
}

function usat_forceRedirect() {
	try {
		// kill any on exit pop ups
		popOffer_programEnabled = false;
	}
	catch (e) {
	}
	var pubCode = usat_gup('pub');
	var keycode = usat_gup('keycode');
	
	var newLoc = 'http' + 's://' + 'service.usatoday.com/subscriptions/order/checkout.faces';
	
	if (pubCode.length == 2) {
		
		newLoc = newLoc + '?pub=' + pubCode;
		if (keycode.length == 5) {
			newLoc = newLoc + '&keycode=' + keycode;
		}
	}

	document.location = newLoc;
}
