
/*********************************************************************
Validation functions
******************************************************************** */
var errorColor = "transparent";
var okColor = "transparent";
var county = "";
function validateReckoner() {
	/* Ready Reckoner*/
	var error = 0;


	var errorLocation = (document.all != null ? document.all["errorLocation"] : document.getElementById("errorLocation"));
	var ddlHouseType = (document.all != null ? document.all["houseType"] : document.getElementById("houseType"));
	var errorHouseType = (document.all != null ? document.all["errorHouseType"] : document.getElementById("errorHouseType"));
	var ddlBedrooms = (document.all != null ? document.all["bedrooms"] : document.getElementById("bedrooms"));
	var errorBedrooms = (document.all != null ? document.all["errorBedrooms"] : document.getElementById("errorBedrooms"));
	if (validateDDL(ddlHouseType) == false) {
		error = 1;
		errorHouseType.style.display = "block";
	} else {
		errorHouseType.style.display = "none";
	}

	error = validateSquareFootage(error);
	error = validateOtherBuildings(error); 
	error = validateHighSpec(error);
	if (error == 1) {
		return false;
	} else {
		return true;
	}
}
function onReckonerSubmit() {
	if (validateReckoner()) {
		setParentAmount();
		parent.quoteDetailsFadeOut('floatingEstimatorDiv');
		rebuildCostFocus();



	} else {
		return false;
	}
}

/*********************************************************************
Poplates and decides the visibility of the No. of bedrooms
dropdown box depending on the Type of house selected 
*********************************************************************/
function setOptions(event) {
	var houseType = (document.all != null ? document.all["houseType"] : document.getElementById("houseType"));
	var chosen = houseType.value;
	 //(document.all != null ? document.all["bedrooms"] : 
	var bedrooms = document.getElementById("bedrooms");

	bedrooms.options.length = 0;
	if (chosen == "T") {
		bedroomDisplays("visible");
		bedrooms.options[bedrooms.options.length] = new Option("1 - 2", "1");
		bedrooms.options[bedrooms.options.length] = new Option("3+", "2");
	}
	if (chosen == "S") {
		bedroomDisplays("visible");
		bedrooms.options[bedrooms.options.length] = new Option("1 - 3", "3");
		bedrooms.options[bedrooms.options.length] = new Option("4+", "4");
	}
	if (chosen == "D") {
		bedroomDisplays("hidden");
	}
	if (chosen == "B") {
		bedroomDisplays("hidden");
	}
}
/*********************************************************************
Validates the text entered in the High Spec text area and 
displays any errors that result.
*********************************************************************/
function validateHighSpec(error) {
	var highSpec = (document.all != null ? document.all["highSpec"] : document.getElementById("highSpecId"));
	var errorHighSpec = (document.all != null ? document.all["errorHighSpec"] : document.getElementById("errorHighSpec"));
	var errorSpecMinMaxValue = (document.all != null ? document.all["errorSpecMinMaxValue"] : document.getElementById("errorSpecMinMaxValue"));
	clearValidationErrors(errorHighSpec, errorSpecMinMaxValue);
	var value = highSpec.value;
	if (value > 0) {
		if (!validatePositiveNumeric(value)) {
			error = 1;
			errorHighSpec.style.display = "block";
		} else {
			if (!validateMinMaxValue(value)) {
				error = 1;
				errorSpecMinMaxValue.style.display = "block";
			}
		}
	}
	return error;
}
/*********************************************************************
Validates the text entered in the Other Buildings text area and 
displays any errors that result.
*********************************************************************/
function validateOtherBuildings(error) {
	var otherBuilding = document.getElementById("otherBuilding");
	var errorOtherBuilding = document.getElementById("errorOtherBuilding");
	var errorOtherMinMaxValue = document.getElementById("errorOtherMinMaxValue");
	var value = otherBuilding.value;
	clearValidationErrors(errorOtherMinMaxValue, errorOtherBuilding);
	if (value > 0) {
		if (!validatePositiveNumeric(value)) {
			error = 1;
			errorOtherBuilding.style.display = "block";
		} else {
			if (!validateMinMaxValue(value)) {
				error = 1;
				errorOtherMinMaxValue.style.display = "block";
			}
		}
	}
	return error;
}
/*********************************************************************
Validates the text entered in the Square Footage text area and 
displays any errors that result.
*********************************************************************/
function validateSquareFootage(error) {
	var squarefootage = (document.all != null ? document.all["squareFootage"] : document.getElementById("squareFootage"));
	var squarefootageErr = (document.all != null ? document.all["errorSquareFootage"] : document.getElementById("errorSquareFootage"));
	var minMaxFootageErr = (document.all != null ? document.all["errorMinMaxFootage"] : document.getElementById("errorMinMaxFootage"));
	var validNumberErr = (document.all != null ? document.all["errorValidNumber"] : document.getElementById("errorValidNumber"));
	clearValidationErrors(squarefootageErr, minMaxFootageErr);
	if (!validatePositiveNumeric(squarefootage.value)) {
		error = 1;
		squarefootageErr.style.display = "block";
	} else {
		if (!validateMinMaxFootage(squarefootage.value)) {
			error = 1;
			minMaxFootageErr.style.display = "block";
		}
	}
	return error;
}
/*********************************************************************
Sets up some vars when the rebuild-estimator page loads
*********************************************************************/
function autoValidationReckonerInit() {




    var parentCountyId = parent.document.getElementById("countyId").value;
	 if(parentCountyId !=null)
    {
		var countyName = getCountyName(parentCountyId);
		var countyLabel = document.getElementById("countyLabel");
		countyLabel.innerHTML = countyName;
	}
	else
	{
		countyLabel.innerHTML = ""; 
	}
	


	var houseType = (document.all != null ? document.all["houseType"] : document.getElementById("houseType"));
	var bedrooms = (document.all != null ? document.all["bedrooms"] : document.getElementById("bedrooms"));
	var errorHouseType = (document.all != null ? document.all["errorHouseType"] : document.getElementById("errorHouseType"));
	var errorBedrooms = (document.all != null ? document.all["errorBedrooms"] : document.getElementById("errorBedrooms"));
	var squareFootage = (document.all != null ? document.all["squareFootage"] : document.getElementById("squareFootage"));
	var squareFootageErr = (document.all != null ? document.all["errorSquareFootage"] : document.getElementById("errorSquareFootage"));
	bedrooms.errorMessage = errorBedrooms;
	houseType.errorMessage = errorHouseType;
	squareFootage.errorMessage = squareFootageErr;
	squareFootage.isNumericValidation = true;
	var house = (document.all != null ? document.all["houseType"] : document.getElementById("houseType").value);
	var roomsNum = (document.all != null ? document.all["bedrooms"] : document.getElementById("bedrooms").value);
	getCountyName();
	houseType.onchange = function (evt) {
		setOptions(houseType);
		autoValidateDropDownSelection(evt);

	};
}
/*********************************************************************
Calculates the total cost of rebuilding the house based on all 
the details entered
*********************************************************************/
function calculateRebuild() {
	var squarefootage = document.getElementById("squareFootage");
	var otherBuilding = document.getElementById("otherBuilding");
	var highSpec = document.getElementById("highSpecId");
	var houseSquareFootage = squarefootage.value;
	var otherBuildingsValue = otherBuilding.value;
	var highSpecValue = highSpec.value;
	var costPerFoot = getHouseCostPerFoot();
		if(costPerFoot == 0){
		return 0;
	}
	var totalCost = ((costPerFoot * houseSquareFootage) + Math.abs(highSpecValue) + Math.abs(otherBuildingsValue));
	return totalCost;
}
/*********************************************************************
Puts the resulting total cost of rebuilding the house in a text area
on the parent window of the div.
*********************************************************************/
function setParentAmount() {

	var pardoc = parent.document;
	var parentAmount = pardoc.getElementById("rebuildCost");
	var totalCost = calculateRebuild();
	parentAmount.value = totalCost;
	return true;
}
function clearValidationErrors(error1, error2) {
	error1.style.display = "none";
	error2.style.display = "none";
}
function validateMinMaxFootage(footage) {
	if (footage < 600 || footage > 5000) {
		return false;
	} else {
		return true;
	}
}
function clearErrors(roomNum) {
	roomNum.errorMessageSemiDetached.style.display = ("None");
	roomNum.errorMessageTerraced.style.display = ("None");
	roomNum.errorMessageDetached.style.display = ("None");
	roomNum.errorMessageBunglow.style.display = ("None");
}
function validateNumeric(val) {
	/* Numeric */
	if (val == "") {
		return false;
	}
	var validChars = "0123456789";
	var Char;
	for (i = 0; i < val.length; i++) {
		Char = val.charAt(i);
		if (validChars.indexOf(Char) == -1) {
			return false;
		}
	}
	return true;
}
function validatePositiveNumeric(val) {
	/* Numeric */
	if (val == "") {
		return false;
	}
	var validChars = "0123456789";
	var Char;
	var nonZero = false;
	for (i = 0; i < val.length; i++) {
		Char = val.charAt(i);
		if (validChars.indexOf(Char) == -1) {
			return false;
		}
		if (Char != "0") {
			nonZero = true;
		}
	}
	if (!nonZero) {
		return false;
	}
	return true;
}
function checkCharacterEnteredIsNumber(elementId) {
	var element = document.getElementById(elementId);
	if (isNaN(new Number(element.value))) {
		element.value = element.value.substring(0, element.value.length - 1);
	}
}
/*
 * Validates that value other than the default '----------- Please select ----------' or 'empty' has been selected.
  */
function validateSelectOption(val) {
	/* Value other than default '---------Please Select---------' */
	if (val == "" || "----------- Please select ----------" == val || "empty" == val) {
		return false;
	} else {
		return true;
	}
}
/* Arekibo Validation functions */
function validateDDL(ddl) {
	return (ddl != null && ddl.selectedIndex > 0);
}
function autoValidateDropDownSelection(ev) {
	if (!ev) {
		ev = event;
	}
	var input = ev.target || ev.srcElement;
	var error = !validateSelectOption(input.value);
	input.errorMessage.style.display = (error ? "block" : "none");
	input.parentNode.style.backgroundColor = (error ? errorColor : okColor);
}
/*
 * Validates that value other than the default '----------- Please select ----------' or 'empty' has been selected.
  */
function validateSelectOption(val) {
	/* Value other than default '---------Please Select---------' */
	if (val == "" || "----------- Please select ----------" == val || "empty" == val) {
		return false;
	} else {
		return true;
	}
}
/*********************************************************************
Formats the monetary values entered correctly.
*********************************************************************/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, "");
	if (isNaN(num)) {
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	


	num = Math.floor(num / 100).toString();
	if (cents < 10) {
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 * i + 3)) + "," + num.substring(num.length - (4 * i + 3));
	}
	return (((sign) ? "" : "-") + num + ".");
}
function setCounty() {
	var parentCounty = (document.all != null ? parent.document.all["countyID"] : parent.document.getElementById("countyID"));
	var countySelect = (document.all != null ? document.all["countyID"] : document.getElementById("countyID"));
	var countySpan = (document.all != null ? document.all["countyLabel"] : document.getElementById("countyLabel"));
	countySpan.innerHTML = parentCounty.value;
}
/*********************************************************************
Function to ensure that a pirticular type of house has the correct number of rooms
in them.
******************************************************************** */
function getNumBeds(house) {
	var rooms = (document.all != null ? document.all["bedrooms"] : document.getElementById("bedrooms")).value;
	var bedValue = "";
	if (house == "T") {
		if (rooms == 1) {
			bedValue = 2;
		} else {
			bedValue = 3;
		}
	} else {
		if (house == "S") {
			if (rooms == 3) {
				bedValue = 3;
			} else {
				bedValue = 4;
			}
		} else {
			bedValue = 4;
		}
	}
	return bedValue;
}

// This function removes non-numeric characters 
function stripNonNumeric(str) {
	str += "";
	var validChars = "0123456789";


	var out = "";
	var char = "";
	var i = 0;
	for (i = 0; i < str.length; i++) {
		char = str.charAt(i);
		if (!(validChars.indexOf(char) == -1)) {
			out += str.charAt(i);



		}
	}
	if (out == "") {
		return "";
	} else {
		return out;
	}
}
function getHouseCostPerFoot() {
	var parDoc = parent.document;
	var countyId = parDoc.getElementById("countyId");



	var houseType = (document.all != null ? document.all["houseType"] : document.getElementById("houseType")).value;
	var numBeds = getNumBeds(houseType);
	var area = getCountyArea(countyId);
	var costPerFoot = getCostAtArea(area, houseType, numBeds);
	return costPerFoot;
}

/*********************************************************************
Gets the county name from the county id passed to the rebuild calculator 
*********************************************************************/
function getCountyName(countyId) {


	var countyNames = {28:"Carlow ", 29:"Cavan ", 30:"Clare ", 31:"Co. Dublin ", 32:"Cork City ", 33:"Co. Cork ", 34:"Donegal ", 36:"Dublin 1 ", 37:"Dublin 2 ", 38:"Dublin 3 ", 39:"Dublin 4 ", 40:"Dublin 5 ", 41:"Dublin 6 ", 42:"Dublin 6W ", 43:"Dublin 7 ", 44:"Dublin 8 ", 45:"Dublin 9 ", 46:"Dublin 10 ", 47:"Dublin 11 ", 48:"Dublin 12 ", 49:"Dublin 13 ", 50:"Dublin 14 ", 51:"Dublin 15 ", 52:"Dublin 16 ", 53:"Dublin 17 ", 54:"Dublin 18 ", 55:"Dublin 19 ", 56:"Dublin 20 ", 57:"Dublin 21 ", 58:"Dublin 22 ", 59:"Dublin 23 ", 60:"Dublin 24 ", 61:"Galway ", 62:"Kerry ", 63:"Kildare ", 64:"Kilkenny ", 65:"Laois ", 66:"Leitrim ", 67:"Co. Limerick ", 68:"Limerick City ", 69:"Longford ", 70:"Louth ", 71:"Mayo ", 72:"Meath ", 73:"Monaghan ", 74:"Offaly ", 75:"Roscommon ", 76:"Sligo ", 83:"Co. Tipperary ", 79:"Waterford ", 80:"Westmeath ", 81:"Wexford ", 82:"Wicklow "};

	return countyNames[countyId];
}
/*********************************************************************
Sets up data objects with the cost to rebuild a type of house per
square foot.
*********************************************************************/
function getCostAtArea(area, houseType, numBeds) {	
	var corkArea = {T2:137, T3:129, S3:137, S4:123, D4:125, B4:122};
	var dublinArea = {T2:179, T3:171, S3:177, S4:162, D4:168, B4:159};
	var galwayArea = {T2:139, T3:130, S3:133, S4:123, D4:124, B4:119};
	var waterfordArea = {T2:133, T3:127, S3:135, S4:121, D4:125, B4:116};
	var limerickArea = {T2:139, T3:128, S3:137, S4:119, D4:129, B4:129};
	var pleaseSelectArea = {T2:0, T3:0, S3:0, S4:0, D4:0, B4:0};	
	return (eval(area)[houseType + numBeds]);
}

/*********************************************************************
Divdes each county into a region in order to obtain the cost for
that county.
*********************************************************************/
function getCountyArea(countyId) {
	county = countyId.value;

	var countyAreas = {28:"waterfordArea", 29:"galwayArea", 30:"galwayArea", 31:"dublinArea", 32:"corkArea", 33:"corkArea", 34:"galwayArea", 36:"dublinArea", 37:"dublinArea", 38:"dublinArea", 39:"dublinArea", 40:"dublinArea", 41:"dublinArea", 42:"dublinArea", 43:"dublinArea", 44:"dublinArea", 45:"dublinArea", 46:"dublinArea", 47:"dublinArea", 48:"dublinArea", 49:"dublinArea", 50:"dublinArea", 51:"dublinArea", 52:"dublinArea", 53:"dublinArea", 54:"dublinArea", 55:"dublinArea", 56:"dublinArea", 57:"dublinArea", 58:"dublinArea", 59:"dublinArea", 60:"dublinArea", 61:"galwayArea", 62:"limerickArea", 63:"dublinArea", 64:"waterfordArea", 65:"waterfordArea", 66:"galwayArea", 67:"limerickArea", 68:"limerickArea", 69:"galwayArea", 70:"dublinArea", 71:"galwayArea", 72:"dublinArea", 73:"galwayArea", 74:"galwayArea", 75:"galwayArea", 76:"galwayArea", 83:"limerickArea", 79:"waterfordArea", 80:"galwayArea", 81:"waterfordArea", 82:"dublinArea", 84:"pleaseSelectArea"};
	var area = countyAreas[county];
	return area;
}
/*********************************************************************
This function includes all necessary js files for the application  
*********************************************************************/
function include(file) {
	var script = document.createElement("script");
	script.src = file;
	script.type = "text/javascript";
	script.defer = true;
	document.getElementsByTagName("head").item(0).appendChild(script);
}
function validateMinMaxValue(value) {
	if (value < 1000 || value > 100000) {
		return false;
	} else {
		return true;
	}
}
function bedroomDisplays(option) {
	displaybedrooms = (document.all != null ? document.all["bedroomDisplay"] : document.getElementById("bedroomDisplay"));
	displaybedrooms.style.visibility = option;
}

function fadeOutObject() {
	var objToFadeOut = (document.all != null ? parent.document.all["floatingAssumptionsDiv"] : parent.document.getElementById("floatingAssumptionsDiv"));
	if (objToFadeOut == null) {
		return;
	}
	setOpacity(objToFadeOut, 2);
	objToFadeOut.style.visibility = "visible";
	setTimeout("animateFadeOut(parent.document.getElementById('" + objToFadeOut.id + "'));", 30);
}
/*********************************************************************
This function sets the county name on the rebuild-estimator html page
*********************************************************************/
function setEstimatorCounty(countyId) {

	frames['estimatorIFrame'].location.href='http://www.fbd.ie/insurance/home-insurance/rebuild-estimator/index.htm';
	
	//insurance/home-insurance/rebuild-estimator/index.htm';

}

/*********************************************************************
This function is a quick fix for Issue 1395. This puts focus in the rebuild 
field on closing the estimator which forces the onblur validation to be fired again
*********************************************************************/
function rebuildCostFocus()

{

      var rebuildCost = parent.document.getElementById("rebuildCost");

      rebuildCost.focus();

}

