/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session) [expires.toGMTString() use string instead]
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Check if the Cookies are blocked.
 *
 * Returns a true if cookies are blocked
 */
function isCookieBlocked() {
    // Remove this check
    return false;	
    
	var dc = document.cookie;
	// No cookie
	if (!dc) {
		alert ("Cookies are required to create MyList. Visit the menu Tools/ Internet Options/Security to change your cookie settings.");
		return true;	
	}
	return false;
 }
 
/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
	var dc = document.cookie;
	// No cookie
	if (!dc) {
		return null;	
	}
    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));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/**
 * Checks for a value in the cookie.
 *
 * name		  Name of the desired cookie.
 * value	  The value to search in the cookie
 *
 */
function isCookieNewValue(name,value) {
	var cookieValues;
	var isNewValue;
	 
	isNewValue = true;
	cookieValues = getCookie(name);
	if (cookieValues) {
	
		var cookieValuesArray = cookieValues.split(",");
		
		for (var x=0; x<cookieValuesArray.length ; x++ ){
			if (cookieValuesArray[x] == value) {
				// If there is already a value in the cookie
				isNewValue = false;
				//alert ('This Property is already on your short list.');
			}
			
		}
	} 
		
	return isNewValue;
}

/**
 * Deletes a value from a cookie.
 *
 * name		  Name of the desired cookie.
 * value	  The value to search in the cookie
 *
 */
 
 function deleteCookieValue(name,value) {
	var cookieValues;
	var cookieNewValues;
	cookieValues = getCookie(name);
	
	if (cookieValues) {
		cookieNewValues = "";
		var cookieValuesArray = cookieValues.split(",");
		for (var x=0; x<cookieValuesArray.length ; x++ ){
			if (!(cookieValuesArray[x] == value)) {
				cookieNewValues =  cookieNewValues + cookieValuesArray[x]  + ',' ;
			}			
		}
		cookieNewValues = cookieNewValues.substring(0,cookieNewValues.lastIndexOf(','));
		
		var todays_date = new Date();	// Current Date  
		var expires_date = new Date(todays_date.getTime() + (7 * 86400000)); // 1 weeks from now
		setCookie(name,cookieNewValues,expires_date,'/')
	}	
 }

/**
 * Update the value of the specified cookie.
 *
 * name		  Name of the desired cookie.
 * delimiter  The delimiter between the values of the cookie.
 *
 */
function updateMyList(value) {
	 var oldCookieValue;
	
	 // Append the new cookie value 
	if (isCookieNewValue('ShortList')) {
		oldCookieValue = getCookie('ShortList');
		if (oldCookieValue) {
			value = oldCookieValue  + ',' + value;			
		} 
	}	
	
	var todays_date = new Date();	// Current Date  
	var expires_date = new Date(todays_date.getTime() + (7 * 86400000)); // 1 weeks from now
	setCookie('ShortList',value,expires_date,'/');
}

/**
 * Get the number of Properties in the ShortList 
 *
 * name		  Name of the desired cookie.
 * delimiter  The delimiter between the values of the cookie.
 *
 */
function MyListNumOfProp() {
	var cookieValues;
	var returnValue;
	 
	returnValue = 0;
			
	cookieValues = getCookie('ShortList');
	
	if (cookieValues) {
		var cookieValuesArray = cookieValues.split(",");
		
		if (cookieValuesArray) {
			returnValue =  cookieValuesArray.length;
		}
	}	
	
	return returnValue;
}

/**
 * Display Shortlist cookie information 
 *
 * name		  Name of the desired cookie.
 * delimiter  The delimiter between the values of the cookie.
 *
 */
function AddToMyList(PropertyID,State){
		
	if (isCookieBlocked() ) {
		return;
	}

	AddPropertyToMyList (PropertyID,State,true);
	//alert(State + ' ' + PropertyID + ' has been added to MyList');
	window.open("/home/property/addmylist.aspx?pids=" + PropertyID, "_blank", "status=no,toolbar=no,location=no,resizable=yes,menubar=no,width=550,height=250,modal=yes");

	// Display On MyList link
	var propertydisplayaddmyist = document.getElementById("propertydisplayaddmyist_" + PropertyID);
	var propertydisplayonmyist = document.getElementById("propertydisplayonmyist_" + PropertyID);
	if (propertydisplayaddmyist && propertydisplayonmyist) {
	    HideBlock("propertydisplayaddmyist_" + PropertyID);
	    ShowBlock("propertydisplayonmyist_" + PropertyID);
	}		
}

function AddPropertyToMyList (PropertyID,State,ShowMessage) {
	var oDiv = document.getElementById("divShortList");
	var oDivSearch = document.getElementById("divShortListAdd");
	var oSpanNumOfProps = document.getElementById("spanMyListNumOfProps");
	var oSpanMyListTabCount = document.getElementById("myListTabCount");
	
	// View new properties in short list
	if (isCookieNewValue('ShortList',PropertyID)) {
	
		updateMyList(PropertyID);
	}
	
	// Add the comment to the page	
	if (oDiv) {
		var content = oDiv.innerHTML;
		if(content.indexOf("Search and add properties")>-1){
			content = "";
		}
		oDiv.innerHTML =  content +  '<span class="shortlist">&nbsp;&nbsp;&nbsp;-&nbsp;</span><a class="shortlist" href="javascript:GotToListPropertyPage(' + PropertyID + ');">' + State + '&nbsp;&nbsp;' + PropertyID + '</a><br>';
		//oDiv.style.visibility = "visible";	
	}
	if (oDivSearch) {
		if (ShowMessage){
			// Search Page confirmation
			oDivSearch.innerHTML  = State  + ' ' + PropertyID +  ' has been added to your list.<br>';
		} else {
			oDivSearch.innerHTML  = 'Selected property(s) have been added to your list.<br>';
		}
    }

    // Display On MyList link
    var propertydisplayaddmyist = document.getElementById("propertydisplayaddmyist_" + PropertyID);
    var propertydisplayonmyist = document.getElementById("propertydisplayonmyist_" + PropertyID);
    if (propertydisplayaddmyist && propertydisplayonmyist) {
        HideBlock("propertydisplayaddmyist_" + PropertyID);
        ShowBlock("propertydisplayonmyist_" + PropertyID);
    }

    var count = MyListNumOfProp();
	
	if (oSpanNumOfProps) {
	    oSpanNumOfProps.innerHTML = count;
	}
	if (oSpanMyListTabCount) {
        oSpanMyListTabCount.innerHTML = "(" + count + ")";
	}
}

// Add Multiple Properties
function AddToMyListSplit (PropStateSplit) {
	var arrPropStateSplit = PropStateSplit.split("|");
	
	if (arrPropStateSplit.length >0) {
		var PropertyID = arrPropStateSplit[0];
		var State = arrPropStateSplit[1];
		
		AddPropertyToMyList (PropertyID,State,false);
	}

}

// From includes/componebt/ShortList.asp,  check of there are cookies in short list
function ValidateList() {
	if (!getCookie("ShortList")) {
		alert ('There are no properties in MyList');
		return false;
	} 
	return true;
}

// From includes/componebt/ShortList.asp, redirect to manage my list
function ManageList  () {
	if (!ValidateList()) {
		return ;
	}
	window.location.href = '/property/propShortList.aspx';
}