function readCookiereturn()
{
	var name = "wishlisttest1";
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') 
		{
			c = c.substring(1,c.length);
		}
		
		if (c.indexOf(nameEQ) == 0)
		{ 
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function createCookie(value)
{
	var name = 'wishlisttest1';
	var days = 365;
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie()
{
	eraseCookie2(-1);
}

function eraseCookie2(value)
{
	var name = 'wishlisttest1';
	var days = "";
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function showDiv(id, PropertyID, cookie) 
{
	try
	{
		var emailaddi;
		if(cookie == 'n')
		{
			emailaddi = readCookiereturn();
			if(emailaddi == null || emailaddi == -1)
			{
				var main = id + '_' + PropertyID;
			    document.getElementById(main).style.display = 'block';
			}
			else
			{
				_readCookie(PropertyID);
			}
		}
		else
		{
			_readCookie(PropertyID);
		}
	}
	catch(e)
	{
		_error_message("file : "+e.fileName+" <br >on line :"+e.lineNumber+"<br>"+e.message);
	}
}

function _readCookie(PropertyID)
{
	var name = 'wishlisttest1';
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) 
		{
			addtowishlist(PropertyID, c.substring(nameEQ.length,c.length));
		}
	}
}

function addtowishlist(transactionid, email)
{
 	
	var data = $A();
 	data['email']	= email;
 	data['transactionID']	= transactionid;
 	CtrlCall("../_shared/js_to_ctrl/property_search/?param=function:_wishlist_set_wishlist;lang:js;","data="+serialize(data),"test");

	$('savelink_'+transactionid).innerHTML = "In Favourites";
}

function test(response)
{
	//alert(response.responseText);
}

function LoginHTML(PropertyID)
{
    var hideDivClose = "hideDivClose(\'wishlistdiv\', " + PropertyID + ");";
    var hideDiv      = "hideDiv(\'wishlistdiv\', " + PropertyID + ");";
	return '<div style=\"position:absolute;display:none;z-index:200;width:274;height:67;backgroundColor:#ffffff;\" id=\"wishlistdiv_' + PropertyID + '\">' +    
					'<table>' + 
						'<tr>' + 
							'<td>' +
								'Email :' +
							'</td>' +
							'<td>' +
								'<input type=\"text\" id=\"wishlistemail_' + PropertyID + '\" class=\"body\">' + 
							'</td>' + 
						'</tr>' + 
						'<tr>' + 
							'<td align=left>' +
								'<input class=\"button_wish\" type=\"button\" value=\"Close\" onclick=\"'+ hideDivClose + '\">' +
							'</td>' +					
							'<td align=right>' + 
								'<input class=\"button_wish\" type=\"button\" value=\"Save/login\" onclick=\"' + hideDiv + '\">' +
							'</td>' + 
						'</tr>' +
					'</table>' + 
				  '</div>';
}

function hideDivClose(id, PropertyID)
{	
		var main = id + '_' + PropertyID;
		document.getElementById(main).style.display = 'none';				
}

function trim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
var PropertyIDGlob;

function hideDiv(id, PropertyID)
{	
		var textbox    = 'wishlistemail_'+PropertyID;
		PropertyIDGlob = PropertyID;
		var email = trim(document.getElementById(textbox).value)
		if(email != '' )
		{
			CtrlCall("../_shared/js_to_ctrl/property_search/?param=function:_wishlist_Email_validate;lang:js;","data="+email,"emailValidated");
		}
		else
		{
			alert("Please enter a valid email thank you!");
		}
}

function emailValidated(response)
{
 	var Email = $M(response);
	if(Email['email'] == 'invalid')
	{
		alert('Please enter a valid email thank you!');
	}
	else
	{
		var main = 'wishlistdiv_' + PropertyIDGlob;
		document.getElementById(main).style.display = 'none';
		createCookie(document.getElementById('wishlistemail_'+PropertyIDGlob).value, PropertyIDGlob);
		addtowishlist(PropertyIDGlob, Email['email']);
	}
}

function ValidateLogin()
{
	var email = trim($('login').value);
	if(email != '' )
	{
		new Ajax.Request('/data/validateEmail.php',
		{
			onSuccess : function(response)
			{
				if(response.responseText == 'invalid')
				{
					alert('Please enter a valid email thank you!');
				}
				else
				{
					addemailaddi(email);
					createCookie(email);
					document.location.href = '/property/new-zealand/favourites/';
				}
					
			},
			onFailure : function(request)
			{
				alert("error...");
			},
			parameters : "email=" + email
		});
	}
	else
	{
		alert("Please enter a valid email thank you!");
	}
}

function addemailaddi(email)
{
	new Ajax.Request('/data/emailAdding.php',
	{
		onSuccess : function(response)
		{
			
		},
		onFailure : function(request)
		{
			alert("error...");
		},
		parameters : "emailaddress=" + email
	});
}

function deleteallfav(email)
{
	new Ajax.Request('/data/deleteEmailAddress.php',
	{
		onSuccess : function(response)
		{
			document.location.href = '/property/new-zealand/favourites/';
		},
		onFailure : function(request)
		{
			alert("error...");
		},
		parameters : "emailaddress=" + email
	});
}

function seeFavourites()
{
	emailaddi = readCookiereturn();
			
	if(emailaddi == null)
	{
		document.location.href = "/property/new-zealand/favourites/";
	}
	else
	{
		document.location.href = "/property/new-zealand/favourites/";
	}
}
var TransactionIDs =$H();
function transactionID_RS(response)
{
	var tmp = $M(response);
	var tmdReponse = tmp.property_Data.Properties[0].Property;
	tmdReponse.each(function(property)
	{
		TransactionIDs[property.TransactionID] = property.TransactionID;		
	});
}