	// preload image
	pic1 = new Image(116,15); 
	pic1.src="/images/client_login_w.gif";

	function upDn(ud) {
		if (ud == 'down') {
			Effect.BlindDown('customsearch_box', {duration: .5});
			$('customsearch_label').update('<a href="#" onclick="upDn(\'up\'); return false;"><img src="/images/customsearch_button_over.gif" width="157" height="35" alt="Custom Search" border="0" id="customsearch_button"></a>');
		} else if (ud == 'up') {
			Effect.BlindUp('customsearch_box', {duration: .0});
			$('customsearch_label').update('<a href="#" onclick="upDn(\'down\'); return false;"><img src="/images/customsearch_button.gif" width="157" height="27" alt="Custom Search" border="0" id="customsearch_button"></a>');
		}
	}



	function auth_login(x, y, z) { 
		/*
		x = email
		y = saved_users_id (SESSION)
		z = home_id
		*/
		// alert("email: " + x.value + " session: " + y + " home_id: " + z);

		 if(x.value != '')
			{ 
				var ajax = new Ajax.Request('/classes/login.php', 
				{ 
					parameters: {email: $(x).value},
					evalScripts: true,
					evalJS: true,

					onComplete: function(transport) { 
												
						// Evaluate the JavaScript
						eval(transport.responseText); //alert(transport.responseText);
												
						// Now save the listing with the ID we got back
						save_listing(sessionid, z); //alert(sessionid + " " + z);
						
						// Refresh the page
						setTimeout('redirect_to_self();', 1200);
					} //END onComplete
					
				}); //end ajax request 
				
			}
			
	}
	
	function redirect_to_self() {
		var my_page = window.location.href;
		window.location = my_page;
	}
	
	function page_redirect(x)
	{
		//console.log("page_redirect -- " + x);
		window.location = x;
		//location.reload(true);
	}
	
	function get_listings(x)
	{ //console.log("get_listings -- " + x);
		if(x != ''){
			// $('favs').update('');
			var ajax = new Ajax.Updater('favs', '/classes/get_listings.php', {method: 'get'});
			//get_listings(x);
		}
	}
	
	function blind_up()
	{ //console.log("UP started");
		new Effect.BlindUp('login');
	//console.log("UP complete");
	}
	
	function blind_down()
	{ //console.log("DOWN started");
		Effect.BlindDown('login');
	//console.log("DOWN complete");
	} 
	
	function change_to_add()
	{ //console.log("add started");
		Effect.Fade("remove_home", { duration: 0.25 });
		Effect.Appear("add_home", { duration: 0.25 });
	//	console.log("radd completed");
	}
 
	function change_to_remove()
	{// console.log("remove started");
	  Effect.Fade("add_home", { duration: 0.25 });
	// console.log("fade completed");
	  Effect.Appear("remove_home", { duration: 0.25 });
		//console.log("remove X 2 completed");
	}

	function update_div()
	{ //console.log("update_div begin");
		var ajax = new Ajax.Updater('notice', '/includes/saved_favs.php?type=ajax');
		//console.log("update_div end");
	}
	
	function updateAll (type, loggedin) {
		//console.log("updateAll begin");
		update_div();
		
		if (loggedin == 'yes') { //console.log("if 1");
			if (type == 'remove') { //console.log("else if");
				change_to_remove();
				blind_up();
			}
			else { //console.log("if 2");
				change_to_add();
			}
			
		}
		else {
			// console.log("refresh called");
			var my_page = window.location.href;
			//console.log(my_page);
			window.location = my_page;
			//console.log("refresh complete");
		}
		//console.log("updateAll end");
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function save_listing(x, y)
	{ // alert("save_listing -- " + x + " AND " + y);
		
		//console.log("save_listing -- " + x + " AND " + y);
		//x = saved_users_id
		//y = home_id
		//$(x).value
		 if( x != '')
		{
				if (!loggedin) {
					var loggedin = 'yes';
				};
				
				var ajax = new Ajax.Request('/classes/save_listing.php', {
					parameters: { saved_users_id: x, home_id: y },
					onComplete: function() {updateAll('remove', loggedin);}
				});
				// update_div();
				// change_to_remove();
				// var my_page = window.location.href;
				//console.log(my_page);
				// window.location = my_page;;
		}else{ 
			blind_down();
			var loggedin = 'no';
		} 	
		
	}

	function remove_listing(x, y)
	{ // console.log("remove_listing -- " + x + " AND " + y);
		//x = saved_users_id
		//y = home_id
				
		if(x != '')
		{
				var ajax = new Ajax.Request('/classes/remove_listing.php', {
					parameters: { saved_users_id: x, home_id: y},
					onComplete: function() {updateAll('add', 'yes');}
				});

		}else{ 
			blind_down();
		}
	} 
	
	function remove_from_favorites (x, y) {
		var answer = confirm("Are you sure you want to remove this home from your favorites? This cannot be undone.");
		
		if (answer) {
			remove_listing(x, y);
			
			$('favs').update('');
			
			get_listings(x);
		 
		};
		
	}
	
	
	//TAKES VALUE OF CHECKBOXS(PACKAGES) AND ADDS TO THE MONTHLY PAYMENT OF THE HOUSE
	function calc_checkboxes(x) 
	{ //x = home_id
      var a = parseFloat(0);
	  var b = parseFloat(0);
	  var c = parseFloat(0);
	  var d = parseFloat(0);

		 if($('checkbox_1').checked == true)
		{
			a = parseFloat($('checkbox_1').value);
		//	console.log("a: " + a);
		}
		else
		{
			a = parseFloat(0);
		//	console.log("a: " + a);
		}

		if($('checkbox_2') != null){
			if($('checkbox_2').checked == true)
			{
				b = parseFloat($('checkbox_2').value);
			//	console.log("b: " + b);
			}
			else
			{
				b = parseFloat(0);
			//	console.log("b: " + b);
			}
		}
		//	console.log(3);


		if($('checkbox_3') != null){
			if($('checkbox_3').checked == true)
			{
			//	console.log(13);
				c = parseFloat($('checkbox_3').value);
			//	console.log("c: " + c);
			}
			else{
				//console.log(23);
				c = parseFloat(0);
				//console.log("c: " + c);
			}
		}
		//	console.log(4);
		
		if($('checkbox_4') != null){
			if($('checkbox_4').checked == true)
			{

				d = parseFloat($('checkbox_4').value);
				//console.log("d: " + d);

			}
			else{
				d = parseFloat(0);
				//console.log("d: " + d);
			} 
		}

			total_packages = a + b + c + d;
			//console.log("Total Packages: " + total_packages); 
			
			var ajax = new Ajax.Updater('homebox_price','/includes/add_package.php', {parameters: { home_id: x, packages: total_packages}}); 
			var ajax = new Ajax.Updater('homebox_price2','/includes/add_package.php', {parameters: { home_id: x, packages: total_packages}}); 
			
	}
	
	// Just to test if the functions.js is included
	function searchHomes () {
		
		new Ajax.Updater('searchresults', '/searchajax.php', {
		  parameters : $('searchform').serialize(true) 
		});
				
	}
	
	
	function customize_home () {
		
		// alert("Package ID "+id.value);
		
		customize = $('customizeform').serialize(true);
				
		homeid = customize['homeid'];
		// alert( 'Home ID: ' + test['homeid'] + "\n" + 'Upgrades: ' + test['upgrades']);
		
		/*
		homeid = home id numeric
		*/

		// Call the updater to hit the database
		var customize = new Ajax.Request('/includes/customize.php', 
		{
		  	parameters : $('customizeform').serialize(true),
			evalScripts: true,
			evalJS: true,
			onComplete: function(transport) {
				// Evaluate the JavaScript
				eval(transport.responseText);	
				
				var ajax = new Ajax.Updater('homebox_price','/includes/customize_price.php', {parameters: { homeid: homeid}}); 
				var ajax = new Ajax.Updater('homebox_price2','/includes/customize_price.php', {parameters: { homeid: homeid}});
							
			}
			
		}); 
		
		
				
	}
