// Properties
Hashtable.prototype.hash	 	= null;
Hashtable.prototype.keys		= null;
Hashtable.prototype.location	= null;
//

// Globals
var selectedCategories = new Hashtable();
//

function HideReportFilter() {

	if (document.viewreports.report.value == 'c' || document.viewreports.report.value == 'd' || document.viewreports.report.value == 'g') {
		document.getElementById('ie7prompt').style.display = ''
	} else {
		document.getElementById('ie7prompt').style.display = 'none';
	}
	
	if (!document.getElementById('filters')) return false;
	
	if (document.getElementById('filters').style.display == '') {
		document.getElementById('filters').style.display = 'none';
		if (document.viewreports.filter.value != null) document.viewreports.filter.value = '';
	}
}

function MakeAttachmentOption(thetext,thevalue) {

  	var elOptNew = window.opener.document.createElement('option');
  	elOptNew.text = thetext;
  	elOptNew.value = thevalue;
  	var elSel = window.opener.document.getElementById('attachments');

	if (!window.opener.document.getElementById('attachments')) return false;
	
  	try {
    	elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  	}
  	catch(ex) {
    	elSel.add(elOptNew); // IE only
  	}
}

function MakeFileOption(thetext,thevalue) {
	
  	var elOptNew = window.opener.document.createElement('option');
  	elOptNew.text = thetext;
  	elOptNew.value = thevalue;
  	var elSel = window.opener.document.getElementById('files');

	if (!window.opener.document.getElementById('files')) return false;
	
  	try {
    	elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  	}
  	catch(ex) {
    	elSel.add(elOptNew); // IE only
  	}
}

function LaunchAttachmentUploadPopup(sessionid) 
{
	var windowX = (screen.width/2)-(500/2);
	var windowY = (screen.height/2)-(250/2);

	myAttachmentUploadPopup=window.open('uploadattachment.php?sessionid='+sessionid,'myAttachmentUploadPopup','width=500,height=250,left='+windowX+',top='+windowY+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=no');
	myAttachmentUploadPopup.focus();
	return false;
}

function TriggerMessageActions(action,form)
{
	form = eval("document."+form);

	if (action == "send") {
		
		form.mode.value = "send";
		
		var fRet; 
   		fRet = confirm('Do you really want to send this message?'); 
   		if (fRet == true) {
			form.submit();
   		}
	}
	
	if (action == "edit") {
		form.mode.value = "edit";
		form.submit();
	}

	if (action == "copy") {
		form.mode.value = "copy";
		form.submit();
	}
	
	if (action == "delete") {
	   
	    form.mode.value = "delete";
		
		var fRet; 
   		fRet = confirm('Do you really want to delete this message?'); 
   		if (fRet == true) {
			form.submit();
   		}
	}
}

function SubmitMessageForm(submitmode)
{
	document.messageform.submitmode.value = submitmode;
	document.messageform.submit();
}

function UpdateContentLayers(value)
{
	if (value == "HTML & Text") {
		document.getElementById('htmlcontent').style.display = '';
		document.getElementById('textcontent').style.display = '';
	} else if (value == "HTML") {
		document.getElementById('htmlcontent').style.display = '';
		document.getElementById('textcontent').style.display = 'none';			
	} else if (value == "Text") {
		document.getElementById('htmlcontent').style.display = 'none';
		document.getElementById('textcontent').style.display = '';			
	} else {
		document.getElementById('htmlcontent').style.display = 'none';
		document.getElementById('textcontent').style.display = 'none';		
	}
}

function UpdateEventDate(form)
{
	if (form.updateenddate.checked == true)
	{
		form.end_date_day.value=form.start_date_day.value;
		form.end_date_month.value=form.start_date_month.value;
		form.end_date_year.value=form.start_date_year.value;
	}
}

function RemoveCategory(category,currentcategory) 
{
	selectedCategories.remove(category);
	
	var categoryField = currentcategory; // var categoryField = "";
	
	var categoryString = "<table>\n";
	
	selectedCategories.moveFirst();
	while (selectedCategories.next()) { //While we have more elements
		//alert (selectedCategories.getKey() + " = " + selectedCategories.getValue());
		categoryString = categoryString + "<tr><td class=\"mainstyle\" valign=\"top\">&#149;</td><td class=\"mainstyle\">";
		categoryString = categoryString + selectedCategories.getValue();
		categoryString = categoryString + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		categoryString = categoryString + "[<a href=\"#categories\" onclick=\"javascript:RemoveCategory(" + selectedCategories.getKey() + "," + currentcategory + ")\">Remove from category</a>]</td></tr>\n";
		if (categoryField != "") categoryField = categoryField + ",";
		 categoryField = categoryField + selectedCategories.getKey();
	}

	if (categoryString == "<table>\n") categoryString = categoryString + "<tr><td class=\"mainstyle\">No categories selected.</td></tr>\n";
	
	categoryString = categoryString + "</table>";
	
	document.getElementById("selectedCategories").innerHTML = categoryString;
	
	document.articleform.categories.value = categoryField;
	
	//alert (document.articleform.categories.value) ;
}

function UpdateCategories(category,breadcrumb,currentcategory) 
{
	if (category != currentcategory) {
	
		puttype = parent.opener.selectedCategories.put(category, breadcrumb);

		if (puttype == 'created') {
	
			currentCategories = parent.opener.document.articleform.categories.value
			if (currentCategories == "") {
				parent.opener.document.articleform.categories.value = category;
			} else {	
				parent.opener.document.articleform.categories.value = currentCategories + "," + category;
			}
		}

		//alert (parent.opener.document.articleform.categories.value) ;
	
		var categoryString = "<table>\n";
	
		parent.opener.selectedCategories.moveFirst();
		while (parent.opener.selectedCategories.next()) { //While we have more elements
			//alert (parent.opener.selectedCategories.getKey() + " = " + parent.opener.selectedCategories.getValue());
			categoryString = categoryString + "<tr><td class=\"mainstyle\" valign=\"top\">&#149;</td><td class=\"mainstyle\">";
			categoryString = categoryString + parent.opener.selectedCategories.getValue();
			categoryString = categoryString + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			categoryString = categoryString + "[<a href=\"#categories\" onclick=\"javascript:RemoveCategory(" + parent.opener.selectedCategories.getKey() + "," + currentcategory + ")\">Remove from category</a>]</td></tr>\n";
		}

		categoryString = categoryString + "</table>\n";
	
		parent.opener.document.getElementById("selectedCategories").innerHTML = categoryString;
	
		//alert(dump(parent.opener.selectedCategories));
	}
	
	window.close();
}

function LaunchCategorySelector(sessionid,currentcategory) 
{	
	var windowX = (screen.width/2)-(400/2);
	var windowY = (screen.height/2)-(600/2);

	myCategorySelectorPopup=window.open('categoryselector.php?sessionid='+sessionid+'&currentcategory='+currentcategory,'myCategorySelectorPopup','width=400,height=600,left='+windowX+',top='+windowY+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=no');
	myCategorySelectorPopup.focus();
	return false;
}

function DeleteFileCheck()
{
   var fRet; 
   fRet = confirm('Do you really want to delete this file?'); 
   if (fRet != true) {
		return false;
   }
} 

function ViewFile(form) 
{
	if(IsEmpty(form.files)) 
    { 
      alert('Please select a file!') ;
      form.files.focus(); 
      return false; 
	
	} else {

		file = "http://www.rotary1050.org/uploads/" + form.files.value;
		myFile=window.open(file,'myFile','scrollbars=yes,resizable=yes,location=yes,directories=yes,status=yes,menubar=yes');
		myFile.focus();
		return false;
    }
}

function LaunchFileHTMLTagsPopup(sessionid,form) 
{
	if(IsEmpty(form.files)) 
    { 
      alert('Please select a file!') ;
      form.files.focus(); 
      return false; 
	
	} else {
	
		var windowX = (screen.width/2)-(500/2);
		var windowY = (screen.height/2)-(250/2);

		file = form.files.value;
		myFileHTMLTagsPopup=window.open('viewhtmltags.php?sessionid='+sessionid+'&file='+file,'myFileHTMLTagsPopup','width=500,height=250,left='+windowX+',top='+windowY+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=no');
		myFileHTMLTagsPopup.focus();
		return false;
    }
}

function LaunchFileUploadPopup(sessionid) 
{
	var windowX = (screen.width/2)-(500/2);
	var windowY = (screen.height/2)-(250/2);

	myFileUploadPopup=window.open('uploadfile.php?sessionid='+sessionid,'myFileUploadPopup','width=500,height=250,left='+windowX+',top='+windowY+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=no');
	myFileUploadPopup.focus();
	return false;
}

function updateGuidelineVisibility(id,adminuser,updatetype,imagename)
{
	if (updatetype == 'onload') {
	
		var current = getCookie(adminuser + '_' + id);
		if (current == 0) {
			document.getElementById(id).style.display = 'none';
			document[imagename].src = "images/icon_show_guidelines.gif";
		}
			
	} else {
	
		if (document.getElementById(id).style.display == 'none') {
       		document.getElementById(id).style.display = '';
			guideline = 1;
			document[imagename].src = "images/icon_hide_guidelines.gif";
		} else {
			document.getElementById(id).style.display = 'none';
			guideline = 0;
			document[imagename].src = "images/icon_show_guidelines.gif";
    	}
	
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		setCookie(adminuser + '_' + id, guideline, now, null, null, null);
	}		
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function ShowDisabledMessage() {

   alert('This facility is currently disabled.');
} 

function DeleteCheck()
{
   var fRet; 
   fRet = confirm('Do you really want to delete this record?'); 
   if (fRet != true) {
		return false;
   }
} 

function ValidateLogin(form)
{
   if(IsEmpty(form.loginname)) 
   { 
      alert('Enter a login name and password to enter') ;
      form.loginname.focus(); 
      return false; 
   } 

   if(IsEmpty(form.loginpassword)) 
   { 
      alert('Enter a login name and password to enter') ;
      form.loginpassword.focus(); 
      return false; 
   } 
}
 
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function checkMail(sEmail)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(sEmail)) return true;
}

function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to

function insertcode(tag, desc, textarea)
{
    // our textfield
    if (!textarea) textarea = document.getElementById("The_TextArea");

    // our open tag
    var open = "<" + tag + ">";

    // our close tag
    var close = "</" + tag + ">";
	
    if(!textarea.setSelectionRange)
    {
        var selected = document.selection.createRange().text; 
        if(selected.length <= 0)
        { 		
            // no text was selected so prompt the user for some text
            var promptValue= prompt("Please enter the text you'd like to " + desc, "");
            if(promptValue!=null && promptValue!=""){
                textarea.value += open + promptValue + close;	
            }
        }
        else
        {
            // put the code around the selected text
            document.selection.createRange().text = open + selected + close; 
        }
    }
    else
    {
        // the text before the selection
        var pretext = textarea.value.substring(0, textarea.selectionStart);
        
        // the selected text with tags before and after
        var codetext = open + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + close;

        // the text after the selection
        var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);
        
        // check if there was a selection
        if(codetext == open + close)
        {
            //prompt the user
            codetext = open + prompt("Please enter the text you'd like to " + desc, "") + close;
        }

        // update the text field
        if(codetext!=open+close && codetext!=""){
        	textarea.value = pretext + codetext + posttext;
        }
    }

    // set the focus on the text field
    textarea.focus();
}

// inserts an image by prompting the user for the url
function insertimage(textarea)
{
    // our textfield
    if (!textarea) textarea = document.getElementById("The_TextArea");

    // our image
    var sInput = prompt("Please enter the image url", "http://")
    if (sInput&&sInput!='http://') {
        var image = "<img src='" + sInput + "' alt=''>";

        if(!textarea.setSelectionRange)
        {
            // get selected text
            var selected = document.selection.createRange().text; 

            if(selected.length <= 0)
            { 
                // no text was selected so add the image to the end
                textarea.value += image;
            }
            else
            {
                // replace the selection with the image
                document.selection.createRange().text = image; 
            }
        }
        else
        {
            // the text before the selection
            var pretext = textarea.value.substring(0, textarea.selectionStart);

            // the text after the selection
            var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);

            // update the text field
            textarea.value = pretext + image + posttext;
        }
    }

    // set the focus on the text field
    textarea.focus();
}

// inserts a link by prompting the user for a url
function insertlink(linktype,textarea)
{
    // our textfield
    if (!textarea) textarea = document.getElementById("The_TextArea");
    
	// our link
    if (linktype == 'hyperlink') {
		var url = prompt("Please enter the url", "http://");
	}
	if (linktype == 'mailto') {
		var url = prompt("Please enter the email address", "");
		url = "mailto:" + url;
	}
	
    if (url&&url!='http://'&&url!='mailto:') {
        var link = "<a href='" + url + "'>" + url + "</a>";

        if(!textarea.setSelectionRange)
        {
            // get selected text
            var selected = document.selection.createRange().text; 

            if(selected.length <= 0)
            { 
                // no text was selected so add the link to the end
                textarea.value += link;
            }
            else
            {
                // replace the selection with the link
                document.selection.createRange().text = "<a href='" + url + "'>" + selected + "</a>";
            }
        }
        else
        {
            // the text before the selection
            var pretext = textarea.value.substring(0, textarea.selectionStart);

			// the selected text
			var selected = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			if(selected.length <= 0) {
				selected = url;
				if (linktype == 'mailto') selected = selected.substring(7,selected.length);
			}	
			
            // the text after the selection
            var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length);

            // update the text field
			link = "<a href='" + url + "'>" + selected + "</a>";
            textarea.value = pretext + link + posttext;
        }
    }
    // set the focus on the text field
    textarea.focus();
}

function Hashtable()
{
	this.hash = new Array();
	this.keys = new Array();

	this.location = 0;
}

Hashtable.prototype.get = function (key)
{
	return this.hash[key];
}

Hashtable.prototype.put = function (key, value)
{
	if (value == null)
		return null;

	if (this.hash[key] == null) {
		this.keys[this.keys.length] = key;
		puttype = 'created';
	} else {
		puttype = 'replaced';
	}	

	this.hash[key] = value;
	
	return puttype;
}

Hashtable.prototype.remove = function (key)
{
	for (var i = 0; i < this.keys.length; i++){
		//did we found our key?
		if (key == this.keys[i]){
			//remove it from the hash
			this.hash[this.keys[i]] = null;
			//and throw away the key...
			this.keys.splice(i ,1);
			return;
		}
	}
}

Hashtable.prototype.next = function ()
{
	if (++this.location < this.keys.length)
		return true;
	else
		return false;
}

Hashtable.prototype.moveFirst = function ()
{
	try {
		this.location = -1;
	} catch(e) {/*//do nothing here :-)*/}
}

Hashtable.prototype.moveLast = function ()
{
	try {
		this.location = this.keys.length - 1;
	} catch(e) {/*//do nothing here :-)*/}
}

Hashtable.prototype.getKey = function ()
{
	try {
		return this.keys[this.location];
	} catch(e) {
		return null;
	}
}

Hashtable.prototype.getValue = function ()
{
	try {
		return this.hash[this.keys[this.location]];
	} catch(e) {
		return null;
	}
}