//Scripts to manage layers (From Apple Developer Network)
//
// ************************
// layer utility routines *
// ************************
function isset(varname){
    //NOTE: This only works for global scope variables
	return(typeof(window[varname])!='undefined');
}

function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
	// get a reference to the cross-browser style object and make sure the object exists
	var styleObject = getStyleObject(objectId);
	if(styleObject) {
		styleObject.visibility = newVisibility;
		
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
} // changeObjectVisibility

function changeObjectDisplay(objectId, newVisibility) {
	// get a reference to the cross-browser style object and make sure the object exists
	var styleObject = getStyleObject(objectId);
	if(styleObject) {
		styleObject.display = newVisibility;
		
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
} // changeObjectVisibility


function moveObject(objectId, newXCoordinate, newYCoordinate) {
	// get a reference to the cross-browser style object and make sure the object exists

	var styleObject = getStyleObject(objectId);
	if(styleObject) {
		if (document.layers)
		{
			styleObject.left = newXCoordinate;
			styleObject.top = newYCoordinate;
		}
		else
		{
			styleObject.left = newXCoordinate + "px";
			styleObject.top = newYCoordinate + "px";
		}

		return true;
	} else {
		// we couldn't find the object, so we can't very well move it
		return false;
	}
} // moveObject

function toggleColor(rowID, status){
	var element = document.getElementById(rowID).getElementsByTagName('DIV')[0];
	if("on" == status) {
		element.className += " selectedIncident";
	}
	else {
		element.className = element.className.replace(/ selectedIncident/g, '');
	}
} // toggleColor

//functions to manage messaging
var setMessageTimeout = null;
function setMessage(message, messageType){
	if (setMessageTimeout) clearTimeout(setMessageTimeout);
	setMessageTimeout = null;
	messageDiv = document.getElementById("message");
	messageDiv.innerHTML = message;
	if(message==""){
		messageDiv.style.display = 'none';
	}else{
		messageDiv.style.display = 'block';
		messageDiv.style.borderStyle = 'solid';
		messageDiv.style.borderWidth = '1px';
		messageDiv.style.borderWidth = '1px';
		messageDiv.style.padding = '3px';
		messageDiv.style.fontSize = "12px";
		if (typeof(messageType)!='undefined'){
		    //Note, you cannot use the isset function because of the local scope of variable color
		    if ("ERROR" == messageType){
	    		messageDiv.style.background = "red";
	    		messageDiv.style.color  = "white";
	    		messageDiv.style.borderColor = "red";  
		    }else if ("WARNING" == messageType){
	    		messageDiv.style.background = "yellow";
	    		messageDiv.style.color  = "black";	  
	    		messageDiv.style.borderColor = "yellow";      
		    }else if ("DISPLAY" == messageType){
		        messageDiv.style.background =  '#D5EFFC';
		        messageDiv.style.color  = "black";	
		        messageDiv.style.borderColor = '#B5D0EB';
		    }
		}
	}
}

function validateEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert(t("Invalid E-mail"))
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert(t("Invalid E-mail"))
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert(t("Invalid E-mail"))
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert(t("Invalid E-mail"))
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert(t("Invalid E-mail"))
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert(t("Invalid E-mail"))
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert(t("Invalid E-mail"))
		    return false
		 }

 		 return true					
	}
	
function toggleDiv(divid){
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
    }else{
      document.getElementById(divid).style.display = 'none';
    }
  }
 function onEnter( evt, frm ) {
 	var keyCode = null;

 	if( evt.which ) {
 		keyCode = evt.which;
 	} else if( evt.keyCode ) {
 		keyCode = evt.keyCode;
 	}
 	if( 13 == keyCode ) {
 		//frm.btnEnter.click();
 		return true;
 	}
 	return false;
 }
 
 function unrollBtn(img, search, replace) {
	if (!search)
		search = /roll/;
	if (!replace)
		replace = "inactive";
	rollBtn(img, search, replace);
}

function rollBtn(img, search, replace) {
	if (!search)
		search = /inactive/;
	if (!replace)
		replace = "roll";
	img.src = img.src.replace(search, replace);
}

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}
}

/*
The JavaScript Source :: http://javascript.internet.com
Created by: Dustin Diaz :: http://www.dustindiaz.com/ */
function getElementsByClass(searchClass,node,tag) {
  var classElements = new Array();
  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if (pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function showGlobal(targetEl) { //function to hide/reveal submenu items on the global menu.
    hideItems = getElementsByClass('globalNavHide');
    for(i=0; i<hideItems.length; i++) {
        hideItems[i].style.display = "None";
    }
    showScope = targetEl.parentNode;
    showItems = getElementsByClass('globalNavHide', showScope);
    for(j=0; j<showItems.length; j++) {
        showItems[j].style.display = "Block";
    }
}

function CookieHandler() {
	// example
	//var c = new CookieHandler();
	//c.setCookie('name', 'value', 60*60*24); // set cookie ofr one day (24 hours)
    this.setCookie = function (name, value, seconds) {
 
        if (typeof(seconds) != 'undefined') {
            var date = new Date();
            date.setTime(date.getTime() + (seconds*1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else {
            var expires = "";
        }
 
        document.cookie = name+"="+value+expires+"; path=/";
    }
 
    this.getCookie = function (name) {
 
        name = name + "=";
        var carray = document.cookie.split(';');
 
        for(var i=0;i < carray.length;i++) {
            var c = carray[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
        }
 
        return null;
    }
 
    this.deleteCookie = function (name) {
        this.setCookie(name, "", -1);
    }
}
