// R HOUSE MY HOUSE LIST COOKIE FUNCTIONS
// VERSION 2.3	last modified 30/01/02
// HIDDEN FIELDS TO ASP MOVED DIRECT TO ASP PAGE
// Requires Cookie.js for storeMasterCookie() storeIntelligentCookie() and Get_Cookie() functions

// CONSTANTS
	var Cookies=true;
	var tablesArray=new Array();
	var listArray=new Array();
	var fieldNum=4;
	var listIsEmpty=false;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	VIEW MY LIST FUNCTIONS USED ON PAGE ONLY -- NOT GLOBAL	////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// PRINT MY LIST SELECT BOX
	function myListBox(){
		document.write('<select name="houseList" size="5">');
		var isEmpty=createListArray();
		if((isEmpty)||(isEmpty=="NONE")){
			document.write('<option value="NONE"> -- No Houses in List -- </option>');
			listIsEmpty=true;
		} else {
			for(x=0;x<(listArray.length-1);x++){
				var theHTML='<option value=" '+x+'">'+listArray[x][1]+' Beds -- '+listArray[x][2]+', '+listArray[x][3]+'.</option>';
				document.write(theHTML);
			}
		}
		document.write('</select>');
	}

// DISABLE FUNCTION BUTTONS IF THE LIST HAS NO ITEMS OR COOKIE IS MISSING
	function checkList(){
		if(listIsEmpty){
			document.all.listForm.next.disabled=true;
			document.all.listForm.remove.disabled=true;
			document.all.listForm.empty.disabled=true;
		}
	}

//  VIEW MY LIST PAGE FUNCTION BUTTON -- REMOVE ITEM FROM COOKIE CART
 	function removeHouse() {	
		var remLine=document.listForm.houseList.selectedIndex;
		if(remLine==-1){									 
			alert('Please select a house from the list.');
		} else {
			createListArray();
			if(listArray.length==2){
				emptyList();
				meRefresh();
				return;
			}
			while(remLine!=listArray.length-2){
				listArray[remLine]=listArray[remLine+1];
				remLine++;
			}
		writeToCookie(remLine-1);
		meRefresh();
		}
	}

// VIEW MY LIST PAGE FUNCTION BUTTON -- EMPTY HOUSE LIST
	function emptyList(reFreshMe){
		storeIntelligentCookie('R_stuList','EMPTY');
		if(reFreshMe!="false"){
			meRefresh();
		}
	}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	MY LIST FUNCTIONS GLOBAL FOR ALL SEARCH PAGES		////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// GET COOKIE DATA INTO listArray
	function createListArray(){
		var cookieData=Get_Cookie('R_stuList');	
		
		if(!cookieData){
			alert('No cookie found cannot create listArray[].');
			return('NONE');
			}
		
		if(cookieData=="EMPTY"){return true;}
		
		var tablesArray=cookieData.split('#');
		var not_end=true;
		var x=0;
		while(not_end){
			listArray[x]=tablesArray[x].split('?');
			if(!listArray[x][0]){not_end=false;}			
			x++;		
		}
	}

// ADD HOUSE TO COOKIE LIST
	function addHouse(ID,beds,street,pcode){

		// Create Item array to add to List Array
		var thisHouseArray=new Array(ID,beds,street,pcode);

		// Generate the List Array
		var isEmpty=createListArray();

		// Alert Variables
		var AddedAlert="This house has been added to your viewing list :-)";
		var AlreadyAlert="This house is already in your viewing list!";
		var NoCookieAlert="No R House List Cookie found, please enable cookies to use this facility";

		// If MyList Cookie cannot be found then inform user and cancel action
		if(isEmpty=="NONE"){
			//cookieNotFound(ID,beds,street,pcode);
			alert(NoCookieAlert);
			return;
		}
		
		// If MyList Cookie found but Empty then write in first item and end task
		if(isEmpty) { 
			listArray[0]=thisHouseArray;
			writeToCookie(0);
			alert(AddedAlert);
			return;						
		}
		
		// Cookies and items exist... Scroll through list and check the item is not already present. If present alert user and cancel task
		var not_found=true;
		for(x=0;x<(listArray.length-1);x++){
			if(listArray[x][0]==ID){
					alert(AlreadyAlert);
					not_found=false;
					return;
					x++;
			}
		}
			
		// Add the item and confirm to user if not present in the list
		if(not_found) {
			listArray[listArray.length-1]=thisHouseArray;
		}
		writeToCookie(listArray.length-1);
		alert(AddedAlert);		

 	}
	
// WRITE CART INFO TO COOKIE
 	function writeToCookie(x) {
		var cookieStr='';
		row_num=0;
			while(row_num<=x) {
				for(s=0;s<fieldNum;s++) {
					var recStr="?";
					if(s==(fieldNum-1)){recStr="#";}
					cookieStr=cookieStr+listArray[row_num][s]+recStr;
				}
			row_num++;
			}	
		storeIntelligentCookie('R_stuList',cookieStr);
 	}
	
// NO COOKIES GODDAMIT THIS IS DODGEY CODE SEEMS TO CAUSE CRASH
	function cookieNotFound(ID,beds,street,pcode){
		if(Cookies){
			storeIntelligentCookie('R_stuList','EMPTY');
			addHouse(ID,beds,street,pcode);
			Cookies=false;
			return;
		}else{
			alert('You must enable cookies to use the house list');
		}
	}
	
	function meRefresh(){
		location.href=location.href;
		}
		

	
	
	