function showMe(id, id2, id3)
{
	if (document.getElementById(id).style.display == "block")
	{
		document.getElementById(id).style.display = "none";
	}
	else
	{
		document.getElementById(id).style.display = "block";
		document.getElementById(id2).style.display = "none";
		document.getElementById(id3).style.display = "none";
	}
}

function processform(errordiv)
{
	document.friendform.submitbutton.disabled = true;

	var ajaxRequest;
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				document.friendform.submitbutton.disabled = false;
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		    var temp = new Array();
            temp = ajaxRequest.responseText.split(':::');
			if (temp[0] == "0")
			{
				document.getElementById(errordiv).innerHTML = "<span style=\"color:red;font-weight:bold;\">The following error(s) occurred:<br>" + temp[1] + "</span>";
				document.friendform.submitbutton.disabled = false;
			}
			else
			{
				document.friendform.submitbutton.disabled = false;
				document.friendform.yourname.value = "";
				document.friendform.friendname.value = "";
				document.friendform.email.value = "";
				document.friendform.security_code.value = "";
				document.getElementById(errordiv).innerHTML = "<span style=\"color:#003366;font-weight:bold;\">Your email was sent successfully.</span>";
			}
		}
	}
	ajaxRequest.open("GET", "../homepages/contactprocess.php?action=process&yourname="+document.friendform.yourname.value+"&friendname="+document.friendform.friendname.value+"&email="+document.friendform.email.value+"&property="+document.friendform.property.value+"&propertyurl="+document.friendform.propertyurl.value+"&body=This property was sent to you by "+document.friendform.yourname.value+"&security_code="+document.friendform.security_code.value, true);
	ajaxRequest.send(null); 
}

function processcontactusform(errordiv)
{
	document.contactusform.submitbutton.disabled = true;

	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				document.contactusform.submitbutton.disabled = false;
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		    var temp = new Array();
            temp = ajaxRequest.responseText.split(':::');
			if (temp[0] == "0")
			{
				document.getElementById(errordiv).innerHTML = "<span style=\"color:red;font-weight:bold;\">The following error(s) occurred:<br>" + temp[1] + "</span>";
				document.contactusform.submitbutton.disabled = false;
			}
			else
			{
				document.contactusform.submitbutton.disabled = false;
				document.contactusform.name.value = "";
				document.contactusform.email.value = "";
				document.contactusform.phone.value = "";
				document.contactusform.security_code.value = "";
				document.getElementById(errordiv).innerHTML = "<span style=\"color:#003366;font-weight:bold;\">Your email was sent successfully.</span>";
			}
		}
	}
	ajaxRequest.open("GET", "../homepages/contactusprocess.php?action=process&name="+document.contactusform.name.value+"&email="+document.contactusform.email.value+"&phone="+document.contactusform.phone.value+"&property="+document.contactusform.property.value+"&propertyurl="+document.contactusform.propertyurl.value+"&body="+document.contactusform.body.value+"&security_code="+document.contactusform.security_code.value, true);
	ajaxRequest.send(null); 
}

function getLargeImage(mypath, myfilename)
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		    var temp = new Array();
            //document.write(ajaxRequest.responseText);
			document.getElementById('largeimage').src = "../" + ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "/includes/getlargeimage.php?filename="+myfilename);
	ajaxRequest.send(null); 
}


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 stringFilter(input)
{
	filteredValues = "1234567890";     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < input.length; i++) {  // Search through string and append to unfiltered values to returnString.
		var c = input.charAt(i);
		if (filteredValues.indexOf(c) >= 0) 
			returnString += c;
	}
	return returnString;
}

function calResStampDuty(){
	var arr_cat1 = new Array();
	arr_cat1 = [0,0,0,0];
	var arr_cat2 = new Array();
	arr_cat2 = [0,0,7,7];
	var arr_cat3 = new Array();
	arr_cat3 = [0,0,9,9];

	//var num_housePrice = parseInt(window.document.calculator1.num_housePrice.value);
	var num_housePrice = stringFilter(window.document.calculator1.num_housePrice.value);
	if(num_housePrice==""){
		document.getElementById('errormsgstamp').innerHTML = "Please enter a house price value";
	}
	else if(num_housePrice.toString()=="NaN")
	{
		document.getElementById('errormsgstamp').innerHTML = "Please enter a numeric house price value";
	}
	else{
		var num_buyerType = window.document.calculator1.num_buyerType.selectedIndex;
		var num_percentage;
		var num_stampDuty;

		if(num_housePrice == ""){
			document.getElementById('errormsgstamp').innerHTML = "Please enter a house price";
			return false;
		}
		
		var num_stampDuty = 0;
		if(num_housePrice <= 125000){
			num_percentage = arr_cat1[num_buyerType];
			num_stampDuty = ((num_housePrice / 100) * num_percentage);
		}
		else if ((num_housePrice >= 125001)&&(num_housePrice <= 1000000)){
			num_percentage = arr_cat2[num_buyerType];
			num_stampDuty = ((num_housePrice - 125000) * (num_percentage /100));
		}
		else if (num_housePrice > 1000000){
			num_percentage1 = arr_cat2[num_buyerType];
			num_stampDuty_rate1 = (875000 * (num_percentage1  / 100));
			num_percentage2 = arr_cat3[num_buyerType];
			num_stampDuty_rate2 = ((num_housePrice - 1000000) * (num_percentage2  / 100));
			num_stampDuty = num_stampDuty_rate1 + num_stampDuty_rate2;
		}
		document.getElementById('stampDutyLabel').innerHTML = "Stamp Duty :";
		document.getElementById('stampDuty').value = "€" + addCommas(Math.round(num_stampDuty)) + "";
	}
}

function calStampDuty(){
	var num_housePrice = stringFilter(window.document.calculator1.num_housePrice.value);
	if(num_housePrice==""){
		document.getElementById('errormsgstamp').innerHTML = "Please enter a property price value";
	}
	else if(num_housePrice.toString()=="NaN")
	{
		document.getElementById('errormsgstamp').innerHTML = "Please enter a numeric property price value";
	}
	else{
		var num_stampDuty;

		if(num_housePrice == ""){
			document.getElementById('errormsgstamp').innerHTML = "Please enter a property price";
			return false;
		}
		
		var num_stampDuty = 0;
		if(num_housePrice <= 10000)
			num_stampDuty = 0;
		else if ((num_housePrice >= 10001)&&(num_housePrice <= 20000))
			num_stampDuty = num_housePrice * 0.01;
		else if ((num_housePrice >= 20001)&&(num_housePrice <= 30000))
			num_stampDuty = num_housePrice * 0.02;
		else if ((num_housePrice >= 30001)&&(num_housePrice <= 40000))
			num_stampDuty = num_housePrice * 0.03;
		else if ((num_housePrice >= 40001)&&(num_housePrice <= 70000))
			num_stampDuty = num_housePrice * 0.04;
		else if ((num_housePrice >= 70001)&&(num_housePrice <= 80000))
			num_stampDuty = num_housePrice * 0.05;
		else if ((num_housePrice >= 80001)&&(num_housePrice <= 100000))
			num_stampDuty = num_housePrice * 0.06;
		else if ((num_housePrice >= 100001)&&(num_housePrice <= 120000))
			num_stampDuty = num_housePrice * 0.07;
		else if ((num_housePrice >= 120001)&&(num_housePrice <= 150000))
			num_stampDuty = num_housePrice * 0.08;
		else
			num_stampDuty = num_housePrice * 0.09;

		document.getElementById('stampDutyLabel').innerHTML = "Stamp Duty :";
		document.getElementById('stampDuty').value = "€" + addCommas(Math.round(num_stampDuty)) + "";
	}
}

function clearResult(){
	document.getElementById('stampDuty').value = "";
}
