function SelectTab(tabId, divId)
{
   try
   {
      if (navigator.appName == "Microsoft Internet Explorer")
      {
         $('NewsTab').className = 'Tab TabOff'; $('NewsDiv').setStyle({ 'display': 'none' });
         $('EventsTab').className = 'Tab TabOff'; $('EventsDiv').setStyle({ 'display': 'none' });
         $('LinksTab').className = 'Tab TabOff'; $('LinksDiv').setStyle({ 'display': 'none' });
         $(tabId).className = 'Tab TabOn';
      }
      else
      {
         $('NewsTab').setAttribute('class', 'Tab TabOff'); $('NewsDiv').setStyle({ 'display': 'none' });
         $('EventsTab').setAttribute('class', 'Tab TabOff'); $('EventsDiv').setStyle({ 'display': 'none' });
         $('LinksTab').setAttribute('class', 'Tab TabOff'); $('LinksDiv').setStyle({ 'display': 'none' });
         $(tabId).setAttribute("class", 'Tab TabOn');
      }

      LoadData(divId);

   }
   catch (e) { }
}

function LoadData(divId)
{
   if (divId == 'EventsDiv' && $('EventsDiv').innerHTML == '')
   {
      $('EventsDiv').setStyle({ 'paddingTop': '10px' });

      var parentDiv = new Element('div');
      parentDiv.setStyle({ 'borderBottom': '1px solid #ccc', 'height': '18px', 'marginLeft': '5px', 'marginRight': '5px' });

      var textDiv = new Element('div');
      textDiv.setStyle({ 'float':'left', 'color': '#333', 'fontSize': '12px', 'fontWeight': 'bold', 'width':'110px' });
      textDiv.update('Upcoming Events');
      parentDiv.insert(textDiv);
      var linkDiv = new Element('div');
      linkDiv.setStyle({ 'float': 'right' });
      var anchor = new Element('a', { 'href': '/parents/school-calendar.aspx' });
      var img = new Element('img', { 'src': '/images/common/small_calendar.gif', 'width': '16', 'height': '16', 'alt': 'View School Calender' });
      img.setStyle({ 'position': 'relative', 'paddingBottom': '4px' });
      anchor.insert(img);
      linkDiv.insert(anchor);

      parentDiv.insert(linkDiv);

      $('EventsDiv').insert(parentDiv);

      var cal = new Calendar('countrydaylargo.com_qb6f8j8jnt3fr483kv4783sndo%40group.calendar.google.com');
      cal.onsuccess = function (c) { LoadEvents(c, 'EventsDiv', 12); }
      cal.loadFeed();
   }

   if (divId == 'NewsDiv' && $('NewsDiv').innerHTML == '')
   {
      new Ajax.Updater('NewsDiv', '/HPNews.aspx', { evalScripts: true });
   }


   $(divId).show();

}

function on()
{
   this.addClassName('menu_item_hover');
   this.setStyle({ 'color': 'white' });
}
function off()
{
   this.removeClassName('menu_item_hover');
   this.setStyle({ 'color': '#003399' });
}


function sfHover()
{
   var topMenuItems = $('TopMenuBar').select('ul li a[id]');

   for (var i = 0; i < topMenuItems.length; i++)
   {
      topMenuItems[i].onmouseover = function ()
      {
         this.addClassName('menu_item_hover');
         this.setStyle({ 'color': 'white' });
      }

      topMenuItems[i].onmouseout = function ()
      {
         this.removeClassName('menu_item_hover');
         this.setStyle({ 'color': '#003399' });
      }

      var childMenuItems = topMenuItems[i].up().select('ul li a');

      childMenuItems.each(function (a)
      {
         a.observe('mouseover', on.bind(topMenuItems[i]));
         a.observe('mouseout', off.bind(topMenuItems[i]));
      });
   }
}

function PrintThis( title, text, height, width, printAreaDivName ) 
{
    var generator = window.open('', 'name', 'height=' + height + ',width=' + width + ',scrollbars=1,resizable=1');

	generator.document.write('<html><head><title>Country Day School</title>');
	generator.document.write('<link rel="stylesheet" href="/styles/CountryDay_v2.css">');
	generator.document.write('</head><body class="PrintBody" LEFTMARGIN="10" TOPMARGIN="10" MARGINWIDTH="0" MARGINHEIGHT="0" style="margin-right:10px;" onload="window.print();">');
	
	if ( title != null && title.toString().length > 0 )
	    generator.document.write('<div class="BodyCopyHeader" style="padding-left:10px; padding-top:10px;">' + title + '</div>');
	
	if ( text != null && text.toString().length > 0 )
	    generator.document.write('<div class="BodyCopy" style="padding-left:10px;">' + text + '</div>');
	
	var printArea = document.getElementById( printAreaDivName );

	generator.document.write('<div style="padding-left:10px;">');

	generator.document.write( printArea.innerHTML.replace(/<a.+>(.*?)<\/a>/g, "$1").replace(/add/g, "") );
	generator.document.write('</div></body></html>');
	generator.document.close();
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}


function setCookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}



function getAnchorPosition(anchorname) { var useWindow = false; var coordinates = new Object(); var x = 0, y = 0; var use_gebi = false, use_css = false, use_layers = false; if (document.getElementById) { use_gebi = true; } else if (document.all) { use_css = true; } else if (document.layers) { use_layers = true; } if (use_gebi && document.all) { x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]); y = AnchorPosition_getPageOffsetTop(document.all[anchorname]); } else if (use_gebi) { var o = document.getElementById(anchorname); x = AnchorPosition_getPageOffsetLeft(o); y = AnchorPosition_getPageOffsetTop(o); } else if (use_css) { x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]); y = AnchorPosition_getPageOffsetTop(document.all[anchorname]); } else if (use_layers) { var found = 0; for (var i = 0; i < document.anchors.length; i++) { if (document.anchors[i].name == anchorname) { found = 1; break; } } if (found == 0) { coordinates.x = 0; coordinates.y = 0; return coordinates; } x = document.anchors[i].x; y = document.anchors[i].y; } else { coordinates.x = 0; coordinates.y = 0; return coordinates; } coordinates.x = x; coordinates.y = y; return coordinates; }
function getAnchorWindowPosition(anchorname) { var coordinates = getAnchorPosition(anchorname); var x = 0; var y = 0; if (document.getElementById) { if (isNaN(window.screenX)) { x = coordinates.x - document.body.scrollLeft + window.screenLeft; y = coordinates.y - document.body.scrollTop + window.screenTop; } else { x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset; y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset; } } else if (document.all) { x = coordinates.x - document.body.scrollLeft + window.screenLeft; y = coordinates.y - document.body.scrollTop + window.screenTop; } else if (document.layers) { x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset; y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset; } coordinates.x = x; coordinates.y = y; return coordinates; }
function AnchorPosition_getPageOffsetLeft(el) { var ol = el.offsetLeft; while ((el = el.offsetParent) != null) { ol += el.offsetLeft; } return ol; }
function AnchorPosition_getWindowOffsetLeft(el) { return AnchorPosition_getPageOffsetLeft(el) - document.body.scrollLeft; }
function AnchorPosition_getPageOffsetTop(el) { var ot = el.offsetTop; while ((el = el.offsetParent) != null) { ot += el.offsetTop; } return ot; }
function AnchorPosition_getWindowOffsetTop(el) { return AnchorPosition_getPageOffsetTop(el) - document.body.scrollTop; }

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
        while (1)
    {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
        while (1)
    {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}



function trim(stringToTrim)
{
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim)
{
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim)
{
    return stringToTrim.replace(/\s+$/, "");
}


// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
// Maximum no of digits in an america phone no.
var maxDigitsInIPhoneNumber = 13;
//US Area Code
var AreaCode=new Array(205,251,659,256,334,907,403,780,264,268,520,928,480,602,623,501,479,870,242,246,441,250,604,778,284,341,442,628,657,669,747,752,764,951,209,559,408,831,510,213,310,424,323,562,707,369,627,530,714,949,626,909,916,760,619,858,935,818,415,925,661,805,650,600,809,345,670,211,720,970,303,719,203,475,860,959,302,411,202,767,911,239,386,689,754,941,954,561,407,727,352,904,850,786,863,305,321,813,470,478,770,678,404,706,912,229,710,473,671,808,208,312,773,630,847,708,815,224,331,464,872,217,618,309,260,317,219,765,812,563,641,515,319,712,876,620,785,913,316,270,859,606,502,225,337,985,504,318,318,204,227,240,443,667,410,301,339,351,774,781,857,978,508,617,413,231,269,989,734,517,313,810,248,278,586,679,947,906,616,320,612,763,952,218,507,651,228,601,557,573,636,660,975,314,816,417,664 ,406,402,308,775,702,506,603,551,848,862,732,908,201,973,609,856,505,575,585,845,917,516,212,646,315,518,347 ,718,607,914,631,716,709,252,336,828,910,980,984,919,704,701,283,380,567,216,614,937,330,234,440,419,740,513 ,580,918,405,905,289,647,705,807,613,519,416,503,541,971,445,610,835,878,484,717,570,412,215,267,814,724,902,787,939,438,450,819,418,514,401,306,803,843,864,605,869,758,784,731,865,931,423,615,901,325,361,430,432,469,682,737,979,214,972,254,940,713,281,832,956,817,806,903,210,830,409,936,512,915,868,649,340,385,435,801,802,276,434,540,571,757,703,804,509,206,425,253,360,564,304,262,920,414,715,608,307,867)

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function checkInternationalPhone(strPhone){
strPhone=trim(strPhone)
if(strPhone.indexOf("00")==0) strPhone=strPhone.substring(2)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("+")==0) strPhone=strPhone.substring(1)
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf(")")==-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
if(strPhone.length>10){var CCode=s.substring(0,s.length-10);}
else{CCode="";}
if(strPhone.length>7){var NPA=s.substring(s.length-10,s.length-7);}
else{NPA=""}
var NEC=s.substring(s.length-7,s.length-4)
if(CCode!="" && CCode!=null){
	if(CCode!="1" && CCode!="011" && CCode!="001") return false
	}
if(NPA!=""){
  if(checkAreaCode(NPA)==false){ //Checking area code is vaid or not
  	return false
	}
}
else{return false}
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber  &&  s.length <= maxDigitsInIPhoneNumber );
}

//Checking area code is vaid or not
function checkAreaCode(val){
	var res=false;
	for (var i=0; i<AreaCode.length;i++){
		if(AreaCode[i]==val) res=true;
	}
	return res
}



function isDate(strValue)
{
    /************************************************
    DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

    PARAMETERS:
    strValue - String to be tested for validity

    RETURNS:
    True if valid, otherwise false.

    REMARKS:
    Avoids some of the limitations of the Date.parse()
    method such as the date separator character.
    *************************************************/
    var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

    //check to see if in correct format
    if (!objRegExp.test(strValue))
        return false; //doesn't match pattern, bad date
    else
    {
        var strSeparator = strValue.substring(2, 3)
        var arrayDate = strValue.split(strSeparator);
        //create a lookup for months not equal to Feb.
        var arrayLookup = { '01': 31, '03': 31,
            '04': 30, '05': 31,
            '06': 30, '07': 31,
            '08': 31, '09': 30,
            '10': 31, '11': 30, '12': 31
        }
        var intDay = parseInt(arrayDate[1], 10);

        //check if month value and day value agree
        if (arrayLookup[arrayDate[0]] != null)
        {
            if (intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
                return true; //found in lookup table, good date
        }

        //check for February (bugfix 20050322)
        //bugfix  for parseInt kevin
        //bugfix  biss year  O.Jp Voutat
        var intMonth = parseInt(arrayDate[0], 10);
        if (intMonth == 2)
        {
            var intYear = parseInt(arrayDate[2]);
            if (intDay > 0 && intDay < 29)
            {
                return true;
            }
            else if (intDay == 29)
            {
                if ((intYear % 4 == 0) && (intYear % 100 != 0) ||
             (intYear % 400 == 0))
                {
                    // year div by 4 and ((not div by 100) or div by 400) ->ok
                    return true;
                }
            }
        }
    }
    return false; //any other values, bad date
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function () { if (oldonload) { oldonload(); } func(); };
    }
}

