/**
*
*
*/
function calculate(q, a, c) {
	var question = document.getElementById(q).value;
	var answer = document.getElementById(a);
	var returnVal = 0;
	if (!isNaN(question) && question != "")
	{
		returnVal = eval(question + c);
		answer.value = returnVal.toFixed(2);
	}
	else
	{
		alert(msg.number_only);
	}
}
/**
* make property image thumbnails change large image
* on property detail page
* @return void;
*/
var imageSwap = {
    largeImage: false,
    thumbs: false,
    init: function()
    {
        imageSwap.largeImage = $('#largeimage');
        imageSwap.thumbs     = $('#thumbs a');
        imageSwap.setThumbs();
    },
    setThumbs: function()
    {
        imageSwap.thumbs.click(function()
        {
            imageSwap.largeImage.attr('src', this.href);
            return false;
        });
    }
}

/**
* Assign function to body onload;
*
*/
$(document).ready(function()
{
    imageSwap.init();
    featured.init();
});

function changeLanguage(lang)
{
	$('#langVal').val(lang);
	document.formLang.submit();
}

function doQuickSearch(basicUrl, keywords)
{

	if(keywords != '')
	{
		document.location.href = basicUrl + keywords + '/';
	}else
	{
		alert(msg.empty_keywords);
	}
	return false;
}

function changeLargeImage(elem)
{
	document.getElementById('largeimage').src = elem.href;
}

function sendToFriend()
{
	var email = $('#send_to_email');
	var name = $('#send_to_name');
	var err = '';
	if($(name).val() == '')
	{
		err += msg.empty_name + '\n\r';
	}
	
	if($(email).val() == '')
	{
		err += msg.invalid_firend_email + '\n\r';
	}
	
	if(err != '')
	{
		alert(err);
	}else
	{
		
		$('#ajaxImage').show();
		$.getJSON(url.send_to_frield, {'name':$(name).val(), 'email':$(email).val(), 'property':$('#send_to_property').val()}, 
		
			function(json)
			{
				if(typeof(json.error) == 'undefined')
				{
					alert('unexpected error.');
				}else if(json.error != '')
				{
					alert(json.error);
				}else
				{
					alert(json.msg);
				}
				
				$('#ajaxImage').hide();
				$('#dialog').jqmHide();
				
			}
		)
	}
	
	return false;
}

function turnRegionOn(id)
{
	if(document.getElementById(id).style.display == 'none')
	{
		document.getElementById(id).style.display = '';
	}else
	{
		document.getElementById(id).style.display = 'none';
	}
}

function changeContactCountry(elem)
{
	var countryId = elem.options[elem.selectedIndex].value;
	var offcies = (typeof(countryOffices[countryId])?countryOffices[countryId]:[]);
	//remove all 
	var officeElem = document.getElementById('officeName');

	var leng = officeElem.options.length;

	for(var i = (leng - 1); i >0 ; i-- )
	{
		officeElem.options[i] = null;			
	}
	for(var j in offcies)
	{
		officeElem.options[officeElem.options.length] = new Option( (offcies[j].name?offcies[j].name:offcies[j].name_en), offcies[j].email);	
	}
	
}