var expDays = 100;
var exp = new Date(); 

exp.setTime(exp.getTime() + (expDays*24*60*60*1000));


var sap = "~~sap~~";

function getcookiearr(field){
	var cookieval = GetCookie (field);
	thecookiearr = new Array();
	if (cookieval !=null)
		thecookiearr = cookieval.split(sap);
	return thecookiearr;
}
	
function writecookiearr(field, thearr){
	thestr = thearr[0];
	for (i=1; i<thearr.length; i++){
		if (thearr[i]!="")
			thestr +=sap+thearr[i];
	}
	if (thestr==sap)
		thestr = "";
	/*SetCookie (field, thestr, exp, null,'alpha.ixzon.net');
	SetCookie (field, thestr, exp, null,'basecreate.com');*/
	SetCookie (field, thestr);
	/*SetCookie (field, thestr, null, "/colliers_20070529/", "basecreate.com");*/
	//SetCookie (field, thestr, null, "/colliers/20070529/", "alpha.ixzon.net");
}
	
function savecookie(field, refno){
	thecookies = getcookiearr(field);
	for (i=0; i<thecookies.length; i++){
		if (thecookies[i]==refno){
			alert("You had already selected this property");
			return true;
		}		
	}
	thecookies[i] = refno;
	writecookiearr(field, thecookies);

	return true;
}

function delcookie(field, refno){
	thecookies = getcookiearr(field);
	for (i=0; i<thecookies.length; i++){
		if (thecookies[i]==refno){
			thecookies[i] = "";
			break;
		}		
	}
	writecookiearr(field, thecookies);

	return true;/*
	var cookieval = GetCookie (field);
	if (cookieval == null)
		return true;
	else {
		var cookieval = cookieval.replace(refno, "");	
		var cookieval = cookieval.replace(sap+sap, '');	
		SetCookie (field, cookieval);
	}
	return true;*/
}

function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  

	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;

}

function SetCookie (name, value) {  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : exp;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
						((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
						((path == null) ? "" : ("; path=" + path)) +  
						((domain == null) ? "" : ("; domain=" + domain)) +    
						((secure == true) ? "; secure" : "");
	/*alert(name + "=" + escape (value) + 
						((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
						((path == null) ? "" : ("; path=" + path)) +  
						((domain == null) ? "" : ("; domain=" + domain)) +   
						((secure == true) ? "; secure" : ""));					*/
}

function cookieForms() {  
	var mode = cookieForms.arguments[0];
	for(f=1; f<cookieForms.arguments.length; f++) {
		formName = cookieForms.arguments[f];
		if(mode == 'open') {	
			cookieValue = GetCookie('saved_'+formName);
			if(cookieValue != null) {
				var cookieArray = cookieValue.split('#cf#');
				if(cookieArray.length == document[formName].elements.length) {
					for(i=0; i<document[formName].elements.length; i++) {
						if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }
						else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }
						else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }
						else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }

					}
				}
			}
		}

		if(mode == 'save') {	
			cookieValue = '';
			for(i=0; i<document[formName].elements.length; i++) {
				fieldType = document[formName].elements[i].type;
				if(fieldType == 'password') { passValue = ''; }
				else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }
				else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }
				else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }
				else { passValue = document[formName].elements[i].value; }
				cookieValue = cookieValue + passValue + '#cf#';
			}
			cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter
			SetCookie('saved_'+formName, cookieValue, exp);		

		}	
	}
}

function addToSelections(theid){
	savecookie("refs", theid);	
	xajax_show_cookiebox(0);
	document.getElementById("cookiebox").scrollTop = 0;
	return false;
}
function removeFromSelections(theid){
	delcookie("refs", theid);
	xajax_show_cookiebox(0);
	return false;
}
function removeAllSelections(){
	SetCookie ("refs", "");
	xajax_show_cookiebox(0);
	return false;
}