// toggle the display state of the child menu group.
function menuclick(func,divno,url,rightcolumn)
{
     // update required blocks per menu click.
//     quote();

     switch (func)
          {
          // if the item clicked is a menu check for collpase or expand.
          case 'm':
               {
               var object=document.getElementById('menuitems'+divno);
     
               // also need to call the server and tell it to set the state of the
               // display style in the session for this child group for when the
               // page is reloaded.
               if (object.style.display=='')
                    {
                    object.style.display='none';
                    ret=serverFunction('modules.php?mod=ajax&req=setdisplay&val=none&div='+divno);
                    }
               else
                    {
                    object.style.display='';
                    ret=serverFunction('modules.php?mod=ajax&req=setdisplay&val=block&div='+divno);
                    }
               // a module may be triggered by clicking on a menu parent.
               if (url!='')
                    displayText('maincontent','modules.php?mod='+url);
               break;
               }
               
          case 'u':
               {
               switch(url)
                    {
                    case 'homepage':
                         {
                         displayText('maincontent','modules.php?mod=ajax&req=homepage');
                         break;
                         }
/*
                    case 'sitepages':
                         {
                         displayText('maincontent','modules.php?mod='+url+'&req='+divno);
                         break;
                         }
*/
                    case 'theme_builder':
                         displayText('maincontent','modules.php?mod='+url);
                         makeColorSelectors(this);
                         break;
					case 'renew':
                         displayText('maincontent','modules.php?mod=register&req='+divno);
                         break;
                    default:
                         displayText('maincontent','modules.php?mod='+url+'&req='+divno);
                    }
     
//               announcements();
//               callCalendar();
//               contacts();
               break; 
               }
     
          case 'sp':
               {
               displayText('maincontent','modules.php?mod=sitepages&req=displaypage&pageid='+url);
               break;
               }
          case 'spcat':
               {
               displayText('maincontent','modules.php?mod=sitepages&req=displaycat&catid='+url);
               break;
               }
          }
}


// *************************** BLOCK FUNCTIONS ***************************
// these functions display the blocks in their respective theme blocks.
// ***********************************************************************


function footer()
{
     var url='modules.php?mod=ajax&req=footer';
     displayText('footerinfo',url);
}


function displayText(id, url) {
	var element = document.getElementById(id);

	if (!element) {
		alert("Bad id " + id +
			"passed to clientSideInclude." +
			"You need a div or span element " +
			"with this id in your page.");
		return;
	}

  element.innerHTML = serverFunction(url);
}

function isEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function serverFunction(url)
{
	var req = false;
	// For Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		}
	} else if (window.ActiveXObject) {
		// For Internet Explorer on Windows
		try {
			   req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			req = false;
			}
		}
	}

	if (req) {
	// Synchronous request, wait till we have it all

	req.open('GET', url, false);
	req.send(null);

	if(req.responseText) {
	   if(req.responseText.substring(0,7) == 'debug!!') {
	       alert(req.responseText.substring(7));
	   }
	}

	return(req.responseText);
	} else {
		alert("Sorry, your browser does not support " +
			"XMLHTTPRequest objects. This page requires " +
			"Internet Explorer 5 or better for Windows, " +
			"or Firefox for any system, or Safari. Other " +
			"compatible browsers may also exist.");
	}
}


/*************** START COOKIE FUNCTIONS ***************/

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 ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function eraseCookie(name)
{
  createCookie(name, "", -1);
}

/*************** END COOKIE FUNCTIONS ***************/

function isDate(dateStr) 
{
     var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
     var matchArray = dateStr.match(datePat); // is the format ok?

// alert(matchArray);    

     if (matchArray == null) {          
          return false;
     }
     month = matchArray[1]; // p@rse date into variables
     day = matchArray[3];
     year = matchArray[5];
// alert('month:'+month+' day:'+day+' year:'+year);
// return true;
     
     if (month < 1 || month > 12) { // check month range
          return false;
     }
     
     if (day < 1 || day > 31) {
          return false;
     }
     
     if ((month==4 || month==6 || month==9 || month==11) && day==31) {
          return false;
     }
     
     if (month == 2) { // check for february 29th
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
          if (day > 29 || (day==29 && !isleap)) {
               return false;
          }
     }
     return true; // date is valid
}

function leftTrim(sString)
{
     while (sString.substring(0,1) == ' ')
          {
          sString = sString.substring(1, sString.length);
          }
     return sString;
}

var chkDot = true;
function validEmail(eAddr)
{
    var result = false;
    var ndxAt = ndxDot =  0;

    ndxAt  = eAddr.indexOf("@");
    ndxDot = eAddr.indexOf(".") ;
    ndxDot = eAddr.lastIndexOf(".") ;

    if ((ndxDot < 0) || (ndxAt < 0))
        result=false; //alert("You must enter a valid email address.");
    else
        result=true;
    return result;
}

// puts page into any div or span section.
function displaydiv(div,page)
{
     var url='modules.php?mod=ajax&req=anypage&page='+page;
     displayText(div,url);
}

// display the main menu in the menu span area.
function mainmenu()
{
     var url='modules.php?mod=ajax&req=menu';
     displayText('mainmenu',url);
}

/*function quote()
{
     var url='modules.php?mod=ajax&req=quote';
     displayText('quoteblock',url);
}

function announcements()
{
     var url='modules.php?mod=ajax&req=announcements';
     displayText('annblock',url);
}
*/
function contacts()
{
     var url='modules.php?mod=ajax&req=contacts';
     displayText('contactsblock',url);
}

function stopDocumentUpload(success,path,tmp)
{
// alert(success+"file:"+path+"\ntmp:"+tmp);
//       var result = '';
      if (success == 1){
         alert("Your file has been uploaded.");
      }
      else if (success == 2){
	alert("Please enter a file to upload.");
      }
      else
      {
         alert("Your file upload resulted in error.\nPlease contact Big Sky Penguin.");
      }

}

// set the number of days for a dropdown based on the days in the month field.
function setDays(formname,field)
{
	var object = document.getElementById(field);
	var form = document.getElementById(formname);

        var year = form.bday_year.value;
	var month = form.bday_month.value;

        // if got here via year onchange and month has not been selected return.
        // the onchange is in the year in case they go back and select a different year after selecting the other fields.
        // in such a case the number of days needs to be adjusted correctly.
        if (month == 0)
          return false;

	// clear out the current options.
	object.options.length = 0;
     
        month = month - 1;  // minus 1 to to get to 0 - 11 for Jan - Dec.

        var days = daysInMonth(month,year);

// alert('month:'+month+' year:'+year+' days:'+days);
    
        for (x=1; x<days+1; x++)
               object.options[x-1] = new Option(x,x);
}

function daysInMonth(month,year) 
{
     return 32 - new Date(year, month, 32).getDate();
}

function getAge(year,month,day)
{
     // make sure real values have been passed.
     if (year == 0 || month == 0 || day == 0)
          return -1;

     /* the minumum age you want to allow in */
     var minutes = 1000*60;
     var hours = minutes*60;
     var days = hours*24;
     var years = days*365;

     var theirDate = new Date(year, month, day);
     var today = new Date;
     var span = today.getTime() - theirDate.getTime();

     var age = parseInt(span / years);
     return age;
}

Date.prototype.getAgeInYears = function() 
{
     var now = new Date(); 
     var d = new Date(this.getTime()); 
     d.setYear(now.getFullYear()); 
     return d > now ? now.getFullYear() - this.getFullYear() - 1 : now.getFullYear() - this.getFullYear();
}

function limitText(limitField, limitCount, limitNum) 
{
	if (limitField.value.length > limitNum) 
        {
        limitField.value = limitField.value.substring(0, limitNum);
	   } 
        else 
        {
		limitCount.value = limitNum - limitField.value.length;
	   }
}




