function CIPPropertySearch_GetSelectedAreas(){
	//returns a multi dimenisonal array
	//element 1 contains the area names (label purposes)
	//element 2 contains the area names or codes e.g. GBS codes
	var oareas = [];
	oareas.push([]);
	oareas.push([]);
	var oigid = HttpManager.Document.GetObject("igid");
	if ( oigid ){
		if ( oigid.value.length > 0 && parseInt(oigid.value) != 2 ){
			oareas[0][0] = oigid.options[oigid.options.selectedIndex].text;
			oareas[1][0] = oigid.value;
		}
	}
	return oareas;

}

function CIPPropertySearch_OnStart(){
	CIPPropertySearch_Information_Hide();
	var oform = document[HttpManager.Document.GetObject("__formToMonitor").value];
	var odataValidation = new EstateWeb_Objects_DataValidationManager();
	var iminprice = document.isc.minprice.value;
	var imaxprice = document.isc.maxprice.value;
	var iminbeds = document.isc.bedrooms.value;
	var ilistingtype = document.isc.ListingType.value;
	var sareas = "";
	var aareas = CIPPropertySearch_GetSelectedAreas();
	for ( var i = 0; i < aareas[0].length; i ++ ){
		var aArea = aareas[0][i];
		if ( aArea.length > 0 ){
			if ( sareas.length == 0 ){
				sareas = aArea;
			}else if ( (i == (aareas[0].length - 1 )) && aareas[0].length > 1 ){
				sareas += " and " + aArea;
			}else{
				sareas += ", " + aArea;
			}		
		}

	}
	if ( sareas.length == 0 ){
		sareas = "all areas";
	}
	if ( iminprice.length == 0 || imaxprice.length == 0 ){
		alert("For email alerts, please select a minimum price and a maximum price");
	}else{
		var smessage = "{0} {1}roperties for {2} {3} in and around {4}";
		smessage = HttpManager.Strings.Format(smessage, 
																			( iminbeds.length > 0 ? iminbeds + " bedroom" : "" ),
																			( iminbeds.length > 0 ? "p" : "P" ),
																			( ilistingtype == "5" ? "sale" : "rent" ),
																				( iminprice.length > 0 && imaxprice.length > 0 ? "between " + odataValidation.currency.toLabel(iminprice) + " and "+odataValidation.currency.toLabel(imaxprice) : 
																					( iminprice.length > 0 ? "from " + odataValidation.currency.toLabel(iminprice) : 
																						( imaxprice.length > 0 ? "up to " +odataValidation.currency.toLabel(imaxprice) : "" )
																					)
																				),
																			sareas
																			);
		HttpManager.Document.GetObject("txtCIPSearchRequirements").innerHTML = smessage;
		odialog_cippropertysearch.show();	
	}

}


function CIPPropertySearch_Information_Hide(){
	HttpManager.Document.GetObject("pnlCIPSearchValidate").style.display = "none";
}

function CIPPropertySearch_Information_Show(mode,message){
	switch( mode ){
		case 1:
			HttpManager.Document.GetObject("imgSearchValidate").src = "/images/controls/cip/propertysearch/warning_icon.gif";
			break;
		case 2:
			HttpManager.Document.GetObject("imgSearchValidate").src = "/images/controls/cip/propertysearch/wait_icon.gif";
			break;
		case 3:
			HttpManager.Document.GetObject("imgSearchValidate").src = "/images/controls/cip/propertysearch/success_icon.gif";
			break;
		case 4:
			HttpManager.Document.GetObject("imgSearchValidate").src = "/images/controls/cip/propertysearch/error_icon.gif";
			break;
	}
	HttpManager.Document.GetObject("pnlCIPSearchValidate").style.display = "block";
	HttpManager.Document.GetObject("txtCIPSearchValidate").innerHTML = message;
}

function CIPPropertySearch_OnContinue(){
	CIPPropertySearch_Information_Hide();
	odialog_cippropertysearch.sizeElements();
	if ( HttpManager.Document.GetObject("chkCIPSignup").checked ){
		var msg = CIPPropertySearch_Validate();
		if ( msg.length > 0 ){
			CIPPropertySearch_Information_Show(1,msg);
			odialog_cippropertysearch.sizeElements();
		}else{
			CIPPropertySearch_Information_Show(2, "Please wait...");
			setTimeout(function(){
				//create person
				var ocipmanager = new EstateWeb_Objects_CIPManager();
				with ( ocipmanager.applicant.details ){
					title = HttpManager.Document.GetObject("slCIPTitle").value;
					firstName =  HttpManager.Document.GetObject("txtCIPFirstName").value;
					lastName = HttpManager.Document.GetObject("txtCIPLastName").value;
					emailAddress = HttpManager.Document.GetObject("txtCIPEmailAddress").value;
					mobileNumber = HttpManager.Document.GetObject("txtCIPMobileNumber").value;
					minimumPrice = ( document.isc.minprice.value.length > 0 ? document.isc.minprice.value : -1 );
					maximumPrice = ( document.isc.maxprice.value.length > 0 ? document.isc.maxprice.value : -1 );
					minimumBedrooms = ( document.isc.bedrooms.value.length > 0 ? document.isc.bedrooms.value : -1 );
					listingType = ( document.isc.ListingType.value == "5" ? "5" : "6" );
					areaIDs = CIPPropertySearch_GetSelectedAreas()[1];
				}
				var iresult = ocipmanager.applicant.create();
				switch ( iresult ){
					case ocipmanager.saveRecordResponseTypes.alreadyExists:
						CIPPropertySearch_Information_Show(1, "You have already joined our alerts system!");
						break;
					case ocipmanager.saveRecordResponseTypes.success:
						CIPPropertySearch_Information_Show(3, "Completed");
						setTimeout( function(){ document[HttpManager.Document.GetObject("__formToMonitor").value].submit(); }, 600 );
						break;
					default:
						CIPPropertySearch_Information_Show(4, "The service is unavailable, please try again later");
						break;
				}
			}, 500);
		}
	}else{
		document[HttpManager.Document.GetObject("__formToMonitor").value].submit();
	}
}

function CIPPropertySearch_Validate(){
	var odataValidation = new EstateWeb_Objects_DataValidationManager();
	if ( HttpManager.Document.GetObject("slCIPTitle").value.length == 0 ){
		return "Please select an appropriate title";
	}
	if ( HttpManager.Document.GetObject("txtCIPFirstName").value.length < 2 ){
		return "Please enter your first name";
	}
	if ( HttpManager.Document.GetObject("txtCIPLastName").value.length < 2 ){
		return "Please enter your last name";
	}
	if (! odataValidation.email.emailAddress.validate(HttpManager.Document.GetObject("txtCIPEmailAddress").value) ){
		return "Please enter a valid email address";
	}
	if ( HttpManager.Document.GetObject("txtCIPMobileNumber").value.length < 2 ){
		return "Please enter a contact number";
	}
	return "";
}

function CIPPropertySearch_EmailSaveOptionsOnClick(){
	HttpManager.Document.GetObject("emailAlert").style.display = "none";
}


if ( typeof EstateWeb_Objects_DialogManager != "undefined" ){
	odialog_cippropertysearch = new EstateWeb_Objects_DialogManager("cippropertysearch");
}

with ( odialog_cippropertysearch ){
	parentID = "body";
	styleID = 2;
	parentMarginLeftOffset = 0;
	backgroundOptions.opacity = 0.5;
	dialogOptions.backgroundColour = "#000000";
	dialogOptions.width = 510;
	dialogOptions.height = 420;
	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.panel;
	dialogOptions.panelOptions.contentID = "pnlPropertySearchCIPPanel";
}