
function checkKey(evt){
	var t = 0;
}

function send(){
	f.action='proc_add_event.php';

	errMsg = new String("");
	selDays = new String("");
	var tblBase = get('MonthCal').childNodes(1);
//alert(get('MonthCal').childNodes(1).tagName);
	//determine which dates have been selected
	for (var i = 1; i<tblBase.childNodes.length;i++ ){
			
			var tds = tblBase.childNodes(i).childNodes;
			
			for (var x = 0;x< tds.length;x++){
				var node = tds(x).firstChild;
				
				var blnIsSel = (node.style.backgroundColor != '')
//alert('sel ' + blnIsSel);
				if ((blnIsSel) && (node.innerText != '')){
					selDays += node.innerText + '|';
				}
			}
	}
	
	selDays = selDays.substring(0,selDays.length-1);

	//validation
	if (selDays == ""){
		errMsg += "At least one calendar date must be selected.\nYou may select-unselect multiple days by clicking the table header,\nor individual days by clicking the day number.\n";
	} else {
		 get('selDays').value = selDays;
	}
	
	if (f.event_name.value ==""){
		errMsg += "Event Name cannot be empty\n";
	}

	if (f.location.value ==""){
		errMsg += "Location cannot be empty\n" ;
	}

	if (f.event_desc.value ==""){
		errMsg += "Event Description cannot be empty\n" ;
	}

	if (f.event_type.value ==""){
		errMsg += "Event Type must be specified\n" ;
	}
	
	if (f.activity.value ==""){
		errMsg += "Activity performed on these dates must be stated\n" ;
	}

	if (f.hour.value ==""){
		errMsg += "Time of event must be specified\n" ;
	}

	if ((f.hour.value !="" || f.min.value !="") && (f.hour.value =="" || f.min.value =="")){
		errMsg += "If either Hour or Minute of Time input is filled in, then BOTH must be filled in\n" ;
	}
	
	if (f.hour.value !=""  && (f.hour.value < 1 || f.hour.value > 12)){
		errMsg += "Hour must be between 1 and 12\n" ;
	}

	if (f.min.value !=""  && (f.min.value < 0 || f.min.value > 59)){
		errMsg += "Minute must be between 0 and 59\n" ;
	}

	if ((f.e_hour.value !="" || f.e_min.value !="") && (f.e_hour.value =="" || f.e_min.value =="")){
		errMsg += "If either Hour or Minute of End Time input is filled in, then BOTH must be filled in\n" ;
	}
	
	if (f.e_hour.value !=""  && (f.e_hour.value < 1 || f.e_hour.value > 12)){
		errMsg += "Hour of End Time must be between 1 and 12\n" ;
	}

	if (f.e_min.value !=""  && (f.e_min.value < 0 || f.e_min.value > 59)){
		errMsg += "Minute of End Time must be between 0 and 59\n" ;
	}

	if ((f.e_hour.value !="" || f.e_min.value !="") && (f.hour.value =="" || f.min.value =="")){
		errMsg += "An End Time cannot be entered without a valid Start Time\n" ;
	}

	if ((f.e_hour.value !="" || f.e_min.value !="") && (f.hour.value !="" || f.min.value !="")){
	   
		var pattern = /\b0+/;
   			
		s_hr=new String(f.hour.value);
		s_min=f.min.value;
		e_hr=new String(f.e_hour.value);
		e_min=f.e_min.value;

		s_hr = s_hr.replace(pattern,"");
		e_hr = e_hr.replace(pattern,"");

		if (f.e_ampm.value=="PM"){	
			e_hr=(e_hr < 12)?parseInt(e_hr) + 12:1;		
		}
		
		if (f.ampm.value=="PM"){	
			s_hr=(s_hr < 12)?parseInt(s_hr) + 12:1;		
		}

		//new Date(yy,mm,dd,hh,mm,ss)
		s_date = new Date(2004,07,24,s_hr,s_min,00);
		e_date = new Date(2004,07,24,e_hr,e_min,00);

		if (e_date.getTime() < s_date.getTime()){
			errMsg += "End Time cannot be before the Start Time\n" ;
		}
	}

	if (errMsg==""){
		f.submit();
	} else {
		errMsg= "Please fix the following errors:\n"  + errMsg;
		alert(errMsg);
	}
}


function remove(){
	f.action="proc_del_event.php";
	errMsg = new String("");

	selEvents = new String("");
	var tblBase = get('MonthCal').childNodes(1);
	
	//determine which events have been selected
	for (var i = 1; i<tblBase.childNodes.length;i++ ){

		var tds = tblBase.childNodes(i).childNodes;

		for (var x = 0;x< tds.length;x++){

			for (var j=0;j<tds(x).childNodes.length;j++){
				var node = tds(x).childNodes(j);
				var blnIsSel = (node.style.backgroundColor != '');
				if ((blnIsSel) && (node.className == 'cal')){
					selEvents += node.key + '|';
				}	

			}
		}
	}
	selEvents = selEvents.substring(0,selEvents.length-1);
		//validation
	if (selEvents == ""){
		errMsg += "At least one event must be selected in order to delete.  Click the event title to select/unselect it.\n";
	} else {
		get('selEvents').value = selEvents;
	}
	
	if (errMsg==""){
		f.submit();
	} else {
		errMsg= "Please fix the following errors:\n"  + errMsg;
		alert(errMsg);
	}
}

function createXHR(){
	var xhr;

	try{	
		xhr = new ActiveXObject("MSXML2.XMLHTTP");
		

	}catch(e){
		try{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xhr=false;
		}
	}
	
	if(!xhr && typeof XMLHttpRequest != 'undefined'){
		xhr= new XMLHttpRequest();
	}

	return xhr;
}

var xmlDoc;

function loadXML(strXML)
{
//alert("loadXML in - " + strXML);
// code for IE
if (window.ActiveXObject)
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(strXML);
  
  return xmlDoc;
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xmlDoc=document.implementation.createDocument("","",null);

  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(strXML, "text/xml");
  if(xmlDoc == null) alert("xml Doc Load Failed");

  return xmlDoc;
  
  //xmlDoc.onload=passXML(xmlDoc);
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}

function passXML(doc){
	return doc;
}

var xhr;

function getNodeValue(nd){
	var strVal = "";
	
	if ((nd.length != 0) && (nd.item(0).hasChildNodes())){
		strVal = nd.item(0).childNodes.item(0).nodeValue;
	}
	
	return strVal;
}

function filterSelect(){
	if (xhr.readyState==4){
		var res = xhr.responseText;
//alert(res);
		xmlDoc = loadXML(res);
		var nList = xmlDoc.getElementsByTagName("select");
   		var opts = get(getNodeValue(nList));
   		var oOption;
   		
        for (var i=0;i<opts.options.length;i++){
        	opts.options.remove(i);
        	i--;
		}

		nVals = xmlDoc.getElementsByTagName("value");
		nTxt = xmlDoc.getElementsByTagName("text");

		if (nVals.length > 0){
			oOption = document.createElement("OPTION");
   			opts.add(oOption);
   			oOption.innerText ="";
			oOption.value = "";
		}
		for (var i = 0; i < nVals.length; i++){
			//alert(nVals[i].firstChild.nodeValue + " of " + nVals.length);
			oOption = document.createElement("OPTION");
   			opts.options.add(oOption);
   			oOption.innerText = nTxt[i].firstChild.nodeValue;
   			oOption.value = nVals[i].firstChild.nodeValue;   			
		}
	}
}

function ddFilter(selId,child){
	xhr = createXHR();
	xhr.onreadystatechange = filterSelect;
	xhr.open("GET","filterSelect.php?dt=" + new Date() + "&id=" + selId + "&child_id=" + child);
	xhr.send("dt=" + new Date() + "&id=" + selId + "&child_id=" + child);
}

function call_cal(yr, mnth){
	//alert("call cal " + mnth + "-" + yr + "; scroll-" + scroll );
	xhr = createXHR();
	xhr.onreadystatechange = setCal;
	xhr.open("GET","calendar.php?dt=" + new Date() + "&year=" + yr + "&month=" + mnth);
	xhr.send();
}

function setCal(){

	if (xhr.readyState==4){
		var bio = "";
		var res = xhr.responseText;
		if (get('ifCal') == null){
			document.body.innerHTML =  res ;
		}else {
			get('ifCal').innerHTML = res;
		}
	}
}