// open visual info
function openReplace(sUrl) {

	var url = sUrl;
	winReplace = window.open(url, "replace","scrollbars=yes, status=yes, menubar=no, width=540, height=350, resizable=yes");
	winReplace.focus();
}
function openMessage(sUrl) {
	var url = sUrl;
	msWin = window.open(url, "ms","scrollbars=no, status=yes, menubar=no, width=450, height=400, resizable=yes");
	msWin.focus();
}

//pass a form name an array of itmes and an array of descriptive names - for the alert messages.
function checkFormItems(form, items, messages) {
	for (var i=0; i<items.length; i++) {				
		formItem = eval(form + "." + items[i]);
		formItemValue = eval(form + "." + items[i] + ".value");
		if (formItemValue == "" || formItemValue == "any") {
			alert("Please fill in the field: " + "\"" + messages[i] + "\"");
			formItem.focus();
			return false;
		}
	}
	return true;
}

function checkIsNumber(form, items, messages) {
	for (var i=0; i<items.length; i++) {				
		formItem = eval(form + "." + items[i]);
		formItemValue = eval(form + "." + items[i] + ".value");
		if (formItemValue != "") {			
			if (isNaN(formItemValue)) {
				alert("The field: " + "\"" + messages[i] + "\"" + "is not a valid number.");
				formItem.focus();
				return false;
			}
		}
	}
	return true;
}

// indicates how many characters have been entered in a textfield.
function numCharCount(form, field, length_limit) {	
	var length = calculate_msglen(eval("document" + "." + form + "." + field + ".value"));
	textlimit.innerText = length;
	if (length > length_limit) {
		alert("Warning: " + length_limit + " is the max number of characters you can enter for this field. \n Please cut down your text.");
	}
}	

function calculate_msglen(message)
{
	var nbytes = 0;	
	for (i=0; i<message.length; i++) {		
		var ch = message.charAt(i);
		if(escape(ch).length > 4) {
			nbytes += 2;
		} else if (ch == '\n') {
			if (message.charAt(i-1) != '\r') {
				nbytes += 1;
			}
		} else if (ch == '<' || ch == '>') {
			nbytes += 4;
		} else {
			nbytes += 1;
		}
	}
	return nbytes;
}

// used in propertyAdd.asp propertyEdit
function chkPropertyFormItems(status) {
	if (status == "For Rent") {
		var arrayFormItem = new Array('cboAgent1', 'cboType', 'txtRoadNum', 'txtRoadName', 'cboSuburb', 'cboPriceOption', 'cboPaymentPeriod', 'cboLeaseTerm', 'cboNumBed', 'cboNumBath', 'txtDescriptionShort', 'txtDescriptionLong')
		var arrayFormItemDescription = new Array('Listing Agent 1', 'Property Type', 'Rd Num', 'Rd Name', 'Suburb', 'Price Option', 'Payment Period', 'Lease Term', 'Bedrooms', 'Bathrooms', 'Short Description', 'Long Description')
		var	arrayCurrencyFormItem = new Array('txtPrice', 'txtPriceLow', 'txtPriceHigh',  'txtBond', 'txtInternal', 'txtExternal');
		var	arrayCurrencyFormItemDescription = new Array('Price', 'Price Low', 'Price High', 'Bond','Internal Sqm', 'External Sqm');
	}
	else {
		var arrayFormItem = new Array('cboAgent1', 'cboType', 'txtRoadNum', 'txtRoadName', 'cboSuburb', 'cboPriceOption', 'cboNumBed', 'cboNumBath', 'txtDescriptionShort', 'txtDescriptionLong')
		var arrayFormItemDescription = new Array('Listing Agent 1', 'Property Type', 'Rd Num', 'Rd Name', 'Suburb', 'Price Option', 'Bedrooms', 'Bathrooms', 'Short Description', 'Long Description')
		var	arrayCurrencyFormItem = new Array('txtPrice', 'txtPriceLow', 'txtPriceHigh', 'txtRatesCouncil', 'txtRatesWater', 'txtRatesStrata', 'txtLotsize', 'txtInternal', 'txtExternal', 'txtOutgoings');
		var	arrayCurrencyFormItemDescription = new Array('Price', 'Price Low', 'Price High', 'Council Rates', 'Water Rates', 'Strata Rates', 'Lot Size', 'Internal Sqm', 'External Sqm', 'Outgoings');
	}
	
	if (checkFormItems('frmProperty', arrayFormItem, arrayFormItemDescription)) {
		var strPriceOption = frmProperty.cboPriceOption.value;
		switch (strPriceOption) {
			case "Range":
				frmProperty.txtPrice.value = "";
				if (frmProperty.txtPriceLow.value == "") {
					alert("Please fill in the field: " + "\"From\"");
					frmProperty.txtPriceLow.focus();
					return false;
				}
				if (frmProperty.txtPriceHigh.value == "") {
					alert("Please fill in the field: " + "\"To\"");
					frmProperty.txtPriceHigh.focus();
					return false;				
				}
				break;
			case "Price":
				frmProperty.txtPriceLow.value = "";
				frmProperty.txtPriceHigh.value = "";
				if (frmProperty.txtPrice.value == "") {
					alert("Please fill in the field: " + "\"Price\"");
					frmProperty.txtPrice.focus();
					return false;
				}
				break;	
			case "PriceFrom":
				frmProperty.txtPriceLow.value = "";
				frmProperty.txtPriceHigh.value = "";
				if (frmProperty.txtPrice.value == "") {
					alert("Please fill in the field: " + "\"Price\"");
					frmProperty.txtPrice.focus();
					return false;
				}
				break;	
			case "EOI":
				frmProperty.txtPriceLow.value = "";
				frmProperty.txtPriceHigh.value = "";
				if (frmProperty.txtPrice.value == "") {
					alert("Please fill in the field: " + "\"Price\"");
					frmProperty.txtPrice.focus();
					return false;
				}
				break;
		}
		return (checkIsNumber('frmProperty', arrayCurrencyFormItem, arrayCurrencyFormItemDescription))
	}
	else
		return false;

	return true;
}
