/* MACROMEDIA */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function showHideSubMenu(subbarColor,subMenu) {
	var submenuULs = document.getElementsByTagName("ul");
	var max = submenuULs.length;
	for (i=0;i<max;i++) {
		var submenuULObj = submenuULs.item(i)
		if (submenuULObj.style.display == "block") {
			submenuULObj.style.display = "none";
		}
	}
	if (subMenu != "") {document.getElementById(subMenu).style.display = "block";}
	swapSubBar(subbarColor);
}

/* CUSTOM */
function swapSubBar(subbarColor) {
	document.getElementById("submenus").style.backgroundImage = "url(images/subbar_" + subbarColor + ".gif)";
	document.getElementById("submenus").style.backgroundRepeat = "no-repeat";
}

/* ORDER CENTER */
function setDays() {
	// ensures that a <select /> menu with days of the month coordinates with another <select /> for the month
	
	var year = document.getElementById("serviceYear");
	var month = document.getElementById("serviceMonth");
	var selectedYear = year.options[year.selectedIndex].value;
	var selectedMonth = month.options[month.selectedIndex].value;
	
	var newDays;
	if (selectedMonth == 2) {
		if (selectedYear/4 == parseInt(selectedYear/4)) {
			newDays = 29;
		} else {
			newDays = 28;
		}
	} else if (
		(selectedMonth == 1) || 
		(selectedMonth == 3) || 
		(selectedMonth == 5) || 
		(selectedMonth == 7) || 
		(selectedMonth == 8) || 
		(selectedMonth == 10) || 
		(selectedMonth == 12)) {
		newDays = 31;
	} else {
		newDays = 30;
	}
	
	//empty select box and reset with proper number of days
	var serviceDay = document.getElementById("serviceDay");
	serviceDay.options.length = 0;
	serviceDay.options[0] = new Option("1","1",true,false);
	for (var i = 0; i < newDays; i++) {
		serviceDay.options[i+1] = new Option(i+1,i+1,false,false);
	}
}

function disenableOthers(triggerPhrase,container) {
	
	/*
	
	This function serves the following purpose:
	
		To allow users the option of a predefined 
	(perhaps generated dynamically) set of <option />s 
	via a <select /> element the ability to select 
	"Other" and fill in the blank. The blank being 
	a supplementary <input type="text" />. "Other" 
	being a trigger phrase of your choice.
	
	This function relies on the following convention:
	
		Each <select />'s supplementary <input /> should 
		be ID'd as such:
	
			selectID + triggerPhrase = otherInputID
		
		Also, <select />'s that are to have this ability must 
		be of the class "allows-other"
		
	*/
	
	var selects = document.getElementsByTagName("select");		
	for (var s=0; s<selects.length; s++) {
		
		if (selects[s].className.indexOf('allows-other') != -1) {
			
			// disable associated 'other' <input /> by default
			var theOther = document.getElementById(selects[s].getAttribute("id") + triggerPhrase);
			theOther.disabled = true;
			
			// assign behavior to <select />
			selects[s].onchange = function() {
				
				// when the "Other" <option /> is selected,
				// the <input /> is enabled/focused.
				// it is disabled/blurred when the "Other" <option /> is deselected.
				
				// reclaim theOther, otherwise ended up with all <select />s changing the last 'other'
				// <input /> in the series
				var theOther = document.getElementById(this.getAttribute("id") + triggerPhrase);
				
				if (this.options[this.selectedIndex].value == triggerPhrase) {
					theOther.disabled = false;
					theOther.focus(); 
				} else {
					theOther.disabled = true;
				}
				
			};
			
		}
		
	}
	
}

/* External Links - author uknown. if you know, please remind me. :( */
function externalLinks() {
	
	if (!document.getElementsByTagName) return;
	
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_new";
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "blank")
		anchor.target = "_blank";
	}
}

/* INIT */
function init() {
	MM_preloadImages('images/subbar_blue.gif','images/subbar_green.gif','images/subbar_red.gif','images/subbar_yellow.gif');
	externalLinks();
	
	/* Order Center functions */
	disenableOthers("Other");
	
	if ((document.getElementById("serviceMonth")) && (document.getElementById("serviceYear"))) {
		// these exist on the "add job" page
		document.getElementById("serviceMonth").onchange = setDays;
		document.getElementById("serviceYear").onchange = setDays;
	}
}

window.onload = init;





