// ***************************** PHP -> JS functions ***************************** */

function number_format( number, decimals, dec_point, thousands_sep ) {
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57     
 
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? '.' : dec_point;
    var t = thousands_sep == undefined ? ',' : thousands_sep, s = n < 0 ? '-' : '';
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + '', j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
}

function rtrim ( str, charlist ) {
    // *     example 1: rtrim('    Kevin van Zonneveld    ');
    // *     returns 1: '    Kevin van Zonneveld'
 
    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    var re = new RegExp('[' + charlist + ']+$', 'g');
    return str.replace(re, '');
}

function in_array(needle, haystack, strict) {
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
 
    var found = false, key, strict = !!strict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}

// ***************************** NEW mysite jQuery functions ***************************** */

function error_WAP_property(section)
{
	if(section=="save") alert( "There was an error, property has not been not saved." );
	if(section=="delete") alert( "There was an error, property has not been not deleted." );
}

function save_WAP_property(enc_data, section)
{
	$.ajax({
		type: "GET",
		url: site_url+"/ajax/WAP_property.php",
		data: "action=save&enc_data="+enc_data,
		dataType: 'text',
		success: function(res){
			//alert( "Data Saved: " + res );
			if(!res) error_WAP_property('save');
			
			if(section=="search") $('#plist_'+res).html("<img src='"+site_url+"/images/buttons/btn_psaved_list.gif' alt='property saved'/>");
			
			if(section=="pdetails") $('#plist').html("<img src='"+site_url+"/images/buttons/btn_psaved_pdetails.gif' alt='property saved'/>");
			
			if(section=="fproperty") $('#plist_'+res).html("Property added to Favourites");
			
			if(section=="map") $('#plist').html("<img src='"+site_url+"/images/save_icon.png' alt='PROPERTY SAVED'/>SAVED");
			
		},
		error: function(){
			error_WAP_property('save');
		}
	});	
}

function delete_WAP_property(enc_data)
{
	$.ajax({
		type: "GET",
		url: site_url+"/ajax/WAP_property.php",
		data: "action=delete&enc_data="+enc_data,
		dataType: 'text',
		success: function(res){
			//alert( "Data Saved: " + res );
			if(!res) error_WAP_property('delete');
			
			$('#plist_'+res).empty();
			
			var count = $('#props_count').html();
			count--;
			$('#props_count').text(count);

			var props_suff = (count!=1 ? "ies" : "y");
			$('#props_suffix').html(props_suff);
		},
		error: function(){
			error_WAP_property('delete');
		}
	 });
}

function add_focus(id){
	document.getElementById( id ).firstChild.focus();
}

function swap_input(id)
{
	//document.getElementById("password_input").firstChild.blur();
	document.getElementById("password_input").innerHTML = "<input type=\"password\" name=\"Password\" id=\""+id+"\" value=\"\" />";
	setTimeout( "document.getElementById(\""+id+"\").focus()",30);
	//console.log( id );
	return;
}