
function format_num(field){
	var num = field.value;
	num = addCommas(num);
	
	// Only display 2 decimals
	// get index of dot
	var dot = num.lastIndexOf(".");
	// if there was a dot
	if(dot > 0){
		// slice the string, i.e. remove all except dot and two decs
		num = num.slice(0,dot + 4);
	}
	field.value = num;

	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function email_notify(){
	
	(document.join.province.disabled == true) ?  document.join.province.disabled = false : document.join.province.disabled = true;
	(document.join.city.disabled == true) ?  document.join.city.disabled = false : document.join.city.disabled = true;
	(document.join.type.disabled == true) ?  document.join.type.disabled = false : document.join.type.disabled = true;
	(document.join.price_min.disabled == true) ?  document.join.price_min.disabled = false : document.join.price_min.disabled = true;
	(document.join.price_max.disabled == true) ?  document.join.price_max.disabled = false : document.join.price_max.disabled = true;
	(document.join.bedrooms.disabled == true) ?  document.join.bedrooms.disabled = false : document.join.bedrooms.disabled = true;
	(document.join.matchall.disabled == true) ?  document.join.matchall.disabled = false : document.join.matchall.disabled = true;
	(document.join.quicklookup.disabled == true) ?  document.join.quicklookup.disabled = false : document.join.quicklookup.disabled = true;


}

function check_disabled(){
	
		if(document.join.notify.checked == true){
			document.join.province.disabled = false;
			document.join.city.disabled = false;
			document.join.type.disabled = false;
			document.join.price_min.disabled;	
			document.join.price_min.disabled = false;
			document.join.price_max.disabled = false;
			document.join.bedrooms.disabled = false;
			document.join.matchall.disabled = false;
			document.join.quicklookup.disabled = false;
			
		} else {
			document.join.province.disabled = true;
			document.join.city.disabled = true;
			document.join.type.disabled = true;
			document.join.price_min.disabled;	
			document.join.price_min.disabled = true;
			document.join.price_max.disabled = true;
			document.join.bedrooms.disabled = true;
			document.join.matchall.disabled = true;
			document.join.quicklookup.disabled = true;
		}
	
}

function NewWindow(myPage, myName, Width, Height, Scroll, Resizable, Menubar) {
    var winTop = ((screen.height - Height) / 2);
    var winLeft= ((screen.width - Width) / 2);
    winProps = 'top=' +winTop+ ',left=' +winLeft+ ',height=' +Height+ ',width=' +Width+ ',Scrollbars=' +Scroll+ ',menubar=' +Menubar+ ',Resizable=' +Resizable+ ';'
    Win = window.open(myPage, myName, winProps);

        if (parseInt(navigator.appVersion) >= 4) {
            Win.window.focus(); //set focus to the window
        }
}

function calc_price_com(){

	if(
	!ValidNumber(document.theForm.h1.value) ||
	!ValidNumber(document.theForm.h2.value) ||
	!ValidNumber(document.theForm.h3.value) ||
	!ValidNumber(document.theForm.h4.value)){
		
		alert("Please enter valid numbers for the house prices");
		
	} else {
	
	
	var h1 = parseInt(document.theForm.h1.value.replace(/,/g,""));
	var h2 = parseInt(document.theForm.h2.value.replace(/,/g,""));
	var h3 =parseInt(document.theForm.h3.value.replace(/,/g,""));
	var h4 = parseInt(document.theForm.h4.value.replace(/,/g,""));
	
	
	
	var total = 
	parseInt(h1) + parseInt(h2) + 
	parseInt(h3) + parseInt(h4);
	
	var av = parseInt(total) / 4;
	document.theForm.price_commission.value=av;
	format_num(document.theForm.price_commission);
	}
	
}

function calc_com(){
	var check = document.theForm.com_perc.value.replace(/%/g,"");

	if(!ValidNumber(document.theForm.price_commission.value) ||
	!ValidNumber(check)
	){
		
		alert("Please calculate a valid price with commission and enter a valid commission percentage");
		
	} else {
		var com_perc = parseInt(document.theForm.com_perc.value.replace(/%/g,""));
		var price = parseInt(document.theForm.price_commission.value.replace(/,/g,""));
		
		var commission = price * (com_perc/100);
		document.theForm.com.value=commission;
		format_num(document.theForm.com);
		
	}

	
}

function calc_bottomline(){
	
	if(!ValidNumber(document.theForm.com.value)
		||!ValidNumber(document.theForm.price_commission.value) ){
		
		alert("Please calculate a valid price with commission and a valid commission");
		
	} else {
		var price = parseInt(document.theForm.price_commission.value.replace(/,/g,""));
		var com = parseInt(document.theForm.com.value.replace(/,/g,""));
		
		var bottomline = price - com;
		document.theForm.bottomline.value=bottomline;
		format_num(document.theForm.bottomline);
		
	}

	
}

function ValidNumber(input)
{
	// check for a length greater than 0
	if (input.length < 1)
		return false;

	// check every position and validate it against the allowed characters
	sCheckStr = "0123456789.,";	// allowed characters
	iCnt=0;						// counter for the number of "." characters
	for(iPos=0; iPos < input.length; iPos++)
	{
		// check that the current position in the string is a valid character
		if (sCheckStr.indexOf(input.charAt(iPos)) < 0)
			return false;
		else
		{
			// ensure not more than 1 "." character
			if (input.charAt(iPos) == ".")
			{
				iCnt++;
				if (iCnt>1)
					return false;
			}
		}
	}

	// the number is valid ... return true
	return true;
}

function bookmark(url, description)
{
netscape="Netscape or Firefox Users hit CTRL+D to add a bookmark to this site."
if (navigator.appName=='Microsoft Internet Explorer')
{
window.external.AddFavorite(url, description);
}
else if (navigator.appName=='Netscape')
{
alert(netscape);
}
}

function currency(ZAR){
	
	// remove any commas
	ZAR = ZAR.replace(/,/g,"");
	NewWindow('convert-currency.php?ZAR='+ZAR,'Currency',400,210,'yes','yes','yes');	
}

function select_loading(dropdown){
	dropdown.options.length = 0
	dropdown.disabled = true;
	dropdown.options[dropdown.options.length] = new Option("Loading...","");
	
}

function select_all() {
	
	if (document.theform.all.checked) {
		//check and disable all individual
		for (var i=0; i < document.theform.elements.length; i++) {
			
			if ((document.theform.elements[i].type == "checkbox") && (document.theform.elements[i].name != "all")
			&& (document.theform.elements[i].disabled == false)			
			) {
				document.theform.elements[i].checked = true;
				//document.theform.elements[i].disabled = true;
			}	
		}
		
	} else {
		for (var i=0; i < document.theform.elements.length; i++) {
			if ((document.theform.elements[i].type == "checkbox") && (document.theform.elements[i].name != "all")
			&& (document.theform.elements[i].disabled == false)
			) {
				document.theform.elements[i].checked = false;
				//document.theform.elements[i].disabled = false;
			}	
		}
	}
}