/************************
 **** Global Onload *****
 ************************/
// Register Event handlers using Prototype
Event.observe(window, 'load', function() {
		$$('img.LNavImage').each(function(s) {
			Element.observe(s,'mouseover',LNavHover);
			Element.observe(s,'mouseout',LNavHover);
		});
		$$('a.PDF').each(function(s) {
			Element.observe(s,'mouseup',PopPDF);
			return false;
		});
		$$('a.DOC').each(function(s) {
			Element.observe(s,'mouseup',PopPDF);
			return false;
		});
		$$('a.PPT').each(function(s) {
			Element.observe(s,'mouseup',PopPDF);
			return false;
		});
		$$('a').each(function(s) {
		    if ( (s.href.indexOf("kmi") <= 0) && (s.href.indexOf("kentuckianamasonry.") <= 0) && (s.href.indexOf("script:") <= 0)) {
				//alert(s.href);
				Element.observe(s,'mouseup',offsiteAutoPop);
			}
			return false;
		});
	}
	//  example: Event.observe('signinForm', 'submit', checkForm);
);


/*************************
 **** Special Purpose ****
 *************************/

function LNavHover() {
	srcArr = this.src.split('/site/');
	if (srcArr[1].indexOf("_on")>0) {
		srcArr = srcArr[1].split("_on")
		imgName = srcArr[0]+".gif";
	} else {
		imgName = srcArr[1].split(".")[0] + "_on.gif";
	}
	this.src = "/lib/images/site/" + imgName;
};

function SetFilename(field, filename) {
    eval("document.frmEdit."+field+".value='"+filename+"';");
    if (field.indexOf('_image')) ImageChange(field);
    if (field == "brochure_filename" || field == "manual_filename") {
        pdfDir = (field == "brochure_filename")?"brochures":"manuals";
        $(field + "_testlinkdiv").style.display = "block";
        $(field + "_testlink").href = "/pdfs/" + pdfDir + "/" + filename + ".pdf";
    }
    alert("Don't forget to submit this form when you are finished.")
}

function ImageChange(foo) {
    myImage = $(foo + "_image");
    myImageDiv = $(foo + "_div");
	newSrc = $F($('frmEdit')[foo]);
    myImage.src = "/lib/images/" + imgDir + "/" + newSrc;
    myImageDiv.style.display = "block";
}


/************************
 **** POPUP WINDOWS *****
 ************************/

/**
 * Function to be setup on mouseup of PDF links, to open the PDF in a new window.
 *
 * @author:	Matthew Rasnake	http://www.powercreative.com/
 */
function PopPDF() {
	pdfFile = this.href;
	window.open(pdfFile,'PDFPopup',"width=760,height=550,resizable=yes");
	this.onclick = function() {return false;}
}

/**
 * Function to be setup on mouseup of offsite links, to open the link's URL in a new window.
 *
 * @author:	Matthew Rasnake	http://www.powercreative.com/
 */
function offsiteAutoPop() {
    alert("This link leads away from our site, and will open in a new window.");
	url = this.href;
    var offsitePopup = window.open(url,'offSiteWindow','width=750,height=500,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');
    if (typeof offsitePopup == 'object') {
        offsitePopup.focus();
    }
	this.onclick = function() {return false;}
}

/**
 * Manually called function to open a url in a full new window.
 *
 * @author:	Matthew Rasnake	http://www.powercreative.com/
 * @param: 	url				URL to open (string)
 */
function offsitePopup(url) {
    alert("This link leads away from our site, and will open in a new window.");
    var offsitePopup = window.open(url,'offSiteWindow','width=750,height=500,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');
    if (typeof offsitePopup == 'object') {
        offsitePopup.focus();
    }
    return false;
}


/************************
 *** FORM VALIDATION ****
 ************************/

// This function is used to make sure that required form fields have values.
//    Field_Name: name of a form field in the form of document.form_name.form_field_name that will be validated.
//    Error_Message: this is the alert error message that will be displayed to the user.
function Has_Value(Field_Name, Error_Message) {
	var text;

	text = Field_Name.value;

	while(text.search(/\s/) > -1) {
        text = text.replace(/\s/, "");
    }

    if(text == 0) {
        alert(Error_Message);
        Field_Name.focus();
        return false;
    }
    return true;
}

function getRadioValue( radio ) {
	var i;
	for( i = 0; i < radio.length; i++ ) {
		if( radio[i].checked )
			return radio[i].value;
	}
	return null;
}

function getSelectValue(select) {
    return select[select.selectedIndex].value;
}


/*************************
 **** General Purpose ****
 *************************/

/**
 * Helper function that trims whitespace from beginning and end of a string
 *
 * @author:  Matthew Rasnake  http://www.powercreative.com/
 * @param:   str              a string
 */
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

