YAHOO.namespace("Envision.container");

YAHOO.util.Event.onDOMReady(function () {
	
	// Define various event handlers for Dialog
	var handleSubmit = function() {
		ShowSlider();
		this.submit();
	};
	var handleCancel = function() {
		ShowSlider();
		document.forms['formE'].reset()
		this.cancel();
	};
	var handleSuccess = function(o) {
		document.window.open('www.khov.com');
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
		ShowSlider();
	};

    // Remove progressively enhanced content class, just before creating the module
    YAHOO.util.Dom.removeClass("dialog1", "yui-pe-content-e");
	var oResult
    try{
		oResult = document.forms['formE'].elements['homedesign'];
		} // End try
	catch(err){oResult = null}
	// Instantiate the Dialog
	//YAHOO.Envision.container.dialog1 = new YAHOO.widget.Dialog("dialog1",{zindex : 10001, context : ["show","tl","tr"], close : false, width : "30em", fixedcenter : false, visible : false, constraintoviewport : false, buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true }, { text:"Cancel", handler:handleCancel } ]});
	if(oResult == null){
	YAHOO.Envision.container.dialog1 = new YAHOO.widget.Dialog("dialog1",{color : "black", zindex : 10001,  close : false, width : "25em", fixedcenter : true, visible : false, constraintoviewport : false, buttons : [ { text:"OK", handler:handleCancel } ]});}
	else{
	YAHOO.Envision.container.dialog1 = new YAHOO.widget.Dialog("dialog1",{color : "black", zindex : 10001,  close : false, width : "25em", fixedcenter : true, visible : false, constraintoviewport : false, buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true }, { text:"Cancel", handler:handleCancel } ]});}
    
	// Validate the entries in the form to require that a home desing is entered
	YAHOO.Envision.container.dialog1.validate = function() {
		//var data = this.getData();
		//alert(document.forms['formE'].elements['homedesign'].value);
		if (document.forms['formE'].elements['homedesign'].value.length == 0 ) {
			alert("Please select a home design.");
			return false;
		} else {
		// Put together values to form URL for envision.
			var EnvisionURL=""
			EnvisionURL=document.forms['formE'].elements['HEnvisionURL'].value +"?NHTNumber=" + "KHOVNANIAN"
			EnvisionURL=EnvisionURL + "&Loc1=" + "100"
			EnvisionURL=EnvisionURL + "&Lev1=" + "CORP"
			EnvisionURL=EnvisionURL + "&Loc2=" + document.forms['formE'].elements['HDivision'].value
			EnvisionURL=EnvisionURL + "&Lev2=" + "DIV"
			EnvisionURL=EnvisionURL + "&Loc3=" + document.forms['formE'].elements['HCommunity'].value
			EnvisionURL=EnvisionURL + "&Lev3=" + "COM"
			EnvisionURL=EnvisionURL + "&Loc4=" + document.forms['formE'].elements['HPhase'].value
			EnvisionURL=EnvisionURL + "&Lev4=" + "PHAS"
			EnvisionURL=EnvisionURL + "&Loc5=" + document.forms['formE'].elements['homedesign'].value
			EnvisionURL=EnvisionURL + "&Lev5=" + "PLAN"
			// see if envision is alive
			//if(SiteUnAvailable())
			//{
			//	EnvisionURL = "http://localhost/PageInclude/EvnsitionUnavailable"
			//}
			//alert(EnvisionURL);
			
			window.open(EnvisionURL);
			document.forms['formE'].reset()
			ShowSlider();
			this.cancel();
			//return true;
		}
	};
	

	// Wire up the success and failure handlers
	YAHOO.Envision.container.dialog1.callback = { success: handleSuccess,
						     failure: handleFailure };
	
	// Render the Dialog
	YAHOO.Envision.container.dialog1.render();
	
	// link in left nav

	var oElement = document.getElementById("show");	
	YAHOO.util.Event.addListener(oElement, "click", HideSlider);
	YAHOO.util.Event.addListener("show", "click", YAHOO.Envision.container.dialog1.show, YAHOO.Envision.container.dialog1, true);
	// link top of page - image
	oElement = document.getElementById("showTop");	
	YAHOO.util.Event.addListener(oElement, "click", HideSlider);
	YAHOO.util.Event.addListener("showTop", "click", YAHOO.Envision.container.dialog1.show, YAHOO.Envision.container.dialog1, true);
	// link top of page - text
	oElement = document.getElementById("showTopTxt");	
	YAHOO.util.Event.addListener(oElement, "click", HideSlider);
	YAHOO.util.Event.addListener("showTopTxt", "click", YAHOO.Envision.container.dialog1.show, YAHOO.Envision.container.dialog1, true);
	//YAHOO.util.Event.addListener("hide", "click", YAHOO.Envision.container.dialog1.hide, YAHOO.Envision.container.dialog1, true);

});


// functions to take care of the slider and header issues caused by zindex
function HideSlider()
{
//alert('hide');
try{
	document.getElementById('innerMainNav').style.display='none';
	document.getElementById('Slideshow').style.display='none';
	//document.getElementById('HDPadding').style.display='block';
	}
catch(e){}
}
function ShowSlider()
{
//alert('show');
try{
	document.getElementById('innerMainNav').style.display='block';
	document.getElementById('Slideshow').style.display='block';
	//document.getElementById('HDPadding').style.display='none';
	}
catch(e){}

}

// Check that site is there
function SiteUnAvailable(URL)
{
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

xmlhttp.open("HEAD", URL,true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   if (xmlhttp.status==200) {
   alert("URL Exists!"); 
   return true;}
    else if (xmlhttp.status==404) {
    alert("URL doesn't exist!"); 
    return false;}
     else {
     alert("Status is "+xmlhttp.status); 
     return false;}
  }
 }
 xmlhttp.send(null)

}
