function setCookie(name, value, expire) {
	document.cookie = name+"="+escape(value)+((expire==null)?"":("; expires="+expire.toGMTString()))+';path=/';					  
}

function saveSearch() {
	var location = 0;
	var id_array = new Array(11,12,13, 21,22,23, 31,32, 41,42, 51,52,53, 0);
	for(var i=0;i<14;i++) location += Math.pow(2,i)*document.getElementById('location'+id_array[i]).checked;
	
	if(!getVar('st')){
		//only in search.php
		var housing_type = 1*document.search_form.Other.checked + 2*document.search_form.AP.checked + 4*document.search_form.House.checked;	
		var unit_type = 0;
		for (var i=0;i<7;i++) unit_type += Math.pow(2,i)*document.getElementById('unit_type'+i).checked;
	} else {
		//preserve old value
		if(getCookie("search_criteria")){ 
			var housing_type = getCookieVar('housing_type');		
			var unit_type = getCookieVar('unit_type');
		}
	}
	
	var today = new Date();
	
	//for advance search
	if(getVar('s_mode')=='adv'){
		//var available = document.getElementById('available1').checked;
		var available_year = document.search_form.available_year.value;
		//available_year = available_year.substring(available_year.length-2,2);
		var available_month = document.search_form.available_month.value;
		var available_day = document.search_form.available_day.value;
		var lease = 0;
		for(var i=0;i<4;i++) lease += Math.pow(2,i)*document.getElementById('lease'+i).checked;
	} else {
		//preserve old value
		if(!getCookie("search_criteria")){ 
			//fill in default value
			//var available = true;
			var available_year = ""+today.getYear();
			var available_month = today.getMonth() + 1; //default is this month, +1 becuz javascript default is 0-11
			var lease=12;
		} else {
			//var available = getCookieVar('available');
			var available_year = getCookieVar('available_year');		
			var available_month = getCookieVar('available_month');
			var available_day = getCookieVar('available_day');
			var lease = getCookieVar('lease');
		}
	}
		
	var expires = new Date();
	expires.setTime(today.getTime() + 60*60*24*365);
	
	var cookie_value = 'housing_type='+housing_type+'&'+'unit_type='+unit_type+'&location='+location+'&';
	cookie_value += 'priceLow='+document.search_form.priceLow.value+'&'+'priceHigh='+document.search_form.priceHigh.value;
	cookie_value += '&available_year='+available_year+'&available_month='+available_month+'&available_day='+available_day+'&lease='+lease;
							
	setCookie("search_criteria",cookie_value,expires);																				
}

function getCookie(Name) {
	var search = Name + "=";
	if(document.cookie.length > 0) { // if there are any cookies
		offset = document.cookie.indexOf(search); 
		if (offset != -1) {
			// if cookie exists
			offset += search.length;
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(offset, end));
		}
	}
}

//revised from franciscocharrua.com's get GET variable script
function getCookieVar(name){
	return(getValueOf(name, getCookie("search_criteria")));
}

//retrieve GET Variables from the URL
function getVar(name){
	return(getValueOf(name, document.location.search));
}

function getValueOf(name, source){
	get_string = source;
	return_value = '';
 
	do {
		//This loop is made to catch all instances of any get variable.
		name_index = get_string.indexOf(name + '=');		
		if(name_index != -1){
			get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
			end_of_value = get_string.indexOf('&');
			value = (end_of_value != -1)?get_string.substr(0, end_of_value):get_string;
			if(return_value != '' && value != '') return_value += ', ';
			return_value += value;
		}
	} while(name_index != -1)
	
	//Restores all the blank spaces.
	space = return_value.indexOf('+');
	while(space != -1) {
		return_value = return_value.substr(0, space) + ' ' + return_value.substr(space+1, return_value.length); 
		space = return_value.indexOf('+');
	}
  
 	return(return_value);	
}

function fillForm(){
	if(getVar('st')=='desc') side_bar_switch();
	
	//common entries
	document.search_form.priceLow.value = getCookieVar('priceLow');
	document.search_form.priceHigh.value = getCookieVar('priceHigh');	
	var location = getCookieVar('location');
	var id_array = new Array(11,12,13, 21,22,23, 31,32, 41,42, 51,52,53, 0);
	for(var i=0;i<14;i++) document.getElementById('location'+id_array[i]).checked = (location >> i) % 2;

	if(!getVar('st')){
		//only in search.php
		var housing_type = getCookieVar('housing_type');
		for(var i=0;i<3;i++) document.getElementById('housing_type'+i).checked = (housing_type >> i) % 2;
		var unit_type = getCookieVar('unit_type');
		for(var i=0;i<7;i++) document.getElementById('unit_type'+i).checked = (unit_type >> i) % 2;
		
		if(getVar('s_mode')=='adv'){
			//only in adv search
			//document.getElementById('available1').checked = getCookieVar('available');
			for(var i=0;i<12;i++) document.search_form.available_month.options[i].selected = (1*document.search_form.available_month.options[i].value==getCookieVar('available_month'));
			for(var i=0;i<3;i++) document.search_form.available_year.options[i].selected = (1*document.search_form.available_year.options[i].value==getCookieVar('available_year'));
			for(var i=0;i<31;i++) document.search_form.available_day.options[i].selected = (1*document.search_form.available_day.options[i].value==getCookieVar('available_day'));
			var lease = getCookieVar('lease');
			for(var i=0;i<4;i++) document.getElementById('lease'+i).checked = (lease >> i) % 2;
		}				
	}
}
