// Russell Thompson
// Project: Freelance I.T. Solutions V3 (2009)
// Page: JavaScript / AJAX code
// Jan 2009
// copyright 2009 - you are welcome to copy / use / distribute the code FROM this file as much as you want.
// THIS file may NOT be used, copied, downloaded or altered in any way.

// GLOBAL VARIABLES

var windowX = 0;
var windowY = 0;
var divTop = 0;
var divLeft = 0;
var isIE = (navigator.userAgent.indexOf("MSIE") != -1); // this is to handle javascript bugs with older versions of IE

// FUNCTIONS FOR THE PHOTO GALLERY

function runajax(objId, serverPage, post_get)
{
	// check for a valid IE instance
	var xmlhttp = false;
	
	// using IE?
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		// check for older versions of IE
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			// using a non-Microshit browser
			xmlhttp = false;
		}
	}
	
	// if not using IE, create an instand of the object
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	
	// decide to do POST or GET
	if (post_get)
	{
		// since this is added late, and only for GET instances, post_get should always = get
		var obj = document.getElementById(objId);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		
		xmlhttp.send(null);
	}
	else
	{
	
		// extract the server page and the variable strings from 'serverPage' to be send using POST
		var findChar = "?";
		var indexSpot = serverPage.indexOf(findChar);
		var pageString = serverPage.substring(0, indexSpot);
		// for testing -> alert("pageSting = " + pageString + ".");
		var varString = serverPage.substring(indexSpot+1);
		// for testing -> alert("varString = " + varString + ".");
		var obj = document.getElementById(objId);
		xmlhttp.open("POST", pageString, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		
		xmlhttp.send(varString);
	} // end if-else 
}

// FUNCTION TO CALC BROWSER SIZE

function calcWindowXY()
{
	if (typeof window.innerWidth == 'number')
	{
		windowX = window.innerWidth;
		windowY = window.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientWidth)
	{
		windowX = document.documentElement.clientWidth;
		windowY = document.documentElement.clientHeight;
	}
	else
	{
		windowX = document.body.clientWidth;
		windowY = document.body.clientHeight;
	}
}

function calcDivTopLeft(givenWidth, givenHeight)
{
	displayX = givenWidth / 2;
	displayY = givenHeight / 2;
	
	centerX = windowX / 2;
	centerY = windowY / 2;
	
	divTop = centerY - displayY;
	divLeft = centerX - displayX;
}

function modalPopup(passWidth, passHeight, objId, serverPage, post_get)
{
	calcDivTopLeft(passWidth, passHeight);
	
	// make sure the window is at the top, left
	window.scrollTo(0, 0);
	
	// if user's browser is IE6 or lower, hide selects because of IE bug
	if (isIE)
	{
		for (var i = 0; i < document.getElementsByTagName('select').length; i++)
		{
			document.getElementsByTagName('select')[i].style.visibility = "hidden";
		}
	}
	
	// give div's some 'body'
	document.getElementById('popupBackgroundDiv').style.visibility = "visible";
	
	document.getElementById('popupDisplayDiv').style.top = divTop + "px";
	document.getElementById('popupDisplayDiv').style.left = divLeft + "px";
	document.getElementById('popupDisplayDiv').style.width = passWidth + "px";
	document.getElementById('popupDisplayDiv').style.height = passHeight + "px";
	document.getElementById('popupDisplayDiv').style.visibility = "visible";
	
	var innerDivWidth = passWidth - 22;
	var innerDivHeight = passHeight - 32;
	document.getElementById('popupInnerDisplayDiv').style.width = innerDivWidth + "px";
	document.getElementById('popupInnerDisplayDiv').style.height = innerDivHeight + "px";
	
	// position the close link
	var closeLeft = passWidth - 90;
	document.getElementById('closePopup').style.left = closeLeft + "px";
	
	// for testing
	// alert("serverPage = " + serverPage + ".");
	// alert("objId = " + objId + ".");
	// end test code
	
	runajax(objId, serverPage, post_get);
}

function modalPopupOff()
{
	// force display div to the top (otherwise, when the user opens a new form, it will be at the scroll location of the last form the user was viewing)
	document.getElementById('popupInnerDisplayDiv').scrollTop = 0;
	
	// hide the popup divs
	document.getElementById('popupDisplayDiv').style.visibility = "hidden";
	document.getElementById('popupBackgroundDiv').style.visibility = "hidden";
	
	// if user's browser is IE6 or lower, the selects have been hidden because of IE bug
	// now must un-hide them
	if (isIE)
	{
		for (var i = 0; i < document.getElementsByTagName('select').length; i++)
		{
			document.getElementsByTagName('select')[i].style.visibility = "visible";
		}
	}
	
	// if the user is on the index page, the flash animation has been hidden for the modalLayer to work
	// now un-hide it
	//if (document.getElementById('index_flash'))
	//{
	//	document.getElementById('index_flash').style.visibility = "visible";
	//}
	
	// id the collapsed rows are now visible, they must be collapsed again when the form is turned off or they will remain visible even after the rest of the form is gone.
	// find all tr's in the page
	var trElem = document.getElementsByTagName('tr');
	
	// now move through each one
	for (var t = 0; t < trElem.length; t++)
	{
		// find all tr's with the class name 'formTableRowCollapse'
		if (trElem[t].className == "formTableRowCollapse")
		{
			// check to see if FF (and others) or IE
			if (window.getComputedStyle)
			{
				// this is FF et al - hide tr's
				trElem[t].style.visiblility = "collapse";
				trElem[t].style.display = "none";				
			}
			else
			{
				// this is for IE - hide tr's
				trElem[t].style.display = "none";
			}
		} // end the if to find class name match
	} // end for loop to hide collapsable stuff
}

// function to prepare the serverPage string for AJAX
function prepAJAX(theSwitch, theForm, theObj, special)
{
	switch (theSwitch)
	{
		case "applications":
			var sendPage = "pages/forms.php?theForm=applications";
			modalLayerOff();
			document.getElementById('popupTitle').innerHTML = "Request Application Information";
			
			modalPopup(650, 400, theObj, sendPage, 'get');
		break;
		
		case "webQuote":
			var sendPage = "pages/forms.php?theForm=webQuote";
			modalLayerOff();
			document.getElementById('popupTitle').innerHTML = "Request Web Design Information";
			
			modalPopup(650, 400, theObj, sendPage, 'get');
		break;
		
		case "marketing":
			var sendPage = "pages/forms.php?theForm=marketing";
			modalLayerOff();
			document.getElementById('popupTitle').innerHTML = "Contact Our Marketing Team";
			
			modalPopup(650, 400, theObj, sendPage, 'get');
		break;
		
		case "hosting":
			var sendPage = "pages/forms.php?theForm=hosting";
			modalLayerOff();
			document.getElementById('popupTitle').innerHTML = "Hosting Price List";
			
			modalPopup(650, 400, theObj, sendPage, 'get');
		break;
		
		case "resMask":
			var myform = theForm;
			var theOccupants = 0;
			
			if (document.getElementById('index_flash'))
			{
				var sendPage = "reserveProcessIndex.php?";
			}
			else
			{
				var sendPage = "../pages/reserveProcess.php?"
			}
			
			if (!special)
			{
				for (var i = 0; i < myform.elements.length; i++)
				{
					sendPage += myform.elements[i].name + "=" + myform.elements[i].value + "&";
				}
			}
			else
			{
				for (var i = 0; i < myform.elements.length; i++)
				{
					if (myform.elements[i].name == "arrival_month")
					{
						theArrivalMonth = myform.elements[i].value;
						
						sendPage += myform.elements[i].name + "=" + myform.elements[i].value + "&";
					}
					else if (myform.elements[i].name == "arrival_day")
					{
						theArrivalDay = myform.elements[i].value;
						
						sendPage += myform.elements[i].name + "=" + myform.elements[i].value + "&";
					}
					else if (myform.elements[i].name == "arrival_year")
					{
						theArrivalYear = myform.elements[i].value;
						
						sendPage += myform.elements[i].name + "=" + myform.elements[i].value + "&";
					}
					else if (myform.elements[i].name == "depart_month")
					{
						theDepartMonth = myform.elements[i].value;
					}
					else if (myform.elements[i].name == "depart_day")
					{
						theDepartDay = myform.elements[i].value;
					}
					else if (myform.elements[i].name == "depart_year")
					{
						theDepartYear = myform.elements[i].value;
					}
					else if (myform.elements[i].name == "adults")
					{
						theOccupants += parseInt(myform.elements[i].value);
					}
					else if (myform.elements[i].name == "children")
					{
						theOccupants += parseInt(myform.elements[i].value);
					}
					else if (myform.elements[i].name == "rooms")
					{
						sendPage += "numOfRooms=" + myform.elements[i].value + "&";
					}
					else
					{
						sendPage += myform.elements[i].name + "=" + myform.elements[i].value + "&";
					}
				}  // end of for loop
						
				var arrivalDate = new Date(theArrivalYear, theArrivalMonth - 1, theArrivalDay);
				var departDate = new Date(theDepartYear, theDepartMonth - 1, theDepartDay);
				
				numOfNights = dateDifference(arrivalDate, departDate);
				
				sendPage += "occupants=" + theOccupants + "&numOfNights=" + numOfNights + "&";
			} // end if-else
			
			// for testing
			// alert("The URL = " + sendPage + ".");
			// end testing
			// for test -> alert("Arrival Date: " + arrivalDate + " Departure Date: " + departDate + ".");
			// for test -> alert("the object: " + theObj + ".");
			
			modalPopup(650, 350, theObj, sendPage);
		break;
		
		case "friendProcess":
			var myform = theForm;
			// this is for testing -> var sendPage = "../pages/checkValues.php?";
			var sendPage = "../pages/tellAFriend.php?";
			
			for (var i = 0; i < myform.elements.length; i++)
			{
				
				sendPage += myform.elements[i].name + "=" + myform.elements[i].value + "&";
			}
			
			runajax(theObj, sendPage, 'get');
		break;
	} // end switch
	
	// for testing
	// alert("The URL = " + sendPage + ".");
	// end testing
	
} // end prepAJAX function

// validation function(s)
function validateForm(theForm)
{
	switch (theForm)
	{
		case "tellAFriend_form":
			var returnValue = false;
			var myForm = document.tellAFriend_form;
			
			if (myForm.rName.value == "" || myForm.rName.value == " ")
			{
				alert("The recipient's name is required.");
				myForm.rName.focus();
			}
			else if (myForm.rEmail.value == "" || myForm.rEmail.value == " ")
			{
				alert("The recipient's e-mail address is required.");
				myForm.rEmail.focus();
			}
			else if (myForm.sName.value == "" || myForm.sName.value == " ")
			{
				alert("Please enter your name.  It is required to send this message.");
				myForm.sName.focus();
			}
			else if (myForm.sEmail.value == "" || myForm.sEmail.value == " ")
			{
				alert("Please enter your e-mail address.  It is required to send this message.");
				myForm.sEmail.focus();
			}
			else
			{
				returnValue = true;
			}
			
			return returnValue;
		break;
		
		case "app":
			var returnValue = false;
			var myForm = document.appRequest;
			
			if (myForm.humanCode.value == "" || myForm.humanCode.value == " " || myForm.humanCode.value == null || myForm.humanCode.value.length == 0)
			{
				alert("This form will not be processed without a correct value in the 'Human Code' field.  Please enter the code.");
				myForm.humanCode.focus();
			}
			else
			{
				var genHumanCode = formHumanCode(myForm.humanCode.value);
				
				if (genHumanCode == myForm.theHumanCode.value)
				{
					if (myForm.fullName.value == "" || myForm.fullName.value == " " || myForm.fullName.value == null)
					{
						alert("Please enter a name so that we know who to contact (it is required).");
						myForm.fullName.focus();
					}
					else if (myForm.phone.value == "" || myForm.phone.value == " " || myForm.phone.value == null)
					{
						if (myForm.email.value == "" || myForm.email.value == " " || myForm.email.value == null)
						{
							alert("Please enter a phone number or e-mail address so that we have a way to contact you.");
							myForm.email.focus();
						}
					}
					else
					{
						returnValue = true;
					}
				} // end if else that checks other required fields
			} // end if else that checks human code
			
			if (returnValue == true)
			{
				// this preps for the ajax call
				var sendPage = "pages/forms.php?";
				var theObj = "popupInnerDisplayDiv";
			
				for (var i = 0; i < myForm.elements.length; i++)
				{
					if (myForm.elements[i].type == "radio")
					{
						// for testing ->						alert("Inside radio if.");
						
						if (myForm.elements[i].checked)
						{
							sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
						}
					}
					else if (myForm.elements[i].type == "checkbox")
					{
						// for testing -> 						alert("Inside checkbox if.");
						
						if (myForm.elements[i].checked)
						{
							sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
						}
					}
					else
					{
						sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
					}
				}
				
				// for testing -> 				alert("The url: " + sendPage);
				runajax(theObj, sendPage, 'get');
			}
			
			return returnValue;				
		break;
		
		case "web":
			var returnValue = false;
			var myForm = document.webRequest;
			
			if (myForm.humanCode.value == "" || myForm.humanCode.value == " " || myForm.humanCode.value == null)
			{
				alert("This form will not be processed without a correct value in the 'Human Code' field.  Please enter the code.");
				myForm.humanCode.focus();
			}
			else
			{
				var genHumanCode = formHumanCode(myForm.humanCode.value);
				
				if (genHumanCode == myForm.theHumanCode.value)
				{
					if (myForm.fullName.value == "" || myForm.fullName.value == " " || myForm.fullName.value == null)
					{
						alert("Please enter a name so that we know who to contact (it is required).");
						myForm.fullName.focus();
					}
					else if (myForm.phone.value == "" || myForm.phone.value == " " || myForm.phone.value == null)
					{
						if (myform.email.value == "" || myForm.email.value == " " || myForm.email.value == null)
						{
							alert("Please enter a phone number or e-mail address so that we have a way to contact you.");
							myForm.email.focus();
						}
					}
					else
					{
						returnValue = true;
					}
				} // end if else that checks other required fields
			} // end if else that checks human code
			
			if (returnValue == true)
			{
				// this preps for the ajax call
				var sendPage = "pages/forms.php?";
				var theObj = "popupInnerDisplayDiv";
			
				for (var i = 0; i < myForm.elements.length; i++)
				{
					if (myForm.elements[i].type == "radio")
					{
						// for testing ->						alert("Inside radio if.");
						
						if (myForm.elements[i].checked)
						{
							sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
						}
					}
					else if (myForm.elements[i].type == "checkbox")
					{
						// for testing -> 						alert("Inside checkbox if.");
						
						if (myForm.elements[i].checked)
						{
							sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
						}
					}
					else
					{
						sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
					}
				}
				
				// for testing -> 				alert("The url: " + sendPage);
				runajax(theObj, sendPage, 'get');
				
			}
			
			return returnValue;				
		break;
		
		case "marketing":
			var returnValue = false;
			var myForm = document.marketingRequest;
			
			if (myForm.humanCode.value == "" || myForm.humanCode.value == " " || myForm.humanCode.value == null)
			{
				alert("This form will not be processed without a correct value in the 'Human Code' field.  Please enter the code.");
				myForm.humanCode.focus();
			}
			else
			{
				var genHumanCode = formHumanCode(myForm.humanCode.value);
				
				if (genHumanCode == myForm.theHumanCode.value)
				{
					if (myForm.fullName.value == "" || myForm.fullName.value == " " || myForm.fullName.value == null)
					{
						alert("Please enter a name so that we know who to contact (it is required).");
						myForm.fullName.focus();
					}
					else if (myForm.phone.value == "" || myForm.phone.value == " " || myForm.phone.value == null)
					{
						if (myform.email.value == "" || myForm.email.value == " " || myForm.email.value == null)
						{
							alert("Please enter a phone number or e-mail address so that we have a way to contact you.");
							myForm.email.focus();
						}
					}
					else
					{
						returnValue = true;
					}
				} // end if else that checks other required fields
			} // end if else that checks human code
			
			if (returnValue == true)
			{
				// this preps for the ajax call
				var sendPage = "pages/forms.php?";
				var theObj = "popupInnerDisplayDiv";
			
				for (var i = 0; i < myForm.elements.length; i++)
				{
					if (myForm.elements[i].type == "radio")
					{
						// for testing ->						alert("Inside radio if.");
						
						if (myForm.elements[i].checked)
						{
							sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
						}
					}
					else if (myForm.elements[i].type == "checkbox")
					{
						// for testing -> 						alert("Inside checkbox if.");
						
						if (myForm.elements[i].checked)
						{
							sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
						}
					}
					else
					{
						sendPage += myForm.elements[i].name + "=" + myForm.elements[i].value + "&";
					}
				}
				
				// for testing -> 				alert("The url: " + sendPage);
				runajax(theObj, sendPage, 'get');
				
			}
			
			return returnValue;				
		break;
	} // end switch
} // end validateForm function

function formHumanCode(passedCode)
{
	var charArray = new Array(passedCode.substring(0, 1), passedCode.substring(1, 2), passedCode.substring(2, 3), passedCode.substring(3, 4), passedCode.substring(4, 5), passedCode.substring(5, 6), passedCode.substring(6));
	var numArray = new Array(7);
	
	for (var x = 0; x < charArray.length; x++)
	{
		switch (charArray[x])
		{
			case "A":
				numArray[x] = 1;
			break;
			
			case "a":
				numArray[x] = 11;
			break;
			
			case "B":
				numArray[x] = 2;
			break;
			
			case "b":
				numArray[x] = 12;
			break;
			
			case "C":
				numArray[x] = 3;
			break;
			
			case "c":
				numArray[x] = 13;
			break;
			
			case "D":
				numArray[x] = 4;
			break;
			
			case "d":
				numArray[x] = 14;
			break;
			
			case "E":
				numArray[x] = 5;
			break;
			
			case "e":
				numArray[x] = 15;
			break;
			
			case "F":
				numArray[x] = 6;
			break;
			
			case "f":
				numArray[x] = 16;
			break;
			
			case "G":
				numArray[x] = 7;
			break;
			
			case "g":
				numArray[x] = 17;
			break;
			
			case "1":
				numArray[x] = 8;
			break;
			
			case "2":
				numArray[x] = 18;
			break;
			
			case "3":
				numArray[x] = 9;
			break;
			
			case "4":
				numArray[x] = 19;
			break;
			
			case "5":
				numArray[x] = 10;
			break;
			
			case "6":
				numArray[x] = 20;
			break;
			
			case "7":
				numArray[x] = 0;
			break;
		} // end switch that coverts chars to nums
	} // end for loop
	
	// for testing -> 	alert("the chars are: "+ charOne + ", " + charTwo + ", " + charThree + ", " + charFour + ", " + charFive + ", " + charSix + ", " + charSeven + ".");
	
	var convertedHumanCode = numArray.join("");
	
	// for testing -> 	alert("The converted string: " + convertedHumanCode + ".");
	
	return convertedHumanCode;
	
} // end formHumanCode function
