/* 
-------------------------------------------------------------

Title :		mesvision.com Javascript common functions
Author : 	Josh Orum, Loud Dog Corp.
URL : 		http://www.louddog.com

Created : 	19 Dec 2006
Modified : 	
Version : 	0.01

Comments:	This should be included in every page.
-------------------------------------------------------------
*/

//***
//***	First, functions that are called onLoad in every page, or
//***	called inline (base,tabOn)
//***

/* *********************************************************** 
 *	FUNCTION:	addLoadEvent(func)
 *	PURPOSE:	
 * 	Add events to the onload.
 *	http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * ********************************************************* */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/* *********************************************************** 
 *	FUNCTION:	namePage()
 *	PURPOSE:	
 * 	Name the page based on test or production
 *	This should be called onLoad
 * ********************************************************* */

function namePage(){
	var pN = (typeof(pageName)!="undefined") ? pageName : "MESVision";
	var prefix = (pXHg==532) ? "Test_" : "";
	document.title=prefix + pN;
}

/* *********************************************************** 
 *	FUNCTION:	checkPre()
 *	PURPOSE:	
 * 	If the site is test, / is returned, otherwise https is retuned
 * ********************************************************* */

function checkPre() {
	return(pXHg==532)?"/":"https://www.mesvision.com/";
}

/* *********************************************************** 
 *	FUNCTION:  sfHover, listOver, listOut
 *	PURPOSE: suckerfish rollover
 * ********************************************************* */
function addHover(container, tag) {
	if ($(container)) {
		if (tag) {
			var elements = $(container).getElementsByTagName(tag);
			for (var i = 0; i < elements.length; i++) {
				elements[i].onmouseover = function() { Element.addClassName(this, 'hover'); }
				elements[i].onmouseout = function() { Element.removeClassName(this, 'hover'); }
			}
		} else {
			$(container).onmouseover = function() { Element.addClassName(this, 'hover'); }
			$(container).onmouseout = function() { Element.removeClassName(this, 'hover'); }
		}
	}
}

//***
//*** Second, useful functions that are applied to every page.
//***

/* *********************************************************** 
 *	FUNCTION:  openwin(url, width, height, name, location, scrollbars)
 *	PURPOSE: 
 *	Pop up a new window named ldwin with a default size of 685 x 480
 *	and no location bar.
 *	Width, height, name and location are optional
 * ********************************************************* */

function openWin(url, width, height, name, location, scrollbars) {
  if (width    == null) width=685;
  if (height   == null) height=450;
  if (location == null) location="no";
  if (name     == null) {
    name="ldwin";
  } else {
    // Strip out spaces
    name = name.replace(/\s+/g, '');
  }
  
  menubar = "no";
  toolbar = "no";
  if (scrollbars == null) scrollbars = "yes";
  resizeable = "yes";
  
  var opts="menubar=" + menubar + ",toolbar=" + toolbar + ",scrollbars="
		   + scrollbars + ",resizable=" + resizeable + ",fullsize=no,width="
           + width + ",height=" + height + ",location=" + location;

  winref=window.open(url,name,opts);
  winref.focus();
}

function closeWin() {
	window.close();
}

//***
//*** Finally, functions that are used on many pages.
//***

/* *********************************************************** 
 *	FUNCTION:  printPage
 *	PURPOSE: 
 *	Print button
 *	This should be attached to any print button on the page.
 * ********************************************************* */
function printPage() {
	window.print();
}

/* *********************************************************** 
 *	FUNCTION:  redactSubnum
 *	PURPOSE: 
 *	Redact a subscriber's number.
 * ********************************************************* */
function redactSubnum(num) {
	num = "*****" + num.substring(5,9);
	document.write(num);
}

/* *********************************************************** 
 *	FUNCTION:  enableFormLinks()
 *	PURPOSE: 
 *  Finds links in forms with a certain class, then assigns 
 *  them onclick actions accordingly.
 *  'submitButton' - Submits its parent form using its own
 *     href attribute.
 *  'cancelButton' - Resets its parent form.
 * ********************************************************* */
function enableFormLinks() {
	enableAction('submitButton', function(link, form) {
		form.action = link.href;
		form.submit();
	});
	
	enableAction('cancelButton', function(link, form) {
		form.reset();
	});
	
	enableAction('printButton', function(link, form) {
		window.print();
	});
}

function enableAction(label, func) {
	document.getElementsByClassName(label).each(function(element) {
		for (var i = 0; i < document.forms.length; i++) {
			if (element.descendantOf(document.forms[i])) {
				element.onclick = function() {
					element.up().addClassName('disabled');
					func(element, document.forms[i]);
					return false;
				}
				break;
			}
		}
	});
}

/* *********************************************************** 
 *	FUNCTION:  showDetails(obj) / hideDetails(obj)
 *	PURPOSE: 
 *	Show and hide a details section when a user clicks a link.
 *
 *	The standard green form table allows additional information to be displayed about a field.
 *	
 *	Fields with additional information have a "show details" link below their label.
 *	
 *	Clicking "show details" displays help text in the table row below the form field, and the
 *	link changes to "hide details".
 *	
 *	Clicking "hide details" restores the table to its original view.
 *	
 *	Naming conventions are important for this technique to work. In the HTML, the "show details"
 *	link is enclosed in a <div> that has an ID in this format:
 *	<div id="help_NAME">
 *	where NAME is a human-readable description of the element that is getting help text.
 *	For instnace, <div id="help_planBasis">.

 *	In the table row immediately below the form field, in a table cell with a colspan=2,
 *	enter the help text in a <div> with this format:
 *	<div class="details" id="details_NAME">
 *	where NAME is the same human-readable description used above. Important: The JavaScript
 *	parses the elements' IDs and uses them to identify the correct help text to display.
 *	The class="details" applies the visual style to the help text.
 *	
 * ********************************************************* */
function showDetails(obj) {
var theDetails = "details_"+obj.slice(5); //Parse the link's ID to identify the correct help text to display
document.getElementById(theDetails).style.display = "block"; //Display the help text
document.getElementById(obj).innerHTML = "<a  href=\"#\" onclick=\"hideDetails(parentNode.id); return false;\">hide details</a>"; //Change the link to "hide details"
}

function hideDetails(obj) {
var theDetails = "details_"+obj.slice(5);
document.getElementById(theDetails).style.display = "none";
document.getElementById(obj).innerHTML = "<a  href=\"#\" onclick=\"showDetails(parentNode.id); return false;\">show details</a>";
}



var FormUtils = {
	value: function(field) {
		switch (this.fieldType(field)) {
			case 'radio' :
				for (var ndx = 0; ndx < field.length; ndx++)
					if (field[ndx].checked) return field[ndx].value;
				return false;
			case 'checkbox': return field.checked;
			case 'select-one':
				if (field.selectedIndex == -1) return false;
				return field[field.selectedIndex].value;
			default: return field.value;			
		}
	},

	fieldType: function(element) {
		if (element[0] && element[0].type == 'radio') return 'radio';
		else if (element.type == 'checkbox') return 'checkbox';
		else if (element.type) return element.type;
		else return false;
	},

	labelFor: function(element) {
		if (!$(element) || !element.id) return false;
		var result = false;
		$A(document.getElementsByTagName('label')).each(function(label) {
			if (label.readAttribute('for') == element.id) result = label;
		});
		return result;
	},
	
	setSelect: function(element, value) {
		for (var i = 0; i < element.options.length; i++) {
			if (element.options[i].value == value) {
				element.selectedIndex = i;
				break;
			}
		}
	},

	fixFormLabels: function() {
		// enable label clicks for IE and Safari
		if( document.all || navigator.userAgent.indexOf("Safari") > 0){ 
			var labels = document.getElementsByTagName("label");
			$A(labels).each ( function(label){
				Event.observe(label, "click", function(){
					var target = $(this.getAttribute('for'));
					if(target.type == 'checkbox' || target.type == 'radio')
						target.checked = target.checked == false ? true : false;
					else target.focus();
				});
			});
		}
	},
	
	formatPhone: function(field) {		
		var phone = field.value.replace(/\D+/g,'');
		var p1 = phone.slice(0,3);
		var p2 = phone.slice(3,6);
		var p3 = phone.slice(6,10);

		field.value = "(" + p1 + ") " + p2 + "-" + p3;
		
		return true;
	}
}