// Function to get contact for page
function getPageContact()
{
	//
	var url = "scripts/getPageContact.php";
	httpRequest("GET", url, true, hr_getPageContact);
}

// Function to handle getPageContact() results
function hr_getPageContact()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivPage = document.getElementById("divPageInfo");
			var eleDivSection = document.getElementById("divSectionInfo");
			var eleDivNavigation = document.getElementById("divNav");
			
			var xmlDoc = request.responseXML;
			//alert(xmlDoc);
			
			var pageTitle = xmlDoc.getElementsByTagName("p-title")[0].firstChild.nodeValue;
			var pageDescription = xmlDoc.getElementsByTagName("p-description")[0].firstChild.nodeValue;
			var pagePath = xmlDoc.getElementsByTagName("p-path")[0].firstChild.nodeValue;
			
			var strHtml = "<h2>" + pageTitle + "</h2>";
			strHtml += "<p>" + pageDescription + "</p>";
			
			// Create the page header and description. Eliminate navigation
			//eleDivPage.innerHTML = strHtml;
			//eleDivNavigation.innerHTML = "";
			//getContactForm();
			//var strNewSection = "<h2>" + pageTitle + " Us</h2>";
			//strNewSection += "<p>" + getContactForm() + "</p>";
			// Create the section header and the first three examples in the sections
			//eleDivSection.innerHTML = strNewSection;
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to get contact for page
function getContactForm()
{
	//
	var url = "scripts/getContactForm.php";
	httpRequest("GET", url, true,  hr_getContactForm);
}

// Function to handle getPageContact() results
function hr_getContactForm()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivSection = document.getElementById("divSectionInfo");
			
			var textDoc = request.responseText;
			//alert(textDoc);
			
			var strNewSection = "<h2>Contact Us</h2>";
			strNewSection += "<p>" + textDoc + "</p>";
			
			// Create the section header and the first three examples in the sections
			eleDivSection.innerHTML = strNewSection;
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to page information
function getPageInfo(strPageId)
{
	//
	var url = "scripts/getPageInfo.php";
	httpRequest("POST", url, true, hr_getPageInfo, "&strPageId=" + escape(strPageId));
}

// Function to get returned page information
function hr_getPageInfo()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivPage = document.getElementById("divPageInfo");
			var eleDivSection = document.getElementById("divSectionInfo");
			var xmlDoc = request.responseText;
			//alert(xmlDoc);
			
			var pageTitle = xmlDoc.getElementsByTagName("page-title")[0].firstChild.nodeValue;
			var pageDescription = xmlDoc.getElementsByTagName("page-description")[0].firstChild.nodeValue;
			
			var strHtml = "<h2>" + pageTitle + "</h2>";
			strHtml += "<p>" + pageDescription + "</p>";
			
			// Create the page header and description
			eleDivPage.innerHTML = strHtml;
			
			var strNewHeader = "Header for porjects.";
			
			// Create the section header and the first three examples in the sections
			eleDivSection.innerHTML = strNewHeader + " Just a start --> ";
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to page and section information
function getSection(strPageType)
{
	//
	var url = "scripts/getSection.php";
	httpRequest("POST", url, true, hr_getSection, "&strPageType=" + escape(strPageType));
}

// Function to get returned page and section information
function hr_getSection()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivPage = document.getElementById("divPageInfo");
			var eleDivSection = document.getElementById("divSectionInfo");
			var xmlDoc = request.responseXML;
			//alert(xmlDoc);
			
			var projectCount = xmlDoc.getElementsByTagName("project").length;
			var pageTitle = xmlDoc.getElementsByTagName("page-title")[0].firstChild.nodeValue;
			var pageDescription = xmlDoc.getElementsByTagName("page-description")[0].firstChild.nodeValue;
			var strHtml = "<h2>" + pageTitle + "</h2>";
			strHtml += "<p>" + pageDescription + "</p>";
			
			// Create the page header and description
			eleDivPage.innerHTML = strHtml;
			var strNewHeader = "Header for porjects.";
			
			// Create the section header and the first three examples in the sections
			eleDivSection.innerHTML = strNewHeader + " Just a start --> ";
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to get section info
function getSectionInfo(strSectionId)
{
	var url = "scripts/getSectionInfo.php";
	httpRequest("POST", url, true, hr_getSectionInfo, "&strSectionId=" + escape(strSectionId));
}

// Function to get returned section information
function hr_getSectionInfo()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivSection = document.getElementById("divSectionInfo");
			var xmlDoc1 = request.responseXML;
			//alert(xmlDoc1);
			
			var sectionTitle = xmlDoc1.getElementsByTagName("section-title")[0].firstChild.nodeValue;
			//alert(sectionTitle);
			var sectionDescription = xmlDoc1.getElementsByTagName("section-description")[0].firstChild.nodeValue;
			//alert(sectionDescription);
			
			var strHtml = "<h2>" + sectionTitle + "</h2>";
			strHtml += "<p>" + sectionDescription + "</p>";
			
			eleDivSection.innerHTML = strHtml;
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to page and section information
function getPageAndSection(intStartNum, strPageType)
{
	var url = "scripts/getPageAndSection.php";
	httpRequest("POST", url, true, hr_getPageAndSection, "&intStartNum=" + escape(intStartNum) + "&strPageType=" + escape(strPageType));
}

// Function to get returned page and section information
function hr_getPageAndSection()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivPage = document.getElementById("divPageInfo");
			var eleDivSection = document.getElementById("divSectionInfo");
			var eleDivNavigation = document.getElementById("divNav");
			
			var xmlDoc = request.responseXML;
			//alert(xmlDoc);
			
			var projectCount = xmlDoc.getElementsByTagName("project").length;
			
			var pageTitle = xmlDoc.getElementsByTagName("p-title")[0].firstChild.nodeValue;
			var pageDescription = xmlDoc.getElementsByTagName("p-description")[0].firstChild.nodeValue;
			var sectionTitle = xmlDoc.getElementsByTagName("s-title")[0].firstChild.nodeValue;
			
			var strHtmlPage = "<h2>" + pageTitle + "</h2>";
			strHtmlPage += "<p>" + pageDescription + "</p>";
			
			// Create the section navigation
			var projectBackNav = xmlDoc.getElementsByTagName("back-num")[0].firstChild.nodeValue;
			var projectNextNav = xmlDoc.getElementsByTagName("next-num")[0].firstChild.nodeValue;
			var projectSectionNav = xmlDoc.getElementsByTagName("section-nav")[0].firstChild.nodeValue;
			var projectTotalCount = xmlDoc.getElementsByTagName("total-rows")[0].firstChild.nodeValue;
			
			// If there is a project then create a table, else just a <h3> message.
			var strHtmlSection = "<h2>" + sectionTitle + "</h2>";
			if(projectCount != 0)
			{
				//
				strHtmlSection += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" id=\"screenShots\">"
				
				for(var intI = 0; intI < projectCount; intI++)
				{
					//alert("something = " + intI);
					var projectTitle = xmlDoc.getElementsByTagName("project-title")[intI].firstChild.nodeValue;
					var projectWebPath = xmlDoc.getElementsByTagName("project-webpath")[intI].firstChild.nodeValue;
					var projectThumb = xmlDoc.getElementsByTagName("project-thumb")[intI].firstChild.nodeValue;
					var projectDescription = xmlDoc.getElementsByTagName("project-description")[intI].firstChild.nodeValue;
					var projectCreated = xmlDoc.getElementsByTagName("project-created")[intI].firstChild.nodeValue;
					
					strHtmlSection += "<tr>";
					strHtmlSection += "<td class=\"scrnSht\">";
					strHtmlSection += "<p><a href=\"" + projectWebPath + "\"><img src=\"" + projectThumb + "\" width=\"180\" height=\"125\" alt=\"Project Picture\" /></a></p>";
					strHtmlSection += "</td>";
					strHtmlSection += "<td class=\"scrnDscrptn\">";
					strHtmlSection += "<p><span class=\"bold\">" + projectTitle + "</span><br />" + projectDescription + "<br />";
					strHtmlSection += "<span class=\"smBold\">Created:&nbsp;" + projectCreated + "</span></p>";
					strHtmlSection += "</td>";
					strHtmlSection += "</tr>";
				}
				
				strHtmlSection += "<table>";
			}
			else
			{
				strHtmlSection += "<h3>There are no entries for this section.<h3>";
			}
			
			// Create the page header and description
			eleDivPage.innerHTML = strHtmlPage;
			
			// Create the section header and the first three examples in the sections
			eleDivSection.innerHTML = strHtmlSection;
			
			eleDivNavigation.innerHTML = "<p><a href=\"javascript:getSectionProjects('" + projectBackNav + "', '" + projectSectionNav + "')\">BACK</></a> - <a href= \"javascript:getSectionProjects('" + projectNextNav + "', '" + projectSectionNav + "')\">NEXT</a></p>";
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to page and section information
function getSectionProjects(intStartNum, strSectionType)
{
	//
	var url = "scripts/getSectionProjects.php";
	httpRequest("POST", url, true, hr_getSectionProjects, "&intStartNum=" + escape(intStartNum) + "&strSectionType=" + escape(strSectionType));
}

// Function to get returned page and section information
function hr_getSectionProjects()
{
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivPage = document.getElementById("divPageInfo");
			var eleDivSection = document.getElementById("divSectionInfo");
			var eleDivNavigation = document.getElementById("divNav");
			
			var xmlDoc = request.responseXML;
			var textDoc = request.responseText;
			
			var projectCount = xmlDoc.getElementsByTagName("project").length;
			
			// Create the section navigation
			var projectBackNav = xmlDoc.getElementsByTagName("back-num")[0].firstChild.nodeValue;
			var projectNextNav = xmlDoc.getElementsByTagName("next-num")[0].firstChild.nodeValue;
			var projectSectionNav = xmlDoc.getElementsByTagName("section-nav")[0].firstChild.nodeValue;
			var projectTotalCount = xmlDoc.getElementsByTagName("total-rows")[0].firstChild.nodeValue;
			
			var strHtmlSection = "<h2>" + projectSectionNav + " Projects</h2>";
			
			// If there is a project then create a table, else just a <h3> message.
			if(projectCount != 0)
			{
				// Create Table for projects
				strHtmlSection += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" id=\"screenShots\">";
				
				for(var intI = 0; intI < projectCount; intI++)
				{
					var projectTitle = xmlDoc.getElementsByTagName("project-title")[intI].firstChild.nodeValue;
					var projectWebPath = xmlDoc.getElementsByTagName("project-webpath")[intI].firstChild.nodeValue;
					var projectThumb = xmlDoc.getElementsByTagName("project-thumb")[intI].firstChild.nodeValue;
					var projectDescription = xmlDoc.getElementsByTagName("project-description")[intI].firstChild.nodeValue;
					var projectCreated = xmlDoc.getElementsByTagName("project-created")[intI].firstChild.nodeValue;
					
					strHtmlSection += "<tr>";
					strHtmlSection += "<td class=\"scrnSht\">";
					strHtmlSection += "<p><a href=\"" + projectWebPath + "\"><img src=\"" + projectThumb + "\" width=\"180\" height=\"125\" alt=\"Project Picture\" /></a></p>";
					strHtmlSection += "</td>";
					strHtmlSection += "<td class=\"scrnDscrptn\">";
					strHtmlSection += "<p><span class=\"bold\">" + projectTitle + "</span><br />" + projectDescription + "<br />";
					strHtmlSection += "<span class=\"smBold\">Created:&nbsp;" + projectCreated + "</span></p>";
					strHtmlSection += "</td>";
					strHtmlSection += "</tr>";
				}
				
				strHtmlSection += "<table>";
			}
			else
			{
				strHtmlSection += "<h3>There are no entries for this section.<h3>";
			}
			
			
			// Create the section header and the first three examples in the sections
			eleDivSection.innerHTML = strHtmlSection;
			
			eleDivNavigation.innerHTML = "<p><a href=\"javascript:getSectionProjects('" + projectBackNav + "', '" + projectSectionNav + "')\">BACK</></a> - <a href= \"javascript:getSectionProjects('" + projectNextNav + "', '" + projectSectionNav + "')\">NEXT</a></p>";
			
			//alert("Total Rows = " + projectTotalCount);
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

// Function to get projects
function getProjects(intStartNumber, arrSection)
{
	var url = "scripts/getSectionData.php";
	url += "?newStartNumber=" + intStartNumber;
	url += "&arrType=" + arrSection;
	
	//alert(url);
	request.open("GET", url, true);
	request.onreadystatechange = updateProjects;
	request.send(null);
}

// Function to update projects
function updateProjects()
{
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			/********** Get XML data to insert into the page **********/
			var xmlDoc = request.responseXML;
			var projectElements = xmlDoc.getElementsByTagName("project");
			//alert("Number of project tags is " + projectElements.length);
			
			// Remove and replace projects
			if(projectElements.length == 3)
			{
				showHideProjects(xmlDoc)
				replaceProjects(xmlDoc)
			}
			else if(projectElements.length == 2)
			{
				showHideProjects(xmlDoc)
				replaceProjects(xmlDoc)
			}
			else if(projectElements.length == 1)
			{
				showHideProjects(xmlDoc)
				replaceProjects(xmlDoc)
			}
		}
	}
}

// Function to open a new window.
function openWindow(strUrl, strWidth, strHeight, strScrollbars)
{
	// check that window exist and then resize it and give it focus 
	// if winObject exist()
	// {
	//  winObject.resizeTo(strWidth, strHeight);// resize it
	// 	winObject.focus();   // give the window focus
	// }
	// else
	// {
	
	var winObject = window.open(strUrl, 'windowOne', 'width=' + strWidth + ',height=' + strHeight + ',scrollbars=' + strScrollbars + ', resizable=yes');
	winObject.resizeTo(strWidth, strHeight);
	winObject.focus();
	
	// }
}

// Function to get contact for page
function sendContactForm()
{
	var frmContact = document.frmContact;
	
	//
	if(strArguments = checkForm())
	{
		var strFrmName = frmContact.frmName.value;
		var strFrmEmail = frmContact.frmEmail.value;
		var strFrmPhone = frmContact.frmPhone.value;
		var strFrmCompany = frmContact.frmCompany.value;
		var strFrmAddress = frmContact.frmAddress.value;
		var strFrmCity = frmContact.frmCity.value;
		var strFrmState = frmContact.frmState.value;
		var strFrmComments = frmContact.frmComments.value;
		var strFrmSubmit ="Submit";
		
		// Variable for post arguments
		var strArguments = "&frmSubmit=" + strFrmSubmit + "&frmCompany=" + strFrmCompany + "&frmName=" + strFrmName + "&frmEmail=" + strFrmEmail + "&frmAddress=" + strFrmAddress + "&frmCity=" + strFrmCity + "&frmState=" + strFrmState + "&frmPhone=" + strFrmPhone + "&frmComments=" + strFrmComments;
		
		//
		var url = "scripts/sendContactForm.php";
		httpRequest("POST", url, true, hr_sendContactForm, strArguments);
	}
	else
	{
		return false;
	}
	
}

// Function to handle getPageContact() results
function hr_sendContactForm()
{
	//alert("Results section");
	//
	if(request.readyState == 4)
	{
		if(request.status == 200)
		{
			var eleDivSection = document.getElementById("divSectionInfo");
			var textDoc = request.responseText;
			var strNewSection = "<h2>Contact Us</h2>";
			strNewSection += textDoc;
			
			// Create the section header and the first three examples in the sections
			eleDivSection.innerHTML = strNewSection;
		}
		else
		{
			alert("A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
			//alert("Fuck, You. A problem occurred with communicating between the XMLHTTPrequest object and the server programme.");
		}
	}
}

/********** Functions for contact page verification **********/

// Function to check for valid email
function validEmail(eMail)
{
  invalidChars =" /:,;"

  if(eMail.indexOf("@") == -1 || eMail.indexOf(".") == -1 || eMail.length < 6)
  {
    alert("The E-Mail address that you entered is invalid please enter it again. \nEx. 'luckyv@yahoo.com'")
    return false
  }
  for(i=0;i<invalidChars.length;i++)
  {
    badChar = invalidChars.charAt(i)
    if(eMail.indexOf(badChar,0) > -1)
    {
      alert("E-Mail cannont contain / : , ; or any spaces. Please enter your E-Mail again.")
      return false
    }   
  }
  return true
}

// Function check for valid phone number
function validPhone(phoneNumber)
{
	var regExPhone = /\d{3}-\d{3}-\d{4}/;
	if(!regExPhone.test(phoneNumber))
	{
		alert("Phone number must be in the following format.\nxxx-xxx-xxxx")
		return false;
	}
	return true;
}

// Function to check the form is filled out
function checkForm()
{
	var frmContact = document.frmContact;
	var strMessage = "The following fields are required:\n\nName\nE-mail\nComments\n\nPlease make sure that they are all filled in.";
	
	
	//alert("Checking form values --- " + strArguments);
	
	
	//var strFrmName = document.getElementById("frmName");
	
	var strFrmName = frmContact.frmName.value;
	//alert(strFrmName);
	
	
	// Check for empty required fields
	
	// Check first name
	if(frmContact.frmName.value == "")
	{
		alert(strMessage);
		frmContact.frmName.focus();
		return false;
	}
	
	//alert(strFrmName);
	
	
	// Check email
	if(frmContact.frmEmail.value == "")
	{
		alert(strMessage);
		frmContact.frmEmail.focus();
		return false;
	}
	if(!validEmail(frmContact.frmEmail.value))
	{
		frmContact.frmEmail.select();
		return false
	}
	
	//alert(frmContact.frmEmail.value);
	
	
	
	// Check that that if phone number was entered it is in a valid US format
	if(frmContact.frmPhone.value != "")
	{
		// Check phone number valid format
		if(!validPhone(frmContact.frmPhone.value))
		{
			frmContact.frmPhone.select();
			return false;
		}
	}
	
	// Check comments
	if(frmContact.frmComments.value == "")
	{
		alert(strMessage);
		frmContact.frmComments.focus();
		return false;
	}
	
	//alert(frmContact.frmComments.value);
	
	
	
	
	
	/*
	//var strArguments = "frmSubmit=" + strFrmSubmit + "&frmCompany=" + strFrmCompany + "&frmName=" + strFrmName + "&frmEmail=" + strFrmEmail + "&frmAddress=" + strFrmAddress + "&frmCity=" + strFrmCity + "&frmState=" + strFrmState + "&frmPhone=" + strFrmPhone + "&frmComments=" + strFrmComments;
	*/
	
	/*
	var strFrmName = document.getElementById("frmName");
	var strFrmEmail = document.getElementById("frmEmail");
	var strFrmPhone = document.getElementById("frmPhone");
	var strFrmCompany = document.getElementById("frmCompany");
	var strFrmAddress = document.getElementById("frmAddress");
	var strFrmCity = document.getElementById("frmCity");
	var strFrmState = document.getElementById("frmState");
	var strFrmComments = document.getElementById("frmComments");
	var strFrmSubmit = document.getElementById("frmSubmit");
	var strFrm = document.getElementById("");
	
		
		
	// Variable for post arguments
	
	
	var strArguments = "&frmSubmit=" + strFrmSubmit + "&frmCompany=" + strFrmCompany + "&frmName=" + strFrmName + "&frmEmail=" + strFrmEmail + "&frmAddress=" + strFrmAddress + "&frmCity=" + strFrmCity + "&frmState=" + strFrmState + "&frmPhone=" + strFrmPhone + "&frmComments=" + strFrmComments
	*/
	
	
	
	return true;
}

