function addEvent(obj, type, fn) {
   if (obj.addEventListener) {
      obj.addEventListener(type, fn, false);
      return true;
   } else if (obj.attachEvent) {
      var r = obj.attachEvent("on"+type, fn);
      return r;
   } else {
      return false;
   }
}

function stopEvent(e) {
   if (e.stopPropagation) {
      e.stopPropagation();   // for DOM-friendly browsers
      e.preventDefault();
   } else {
      e.returnValue = false; // for IE
      e.cancelBubble = true;
   }
}

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }
    return str;
}

function validateRequired(field_id, error_message) {
   var field = document.getElementById(field_id);
   if (trim(field.value)=="")
      return error_message;
   return "";
}

function validateEmail(field_id, error_message) {
   var email = document.getElementById(field_id);
   if (!checkEmail(email))
      return error_message;
   return "";
}

function checkEmail(email) {
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value))
      return true;
   return false;
}

function trim(str) {
   return str.replace(/^\s+|\s+$/g,"");
}

 //Adds value from one listbox to another
function addToList(fromList, toList) {
   from = document.getElementById(fromList);
   to = document.getElementById(toList);

   //Nothing selected in source dropdown
   if (from.selectedIndex==-1) return;

   //Add new value to result dropdown
   var len = to.length;
   for (i = 0; i < from.length; i++) {
      if (from.options[i].selected==true) {
	   to.options[len] = new Option(from.options[i].text, from.options[i].value, false, false);
         len++;
      }
   }

   //Remove new value from target dropdown
   removeFromList(from);
}

//Remove items from listbox
function removeFromList(list) {
    for (i = 0; i < list.length; i++) {
        if (list.options[i].selected==true) {
            list.options[i]= null;
            i--;
        }
    }
}

function handleSelect(type,args,obj) {
   var dates = args[0]; 
   var date = dates[0];
   var year = date[0], month = date[1], day = date[2];
            
   var txtDate1 = document.getElementById("date_field");
   txtDate1.value = formatDate(parseDate(month + "/" + day + "/" + year), "NNN. d, y");
   document.getElementById("calendar_container").style.display = "none";
}

function addMapPoint(map, latitude, longitude, html) {
   var point = new GMarker(new GLatLng(latitude, longitude), new GIcon(G_DEFAULT_ICON, "images/marker.png"));
   GEvent.addListener(point, "mouseover", function() { 
      point.openInfoWindowHtml(html); 
   });
   map.addOverlay(point);
}

function popImage(imageURL,imageTitle){
   PositionX = 200;
   PositionY = 200;
   defaultWidth  = 100;
   defaultHeight = 100;
   var AutoClose = true;
   if (parseInt(navigator.appVersion.charAt(0))>=4){
   var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
   var isNN=(isIE==0)?1:0;
   var optNN='scrollbars=no,toolbar=0,location=0,statusbar=0,menubar=0,directories=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
   var optIE='scrollbars=no,toolbar=0,location=0,statusbar=0,menubar=0,directories=no,width=150,height=100,left='+PositionX+',top='+PositionY;
   if (isNN){imgWin=window.open('about:blank','',optNN);}
   if (isIE){imgWin=window.open('about:blank','',optIE);}
   with (imgWin.document){
   writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
   writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
   writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
   writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(300,300);');
   writeln('width=300-(document.body.clientWidth-document.images[0].width);');
   writeln('height=300-(document.body.clientHeight-document.images[0].height);');
   writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
   writeln('window.innerWidth=document.images["pop-image"].width;');writeln('window.innerHeight=document.images["pop-image"].height;}}');
   writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
   if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
   else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
   writeln('<img name="pop-image" src="'+imageURL+'" style="display:block"></body></html>');
   close();}
}

function updateCal() {
   var txtDate1 = document.getElementById("date_field");
   if (txtDate1.value != "") {
      var date_val = txtDate1.value.replace(".","");
      var date_val = formatDate(parseDate(date_val), "M/d/y");
      YAHOO.calendar.cal1.select(date_val);

      var firstDate = YAHOO.calendar.cal1.getSelectedDates()[0];
      YAHOO.calendar.cal1.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());

      YAHOO.calendar.cal1.render();
      document.getElementById("calendar_container").style.display = "block";
   }
}

function calendar_init() {
   YAHOO.calendar.cal1 = new YAHOO.widget.Calendar("cal1","calendar_container", { close:true } );
   YAHOO.calendar.cal1.selectEvent.subscribe(handleSelect, YAHOO.calendar.cal1, true);
   YAHOO.calendar.cal1.render();
   YAHOO.util.Event.addListener("show", "click", updateCal);
   YAHOO.util.Event.addListener("show", "click", YAHOO.calendar.cal1.show, YAHOO.calendar.cal1, true);
}
