function returnJson(rText)
{
    var chk = "while(1=1) ";
    if(rText.indexOf(chk) > -1){rText = rText.substring(chk.length, rText.length);}
    var func = new Function("return " + rText);
    var jsonObj = func();
    return jsonObj;
}

if(!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function(val, fromIndex)
    {
        if(typeof(fromIndex != "number")) fromIndex = 0;
        for(var index = fromIndex, len=this.length; index < len; index++)
        {
            if(this[index] == val) return index;
        }   
        return -1;
    }
}

function getPageType()
{
    var pg = "index";
    var s = document.location.href;
    if(s.indexOf(".aspx") > -1)
    {
        var sAr = s.split('/');
        var pgAr = sAr[sAr.length - 1].split('.');
        pg = pgAr[0];
    }
    return pg.toLowerCase();
}

function Identifiers(i)
{
    $("#statusHolder").siblings("input[@type=hidden]").each(function(i)
	{
		if($(this).attr("id").indexOf("MyUCRID")> -1){this.MyUCRID = $(this).val();}
		if($(this).attr("id").indexOf("HSAccountID")> -1){this.HSAccountID = $(this).val();}		
	    if($(this).attr("id").indexOf("hidCPID")> -1){this.CPID = $(this).val();}		
	});
}

function GetIdentifiers()
{
	var identifiers = new Object();
	$("input[@type=hidden]").each(function(i)
	{
		if($(this).attr("id").indexOf("MyUCRID")> -1){identifiers["MyUCRID"] = $(this).val();}
		if($(this).attr("id").indexOf("HSAccountID")> -1){identifiers["HSAccountID"] = $(this).val();}		
	    if($(this).attr("id").indexOf("hidCPID")> -1){identifiers["CPID"] = $(this).val();}		
	});
	
	return identifiers;
}

function getCPID()
{
	var cpid;
	$("#wrapper input[@type=hidden]").each(function(i)
	{	
	    if($(this).attr("id").indexOf("hidCPID")> -1){cpid = $(this).val();}		
	});
	return cpid;
}

function loadingComplete()
{
    
	//$("#MyUCR_pageLoading").fadeOut(1000);
	//var statusDiv = $UCR('MyUCR_pageLoading');
    //statusDiv.style.visibility = 'hidden';
    var completePanel = $UCR('completed');
    if(completePanel != null)
    {
        //completePanel.onclick=toggleAccordian;
        completePanel.className = 'accordionTabTitleBarDowncompleted';
    }
    var pgArr = location.href.split('/');
    var pageArr = pgArr[pgArr.length -1].split('.');
    if(pageArr[0] == "landing")
    {
        var curPoll = $UCR('UserPoll1_hidCurrentPollID');
        if(curPoll != undefined){LoadPolls();}
    }
    
    if(pageArr[0] == "messaging")
    {
        GetSirOrient();
    }
    
}

function addLoadEvent(func)
{
    var oldonload = window.onload;
    if(typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function() {
            if(oldonload){
                oldonload();
            }
            func();
        }
    }
}


//leap year
//if (year/4 == parseInt(year/4))
function SetMonthDays(elem)
{
//figure out which drop down was selected by the ID of the element passed in
    var objPre = "ddlDOB";
    if($(elem).attr("id").indexOf("CPID") > -1)
    	objPre = "ddlCPID";

    var d = new Date();
    var m = $UCR(objPre + 'Month').options[$UCR(objPre + 'Month').selectedIndex].value;
    var y = $UCR(objPre + 'Year').options[$UCR(objPre + 'Year').selectedIndex].value;
    if(m == ""){m = d.getMonth(); }
    if(y == ""){y = d.getYear();}
    var numDays = daysInMonth(m,y);
    var ddlDOBDay = $UCR(objPre + 'Day');
    
    
    ddlDOBDay.options.length = 0;
    ddlDOBDay.options[ddlDOBDay.options.length] = new Option("Day","");
    ddlDOBDay.options[ddlDOBDay.options.length] = new Option("---","");
    var dNum;
    for(var i = 1; i<= numDays; i++)
    {   
        if( i < 10)
        {
            dNum = "0" + i.toString();
        }
        else
        {
            dNum = i.toString();
        }     
        ddlDOBDay.options[ddlDOBDay.options.length] = new Option(i,dNum);
    }    
}

function daysInMonth(month, year)
{
	//gets how many days in the selected month
    if(typeof(month == 'string') && month.length == 2)
    {
        if(month.indexOf('0', 0) == 0 && month != "0")
        {
            var tMon = parseInt(month.charAt(1));
            var m = tMon;
        }
        else
        {
            var m = parseInt(month);
        }
    }
    else
    {
        var m = month;
    }
    array_month = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if(year % 4 == 0 && !month % 100 == 0 || year % 400 == 0)
        array_month[1] = 29;
    if(m > 0)
    {
        return array_month[m-1];
    }
    else
    {return 31;}
}

/* common functions */
//set the global error message color
//var _errColor = "#f97b43";
var _errColor = "red";
var _successColor = "blue";

//shows the error message from the above functions
function eMsg(msg,sColor,elem)
{
    HideErrorElements();
    var div = $UCR(elem);
    if(div != null)
    {
        var cl = div.className;        
        div.style.color=sColor;
        div.innerHTML = msg; 
               
        div.style.display='block';
        Fat.fade_element(div.id)
        setTimeout("TransparentDiv('"+div.id+"')",2000);    
    }
}

function TransparentDiv(div)
{
    $UCR(div).style.backgroundColor='transparent';
}

function $UCR(id)
{
	//this needs to get phased out and started to use jQuery
	// returns document element object
    // get the "application name" set by the master page
    var idPrefix = document.getElementById('MyUCR_hidAppName').value;
    if(typeof id == 'string')
    {
        //need to check if the id has been renamed by the master page
        var chk = idPrefix + "_" + id;
        if(document.getElementById(chk) != null)
        {
            return document.getElementById(chk);
        }
        else
        {
            return document.getElementById(id);
        }
    }
    else
    {
        return id;
    }
}

//hides all error elements
function HideErrorElements()
{
    var errP = document.getElementsByTagName("p");
    for(var i=0;i<errP.length;i++)
    {
        if(errP[i].className == 'err')
        {
            errP[i].style.display = 'none';
        }
    }
}
// clears the innerHTML of the passed in div
function ClearDiv(d)
{
    d.innerHTML = '';
}

//set a timerID to handle abnormally long requests
var timeoutID;
// returns the XMLHttpObject supported by the browser
var req;
function createXMLHttpRequest()
{
    try{return new ActiveXObject("Msxml2.XMLHTTP");} catch (e){}
    try{return new ActiveXObject("Microsoft.XMLHTTP");} catch (e){}
    try{return new XMLHttpRequest();} catch (e){}
    alert("Your browser does not support this application.");
    return null;
}

function ajaxCall(method, url, funcToRun)
{
    if(method == null){method = "GET";}
    req = false;    
    req = createXMLHttpRequest();    
    if(req != null)
    {
        req.onreadystatechange = function()
        {
            if(req.readyState == 4)
            {
                clearTimeout(timeoutID);
                if(req.status == 200)
                {                        
                    eval(funcToRun);
                }
                else
                {
                    showError(req.statusText);
                }
            }
        }
        
        //method="POST";
        //req.open(method, url +"&x="+Math.random(), true);
        req.open(method, url, true);
        if(window.XMLHttpRequest){req.send(null);}
        else if(window.ActiveXObject){req.send();}
    }
}

function timesUp()
{
    req.abort();
    eMsg("We're sorry. There was an error contacting the server. Please try again.", _errColor, "ajaxErr");
}

//show the error from ajaxCall
function showError(msg, elem)
{
    if(document.getElementById)
    {
        if(elem != null){$UCR(elem).innerHTML = msg;}
        else{alert(msg);}
    }
    else{alert(msg);}
}

function addEvent(elementObject, eventName, functionObject)
{
    //hard core way to add an event. use jQuery for future actions
    if(document.addEventListener)
    {
        elementObject.addEventListener(eventName, function(evt){functionObject(elementObject,evt)},false);
        return true;
    }
    else if (document.attachEvent)
    {
        elementObject.attachEvent("on" + eventName, function(){functionObject(elementObject);})
        return true;
    }
}
function removeEvent( obj, type, fn ) { 
  if ( obj.detachEvent ) { 
    obj.detachEvent( 'on'+type, obj[type+fn] ); 
    obj[type+fn] = null; 
  } else 
    obj.removeEventListener( type, fn, false ); 
} 

//binds focus and blur handlers for various form elements created by js
var activeElement = null;
function blurHandler(evt){activeElement=null;}
function focusHandler(evt)
{
    var e = evt ? evt : window.event;
    if(!e) return;
    if(e.target)
    {
        activeElement = e.target;
    }
    else if(e.srcElement)
    {
        activeElement = e.srcElement;
    }
}