// check box check to make sure at least one checkbox is checked
function ValidCheckBox(source, arguments)
{
	for (var i=0; i<document.forms[0].elements.length; i++)
	{
		
		switch (document.forms[0].elements[i].type)
		{				
			case "checkbox":// mark : do not clear cancel fields
				if (document.forms[0].elements[i].checked == true)
				{
					arguments.IsValid = true;
					return;
				}
				break;				
			default:
				break;
		}
	}
	arguments.IsValid = false;
}

function TraverseNaturalPage(TraverseForm)
{
	if (document.forms[TraverseForm].elements["hidReload"].value == "initialvalue")
	{
		document.forms[TraverseForm].elements["hidReload"].value = "newvalue";
		document.forms[TraverseForm].action = document.forms[TraverseForm].hidNextPage.value;
		document.forms[TraverseForm].submit();
		
	}
	else
	{
		try
		{
			history.back(-1)
		}catch (e) {location.href='http://www.justlisted.com'}		
	}
}

function TraversePage(TraverseForm, ParentForm, SubmitButtonName)
{
	try{ 
	
		if (document.forms[ParentForm].elements["hidPopupEnabled"].value=="true")
			document.forms[ParentForm].elements["hidPopupEnabled"].value="false"; 
		else
			document.forms[ParentForm].elements["hidPopupEnabled"].value="true"; 
	}catch (e) {}
	
	if (document.forms[ParentForm].elements["hidReload"].value == "initialvalue")
	{
		document.forms[ParentForm].elements["hidReload"].value = "newvalue";
		document.forms[ParentForm].elements[SubmitButtonName].disabled = true; //Disabled the submit button for reload
		document.forms[TraverseForm].action = document.forms[TraverseForm].hidNextPage.value;
		document.forms[TraverseForm].submit();
	}
	else
	{
		for (var i = 0; i < document.forms[TraverseForm].elements.length; i++)
		{
			var element = document.forms[TraverseForm].elements[i];
			switch (element.type)
			{
				case 'hidden':
					element.id = "tmp2_" + element.id;
					element.name = element.id;
					
			}
		}
		
		for (var i = 0; i < document.forms[ParentForm].elements.length; i++)
		{
			var element = document.forms[ParentForm].elements[i];
			switch (element.type)
			{
				case 'hidden':
					if ((element.name).indexOf('tmp_') >= 0)
					{
						element.id = element.id.substring(4);
						element.name = element.id;
					}
					
			}
		}
	}
}


function NoSuggest(frmName)
{
	document.forms[frmName].action = "NoSuggestedAreas.aspx";
	document.forms[frmName].__VIEWSTATE.name = "NOTVIEWSTATE";
	document.forms[frmName].__EVENTTARGET.value = "";
	document.forms[frmName].submit();
}

function Popup(popupUrl, winName, width, height, scrollbars) {
	var l, t, settings;
	l = (screen.width) ? (screen.width - width) / 2 : 0;
	t = (screen.height) ? (screen.height - height) / 2 : 0;
	settings = "scrollbars=" + scrollbars + ",toolbars=0,width=" + width + ",height=" + height;
	settings = settings + ",top=" + t + ",left=" + l;	
	var msgwindow = window.open(popupUrl, winName, settings);
	msgwindow.focus();
}

function RegPopup(popupUrl, winName, width, height, scrollbars) {
	var msgwindow = window.open(popupUrl, winName, "scrollbars=" + scrollbars + ",toolbars=0,width=" + width + ",height=" + height);
	msgwindow.focus();		
}

function PopupResize(popupUrl, winName, width, height, scrollbars) {
	var l, t, settings;
	l = (screen.width) ? (screen.width - width) / 2 : 0;
	t = (screen.height) ? (screen.height - height) / 2 : 0;
	settings = "scrollbars=" + scrollbars + ",toolbars=0,width=" + width + ",height=" + height;
	settings = settings + ",top=" + t + ",left=" + l + ",resizable=1";	
	var msgwindow = window.open(popupUrl, winName, settings);
	msgwindow.focus();
}

function SelectAll_Evt(frmName, selectAllCheckbox)
{
	if (document.forms[frmName].elements[selectAllCheckbox].checked)				
		SelectAll(frmName);			
	else
		ClearAllExceptMain(frmName);
}

function SelectAll(frmName) // dependent on checkbox name containing "Checkbox"
{
	for (var i=0; i<document.forms[frmName].elements.length; i++)
	{
		
		switch (document.forms[frmName].elements[i].type)
		{				
			case "checkbox":// mark : do not clear cancel fields
							if ((document.forms[frmName].elements[i].name).indexOf("Checkbox") != -1)
							{
								document.forms[frmName].elements[i].checked = true;
							}
							break;
			default: break;
		}
	}
}

function ClearAllExceptMain(frmName) // dependent on checkbox name containing "Checkbox" and main checkbox name containing "MainCheckbox"
{
	for (var i=0; i<document.forms[frmName].elements.length; i++)
	{
		
		switch (document.forms[frmName].elements[i].type)
		{				
			case "checkbox":// mark : do not clear cancel fields
							if ((document.forms[frmName].elements[i].name).indexOf("Checkbox") != -1)
							{
								if ((document.forms[frmName].elements[i].name).indexOf("MainCheckbox") == -1)
									document.forms[frmName].elements[i].checked = false;
							}
							break;
			default: break;
		}
	}
}

function GetPre(frmName)
{
	//alert(document.forms[frmName].selHomeLoan[document.forms[frmName].selHomeLoan.selectedIndex].value);
	if (document.forms[frmName].selHomeLoan[document.forms[frmName].selHomeLoan.selectedIndex].value == "no")
		document.forms[frmName].selGetQualified.disabled = false;
	else
		document.forms[frmName].selGetQualified.disabled = true;
}

function SampleEmail(frmName, Natural)
{
	
	if (Natural == "Natural")
		Popup('../../SampleEmail.aspx','winSample','664','600','yes');
	else	
		Popup('SampleEmail.aspx?JLTD=' + document.forms[frmName].hidtmpSTrack.value,'winSample','664','600','yes');
}

function LoadPopup(ParentForm, hidid) {	
	if (document.forms[ParentForm].elements[hidid].value == "true") {
		Popup('DropOffDontStop.aspx','winPrivacy','343','365','yes')
		
	}
	else
	{
		document.forms[ParentForm].elements[hidid].value = "true";
	}
}

function LoadPopup2(ParentForm, hidid) {	
	if (document.forms[ParentForm].elements[hidid].value == "true") {
		Popup('DropOffHoldOn.aspx','winPrivacy','343','365','yes')
		
	}
	else
	{
		document.forms[ParentForm].elements[hidid].value = "true";
	}
}

function SetPopFalse(frmName)
{
	try{ document.forms[frmName].hidPopupEnabled.value='false'; }catch (e) {}
}

function Rcertify() 
{
	popupWin = window.open('http://www.bbbonline.org/cks.asp?id=103072114515733930', 'Participant','location=yes,scrollbars=yes,width=450,height=300'); 
	window.name = 'opener';
}

function ZipCheck(obj) {

	if (document.forms[0].elements[obj].value != "")
	{
	
		var location=0,newString="",output="",OrigString=document.forms[0].elements[obj].value; 

		location = OrigString.indexOf("-"); 
		if (location >= 0) { 
			newString = OrigString.slice(0,location) + OrigString.slice(location + 1); 
		} else { 
			newString = OrigString; 
		}
	 
		if (newString.length == 5 && !isNaN(newString)) { 
			//output = newString + "-0000";
			output = newString
		}
	 
		if (newString.length == 9 && !isNaN(newString)) { 
			output = newString.slice(0,5) + "-" + newString.slice(5); 
		}
		
		if (output.length > 0) { 
			document.forms[0].elements[obj].value = output; 
		} else { 
			alert ("Invalid Data. Please enter a valid ZIP code.");
			document.forms[0].elements[obj].select(); 
			return false;
		}
	}
	return true;
}

function NatFormValidation()
{
	
	// city, state or zipcode validation
	if ((!(document.forms.frmCustomArea.txtCity.value != "" && document.forms.frmCustomArea.selState[document.forms.frmCustomArea.selState.selectedIndex].value != "")) && document.forms.frmCustomArea.txtZip.value == "")
	{
		alert("Please provide a City and State or Zip Code to proceed.");
		return false;
	}
	
	// bed, bath, and price range validation
	if (document.forms.frmCustomArea.selBeds.selectedIndex == 0 || document.forms.frmCustomArea.selBaths.selectedIndex == 0 || document.forms.frmCustomArea.selPrice[document.forms.frmCustomArea.selPrice.selectedIndex].value == "-9999999")
	{
		alert("Bed, Bath and Price range are required fields. Please fill in these fields before proceeding.");
		return false;
	}
	
	// zip check
	ZipCheck("txtZip");
	
	document.forms.frmCustomArea.action += window.location.search;
		
	return true;
}



