function goHere(somewhere) {
	window.location = somewhere;
}

function submitForm(form, action) {
	form.action = action;
	form.submit();
}

function submitFormThin(form) {
	form.submit();
}

function fieldFocus(element) {
	document.getElementById(element).focus();
}

function isDefined(variable) {
	if(typeof(variable) !="undefined")
		return true;
	else
		return false;
}

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 2;
	while(isDefined(theForm[z]) && theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
		if(theForm[z].type == 'checkbox') {
			theForm[z].checked = theElement.checked;
		}
		z++;
	}
}

function cancelForm(action) {
	window.location.href = action;
}

function toggleDiv(id) {
	element = document.getElementById(id);
	element.className = (element.className.toLowerCase() == 'show'?'hide':'show');
}

function toggleCall(id) {
	h2s = document.getElementsByTagName("h2");
	
	for(i = 0; i < h2s.length; i++) {
		// First, check to see if this H2 is of the class "link"
		thisID = h2s[i];
		
		if(thisID.className == "link") {
			// If it is, then check to see if it's the "droids we're looking for"
			if(id == h2s[i].getAttribute("id")) {
				// If so, turn class name of the the anchor tag inside it to the opposite thing (whatever that is).
				// Also change the right angle quote to a left angle quote thing.
				anchors = h2s[i].getElementsByTagName("a");
				for(j = 0; j < anchors.length; j++) {
					currentAnchor = anchors[j];
					currentAnchor.className = (currentAnchor.className.toLowerCase() == "active"?"inactive":"active");
					currentAnchor.blur();
				}

				// Then, look for the DIV based of that ID and toggle it to the opposite thing
				relatedDIVID = h2s[i].getAttribute("id") + "div";
				relatedDIV = document.getElementById(relatedDIVID);
				
				relatedDIV.className = (relatedDIV.className.toLowerCase() == "show"?"hide":"show");
			}
			else {
				// Otherwise, set the anchor to inactive.  And the thing inside the em to left
				anchors = h2s[i].getElementsByTagName("a");
				for(j = 0; j < anchors.length; j++) {
					currentAnchor = anchors[j];
					currentAnchor.className = "inactive";
				}
				
				// And hide the related DIV
				relatedDIVID = h2s[i].getAttribute("id") + "div";
				relatedDIV = document.getElementById(relatedDIVID);
				
				relatedDIV.className = "hide";
			}
		}
	}
}

function showDiv(id) {
	element = document.getElementById(id);
	element.className = 'show';
}

function hideDiv(id) {
	element = document.getElementById(id);
	element.className = 'hide';
}

function sayWhatHappened(inputID, infoID) {
	var options = document.getElementById(inputID).getElementsByTagName("option");
	var comma = /,/gi;
	
	for(i = 0; i < options.length; i++) {
		if(options[i].getAttribute("value") == document.getElementById(inputID).value) {
			document.getElementById(infoID).innerHTML = "The following actions will happen:<br />&middot; " + options[i].getAttribute("title").replace(comma,"<br />&middot; ");
			break;
		}
	}
}

function sayWhatHappenedThin(inputID, infoID) {
	var options = document.getElementById(inputID).getElementsByTagName("option");
	
	for(i = 0; i < options.length; i++) {
		if(options[i].getAttribute("value") == document.getElementById(inputID).value) {
			document.getElementById(infoID).innerHTML = options[i].getAttribute("title");
			break;
		}
	}
}

function makeArray()    {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

function DayOfWeek(day,month,year) {
	var daysofweek = new makeArray('Sunday',
                               'Monday',
                               'Tuesday',
                               'Wednesday',
                               'Thursday',
                               'Friday',
                               'Saturday');
							   
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;

    return daysofweek[d + 1];
}

function business_days_after(s, days) {
    // parse s for month, day, year
    var dateArray = s.split('/');
	
	someday = DayOfWeek(dateArray[2],dateArray[0]-1,dateArray[1]);
	
	switch(someday) {
		case "Thursday":
			return days_after(s, days + 3);
			break;
		case "Monday":
			return days_after(s, days + 2);
			break;
		default:
			return days_after(s, days + 1);
			break;
	}
}

function days_after(s, days) {
    // parse s for month, day, year
    var dateArray = s.split('/');
    sdate = new Date(dateArray[2]-2000,dateArray[0]-1,dateArray[1]);

	// figure out day, month, year 7 days ago
    var odate = new Date(sdate.getTime() + (days * 86400000));

// return value
    return (odate.getMonth()+1) + '/' + odate.getDate() + '/' + 20 + odate.getYear();
}

function showQuickSchedule(inputID, infoID, dateID, doneDateAdd, addressConfirm, addressConfirmBox) {
	var options = document.getElementById(inputID).getElementsByTagName("option");
	hideDiv(infoID);
	hideDiv(addressConfirm);
	document.getElementById('scheduleDuration').selectedIndex = 0;
	document.getElementById('currentDateNew').readOnly = false;
	
	for(i = 0; i < options.length; i++) {
		if(options[i].getAttribute("value") == document.getElementById(inputID).value) {
			// Okay, we've got a live one here.  Try and find out what it says.
			if(options[i].getAttribute("title").indexOf("Show quick schedule") != -1) {
				showDiv(infoID);
				document.getElementById(addressConfirmBox).value = 0;
			}
			if(options[i].getAttribute("title").indexOf("Add five business days") != -1 && document.getElementById(doneDateAdd).value == 0) {
				document.getElementById(dateID).value = business_days_after(document.getElementById(dateID).value, 5);
				document.getElementById(doneDateAdd).value = 1;
				document.getElementById(addressConfirmBox).value = 0;
			}
			if(options[i].getAttribute("title").indexOf("Disable meeting date") != -1) {
				document.getElementById('currentDateNew').readOnly = true;
			}
			if(options[i].getAttribute("title").indexOf("Change duration to one hour") != -1) {
				document.getElementById('scheduleDuration').selectedIndex = 3;
			}
			if(options[i].getAttribute("title").indexOf("Change duration to two hours") != -1) {
				document.getElementById('scheduleDuration').selectedIndex = 5;
			}
			if(options[i].getAttribute("title").indexOf("Confirm address manually") != -1) {
				showDiv(addressConfirm);
				document.getElementById(addressConfirmBox).value = 1;
			}
			else {
				document.getElementById(addressConfirmBox).value = 0;
			}
		}
	}
}

function oneHourIncrement(startID, endID, checkID) {
	if(parseInt(document.getElementById(checkID).value) == 0) {
		var endValue = parseInt(document.getElementById(startID).value) + 60;
		document.getElementById(endID).value = endValue;
	}
}

function callCenterEdit(openWhat) {
	callCenterWindow = window.open(openWhat,'mywin','left=40,top=40,width=600,height=500,toolbar=0,resizable=1');
}
