/* JavaScripts for MicFac Website
	created:SCKuo 2008_1119
	modified: 2011_1128,SCK: Added code for Google Analytics
		2010_0429,SCK: Containerized in MF
	*/

//Google Analytics:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27352026-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

if (!MF) var MF={};
MF.debugall=0;
MF.getElem = function(fn,dv,debug) {//get element by ID name; debugging message when typo and 'fn' is the calling function
	if (!debug) debug=0;
	if (MF.debugall) debug=1;
	var e=document.getElementById(dv);
	if (!e) {
		if (debug) alert(fn+": Missing element '"+dv+"'.");
		return null;}
	else return e;
}

MF.getElemName = function(fn,dv,debug) {//get element by name (e.g. radio buttons); debugging message when typo and 'fn' is the calling function
	if (!debug) debug=0;
	if (MF.debugall) debug=1;
	var e=document.getElementsByName(dv);
	if (!e) {
		if (debug) alert(fn+": Missing element '"+dv+"'.");
		return null;}
	else return e; //use e[0] to return first element only
}

MF.getRadio = function(dv,debug) {
	if (!debug) debug=0;
	var retval = "";
	var e=MF.getElemName('getRadio',dv);
	for (i = 0; i <e.length; i++) {
		if (e[i].checked) retval = e[i].value;
		}
	if (debug) {
		if (retval == "") alert("getRadio: No Location Chosen");
		else alert("getRadio: "+retval);
		}
	return retval;
	}

MF.checkEmailAddress = function(field) {
// (C) 2000 www.CodeLifter.com, for the regular expression; http://www.codelifter.com
// Note: The next expression must be all on one line; allow no spaces, linefeeds, or carriage returns!
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (goodEmail) return true;
	else return false;
}

MF.confirmDelete = function(IDlabel, prompt2) {
	//if (!prompt2) prompt2=true; //default to prompt user twice when IDlabel specified
	var retval=true;
	
	if (IDlabel)
		retval=confirm("Are you sure you want to delete '" + IDlabel + "'?");
	else prompt2=true;
	if (prompt2 && retval &&
			!(confirm("Are you really sure?  Deleting is irrevocable.")) )
		retval=false;
	return retval;
	}

MF.contactLink = function(domain, user, linkText, classType) { //for hiding email addresses
	// linkText and classType are optional parameters
	var str=user + "@"  + domain;
	var retval="";
	if (domain.search(/\./) < 0) str += ".edu";
	if (!linkText || linkText.length <= 0) linkText=str; //str.replace("@","_AT_");
	retval = '<a href="mailto:' + str + '"';
	if (classType && classType.length > 0) retval += ' class="' + classType + '"';
	retval += '>' + linkText + '</a>';
    document.write(retval);
	return retval;
	}

MF.padNum = function (val, ndig) { //max ndig is 3
	if (!ndig) var ndig=2;
    s = val.toString();
    if (s.length < ndig)
        s = ('000' + s).slice(-ndig);
    return s; 
	}

MF.fullDateTime = function() {
	var rightnow=new Date();
	var retval = MF.padNum(rightnow.getMonth()+1) + '/' + MF.padNum(rightnow.getDate()) + '/' + rightnow.getFullYear() + ' ';
	retval += MF.padNum(rightnow.getHours()) + ':' + MF.padNum(rightnow.getMinutes()) + ':' + MF.padNum(rightnow.getSeconds()) + '.' + MF.padNum(rightnow.getMilliseconds(),3);
	return retval;
	}

MF.NiceHTime = function(h) { //h=duration in hours; returns "d hh:mm"
	var str="";
	if (h<0) {str="-("; h=-h}
	if (h>24) {
		var quot= Math.floor(h/24); h= h - 24*quot;
		str+= quot + "d "; }
	str+= Math.floor(h);
	var minutes=Math.round(60*(h - Math.floor(h)));
	str += ":";
	if (minutes < 10) str += "0";
	str += minutes;
	if (str.charAt(0) == "-") str+= ")";
	return str
}

MF.NiceTDuration = function(h) { //h=duration in hours; returns "d days h.xx hours"
	var str="";
	if (h<0) {str="-("; h=-h}
	if (h>24) {
		var quot= Math.floor(h/24); h= h - 24*quot;
		str+= quot + "days "; }
	str+= h.toFixed(2) + "hrs";
	if (str.charAt(0) == "-") str+= ")";
	return str
}

MF.forceSelect = function(sn, msg) {
	var fn="forceSelect";
	e=MF.getElemName(fn,sn);
	if (e[0].selectedIndex == 0) { //default location always first
		alert(msg); return false; }
	else return true;
	}

MF.getCookie = function(c_name) {
	var i,x,y, ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); //check- does this handle null cookies correctly (no '=')?
	  x=x.replace(/^\s+|\s+$/g,""); //strip leading & trailing spaces
	  if (x==c_name) {
		return unescape(y.replace(/^\s+|\s+$/g,"")); }
	} return null;
}

MF.setCookie = function(c_name,value,exdays,path) {
	var exdate=new Date();
	exdate.setTime(exdate.getTime()+(exdays*24*60*60*1000));
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()) +
		((path==null) ? "" : "; path=" + path); //other sites use toGMTString() instead
	document.cookie=c_name + "=" + c_value;
}

MF.eraseCookie = function(c_name) {
	MF.setCookie(c_name,"",-1); }

MF.onblur = function(el) { //for input text
	if (!el.defaultValue || el.defaultValue=='') return;
	if (el.value=='') el.value=el.defaultValue; }

MF.onclick = function(el) { //for input text
	if (!el.defaultValue || el.defaultValue=='') return;
	if (el.value==el.defaultValue) el.value=''; }
	
MF.hideParent = function(el) { //for GoAway box in floating windows
	el.parentNode.parentNode.style.visibility = "hidden"; }

MF.CSSClass= {}; //CSSClass from Javascript, the Definitive Guide by David Flanagan
MF.CSSClass.is = function(e, c) { // Return true if element e is a member of the class c; false otherwise
    if (typeof e == "string") e = document.getElementById(e);
    var classes = e.className;    // Before doing a regexp search, optimize for a couple of common cases.
    if (!classes) return false;
    if (classes == c) return true;
    return e.className.search("\\b" + c + "\\b") != -1;
};
MF.CSSClass.add = function(e, c) {//Add class c to className of element e if it is not already there
    if (typeof e == "string") e = document.getElementById(e);
    if (MF.CSSClass.is(e, c)) return;
    if (e.className) c = " " + c;
    e.className += c;
};
MF.CSSClass.remove = function(e, c) {// Remove all occurrences (if any) of class c from the className of element e
    if (typeof e == "string") e = document.getElementById(e);
    e.className = e.className.replace(new RegExp("\\b"+ c+"\\b\\s*", "g"), "");
};

MF.stripWhite = function(s) { //to strip white space from a string of HTML/JS/CSS code
	s=s.replace(/\r\n/g," ");
	s=s.replace(/\r/g," ");
	s=s.replace(/\n/g," ");
	s=s.replace(/\s+/g," ");
	s=s.replace(/^ /,"");
	s=s.replace(/$ /,"");
	return s;
}

MF.docInfo = function() {
	document.write("<h3>Checking Browser DOM:</h3><p>");
	document.write("Document.title: <i>", document.title, "</i><br />");
	document.write("Document.URL: <i>", document.URL, "</i><br />");
	document.write("Document.Location: <i>", document.location, "</i><br />");
	document.write("Document.Location.pathname: <i>", document.location.pathname, "</i><br />");
	document.write("Document.Location.search: <i>", document.location.search, "</i><br />");
	document.write("Document.Referrer: <i>", document.referrer, "</i><br />");
	document.write("Document.NameSpaceURI: <i>", document.namespaceURI, "</i><br />");
	document.write("Document.LastModified: <i>", document.lastModified, "</i><br />");
	document.write("Document.baseURI: <i>", document.baseURI, "</i><br />");
	document.write("Location: <i>", location, "</i><br />");
	document.write("Location.pathname: <i>", location.pathname, "</i><br />");
	document.write("Location.search: <i>", location.search, "</i><br />");
	document.write("Window.Location: <i>", window.location, "</i><br />");
	document.write("Window.Location.pathname: <i>", window.location.pathname, "</i><br />");
	document.write("Window.Location.search: <i>", window.location.search, "</i><br />");
	document.write("</p>");
}

/* //No longer needed with new version of MF_contactLink()
function MF_hideStr(str) { //for hiding email addresses, support function to randomize character representaion
	var retval="";
	var rv=0;
	for (i=0; i<str.length; i++) {
		rv=Math.random();
		if (rv > 0.67) retval += str.charAt(i);
		else if (rv > 0.33) retval += "&#x" + str.charCodeAt(i).toString(16);
		else retval += "&#" + str.charCodeAt(i).toString(10);
		}
	return retval;
	}*/


