var picSide = new Image();
var globalRotateTestimoniesCount = 0;
var globalRotateItCount = 0;
var courseSelect = "";	//Storing a reference to which class is selected
var picerror = "img/error.gif";
picSide.src = "img/right.gif";


//Course Swapping
document.observe('dom:loaded', alertIt);
//Show Terms
document.observe('dom:loaded', showIt);
//Show Testimonies
document.observe('dom:loaded', startTest);
//Show Instructors
document.observe('dom:loaded', startInstructor);
//Tab Flipping
document.observe('dom:loaded', pageFlip);
//Tab Flipping
document.observe('dom:loaded', flipIt);
//Form Ajax
document.observe('dom:loaded', ajaxIt);
//Calculate the Price
document.observe('dom:loaded', calcPriceStart);
//Testimony Rotation
//document.observe('dom:loaded', rotateTestimonies);
//Image Rotation
//document.observe('dom:loaded', rotateIt);
//Image Swapping 
//document.observe('dom:loaded', startIt);

//Calculate the price: Sets off the change event
function calcPriceStart() {
	$("numStudents").observe("change", calcPrice);	
	$("numStudents").observe("keypress", calcPrice);
}

//Calculate the price: Get the price from server
function calcPrice() {
	var emailIt=$("emailIt").value="no";
	
	new Ajax.Request( "php/emailIt.php", { 
			method:"post",
			parameters: $("classRequest").serialize(true),			
			onSuccess: updatePrice
	});
}

//Calculate the price: Handle server response
function updatePrice(response){
	$("returnedPrice").innerHTML=response.responseText;
	new Effect.Opacity("returnedPrice", {from: 0, to: 1, duration:.5});
}

//Image Swapping: Get it going
function startIt() {
	var imageId = document.getElementById("imageFlip");
	var imageItems = imageId.getElementsByTagName("img");
	for (var i=0; i<imageItems.length; i++) {
		imageItems[i].onmouseover = switchIt;
		imageItems[i].onmouseout = switchOut;
	}
}

//Image Swapping: Changes onmouseout
function switchOut() {
	var imageOver=this;
	new Effect.Morph(imageOver,{style:'border:3px solid #F2F2F2;', duration:.5});
}

//Image Swapping: Changes onmouseover
function switchIt() {
	var operatingSys = macOSCheck();	
	var imageOver = this;
	var pictures = $("imageFlip").getElementsByTagName("img");
	
	for(var i=0; i<pictures.length; i++) {
		if (imageOver.src == pictures[i].src) {
			$("swapFocus").innerHTML=$("ourFocus").getElementsByTagName("div")[i].innerHTML;
			$("swapFocus").style.display="none";
		}
	}
	if(operatingSys=="mac") {	
		new Effect.Parallel([
			new Effect.Morph(imageOver, {sync:true, style:'border:3px solid #606060;'}),
			new Effect.Appear("swapFocus", {sync:true, from:0, to:1})
			], {duration:.5}
		);	
	} else {
		new Effect.Parallel([
			new Effect.Morph(imageOver, {sync:true, style:'border:3px solid #606060;'}),
			new Effect.Appear("swapFocus", {sync:true, from:0, to:.99})
			], {duration:.5}
		);		
	}
}

//Image Rotation: Rotate through images
function rotateIt() {
	var operatingSys = macOSCheck();
	var pictures = $("imageFlip").getElementsByTagName("img");
	
	if (globalRotateItCount==3) {
		new Effect.Morph(pictures[globalRotateItCount-1],{style:'border:3px solid #F2F2F2;', duration:.5});
		globalRotateItCount=0;		
	}
	if (globalRotateItCount!=0) {
		new Effect.Morph(pictures[globalRotateItCount-1],{style:'border:3px solid #F2F2F2;', duration:.5});
	}
	for(var i=0; i<pictures.length; i++) {
		if (globalRotateItCount == i) {
			$("swapFocus").innerHTML=$("ourFocus").getElementsByTagName("div")[i].innerHTML;
			$("swapFocus").style.display="none";
		}
	}
	if(operatingSys=="mac") {
		new Effect.Parallel([
			new Effect.Morph(pictures[globalRotateItCount], {sync:true, style:'border:3px solid #606060;'}),
			new Effect.Appear("swapFocus", {sync:true, from:0, to:1})
			], {duration:.5}
		);
	} else {
		new Effect.Parallel([
			new Effect.Morph(pictures[globalRotateItCount], {sync:true, style:'border:3px solid #606060;'}),
			new Effect.Appear("swapFocus", {sync:true, from:0, to:.99})
			], {duration:.5}
		);		
		
	}
	globalRotateItCount++;
	setTimeout("rotateIt()", 4000);
}

//Rotate Testimonies
function rotateTestimonies() {
	if ($("coursePage")){
		var testimonies = $("adRotation").getElementsByTagName("div");
	
		if (globalRotateTestimoniesCount==6) {
			globalRotateTestimoniesCount=0;		
		}
	
		for(var i=0; i<testimonies.length; i++) {
			if (globalRotateTestimoniesCount == i) {
				$("swapTestimony").innerHTML=testimonies[i].innerHTML;
			}
		}
	
		new Effect.Opacity("swapTestimony", {duration:.5, from: 0, to: .99});
		globalRotateTestimoniesCount++;
		setTimeout("rotateTestimonies()", 8000);
	}
}

//Form Validation & Form Ajax: Get it going
function ajaxIt() {
	$("submit").observe("click", checkForm);
}

//Form Ajax: Create Request
function getItUp(){
	//var fullName= $("fullName").value;
	//var email=$("email").value;
	//var phone=$("phone").value;
	//var course=$("course").value;
	//var timeFrame=$("timeFrame").value;
	//var emailIt=$("emailIt").value;
	
	var emailIt=$("emailIt").value="yes";
	
	new Ajax.Request( "php/emailIt.php", { 
			method:"post",
			parameters: $("classRequest").serialize(true),
			onSuccess: updateIt
	});
}

//Form Ajax: Deal with the response
function updateIt(response){
	$("getMe").innerHTML=response.responseText;
	new Effect.Opacity("classRequest", {from: 0, to: 1, duration:.5});
}
	
//Form Validation:
function checkForm(e) {
	var errorVal = "";
	document.getElementById("picname").setAttribute('src', 'img/errornone.gif');			
	document.getElementById("picemail").setAttribute('src', 'img/errornone.gif');
	document.getElementById("picphone").setAttribute('src', 'img/errornone.gif');
	errorVal = errorCheck();
	if (errorVal==false){	
		if (e.srcElement) {	//IIE
			e.returnValue=false;
		}
	}else {
		getItUp(); //Form Ajax
		document.getElementById("errorreport").innerHTML="<h3 class='post'>We received your information!</h3>";
	}
}
	
//Form Validation: Compiling errors
function errorCheck() {
	var errormess="";
	errormess = checkPhone();
	errormess += checkEmail();
	errormess += checkName();	
	return errorReport(errormess);
}

//Form Validation: Check phone number
function checkPhone() {
	var returnVal=0;
	var checkphone = document.getElementById("phone").value; 
	checkphone += ""; 
	if( checkphone.length<10 ) { 
		document.getElementById("picphone").setAttribute('src', picerror);
		document.getElementById("phone").focus();
		returnVal = 1;
	} 
	return returnVal;
}
			
//Form Validation: Check email
function checkEmail() {
	var returnVal=0;
	var checkemail=document.getElementById("email").value;
	var emailreg = new RegExp("[a-zA-Z0-9]@[a-zA-Z0-9]");
	if (!emailreg.exec(checkemail)){
		document.getElementById("picemail").setAttribute('src', picerror);
		document.getElementById("email").focus();				
		returnVal=1;
	}
	return returnVal;
}
	
//Form Validation: Check name
function checkName() {
	var returnVal=0;	
	var checkname=document.getElementById("fullName").value;	
	var namereg = new RegExp(".");
	if(!namereg.exec(checkname)){	
		document.getElementById("picname").setAttribute('src', picerror);
		document.getElementById("fullName").focus();				
		returnVal=1;
	}
	return returnVal;
}
		
//Form Validation: Report error
function errorReport(errormess) {
	if (errormess==0) {
		return true;
	} else {
		var finalmessage = "<h3 class='postError'>Please correct the form errors</h3>";
		document.getElementById("errorreport").innerHTML=finalmessage;
		document.getElementById("errorreport").focus();
		
		return false;
	}
}

//Tab Swapping: Get it going
function pageFlip() {
	$("left").onclick = AjaxCallIt;
	$("right").onclick = AjaxCallIt;
}

//Tab Swapping: Analyze user input
function AjaxCallIt() {
	var tabSelect=this.id;
	var whichOne = 'whichOne=' + tabSelect;	
	var picturePick = "url(img/"+tabSelect+".gif)";
	var leftRight = $("tabSelect").childElements()[0].identify();	
	
	$("selector").style.backgroundImage=picturePick;
	
	if(((tabSelect=="left") && (leftRight=="coursePage")) || ((tabSelect=="right") && (leftRight=="whyChooseUs"))) {
		//Stopping the flip from redundancy
	} else {
		flipIt(tabSelect);
	}
}

//Tab Swapping: Swap tabs
function flipIt(response) {
	if(response=="right"){
		$("tabSelect").innerHTML=$("pageFlipChooseUs").innerHTML;	
		startInstructor(); //add event listener for instructors
	} else {
		$("tabSelect").innerHTML=$("pageFlipCourses").innerHTML;
		startTest(); //add event listener for more testimonies
		if(courseSelect){
			swapIt(1);	//We want to redraw from Why Us?
		}
	}

	var operatingSys = macOSCheck();	
	$("tabSelect").style.display="none";
	if(operatingSys=="mac") {
		new Effect.Appear("tabSelect", {duration:.5, from:0, to:.99});
	} else {
		new Effect.Appear("tabSelect", {duration:.5, from:0, to:1});		
	}
	if($("bodyleftmid").id) {
		alertIt();
	}
}

//Show Testimonies: Get it going
function startTest() {
	$("testOver").observe('click', showTest);
	//document.observe('dom:loaded', alertIt);
}

//Show Testimonies: Display student testimonies
function showTest() {
	$("testSwap").style.display = "none";
	$("testSwap").style.position = "absolute";
	$("testSwap").style.top = ".5em";
	$("testSwap").style.left = ".5em";	
	$("testOverlay").style.width="980px";
	$("testOverlay").style.height="780px";
	$("testInside").style.position = "absolute";
	$("testInside").style.left = "125px";
	$("testInside").style.top = "5px";		
	$("testSwap").innerHTML=$("testimony").innerHTML;
	$("testRight").onclick = closeTest;
	$("testRightBot").onclick = closeTest
	new Effect.Fade("termOver", {duration:.5});	
	new Effect.Appear("testSwap", {duration:.5});
	if ($("timeFrame")) {
		new Effect.Fade("timeFrame", {duration:.4});
		new Effect.Fade("course", {duration:.4});
		new Effect.Fade("numStudents", {duration:.4});	
	}
}

//Show Testimonies: Close the testimony box
function closeTest() {
	new Effect.Fade("testSwap", {duration:.5});
	new Effect.Appear("termOver", {duration:.5});	
	if ($("timeFrame")) {
		new Effect.Appear("timeFrame", {duration:.4});
		new Effect.Appear("course", {duration:.4});
		new Effect.Appear("numStudents", {duration:.4});		
	}
}

//Show Terms: Get it going
function showIt() {
	$("termOver").observe('click', showTerms);	
}

//Show Terms: Display the terms and conditions
function showTerms() {
	var theDate = displayMonth();
	changeDate(theDate);
	$("termsSwap").style.display = "none";
	$("termsSwap").style.position = "absolute";
	$("termsSwap").style.top = ".5em";
	$("termsSwap").style.left = ".5em";	
	$("termsOverlay").style.width="980px";
	$("termsOverlay").style.height="780px";
	$("termsInside").style.position = "absolute";
	$("termsInside").style.left = "125px";
	$("termsInside").style.top = "5px";		
	$("termsSwap").innerHTML=$("termsConditions").innerHTML;
	$("termsRight").onclick = closeTerms;
	$("termsRightBot").onclick = closeTerms;	
	new Effect.Fade("termOver", {duration:.5});
	new Effect.Appear("termsSwap", {duration:.5});
	if ($("timeFrame")) {
		new Effect.Fade("timeFrame", {duration:.4});
		new Effect.Fade("course", {duration:.4});
		new Effect.Fade("numStudents", {duration:.4});	
	}
}	

//Show Terms: Close the terms box
function closeTerms() {
	new Effect.Fade("termsSwap", {duration:.5});
	new Effect.Appear("termOver", {duration:.5});
	if ($("timeFrame")) {
		new Effect.Appear("timeFrame", {duration:.4});
		new Effect.Appear("course", {duration:.4});
		new Effect.Appear("numStudents", {duration:.4});	
	}
}

//Show Terms: Get the current month
function displayMonth() {
	var now = new Date();
	var theDate = now.getMonth();
	var monthWords = new Array("January", "February", "March", "April", "May", "June", "July", 
		"August", "September", "October", "November", "December");
	return monthWords[theDate];
}

function changeDate(theDate) {
	$("whichMonth").innerHTML = theDate;
}

//Show Instructors: Get it going
function startInstructor() {
	$("meetTeam").onclick = showInstructor;
}

//Show Instructors: Display the instructors
function showInstructor() {
	new Effect.Fade("instructorSwap2", {duration:.5});		
	$("instructorSwap").style.display = "none";
	$("instructorSwap").style.position = "absolute";
	$("instructorSwap").style.top = ".5em";
	$("instructorSwap").style.left = ".5em";	
	$("instructorOverlay").style.width="980px";
	$("instructorOverlay").style.height="780px";
	$("instructorInside").style.position = "absolute";
	$("instructorInside").style.left = "25px";
	$("instructorInside").style.top = "5px";		
	$("instructorSwap").innerHTML=$("instructors").innerHTML;
	
	$("instructorRight").onclick = closeInstructor;
	$("instructorRightBot").onclick = closeInstructor;
	new Effect.Fade("termOver", {duration:.5});	
	new Effect.Appear("instructorSwap", {duration:.5});
	if ($("timeFrame")) {
		$("timeFrame").style.display = "none";
		$("course").style.display = "none";
	}
	$("instructorRightNext").onclick = showInstructor2;
	$("instructorRightBotNext").onclick = showInstructor2;
}

//Show Instructors: Close the instructor box
function closeInstructor() {
	new Effect.Fade("instructorSwap", {duration:.5});
	new Effect.Appear("termOver", {duration:.5});	
	if ($("timeFrame")) {
		$("timeFrame").style.display = "inline";
		$("course").style.display = "inline";	
	}
}

function showInstructor2() {
	new Effect.Fade("instructorSwap", {duration:.5});	
	$("instructorSwap2").style.display = "none";
	$("instructorSwap2").style.position = "absolute";
	$("instructorSwap2").style.top = ".5em";
	$("instructorSwap2").style.left = ".5em";	
	$("instructorOverlay2").style.width="980px";
	$("instructorOverlay2").style.height="780px";
	$("instructorInside2").style.position = "absolute";
	$("instructorInside2").style.left = "25px";
	$("instructorInside2").style.top = "5px";		
	$("instructorSwap2").innerHTML=$("instructors2").innerHTML;
	
	$("instructorRight2").onclick = closeInstructor2;
	$("instructorRightBot2").onclick = closeInstructor2;
	new Effect.Fade("termOver", {duration:.5});	
	new Effect.Appear("instructorSwap2", {duration:.5});
	if ($("timeFrame")) {
		$("timeFrame").style.display = "none";
		$("course").style.display = "none";
	}
	$("instructorRightNext2").onclick = showInstructor;
	$("instructorRightBotNext2").onclick = showInstructor;

}

//Show Instructors: Close the instructor box
function closeInstructor2() {
	new Effect.Fade("instructorSwap2", {duration:.5});
	new Effect.Appear("termOver", {duration:.5});	
	if ($("timeFrame")) {
		$("timeFrame").style.display = "inline";
		$("course").style.display = "inline";	
	}
}	

//Course Swapping: Get it going
function alertIt() {
	var listId = document.getElementById("bodyleftmid");
	var listIdRight = $("bodyrightmid");
	var listItems = listId.getElementsByTagName("li");
	var listItemsRight = listIdRight.getElementsByTagName("li");
	for (var i=0; i<listItems.length; i++) {
		listItems[i].style.cursor = "pointer";
		listItems[i].onclick = swapIt;
	}
	for (var i=0; i<listItemsRight.length; i++) {
		listItemsRight[i].style.cursor = "pointer";
		listItemsRight[i].onclick = swapIt;
	}
}

//Course Swapping: Changes Link onmouseout
function morphIt() {
	var listItems = document.getElementById("bodyleftmid").getElementsByTagName("li");
	var listItemsRight = $("bodyrightmid").getElementsByTagName("li");
	for (var i=0; i<listItems.length; i++){
		listItems[i].style.color="#FFF";
	}
	for (var i=0; i<listItemsRight.length; i++){
		listItemsRight[i].style.color="#FFF";	
	}
}

//Course Swapping: Changes onmouseover
function swapIt(redrawIt) {
	var operatingSys = macOSCheck();
	var linkTitle;
	var linkOver;
	var getCourse;

	if (redrawIt==1) {	//Redraw page from Why Us
		linkOver=courseSelect.id;	
	} else {	//Standard class swapping
		linkOver=this.id;
		linkTitle=$(linkOver).firstChild.nodeValue;
		courseSelect=this;		
	}
	
	var courseList=$("courseListings").getElementsByTagName("div");
	var regExpression = /c/;
	var courseNumber = linkOver.replace(regExpression, "");
	var courseArray = parseInt(courseNumber) + 1;
	
	linkTitle=$(linkOver).firstChild.nodeValue;
	morphIt();
	if($("course")){
		$("course").options[courseArray].selected = true;
	}
	getCourse = courseList[courseNumber];
	$("classSwapper").innerHTML=getCourse.innerHTML;
	$("classSwapper").style.display="none";
	$("h2Text").innerHTML="<span id='linkFade2'>"+ linkTitle + "</span>";
	$("h2Text").style.display="none";
	
	if(operatingSys=="mac") {
		new Effect.Parallel([
			new Effect.Appear("classSwapper", {sync:true, from:0, to:1}),
			new Effect.Appear("h2Text", {sync:true, from:.5, to:1}),
			new Effect.Opacity("courseBox", {sync:true, from:.9, to:1})
			], {duration:.2}
		);
	} else {
		new Effect.Parallel([
			new Effect.Appear("classSwapper", {sync:true, from:.5, to:1}),
			new Effect.Appear("h2Text", {sync:true, from:.5, to:.99}),
			new Effect.Opacity("courseBox", {sync:true, from:.9, to:1})
			], {duration:.2}
		);
	}
	$(linkOver).style.color="#606060";
}

//Check if the browsing is on the Mac OS
function macOSCheck() {
	var whichOS = "";
	var operatingSystem = navigator.platform;
	if(operatingSystem=="MacIntel") {
		whichOS="mac";
	}
	return whichOS;
}

