// JavaScript Document

// our ajax goodness.
var xmlHttp  
var largeHeader = true;
var bReady = false;
var nPage = 0;





//*******************************************************************************

	/**
	 * First page load
	 */
	function firstload()
	{	
		bReady = true;
// testing admin
		//loadBundle("small", "admin");
		
		// this is the one.
		loadBundle("large", "home");
		
		
		//callPage("admin");
	} // END FUNCTION firstload


//*******************************************************************************
	
	/** load an external javascript file */
	function loadjsfile(filename, filetype)
	{
		  var fileref=document.createElement('script');
		  fileref.setAttribute("type","text/javascript");
		  fileref.setAttribute("src", filename);
		  if (typeof fileref!="undefined")
			  document.getElementsByTagName("head")[0].appendChild(fileref);
	}
//*******************************************************************************

	/**
	 * The stage change event for the XmlHttp Object.
	 */
	function callPage(str)
	{
		//alert("call Page");
		if (str.length==0)
		{ 
				alert("A call to open page w\no params.");
				return;	
		} // END IF
		
		if (str == "home")		//NOTES: Make a call to get multiple pages and update two divs at once.  Parse XML.
			if (!largeHeader)
				loadBundle("large", str);			
			else
				requestpage(str);
		else
			if(largeHeader)
				loadBundle("small", str);
			else
				requestpage(str);
	} // END FUNCTION callPage

	
	
//*****************************************************************************

	
	/**
	 * Request Page calls a single page.
	 */
	function requestpage(str)
	{
		if (checkPage(str))
		{
			scroll(0,0);
		} // END IF
		if (str == "adminLogout")
		{
			tellActionScript(71);
		}
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
				alert ("Browser does not support HTTP Request")
				return
		} // END IF
		
		var url="driver.php";
		url=url+"?q="+str;
		xmlHttp.onreadystatechange=stateChanged ;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	} // END FUCNTION requestpage
	
	
	
//***********************************************************************************
	
	/**
	 * Load the appropriate header and request html.
	 */
	function loadBundle(head, str)
	{
		xmlHttp=GetXmlHttpObject();
		//document.getElementById("headerContent").innerHTML=" head : " + head + " : str " + str; 
		if (xmlHttp==null)
		{
				alert ("Browser does not support HTTP Request");
				return;
		} // END IF
		
		if (head == "large")
			largeHeader = true;
		else
			largeHeader = false;
		
		//sets the flash header.
		getPage(str);
		if (checkPage(str))
		{
			scroll(0,0);
		} // END IF

		var url="link_bundle.php";
		url=url+"?a="+head+"&b="+str;
		xmlHttp.onreadystatechange=stateChangedBundle;
		
		xmlHttp.open("GET",url,true);
		//xmlHttp.overrideMimeType('text/xml');
		xmlHttp.send(null);
	
	} // END FUNCTION
	
	
	
//*******************************************************************************


	/**
	 * Request a log in to the page.
	 */
	function requestlogin()
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
				alert ("Browser does not support HTTP Request");
				return;
		} // END IF
		var a=document.getElementById("user").value;
		var b=document.getElementById("pass").value;
		if (a == "")
		{
			document.getElementById("invalUser").innerHTML= "* Input UserName";
			document.getElementById("invalPw").innerHTML="";
			if (b == "")
				document.getElementById("invalPw").innerHTML="* Input Password";
			return;
			
		}
		if (b == "")
		{
			document.getElementById("invalPw").innerHTML="* Input Password";
			document.getElementById("invalUser").innerHTML= "";
			return;
		}
		var url="link_admin.php";
		url=url+"?a="+a+"&b="+b;
		//xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.onreadystatechange=stateChangedLog;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	} // END FUNCTION requestlogin
	
	
//*******************************************************************************


	/**
	 * Request a log in to the page.
	 */
	function registerBuyer()
	{
	
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
				alert ("Browser does not support HTTP Request");
				return;
		} // END IF
		var a=document.getElementById("first").value;
		var b=document.getElementById("last").value;
		var c=document.getElementById("email").value;
		var d=document.getElementById("address").value;
		var e=document.getElementById("city").value;
		var f=document.getElementById("state").value;
		var g=document.getElementById("zip").value;
		var h=document.getElementById("agentName").value;
		var i=document.getElementById("agentEmail").value;
		var j=document.getElementById("opt").checked;
		
		var k=document.getElementById("image").value;

		var bError= false;
		document.getElementById("invalFirst").innerHTML="";
		document.getElementById("invalLast").innerHTML="";
		document.getElementById("invalEmail").innerHTML="";
		document.getElementById("invalAdd").innerHTML="";
		document.getElementById("invalCity").innerHTML="";
		document.getElementById("invalState").innerHTML="";
		document.getElementById("invalZip").innerHTML="";
		document.getElementById("invalAName").innerHTML="";
		document.getElementById("invalAEmail").innerHTML="";
		document.getElementById("invalCaptcha").innerHTML="";
		if (a == "")
		{
			document.getElementById("invalFirst").innerHTML= "	!";
			bError = true;
		}
		if (b == "")
		{
			document.getElementById("invalLast").innerHTML= "	!";
			bError = true;
		}
		if (c == "")
		{
			document.getElementById("invalEmail").innerHTML= "	!";
			bError = true;
		}
		if (d == "")
		{
			document.getElementById("invalAdd").innerHTML= "	!";
			bError = true;
		}
		if (e == "")
		{
			document.getElementById("invalCity").innerHTML= "	!";
			bError = true;
		}
		if (f == "")
		{
			document.getElementById("invalState").innerHTML= "	!";
			bError = true;
		}
		
		if (g == "")
		{
			document.getElementById("invalZip").innerHTML= "	!";
			bError = true;
		}
		if (h == "")
		{
			document.getElementById("invalAName").innerHTML= "	!";
			bError = true;
		}
		//var ctn = i.split('@');
	//alert(ctn);
//		if (ctn.length == 1 || ctn.length > 2)
		if (i == "")
		{
			document.getElementById("invalAEmail").innerHTML= "	!";
			bError = true;
		}
		if (k == "")
		{
			document.getElementById("invalCaptcha").innerHTML= "	!";
			bError = true;
		}
		if (bError)
			return;
		var url="link_buyerregistration.php";
		url=url+"?a="+a+"&b="+b+"&c="+c+"&d="+d+"&e="+e;
		url=url+"&f="+f+"&g="+g+"&h="+h+"&i="+i+"&j="+j+"&k="+k;
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	} // END FUNCTION registerBuyer
	
	
//*******************************************************************************


	/**
	 * Submit the contact information.
	 */
	function contactForm()
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
				alert ("Browser does not support HTTP Request");
				return;
		} // END IF
		
		var name = document.getElementById("name").value;
		var address = document.getElementById("address").value;
		var city = document.getElementById("city").value;
		var States = document.getElementById("States").value;
		var zip = document.getElementById("zip").value;
		var areaCode = document.getElementById("areaCode").value;
		var prefix = document.getElementById("prefix").value;
		var number = document.getElementById("number").value
		var email = document.getElementById("email").value;
		var Information = document.getElementById("Information").value;
		var HearAbout = document.getElementById("HearAbout").value;
		var ContactMe = document.getElementById("ContactMe").value;
		var comments = document.getElementById("comments").value
		var image = document.getElementById("image").value;
		//alert("image: " + image);
		
		if (name==""||address==""||city ==""||States==""||zip==""||areaCode==""||prefix==""||number==""||number==""||email==""||Information==""||HearAbout==""||ContactMe=="" || image=="")
		{
			
			document.getElementById("error").innerHTML="ERROR! Please make sure you have filled in all the required data.  Thank You.";
			return;
			
		}
		else
		{
			//alert("Send Request:");
			var url="link_contactform.php";
			url=url+"?name="+name+"&address="+address+"&city="+city+"&States="+States+"&zip="+zip;
			url=url+"&areaCode="+areaCode+"&prefix="+prefix+"&number="+number+"&email="+email+"&Information="+Information;
			url=url+"&HearAbout="+HearAbout+"&ContactMe="+ContactMe+"&comments="+comments+"&image="+image;
			xmlHttp.onreadystatechange=stateChanged; 
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null)
			//alert("Send Request: END");
		} // END IF
	} // END FUNCTION contactForm


//******************************************************************************


	/**
	 * The stage change event for the XmlHttp Object.
	 */
	function stateChangedHeader() 
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			document.getElementById("headerContent").innerHTML=xmlHttp.responseText;
		} // END IF
	} // END FUNCTION
	
	
//******************************************************************************


	/**
	 * The stage change event for the XmlHttp Object, parse xml write head and main html to page.
	 */
	function stateChangedBundle() 
	{ 
		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			//alert("stateChangeBundle: " + xmlHttp.readyState + " : " + xmlHttp.status);
			var xmlDoc=xmlHttp.responseXML;
				//alert("We should have it." + xmlHttp.responseText);
			var text = xmlHttp.responseText;
			var arg = text.split('~');
			//	alert(arg);
			document.getElementById("headerContent").innerHTML = arg[3];
			document.getElementById("mainContent").innerHTML =arg[1]; 
		} // END IF
	} // END FUNCTION
	
	
	
//*******************************************************************************


	/**
	 * The stage change event for the XmlHttp Object.
	 */
	function stateChanged() 
	{ 
			//alert("stateChangeBundle: " + xmlHttp.readyState + " : " + xmlHttp.status);
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
				//	alert("We should have it." + xmlHttp.responseText);
			document.getElementById("mainContent").innerHTML=xmlHttp.responseText; 
		} // END IF
		//window.location.reload( false );
	} // END FUNCTION
	
	//*******************************************************************************


	/**
	 * The stage change event for the XmlHttp Object.
	 */
	function stateChangedLog() 
	{ 
			//alert("stateChangeBundle: " + xmlHttp.readyState + " : " + xmlHttp.status);
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 

			if (xmlHttp.responseText == "success")
			{
				//alert("we have a success");
				// load Javaacript and make the call to get the landingpage.
				loadjsfile("admin.js", "js")
				tellActionScript(70);
				var url="link_admin.php";
				url=url+"?a=Conti"+"&b="+"Tony";
				xmlHttp=GetXmlHttpObject();
				//xmlHttp.onreadystatechange=stateChanged;
				xmlHttp.onreadystatechange=stateChangedLog;
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
			}
			else
			{
				document.getElementById("mainContent").innerHTML=xmlHttp.responseText; 
			}
		} // END IF
		//window.location.reload( false );
	} // END FUNCTION


//*******************************************************************************


	/**
	 * Gets the XMLHttpObject to enable ajax control of the html page.
	 */
	function GetXmlHttpObject()
	{
		var xmlHttp=null;
		try
		 {
		 // Firefox, Opera 8.0+, Safari
		 xmlHttp=new XMLHttpRequest();
		 }
		catch (e)
		 {
		 // Internet Explorer
		 try
		  {
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		 catch (e)
		  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		 }
		return xmlHttp;
	} // END FUNCITON
	
	
	
//********************************************************************************************************************************

//********************************************************************************************************************************

// Get the page name, get the page number, check the page for special occurances.

//********************************************************************************************************************************
//********************************************************************************************************************************


// Get the page.

//*****************************************************************************



	function checkPage(str)
	{
		switch(str)
		{
			case "admin": 
				tellActionScript(0);
				return true; 
			break;
	
			case "howtogethere":
				tellActionScript(0);
				 return true; 
			 break;
	
			case "legal": 
				tellActionScript(0);
				 return true; 
			 break;
	
			case "locationmap": 
				tellActionScript(0);
				 return true; 
			 break;
			 
			 case "contact": 
				tellActionScript(21);
				 return true; 
			break;
			
			case "contactform": 
				tellActionScript(20);
				 return false; 
			break;
			
			case "forrealtors": 
				tellActionScript(6);
				 return false; 
			break;
		} // END SWITCH
	} // END FUNCTION checkPage


	function getPage(str)
	{
		
		switch(str)
		{
			case "admin": 
				tellActionScript(0);
	 			return true; 
			break;

			case "howtogethere":
				tellActionScript(0);
				 return true; 
			 break;

			case "legal": 
				tellActionScript(0);
				 return true; 
			 break;
 
			case "locationmap": 
				tellActionScript(0);
				 return true; 
			 break;

			 case "home": 
				//	tellActionScript(0);
				 return false; 
			 break;
			 
			case "amenities": 
				tellActionScript(16);
			 	return false; 
			break;
 
			case "attractions":
				tellActionScript(9);
				 return false; 
			break;

			case "buyerregistration":
				tellActionScript(13);
				 return false; 
			break;
 
			case "contact": 
				tellActionScript(21);
				 return true; 
			break;
 
			case "contactform": 
				tellActionScript(20);
				 return false; 
			break;
 
			case "deerhaven": 
				tellActionScript(3);
				 return false; 
			break;
 
			case "developmentplan": 
				tellActionScript(10);
				 return false; 
			 break;
 
			case "faq1": 
				tellActionScript(11);
				 return false; 
			 break;
 
			case "faq2": 
				tellActionScript(17);
				 return false; 
			 break;
 
			case "forrealtors": 
				tellActionScript(6);
				 return false; 
			 break;
 
			case "history": 
				tellActionScript(7);
				 return false; 
			 break;
			 
			 case "locationmap2": 
				tellActionScript(8);
				 return false; 
			 break;

			case "modelhomes": 	
				tellActionScript(15);
				 return false; 
			 break;
 
			case  "moreabout": 
				tellActionScript(1);
				 return false; 
			 break;
 
			case "newandnote": 
				tellActionScript(5);
				 return false; 
			 break;
 
			case "newsletters": 
				tellActionScript(22);
				 return false; 
			 break;
 
			case "ownershipreview":	
				tellActionScript(14);
				 return false; 
			 break;

			case "requestform": 
				tellActionScript(20);
				return false; 
			 break;
 
			case "requestmore": 
				tellActionScript(4);
				 return false; 
			 break;
 
			case "residences": 
				tellActionScript(2);
				 return false; 
			 break;
 
			case "residencesforsale":
				tellActionScript(12);
				 return false; 
			 break; 
			
			
		} // END SWITCH
	} // END FUNCITON
	
	
//******************************************************************************



	function setPage(index)
	{
		nPage = index;
		switch(index)
		{
			
			case "16":
				callPage("amenities");
			 break;
 
			case "9":
				callPage("attractions");
			break;

			case "13":
				callPage("buyerregistration");
			break;
 
			case "21": 
				callPage("contact");
			break;
 
			case "20": 
				callPage("contactform");
			break;
 
			case "3": 
				callPage("deerhaven");
			break;
 
			case "10": 
				callPage("developmentplan");
			 break;
 
			case "11": 
				callPage("faq1");
			 break;
 
			case "17": 
				callPage("faq2");
			 break;
 
			case "6": 
				callPage("forrealtors");
			 break;
 
			case "7": 
				callPage("history");
			 break;
 
			case "0": 
				callPage("home");
			 break;

			case "8": 
				callPage("locationmap2");
			 break;

			case "15": 	
				callPage("modelhomes");
			 break;
 
			case "1": 
				callPage("moreabout");
			 break;
 
			case "5": 
				callPage("newandnote");
			 break;
 
			case "22": 
				callPage("newsletters");
			 break;
 
			case "14":	
				callPage("ownershipreview");
			 break;

			case "20": 
				callPage("requestform");
			 break;
 
			case "4": 
				callPage("requestmore");
			 break;
 
			case "2": 
				callPage("residences");
			 break;
 
			case "12":
				callPage("residencesforsale");
			 break; 
			
			
		} // END SWITCH
	} // END FUNCITON
	
	
//********************************************************************************************************************************

//********************************************************************************************************************************

// Communication with the flash headers. 

//********************************************************************************************************************************
//********************************************************************************************************************************

	/**
	 * A function called form action script that will 
	 * return our flag to flasg
	 */
	function isReady() 
	{
			 return bReady;
	} // END FUNCITON isReady
	
	
//******************************************************************************

	/**
	 * A function called form action script that will 
	 * return our flag to flasg
	 */
	function whatAmI() 
	{
		//alert("call from actionscript for identity;");
		return Number(nPage);
	} // END FUNCITON isReady
	
	

//******************************************************************************

	/**
	 * Returns the header that is currently on the page.
	 */
 	function theHeader(movieName) 
	{
		//alert(movieName);
//		alert();
         if (navigator.appName.indexOf("Microsoft") != -1) 
		 {
             return window[movieName];
         } 
		 else 
		 {
        	return document[movieName];
       	 } // END IF
     } // END FUCTION theHeader
	 
	
//******************************************************************************


	/** 
	 *
	 */
	function tellActionScript(value) 
	{
		//alert("ActionScript: " );		
//		var value = document.getElementById("third").value;
		if (largeHeader)
		{	
			var head = theHeader("HomeNavMX");
			head.tellActionScript(value);
			//alert("largeHeader " );
		}
		else
		{	
			//alert("Small " );
			try 
			{
				
				var head = theHeader("MainNavMX");
				head.tellActionScript(value);

				//alert("Small ENd" );
			}
			catch(er)
			{
				//alert(er + ": " + value);
			}
		}	
	} // END FUNCTION
	
	
//*****************************************************************************

	/** 
	 * Answers the call from the header.
	 */
     function as3SaysWhat(value) 
	 {
		 //document.getElementById("debugText").innerHTML = " Flash Sent Java a request for link : " + value;
       // alert("as3SaysWhat : " + value);
		setPage(value);
     } // END FUNCTION as3SaysWhat
	
//*******************************************************************************
//*******************************************************************************





//***************************************************************************************************************
//***************************************************************************************************************




	/**
	 * Request Page calls a single page.
	 */
	function adminRequestPage(str)
	{
		
		//alert("request page: " + str);
		//document.getElementById("content").innerHTML="Your text goes here"; 

		if (checkPage(str))
		{
			scroll(0,0);
		} // END IF
		
		
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
				alert ("Browser does not support HTTP Request")
				return
		} // END IF
		
		var url="admin_driver.php";
		url=url+"?q="+str;
		xmlHttp.onreadystatechange=stateChanged2;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
	} // END FUCNTION requestpage
	
	
//*******************************************************************************


	/**
	 * The stage change event for the XmlHttp Object.
	 */
	
	function stateChanged2() 
	{ 
		
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
				//	alert("We should have it." + xmlHttp.responseText);
			document.getElementById("content").innerHTML=xmlHttp.responseText; 
		} // END IF
	} // END FUNCTION 
	
	
