// convert all characters to lowercase to simplify testing 
var agt=navigator.userAgent.toLowerCase(); 

// *** BROWSER VERSION *** 
// Note: On IE5, these return 4, so use is_ie5up to detect IE5. 
var is_major = parseInt(navigator.appVersion); 
var is_minor = parseFloat(navigator.appVersion); 

var is_opera = (agt.indexOf("opera") != -1); 
var is_webtv = (agt.indexOf("webtv") != -1);
var is_safari = (agt.indexOf('safari')!=-1);
var is_gecko = (agt.indexOf('gecko') != -1);

// Note: Opera and WebTV spoof Navigator.  We do strict client detection. 
// If you want to allow spoofing, take out the tests for opera and webtv. 
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) 
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) 
            && (agt.indexOf('webtv')==-1)); 
var is_nav2 = (is_nav && (is_major == 2)); 
var is_nav3 = (is_nav && (is_major == 3)); 
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) || (agt.indexOf("; nav") != -1)) ); 
var is_nav5 = (is_nav && (is_major == 5)); 
var is_nav5up = (is_nav && (is_major >= 5)); 

var is_ie   = (agt.indexOf("msie") != -1 && !is_opera); 
var is_ie3  = (is_ie && (is_major < 4)); 
var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")==-1) && (agt.indexOf("msie 5.5")==-1)&& (agt.indexOf("msie 6.0")==-1) && (agt.indexOf("msie 6.5")==-1)); 
var is_ie4up  = (is_ie  && (is_major >= 4)); 
var is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) ); 
var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
var is_ie55 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5")!=-1) );
var is_ie55up  = (is_ie  && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6  = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.0")!=-1) );
var is_ie7  = (is_ie && (is_major == 4) && (agt.indexOf("msie 7.0")!=-1));
if(is_ie7){is_ie55up=true;}

// KNOWN BUG: On AOL4, returns false if IE3 is embedded browser 
// or if this is the first browser window opened. 
var is_aol   = (agt.indexOf("aol") != -1); 
var is_aol3  = (is_aol && is_ie3); 
var is_aol4  = (is_aol && is_ie4); 

function check_escape(sFolderName)
{
	if(sFolderName.indexOf('%23')!=-1)
		return sFolderName;
	sFolderName = escape(sFolderName);
	return sFolderName;
}

function CurrentView(NewLayer) {
	var LayerNames = ['subListView','subMessageView','subComposeView','subAdvComposeView','subFaxView','subAdvFaxView','subSpellCheckView','subOptionsView','subFinished','subSave','subAddressBook','subCalendarView', 'subTaskListView'], i = LayerNames.length;
	while(i--){
		if (LayerNames[i]==NewLayer){show(LayerNames[i]);}else{hide(LayerNames[i]);}
	}
}

function Get(field, value){
	var url = document.forms[0].action + '&' + field + '=' + (value != null? value : 'Yes');
	document.location = url;
}

function hide(obj) {
	var doc = top.frames[0].document;
	if (doc.getElementById(obj)){
		doc.getElementById(obj).style.visibility = "hidden";
	}
}

function Mesg(Msg){
	if (Msg.length>0 || Msg==' ')
		alert(Msg);
}

var composeWindowCount = 0;
function openWin(url, winName, width, height, features){
	var w = screen.availWidth, h = screen.availHeight;
	var leftPos = (w/2)-(width/2), topPos = (h/2)-(height/2);
	var hWin;
	if(features == "compose"){
		hWin = window.open(url, winName+composeWindowCount,'directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,titlebars=no,toolbar=no,width=' + width + ',height=' + height+ ',screenX=' + topPos + ',screenY=' + leftPos+',top=' + topPos + ',left=' + leftPos);
		composeWindowCount++;
	}else if (features == "yes"){
		hWin = window.open(url, winName, 'directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,titlebars=no,toolbar=no,width=' + width + ',height=' + height+ ',screenX=' + topPos + ',screenY=' + leftPos+',top=' + topPos + ',left=' + leftPos);
		hWin.focus();
	}else if (features == "no"){
		hWin = window.open(url, winName, 'directories=no,location=no,menubar=no,status=no,scrollbars=no,resizable=no,titlebars=no,toolbar=no,width=' + width + ',height=' + height+ ',screenX=' + topPos + ',screenY=' + leftPos +',top=' + topPos + ',left=' + leftPos);
		hWin.focus();
	}
	if(!hWin.opener){hWin.opener = window;}
//	return hWin;
}

function Post(field, value){
	var url = document.forms[0].action + '&' + field + '='+ (value != null? value : 'Yes');
	
	document.forms[0].action = url;
	if (typeof(Validate) == 'undefined' || Validate())
		document.forms[0].submit();
}

function setFocus(args){
	if(args!=null){
		document.forms[0][args].focus();
	}else{
        for(var i=0; i<document.forms[0].length; i++){
		if (document.forms[0].elements[i].type != 'hidden'){
			document.forms[0].elements[i].focus();
			return;
		}
	}

	}
}

function SetOption(name, value){
	var value = new String(value);
	var i = 0;
	var docForm = document.forms[0];
	var formLength = i = docForm.length;
	while(i--){if (docForm.elements[i].name == name){var element = docForm.elements[i];break;}}
	if (element == null){return;}
	switch(element.type){
	case 'checkbox': element.checked = (element.value == value);break;
	case 'radio': i = formLength;while(i--){if (docForm.elements[i].name == name && docForm.elements[i].value == value){	docForm.elements[i].checked = true;}}break;
	case 'select-one': i = element.length;while(i--){if(element.options[i].value.toLowerCase() == value.toLowerCase()){element.selectedIndex = i;}}break;
	case 'select-multiple':	i = element.length;while(i--){if(element.options[i].value.toLowerCase() == value.toLowerCase()){element.options[i].selected = true;}}break;
	case 'text': element.value = value;break;
	case 'hidden':element.value = value;break;
	}
}

function show(obj){
	var doc = top.frames[0].document;
	if (is_nav && !is_nav5up){
		if(typeof(eval('doc.'+obj)) != 'object'){ return ;}
		else{eval('doc.' + obj + '.visibility = "visible"');}
	}else if(is_nav5up){
		if(typeof(eval("doc.getElementById('" + obj + "')")) != 'object'){ return ;}
		else{eval("doc.getElementById('" + obj + "').style.visibility = 'visible'");}
	}else if(is_ie){
		if(typeof(eval('doc.all.'+obj)) != 'object'){ return;}
		else{eval('doc.all.'+obj+'.style.visibility = "visible"');}
	}
	else
	{
		if(typeof(document.getElementById(obj)) != 'object'){return;}
		doc.getElementById(obj).style.visibility = 'visible';
	}
}

function EnableFormFields( bEnabled, Array )
{
	var length = Array.length;

	while( length-- )
	{
		if (!Array[length]) break;
		
		var ff = Array[length];
		if ( ff.tagName == "FIELDSET" ) continue;
		ff.readOnly = ff.disabled = bEnabled ? false : true;
	}
}
