var hexChars = "0123456789ABCDEF";
agt=navigator.userAgent.toLowerCase();
is_mac = (agt.indexOf("mac")!=-1);
supLayers = (document.layers)? true:false;
//supAll = (document.all)? true:false;
supEID = (document.getElementById)? true:false;

function reqFormItem(elElement){
	with(elElement){
		alert(name.replace(/_/gi," ").substring(1,name.length) + " is required");
		focus();
	}
	return false;
}

function reqIntFormItem(elElement){
	with(elElement){
		alert(name.replace(/_/gi," ").substring(1,name.length) + " must be a number.");
		focus();
	}
	return false;
}
function deTrailS(elItem){
	if (elItem.value.charAt(elItem.value.length-1) != ' ') return;
	elItem.value = elItem.value.substr(0,elItem.value.length-1);
	deTrailS(elItem);
}


function checkForm(elForm){
	for (i=0;i<elForm.length;i++){
		with (elForm.elements[i]){
			if (type == "text") deTrailS(elForm.elements[i]);
			if (type != "hidden" && type != "image" && type != "button" && (name.lastIndexOf("_")+1 != name.length) && name.indexOf("_") && (!disabled)){//names begining or ending with an underscore are optional
				if ((type=="text" || type=="password") && !value.length && !length) return reqFormItem(elForm.elements[i]);
				if (!type.indexOf("select") && !selectedIndex) return reqFormItem(elForm.elements[i]);
				if ((name.toLowerCase().indexOf("e-mail") != -1)||(name.toLowerCase().indexOf("email") != -1)){
					if (type=="text" && !emailCheck(elForm.elements[i].value)){
						alert("Please enter a valid e-mail address.");
						elForm.elements[i].focus();
						return false;
					}
				}
			}
			// check for integer/number
			if ((type == "text" && name.charAt(0) == "i") && (value*1 != value || value.charAt(0) == ' ')) return reqIntFormItem(elForm.elements[i]);
			// check for ZIPcode length (5 or 9)
			if ((name.indexOf("ZIPCode") != -1) && !(value.length == 0 || value.length == 5 || value.length == 9 )){alert("Please enter a proper ZIP Code");elForm.elements[i].focus();return false;}
			if ((name.indexOf("Phone1") != -1) && !(value.length == 0 || value.length == 3)){alert("Please enter a proper phone number");elForm.elements[i].focus();return false;}
			if ((name.indexOf("Phone2") != -1) && !(value.length == 0 || value.length == 3)){alert("Please enter a proper phone number");elForm.elements[i].focus();return false;}
			if ((name.indexOf("Phone3") != -1) && !(value.length == 0 || value.length == 4)){alert("Please enter a proper phone number");elForm.elements[i].focus();return false;}
		}
	}
	return true;
}

function emailCheck(emailStr) {
	var emailPat=/^(.+)@(.+)$/;var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";var validChars="\[^\\s" + specialChars + "\]";var quotedUser="(\"[^\"]*\")";var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;var atom=validChars + '+';var word="(" + atom + "|" + quotedUser + ")";var userPat=new RegExp("^" + word + "(\\." + word + ")*$");var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");var matchArray=emailStr.match(emailPat);
	if (matchArray==null) return false;//seperated at the @ symbol
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null)return false;//if the username isn't valid
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null){//if its @ipaddress
		for (var i=1;i<=4;i++)if (IPArray[i]>255)return false;
    	return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray==null)return false;//make sure its a valid domain name
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	return domArr[domArr.length-1].match(/^((biz|info|name|com|net|org|edu|gov|mil|aero|museum|coop|[a-z]{2})$)|(((com|net|org|edu|gov|mil|museum|aero|coop)\.[a-z]{2})$)/);
}

function swapFrame(set,tab){
	if (window["iContentFrame_"+set] == tab) return;

	if (document.location.search.length)
		sStart = document.location.search + "&";
	else
		sStart = "?";

	sQueryString = "page=" + iPage + "&tab=" + tab;
	sURL = "/contentTab.cfm" + sStart + sQueryString;
	new Ajax.Updater('contentFrame_'+set, sURL, { method:'get', evalScripts: true, 
		onSuccess: function(){
			unselectTab($("tab_"+set+'_'+window["iContentFrame_"+set]));
			window["iContentFrame_"+set] = tab;
			selectTab($("tab_"+set+'_'+window["iContentFrame_"+set]));
		}
	});	
}

function selectTab(elTab){
	elTab.style.background="#ffffff";
	if (elTab.parentNode.parentNode.id == "searchTabs")
		elTab.setStyle({
			color : '#336699'
		})
					  
	elTab.style.borderBottomWidth = 0;
	elTab.blur();
}
function unselectTab(elTab){
	if (elTab.parentNode.parentNode.id == "searchTabs")
		elTab.setStyle({
			background : '#336699',
			color : '#FFFFFF'
		})
	else
		elTab.style.background="#CCCCCC";
	elTab.style.borderBottomWidth = "1px";
}

function PopUpWindow(urlString, name, w, h, perc) {
	var winX = 0;
	var winY = 0;
	if (parseInt(navigator.appVersion) >= 4) {
		winX = (screen.availWidth - w)*perc*.01;
		winY = (screen.availHeight - h)*perc*.01;
	}
	popupWin = window.open(urlString, name, 'width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY +', scrollbar.visible=true,scrollbars=yes,resizable=no,maximize=no');
}

function popup(sURL){
	//first we check to see if this popup has been created, if so, don't do it again
	if ($('popup') == null){
		var oDiv = new Element('div', {id: 'popup'});
		var oClose = new Element('span', {id: 'popupC'});
		var oPar = new Element('p', {id: 'popupP'});
		//oDiv.update(oDiv.identify());
		oDiv.addClassName('popUp');
		oClose.addClassName('closeBox');
		oClose.innerHTML = "X";
		$('shadowEditor').insert({top : oDiv});
		$('popup').insert({top:oClose});
		$('popup').insert({bottom:oPar});
		$('popupC').observe('click', function(event){
			$('popup').hide();
		});
	}
	new Ajax.Updater($('popupP'), sURL, 
		{ method:'get', evalScripts: true}
	);
	$('popup').show();
}
