var hasChanged = 0;
var FCKEditors = '';
var FCKEditorSubmitting = '';

//Temporarily Turned Off until a better solution is found
//window.onbeforeunload=checkChanged; 

function checkChanged()
{
    if (hasChanged = 0) {
        if (FCKEditorSubmitting == '' && checkFCKEditorsDirty()) {
        }
    }
            
    if (hasChanged != 0 || (FCKEditorSubmitting == '' && checkFCKEditorsDirty())) {
	    var warning = "If you navigate away from this page, you will lose your changes!";
	    return warning;
	}
	else if (FCKEditorSubmitting != '' && checkFCKEditorsDirty()) {
	    alert('By clicking update, you are only updating this Text Area.  You have also modified other text areas that must be updated seperately');
	}
	//alert(warning);
}

function addFCKEditor(editorname) {
    if (FCKEditors != '')
        FCKEditors = FCKEditors + ',';
    FCKEditors = FCKEditors + editorname;
}

function checkFCKEditorsDirty() {
    if (FCKEditors == '') {
        return false;
    }
    else {
        var FCKEditorsSplit = FCKEditors.split(',');
        var num;
        var ret = false;
        for (num = 0; num < FCKEditorsSplit.length; num++) {
            //Ignore the control that is submitting
            if (FCKEditorsSplit[num] != FCKEditorSubmitting) {
                //alert(FCKEditorsSplit[num]);
                var oEditor = FCKeditorAPI.GetInstance(FCKEditorsSplit[num]) ;
                //alert(oEditor);
                //alert(oEditor.IsDirty());
                ret = ret || oEditor.IsDirty();
            }
        }
        //alert('returning: ' + ret);
        return ret;
    }
}

function openAdminWindow(postbackcode)
{
	window.showModalDialog('./popup.aspx?control=admin','Administration','dialogHeight: 400px; dialogWidth: 500px; center: Yes; help: No; resizable: Yes; status: No;');
	//__doPostBack('', '');
	eval(postbackcode);
}

function openAdminWindowTab(tab, postbackcode)
{
	window.showModalDialog('./popup.aspx?control=admin&tab=' + tab,'Administration','dialogHeight: 400px; dialogWidth: 500px; center: Yes; help: No; resizable: Yes; status: No;');
	//__doPostBack('', '');
	eval(postbackcode);
}

function openSecurityWindow(id, type, postbackcode)
{
	window.showModalDialog('./popup.aspx?control=security&id='+id+'&type='+type,'Security','dialogHeight: 400px; dialogWidth: 550px; center: Yes; help: No; resizable: Yes; status: No;');
	//__doPostBack('', '');
	eval(postbackcode);
}

function getPosition(e){
     var left = 0;
     var top  = 0;

     while (e.offsetParent){
          left += e.offsetLeft;
          top  += e.offsetTop;
          e     = e.offsetParent;
     }

     left += e.offsetLeft;
     top  += e.offsetTop;

     return {x:left, y:top};
}

function getWindowHeight(){
    var myHeight = 0;
    if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
    } else if( document.body && (document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function getWindowWidth(){
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    }
    return myWidth;
}

function getObjTop(e){
     var top  = 0;

     while (e.offsetParent){
          top  += e.offsetTop;
          e     = e.offsetParent;
     }
     
     top  += e.offsetTop;

     return top;
}

function getObjLeft(e) {
    var left = 0;
    
    while(e.offsetLeft){
        left += e.offsetLeft;
        e = e.offsetParent;
    }
    
    left += e.offsetLeft;
    
    return left;
}

var objToResize = '';

function maximizeObjHeight(id, add){
    //alert(id);
    //alert('Old Height: ' + document.getElementById(id).style.height);
    var curHeight = getObjTop(document.getElementById(id));
    //alert('Current Location: ' + curHeight);
    
    var winHeight = getWindowHeight();
    
    //alert('Window Height: ' + winHeight);
    
    document.getElementById(id).style.height = (winHeight - curHeight - 25) + 'px'
    
    //alert('New Height: ' + document.getElementById(id).style.height);
    if (add == true){
        if (objToResize != '')
            objToResize = objToResize + ',';
        objToResize = objToResize + id;
        addEvent(window, 'resize', resizeWindow);
    }
}

var objToResizeWidth = '';

function maximizeObjWidth(id, add){
    //alert(id);
    //alert('Old Height: ' + document.getElementById(id).style.height);
    var curWidth = getObjLeft(document.getElementById(id));
    //alert('Current Location: ' + curHeight);
    
    var winWidth = getWindowWidth();
    
    //alert('Window Height: ' + winHeight);
    
    document.getElementById(id).style.width = (winWidth - curWidth - 25) + 'px'
    
    //alert('New Height: ' + document.getElementById(id).style.height);
    if (add == true){
        if (objToResizeWidth != '')
            objToResizeWidth = objToResizeWidth + ',';
        objToResizeWidth = objToResizeWidth + id;
        addEvent(window, 'resize', resizeWindowWidth);
    }
}

function resizeWindow(){
    //alert(objToResize);
    var objSplit = objToResize.split(',');
    var num;
    for (num = 0; num < objSplit.length; num++) {
        //alert(objSplit[num]);
        maximizeObjHeight(objSplit[num], false);
    }    
}

function resizeWindowWidth(){
    //alert(objToResize);
    var objSplit = objToResizeWidth.split(',');
    var num;
    for (num = 0; num < objSplit.length; num++) {
        //alert(objSplit[num]);
        maximizeObjHeightWidth(objSplit[num], false);
    }    
}


//Used by MenuBar.ascx
function setMenuWidth(add){
    var myWidth = getWindowWidth();
    //alert(myWidth);
    if (myWidth > 1900){
        myWidth = 1900;
    }
    if (myWidth > 0){
        //alert('resizing');
        document.getElementById('tdLinks').style.width = (myWidth - 10) + 'px';
        //alert('resized 1');
        document.getElementById('tblLinks').style.width = (myWidth - 10) + 'px';
        //alert('resized');
    }
    if (add == true){
        //alert('adding');
        addEvent(window, 'resize', resizeMenu);
        //alert('added');
    }
}

function resizeMenu(){
    setMenuWidth(false);
}

function setCalDateOnBlur(textboxID, calID) {
   //alert('blured');
   var textbox = document.getElementById(textboxID);
   //var cal = document.getElementById(calID);
   var datestr = formatdate(textbox.value);

   if ( datestr == "NaN" )
   {
      //do nothing
      
      //textbox.select();
      //alert("Please enter a valid date for " + textbox.name );
      //textbox.focus();
      //return(false);
   }
   else
   {
        textbox.value = datestr;
        //alert('setting to: ' + datestr + ' date: ' + new Date(datestr));
        eval(calID + '.setDate(new Date(datestr), new Date(datestr))');
   }

   //return(true);
}


//found on http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20097456.html
function formatdate(datestr)
{
   var curdate = new Date();
   var curyear = (curdate.getYear()%100)+1980;  // to project 20 years forward and 80 years back (for birthdays, etc)

   datestr = datestr.split("-").join("/").split(".").join("/").split(" ").join("");
   if ( datestr == "" )
   {
      return(datestr);
   }

   // split the datestr into a 3 part array (mm/dd/yy)
   var mmddyy = datestr.split("/");
   if ( mmddyy.length != 3 )
   {
      return("NaN");
   }

   // add leading zeros to month and day
   if ( mmddyy[0].length == 1 )
   {
      mmddyy[0] = "0" + mmddyy[0];
   }

   if ( mmddyy[1].length == 1 )
   {
      mmddyy[1] = "0" + mmddyy[1];
   }

   // convert the year to a four digit year
   if ( mmddyy[2].length == 2 )
   {
      //mmddyy[2] = ( parseFloat(mmddyy[2]) > 49 ) ? '19'+mmddyy[2] : '20'+mmddyy[2];

      if ( Math.abs(curyear-parseFloat('19'+mmddyy[2])) <= Math.abs(curyear-parseFloat('20'+mmddyy[2])) )
      {
         mmddyy[2] = '19'+mmddyy[2];
      }
      else
      {
         mmddyy[2] = '20'+mmddyy[2];
      }
   }

   // rebuild the datestr from the formatted array, and check to see if it contains a valid date
   var tmpdate = new Date( datestr=mmddyy.join("/") );
   if ( tmpdate == "NaN" || tmpdate == "Invalid" )
   {
      return("NaN");
   }

   if ( parseFloat(mmddyy[0]) == tmpdate.getMonth()+1  &&  parseFloat(mmddyy[1]) == tmpdate.getDate()  &&  parseFloat(mmddyy[2]) > 1900  &&  parseFloat(mmddyy[2]) < 2100 )
   {
      return(datestr);
   }

   return("NaN");
}

function doSearch(searchbox)
{
    //alert('searching...');
    var query = document.getElementById(searchbox).value
    //alert(query);
    document.location = './Search.aspx?query=' + query;
    return false;
}

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}