function EstateWeb_Objects_CIPManager(){
	this.applicant = new EstateWeb_Objects_CIPManager_Applicant(this);
	this.saveRecordResponseTypes = new EstateWeb_Objects_CIPManager_SaveRecordResponseTypes();
}

function EstateWeb_Objects_CIPManager_Applicant(o){
	this.parent = o;
	this.details = new EstateWeb_Objects_CIPManager_Applicant_Details();
	this.create = EstateWeb_Objects_CIPManager_Applicant_Create;
	this.retrieve = EstateWeb_Objects_CIPManager_Applicant_Retrieve;
}

function EstateWeb_Objects_CIPManager_Applicant_Details(){
	this.id = -1;
	this.title = "";
	this.firstName = "";
	this.lastName = "";
	this.emailAddress = "";
	this.mobileNumber = "";
	this.isLoggedIn = false;
	this.minimumPrice = -1;
	this.maximumPrice = -1;
	this.minimumBedrooms = -1;
	this.listingType = 5;
	this.areas = [];
	this.areaIDs = [];
	this.hasProfile = false;
}

function EstateWeb_Objects_CIPManager_Applicant_Create(){
	var oresponse = CIPManager.CreateApplicantClientSide(this.details.title,
																				  this.details.firstName,
																				  this.details.lastName,
																				  "",
																				  "",
																				  "",
																				  "",
																				  "",
																				  "",
																				  "",
																				  "",
																				  "",
																				  this.details.mobileNumber,
																				  "",
																				  "",
																				  this.details.emailAddress,
																				  this.details.areas,
																				  this.details.minimumPrice,
																				  this.details.maximumPrice,
																				  this.details.minimumBedrooms,
																				  -1,
																				  -1,
																				  this.details.listingType, 
																				  this.details.areaIDs);
																				  
	return this.parent.saveRecordResponseTypes.match( oresponse.value.ResponseType );
}

function EstateWeb_Objects_CIPManager_Applicant_Retrieve(){
	var oresponse = CIPManager.Profile();
	if (oresponse.value){
		with ( oresponse.value ){
			this.details.title = Title;
			this.details.firstname = FirstName;
			this.details.lastname = LastName;
			this.details.emailaddress = EmailAddress;
			this.details.isLoggedIn = IsLoggedIn;
			this.details.hasProfile = true;
		}
	}
}

function EstateWeb_Objects_CIPManager_SaveRecordResponseTypes(){
	this.success = 0;
	this.alreadyExists = 1;
	this.failure = 2;
	this.match = EstateWeb_Objects_CIPManager_SaveRecordResponseTypes_Match;
}

function EstateWeb_Objects_CIPManager_SaveRecordResponseTypes_Match(val){
	switch (val.toLowerCase()){
		case "success":
			return this.success;
			break;
		case "alreadyexists":
			return this.alreadyExists;
			break;
		case "failure":
			return this.failure;
			break;
	}
}

// inline registration //


var odialog_cippropertysearch;
HttpManager.Browser.events.addHandler("onload", window, function(){
	if ( typeof EstateWeb_Objects_DialogManager != "undefined" ){
		odialog_cippropertysearch = new EstateWeb_Objects_DialogManager("cippropertysearch");
	}else{
		alert("Dialog Manager not found");
	}
});


function EstateWeb_Objects_CIPInlineRegistration(){
	this.setListingType = CIPInlineRegistration_setListingType;
	this.setMinimumPrice = CIPInlineRegistration_setMinimumPrice;
	this.setMaximumPrice = CIPInlineRegistration_setMaximumPrice;
	this.setMinimumBedrooms = CIPInlineRegistration_setMinimumBedrooms;
	this.setAreaNames = CIPInlineRegistration_setAreaNames;
}

function CIPInlineRegistration_setAreaNames(value){
	HttpManager.Document.GetObject("__cip_requirements_areaNames").value = value;
}

function CIPInlineRegistration_setListingType(value){
	HttpManager.Document.GetObject("__cip_requirements_listingtype").value = value;
}

function CIPInlineRegistration_setMinimumPrice(value){
	HttpManager.Document.GetObject("__cip_requirements_minimumprice").value = value;
}

function CIPInlineRegistration_setMaximumPrice(value){
	HttpManager.Document.GetObject("__cip_requirements_maximumprice").value = value;
}

function CIPInlineRegistration_setMinimumBedrooms(value){
	HttpManager.Document.GetObject("__cip_requirements_minimumbedrooms").value = value;
}

var CIPInlineRegistration = new EstateWeb_Objects_CIPInlineRegistration();

function CIPInlineRegistration_Start(){
	
	if ( CIPInlineRegistration_IsRequired() ){
		if ( CIPInlineRegistration_Validate() ){
			
			with ( odialog_cippropertysearch ){
				parentID = "body";
				styleID = 1;
				parentMarginLeftOffset = 0;
				backgroundOptions.opacity = 0.5;
				dialogOptions.backgroundColour = "#000000";
				dialogOptions.width = 510;
				dialogOptions.height = 490;
				dialogOptions.contentPaddingLeft = "15px";
				dialogOptions.contentPaddingRight = "15px";
				dialogOptions.contentPaddingTop = "10px";
				dialogOptions.contentPaddingBottom = "7px";
				dialogOptions.title.text = "Property E-mail Alerts";
				dialogOptions.title.cssClass = "cipPropertySearchRegisterTitle";
				dialogOptions.title.fontColour = "#FFFFFF";
				dialogOptions.type = dialogTypes.page;
				dialogOptions.pageOptions.url = "/Controls/CIP/CIPInlineRegistration.aspx?"+CIPInlineRegistration_GetParmeters();
				show();
			}
			
		}
	}else{
		CIPInlineRegistration_Redirect();
	}
}

function CIPInlineRegistration_onStart(domain,params) {
	with ( odialog_cippropertysearch ){
		parentID = "body";
		styleID = 1;
		parentMarginLeftOffset = 0;
		backgroundOptions.opacity = 0.5;
		dialogOptions.backgroundColour = "#000000";
		dialogOptions.width = 510;
		dialogOptions.height = 490;
		dialogOptions.contentPaddingLeft = "15px";
		dialogOptions.contentPaddingRight = "15px";
		dialogOptions.contentPaddingTop = "10px";
		dialogOptions.contentPaddingBottom = "7px";
		dialogOptions.title.text = "Property E-mail Alerts";
		dialogOptions.title.cssClass = "cipPropertySearchRegisterTitle";
		dialogOptions.title.fontColour = "#FFFFFF";
		dialogOptions.type = dialogTypes.page;
		dialogOptions.pageOptions.url = "http://"+domain + "/centralised/applicants/register.aspx?"+params+"&oid=odialog_cippropertysearch";
		show();
	}
}

function CIPInlineRegistration_Redirect(){
	switch ( parseInt(HttpManager.Document.GetObject("__cip_requirements_redirectionType").value) ){
		case 0:
			if ( HttpManager.Document.GetObject("__cip_requirements_formName").value.length > 0 ){
				if ( HttpManager.Document.GetObject( HttpManager.Document.GetObject("__cip_requirements_formName").value ) ){
					HttpManager.Document.GetObject( HttpManager.Document.GetObject("__cip_requirements_formName").value ).submit();
				}else{
					alert("CIP: Form could not be found");
				}
			}else{
				alert("CIP: Form name not defined");
			}
			break;
		case 1:
			if ( HttpManager.Document.GetObject("__cip_requirements_redirectionURL").value.length > 0 ){
				location.href = HttpManager.Document.GetObject("__cip_requirements_redirectionURL").value;
			}else{
				alert("CIP: No redirection URL could be found");
			}
			
			break;
	}
}

function CIPInlineRegistration_GetParmeters(){
	var aparams = [];
	aparams.push( "minprice=" + ( HttpManager.Document.GetObject("__cip_requirements_minimumprice").value.length > 0 ? HttpManager.Document.GetObject("__cip_requirements_minimumprice").value : -1 ) );
	aparams.push( "maxprice=" + ( HttpManager.Document.GetObject("__cip_requirements_maximumprice").value.length > 0 ? HttpManager.Document.GetObject("__cip_requirements_maximumprice").value : -1 ) );
	aparams.push( "listingtype=" + HttpManager.Document.GetObject("__cip_requirements_listingtype").value );
	aparams.push( "minbedrooms="+ ( HttpManager.Document.GetObject("__cip_requirements_minimumbedrooms").value.length > 0 ? HttpManager.Document.GetObject("__cip_requirements_minimumbedrooms").value : -1 ) );
	aparams.push( "areanames=" + escape(HttpManager.Document.GetObject("__cip_requirements_areaNames").value) );
	aparams.push( "areavalues=" + escape(HttpManager.Document.GetObject("__cip_requirements_areaValues").value) );
	aparams.push( "redirectionType=" + HttpManager.Document.GetObject("__cip_requirements_redirectionType").value );
	aparams.push( "formName=" + HttpManager.Document.GetObject("__cip_requirements_formName").value );
	aparams.push( "redirectionURL=" + escape(HttpManager.Document.GetObject("__cip_requirements_redirectionURL").value.replace("/", "")) );
	aparams.push( "oid=odialog_cippropertysearch" );
	return aparams.join("&");
}

function CIPInlineRegistration_IsRequired(){
	if ( EmailAlerts_Yes ){
		return ( EmailAlerts_Yes.checked || ( EmailAlerts_Yes.checked == false && EmailAlerts_No.checked == false ) );
	}else{
		return false;
	}
}

function CIPInlineRegistration_Validate(){
	if ( EmailAlerts_Yes.checked == false && EmailAlerts_No.checked == false ){
		CIPInlineRegistration_ShowEmailAlertBalloon();
		return false;
	}else if ( parseInt(HttpManager.Document.GetObject("__cip_requirements_maximumprice").value) <= 0 || HttpManager.Document.GetObject("__cip_requirements_maximumprice").value.length == 0 ){
		alert("For email alerts, please select a maximum price");
		return false;
	}else if ( parseInt(HttpManager.Document.GetObject("__cip_requirements_minimumprice").value) >= parseInt(HttpManager.Document.GetObject("__cip_requirements_maximumprice").value) ){
		alert("The minimum price selected cannot be greater than the maximum price selected");
		return false;
	}
	return true;
}


function CIPInlineRegistration_EmailSaveOptionsOnClick(){
	if (HttpManager.Document.GetObject("pnlCIPEmailAlert") ){
		HttpManager.Document.GetObject("pnlCIPEmailAlert").style.display = "none";
	}else{
		alert("Unable to find email alert balloon named pnlCIPEmailAlert");
	}
}

function CIPInlineRegistration_ShowEmailAlertBalloon(){
	if (HttpManager.Document.GetObject("pnlCIPEmailAlert") ){
		HttpManager.Document.GetObject("pnlCIPEmailAlert").style.display = "block";
	}else{
		alert("Unable to find email alert balloon named pnlCIPEmailAlert");
	}
}

