﻿function trim(str) { 
	return trimChars(str, ' ');
}

function trimCrLf(str) { 
	var tmp = '';
	tmp = trimChars(str, '\r\n');
	return tmp;
}

function trimChars(str, chars) {
	// this will get rid of leading chars
	while (str.substring(0, chars.length) == chars)  {
		str = str.substring(chars.length, str.length);
	}

	// this will get rid of trailing chars
	while (str.substring(str.length - chars.length, str.length) == chars) {
		str = str.substring(0, str.length - chars.length);
	}

	return str;
}

function doesTextContainWhiteSpace(str) {
	textIsWhiteSpace = true;
	for (var i=0; i < str.length; i++) {
		if ((str.charCodeAt(i) == 13) && (str.charCodeAt(i + 1) == 10)) {
			i++;
		}
		else if (str.charCodeAt(i) == 32) {
		}
		else {
			textIsWhiteSpace = false;
			break;
		}
   }
   return textIsWhiteSpace;
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function checkAll(fld) {
    var obj = eval(fld);
    if (obj) {
        if (typeof(obj.length) == 'undefined') {
            obj.checked = true;
        }
        else {
            for (i = 0; i < obj.length; i++) {
                obj[i].checked = true;
            }
        }
    }
}

function getWindowSize()
    {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' )
             {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
             } 
        else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
            {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            } 
        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
            {
             //IE 4 compatible
             myWidth = document.body.clientWidth;
             myHeight = document.body.clientHeight;
            }
                  
}

/* 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 nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
var isDocBeingSubmitted = false;
  
function isFormChanged() {
 var rtnVal = false; 
 var frm = document.forms[0];
 var ele = frm.elements;

	 for ( i=0; i < ele.length; i++ ) {
		if ( ele[i].type.length > 0 ) {
			if ( isElementChanged( ele, i ) ) {
				rtnVal = true;
				break;
			}
		}
	}
 return rtnVal;
}

function isElementChanged( ele, i ) {
 var isEleChanged = false; 
 
	switch ( ele[i].type ) { 
	case "text" : 
		if ( ele[i].value != ele[i].defaultValue ) return true;
			break;
	
	case "textarea" : 
		if ( ele[i].value != ele[i].defaultValue ) return true;
			break;

	case "radio" :
		if ( ele[i].checked != ele[i].defaultChecked ) return true;
			break;

	case "select-one" : 
		for ( var x =0 ; x <ele[i].length; x++ ) {
			if ( ele[i].options[ x ].selected != ele[i].options[ x ].defaultSelected ) return true;
		}
		break;

	case "select-multiple" :
		for ( var x =0 ; x <ele[i].length; x++ ) {
			if ( ele[i].options[ x ].selected != ele[i].options[ x ].defaultSelected ) return true;
		}
		break;

	//case "checkbox" :
		//if ( ele[i].checked != ele[i].defaultChecked ) return true;
	
	default:
		return false;
	break;
	}
}

function navigateAway() {
 msg = "----------------------------------------------------------\n";
 msg += "Your form has not been saved.\n";
 msg += "All changes you have made will be lost\n";
 msg += "----------------------------------------------------------";
	if (isFormChanged()){
		//inbuilt get-out: hold control key to bypass message;
		if (isDocBeingSubmitted == false && event.ctrlKey == false) event.returnValue = msg;
	}
}


/* 
 * Cross-browser event handling
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
    return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

/*
 * Disables all 'test' links, that point to the href '#'
 */
function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}

function initTextFields() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

function handleExternalLinks(command, redir) { 
	var hostName = window.location.hostname;
	var links = document.getElementsByTagName("a");
	var linkFrom = window.location;
	if (command == null) command = '';
    if (redir == null) redir = '';
    var linkTo = '';
	
	if (redir.length > 0) {
	    for (var i = 0; i < links.length; i++) {
		    if(links[i].href.indexOf(hostName) == -1) {
			    linkTo = links[i].href;
			    if ((linkTo.indexOf("javascript") != 0) && (linkTo.length > 0)) {
		            links[i].href = redir + 'from=' + encodeURIComponent(linkFrom) + '&to=' + encodeURIComponent(linkTo);
			        switch (command) {
			            case 'target_new_win' :
			                links[i].setAttribute("target", "_blank");
                            break;
			        }
			    }
		    }
	    }
    }
}

/*************************************************************
 * Window Onload Manager (WOM) v1.0
 * Author: Justin Barlow - www.netlobo.com
 *
 * Description:
 * The WOM library of functions allows you to easily call
 * multiple javascript functions when your page loads.
 *
 * Usage:
 * Add functions to WOM using the womAdd() function. Pass the
 * name of your functions (with or without parameters) into
 * womAdd(). Then call womOn() like this:
 *     womAdd('hideDiv()');
 *     womAdd('changeBg("menuopts","#CCCCCC")');
 *     womOn();
 * WOM will now run when your page loads and run all of the
 * functions you have added using womAdd()
 *************************************************************/

/*************************************************************
 * The womOn() function will set the window.onload function to
 * be womGo() which will run all of your window.onload
 * functions.
 *************************************************************/
function womOn(){
	window.onload = womGo;
}

/*************************************************************
 * The womGo() function loops through the woms array and
 * runs each function in the array.
 *************************************************************/
function womGo(){
	for(var i = 0;i < woms.length;i++)
		eval(woms[i]);
}

/*************************************************************
 * The womAdd() function will add another function to the woms
 * array to be run when the page loads.
 *************************************************************/
function womAdd(func){
	woms[woms.length] = func;
}

/*************************************************************
 * The woms array holds all of the functions you wish to run
 * when the page loads.
 *************************************************************/
var woms = new Array();

// Put window.onload calls here
womAdd('initTextFields()');