
MWS_Utils = {
        //Set the time on the home screen
        getTime: function() 
        {
            var d = new Date();
            var weekday = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
            var h = d.getHours(), a = 'am';
            if (h >= 12) {
                a = 'pm';
                if (h > 12) {
                    h = (h - 12);
                }
            }

            var dy = d.getDate();
            if (dy < 10) {
                dy = '0' + dy;
            }

            var m = (d.getMonth() + 1);
            if (m < 10) {
                m = '0' + m;
            }

            var dt = weekday[d.getDay()] + ' ' + m + '/' + dy + '/' + d.getFullYear() + ' ' + h + ':' + d.getMinutes() + ' ' + a;
            return dt;
        },
	
	getFormElementValue: function (theFormElement)
	{
		var theValue = "";
		var j=0;
		if (theFormElement.type=="select-one")
			theValue = theFormElement.options[theFormElement.selectedIndex].value;	
		else if (theFormElement.type=="select-multiple")
		{
			for (var i=0;i<theFormElement.options.length;i++)
			if (theFormElement.options[i].selected)
			{
				if (theValue!="")
					theValue = theValue + "|";

				theValue = theValue + theFormElement.options[i].value;	
			}
			//alert(theValue);
		}
		else
			theValue = theFormElement.value;
		return escape(theValue);
	},
	
	getNodeCDATA: function (xmlDoc, nodeName, defaultValue)
	{
		var returnText = defaultValue;
		try{
		element = xmlDoc.getElementsByTagName(nodeName)[0];

		var ie = (typeof window.ActiveXObject != 'undefined');
		
			if(ie){
				if(element.hasChildNodes){
					returnText = element.childNodes[0].nodeValue;
				}
			}
			else{
				if(element.hasChildNodes){
					returnText = element.childNodes[1].nodeValue;
				}
			}
		}
		catch(e){}
		if (returnText=="")
			returnText = defaultValue;
		return returnText;
	},	
	
	getNodeText: function (xmlDoc, nodeName, defaultValue)
	{
		var returnText = defaultValue;
		try{
			returnText = xmlDoc.getElementsByTagName(nodeName)[0].firstChild.data;
		}catch(e){}

		if (returnText=="")
			returnText = defaultValue;

		return returnText;
	},
	
	getLayer: function (mapObj, layerName)
	{
		var layer = null;
		for (var i=0;i<mapObj.layers.length;i++)
			if(mapObj.layers[i].name==layerName)
				layer = mapObj.layers[i];
		return layer;
	},
	
	createIcon: function (iconFileUrl, width, height)
	{
	
		var icon = null;
		iwidth = width;
		iheight = height;

		icImg = new Image(35,35); 
		icImg.src = iconFileUrl;
		var i = 0 
	
		if (iheight==null)
			iheight = icImg.height;

		if (iwidth==null)
			iwidth = icImg.width;
			
		var size = new OpenLayers.Size(iwidth,iheight);
		var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
		icon = new OpenLayers.Icon(iconFileUrl,size,offset);

		return icon;
	},
	addListItem: function (docObj, controlObj, value, text)
	{
		var opt=docObj.createElement('option');
		opt.value = value;
		opt.text = text;
		//controlObj.options.add(opt, controlObj.options.length);
		controlObj.options.add(opt);
	},
	getSessionID: function ()
	{
		var d=new Date();
		var sessionID=
			d.getUTCFullYear() + "_"+
			(d.getUTCMonth()+1) + "_"+
			(d.getUTCDate()+1) + "_"+
			d.getUTCHours() + "_"+
			d.getUTCMinutes() + "_"+
			d.getUTCSeconds() + "_"+
			d.getUTCMilliseconds() + "_"+
			Math.floor(Math.random()*1000000000001) ;
		return sessionID;
	},

	controlExists: function (controlName)
	{
		var controlExists = false;

		for (var i=0;i<this.MWS_MapObj.controls.length;i++)
		{
			var theControls = this.MWS_MapObj.controls[i].id.split('.');
			var theControls2 = theControls[2].split('_');
			alert(theControls2[0]);
			if (theControls2[0] == controlName) 
				controlExists = true; 
		}

		if (!controlExists)
		{
		
		}
		
		return controlExists;
	},

	pluralizeString: function (name, count, suffix)
	{
		outName = name;
		outName =count + " " + outName + ((count > 1) ? "s":"") + suffix;
		
		if (count <= 0)
			outName = "";
			
		return outName;
	},

	encode64: function (input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	   var output = "";
	   var chr1, chr2, chr3;
	   var enc1, enc2, enc3, enc4;
	   var i = 0;

	   do {
	      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 + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
		 keyStr.charAt(enc3) + keyStr.charAt(enc4);
	   } while (i < input.length);

	   return output;
	},

	decode64: function (input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	   var output = "";
	   var chr1, chr2, chr3;
	   var enc1, enc2, enc3, enc4;
	   var i = 0;

	   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	   do {
	      enc1 = keyStr.indexOf(input.charAt(i++));
	      enc2 = keyStr.indexOf(input.charAt(i++));
	      enc3 = keyStr.indexOf(input.charAt(i++));
	      enc4 = 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);
	      }
	   } while (i < input.length);

	   return output;
	}

}

