// JavaScript Document
function setCompare(obj){
	var listMLNUM = fnGetCookie("HAR_MLNUM");
	//alert(listMLNUM);
	if(obj.checked){
		if (listMLNUM) {
			arrayMLNUM = listMLNUM.split(",");
			if (arrayMLNUM.length >= 5) {
				alert("you may only select 5 listings to compare")
				//obj.checked = false;
				return;
			} 
			else {
				for ( i=0; i<arrayMLNUM.length; i++) {
					if(arrayMLNUM[i]==obj.value){
						//the selected mlnum is already in cookie, return don't do anything
						return;
					}
				}
			}
			// only set after checked less than 5, and no dups
			fnSetCookie("HAR_MLNUM", listMLNUM + "," + obj.value);
			
		} else {
			// set first MLNUM
			fnSetCookie("HAR_MLNUM", obj.value);
		}
	} else 	{
		//remove MLNUM from the list
		if (listMLNUM) {
			arrayMLNUM = listMLNUM.split(",");
			listMLNUM = "";
			for ( i=0; i<arrayMLNUM.length; i++) {
				if(arrayMLNUM[i]!=obj.value){
					listMLNUM = listMLNUM + "," + arrayMLNUM[i];
				}
			}
			if (listMLNUM) {
				// if still have MLNUM left, reset the cookie
				listMLNUM = listMLNUM.substring(1, listMLNUM.length);
				fnSetCookie("HAR_MLNUM", listMLNUM);
			} else {
				// clear the cookie
				fnDeleteCookie("HAR_MLNUM");
			}
		}
	}
	//alert('after set ' + document.cookie)
}

function clearAllCheckbox() {
	//alert(document.form1.COMPARE.checked);
	if (document.form1.COMPARE.checked==true){
		document.form1.COMPARE.checked==false;
	}
	else {
		for (i=0; i<document.form1.COMPARE.length; i++) {
			document.form1.COMPARE[i].checked = false;
		}
	}
}

function fnGetCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


function fnDeleteCookie(name,path,domain) {
    if (fnGetCookie(name)) document.cookie = name + "=" +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ";expires=Mon, 01 Jan 2007 00:00:01 GMT";
}

function fnSetCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}
