/* 
 * Global constants
 */

var ID = { MLB:1, NFL:7, NHL:10, NASCAR:15, CFB:8, NCAAF:8, CBK:5, NCAAB:5, NBA:3, OptionChooseOne:-1 };
var isIE = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1));

/*
 * Utility functions
 */

function showDiv(szDivID, iState) { // 1 visible, 0 hidden
	var obj = document.layers ? document.layers[szDivID] :
	document.getElementById ?  document.getElementById(szDivID).style :
	document.all[szDivID].style;
	obj.visibility = document.layers ? (iState ? "show" : "hide") : (iState ? "visible" : "hidden");
}

function popUp(URL,width,height) {
	if (!width) width = 500;  // default window size
	if (!height) height = 350;
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + width + ",height=" + height + ",left = 390,top = 312');");
}

function validateEmail(str) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return regex.test(str);
}

function validatePhone(phoneNo) { // check to see if phoneNo is a 10 digit phone number
	var regexInteger = /^\d+$/;				
	return !(!regexInteger.test(phoneNo) || phoneNo.length!=10 || phoneNo.substring(0,1)==0 || phoneNo.substring(0,1)==1 );	
}

function objectEval(text) {
	// eval() breaks when we use it to get an object using the { a:42, b:'x' } syntax because it thinks that { and } 
	// surround a block and not an object, so we wrap it in an array and extract the first element to get around this.
	// The regex = [start of line][whitespace]{[stuff]}[whitespace][end of line]
	text = text.replace(/\n/g, ' ');
	text = text.replace(/\r/g, ' ');
	if (text.match(/^\s*\{.*\}\s*$/)) { text = '[' + text + '][0]'; }
	return eval(text);
}
  
// get value of select field given field id
function getSelectVal(id) {
	var el = document.getElementById(id);
	var value = (el.selectedIndex == -1)?'':el.options[el.selectedIndex].value;
	return value;
}

// get value of drop down menu's selected option's display text
function getSelectText(id) {
	var el = document.getElementById(id);
	var value = (el.selectedIndex == -1)?'':el.options[el.selectedIndex].text;
	return value;
}

function setSelectValTo(id, optValue) { document.getElementById(id).value = value; }

// set select drop down menu to the option where its display text (not value) is optText
function setSelectTextTo(id, optText) {
	var el = document.getElementById(id);
	for (var i=0; i<el.options.length; i++)
		if (el.options[i].text.toUpperCase() == optText.toUpperCase()) {
			el.value = el.options[i].value;
			break;
		}
}
    
// get value of select field given field id
function getVal(id) { return document.getElementById(id).value; }

function goToDiv(id){
	anchorPos = document.getElementById(id).offsetTop;
	this.parent.frames.alertEdit.scroll(0,anchorPos);
}

function callAnalyticsPage(pageUrl, showConfirmation) { 
	if (showConfirmation == null) showConfirmation = false;
	if (window.XMLHttpRequest) req = new XMLHttpRequest();
	else if (window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	
	if (showConfirmation) req.onreadystatechange = processAnalyticsRequest;
	req.open("GET", pageUrl, true);
	req.send(null);
}

function processAnalyticsRequest() { 
	if (req.readyState == 4) {
		if (req.status == 200) alert('Page containing conversion code has been called');
		else alert('Call to page containing conversion code failed');
	}
}

function noenter(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charcode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode==13 || charCode==3) return false;
	else return true;		
}

function hover(el,isOverLink) { if (isOverLink)	el.style.cursor = 'pointer';el.style.textDecoration = (isOverLink)?'underline':'none'; }
function hand(el) { el.style.cursor = 'pointer'; }
function stripHtml(str) { return str.replace(/(<([^>]+)>)/ig,""); }
function stripScriptTags(str) { return str.replace(/<\/?script/gi, "<..."); }
function stripNonAlphaNumeric(str) { return str.replace(/[^a-zA-Z0-9]+/g,''); }

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); }

function getElementsByClassName(classname, parent) {
    var a = [];
	parent = parent || document;
    var re = new RegExp('\\b' + classname + '\\b');
    var els = parent.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function addRemClass(el,addClass,remClass) {
	if (!el.className) el.className = ''; 
	var clsnm = el.className; 
	if (addClass && !clsnm.match(RegExp("\\b"+addClass+"\\b"))) clsnm = clsnm.replace(/(\S$)/,'$1 ')+addClass; 
	if (remClass) clsnm = clsnm.replace(RegExp("(\\s*\\b"+remClass+"\\b(\\s*))*","g"),'$2'); 
	el.className=clsnm; 
}

function addCssRule(name,rule) {
	if (!document.styleSheets) return false;
	if (document.styleSheets[0].addRule) document.styleSheets[0].addRule(name, rule, 0);
	else document.styleSheets[0].insertRule(name + " { " + rule + " }", 0); 
}

function _tab(fld,pfx,idx,e) { if(fld.value.length>2 && idx<3) { _gel(pfx+(idx+1)).focus();_fstClr=1;return; } var evt = e || window.event;_fstClr=!_fstClr&&fld.value.length==0;if(fld.value.length==0 && !_fstClr && evt.keyCode==8 && idx>1){_gel(pfx+(idx-1)).focus();_fstClr=0;}} var _fstClr = 0;
function _gel(id) { return document.getElementById(id); }	
function _disp(id, bool) { if (_gel(id)==null) return; _gel(id).style.display = bool?'':'none'; }
function _enab(id, bool) { _gel(id).disabled = !bool; }
function _visible(id) { return _gel(id).style.display != 'none'; }
function _isEnabled(id) { return !_gel(id).disabled; }
function _varExists(varName) { return (eval('typeof '+varName) != 'undefined'); }
function _populateMenu(id, data, def) {	_gel(id).options.length = 0; for (var i=0; i<jsonData.length; i++) _gel(id).options[i] = new Option(data[i].name,data[i].id); }

function checkObj(id,bool){ _gel(id).checked=bool; }
function displayObj(id, bool) { _gel(id).style.display = bool?'':'none'; }
function enableObj(id, bool) { _gel(id).disabled = !bool; }
function updateClassName(id, newClassName) { _gel(id).className = newClassName; }
function trim(str) { return str.replace(/^\s*/, '').replace(/\s*$/, ''); } 
function atLeastOneChecked(arr) { for (var i=0; i<arr.length; i++) if (_gel(arr[i]).checked) return true; return false; }