$(document).ready(function() {

	// Edit single line text input
     $('.single.editable').editable('/ajax/updateprofile/', { 
	data: function(value) {
		return striphtml(value);
	},
	type      : 'text',
	cancel    : 'Cancel',
	indicator: 'Saving...',
	submit    : 'Save',
	cssclass  : 'editing',
	onblur	  : 'submit',
	style: 'inherit',
	tooltip   : 'Click To Edit'
     });


	// Edit multiple line text input
     $('.multi.editable').editable('/ajax/updateprofile/', { 
	data: function(value) {
		return striphtml(value);
	},
	type      : 'textarea',
	cancel    : 'Cancel',
	indicator: 'Saving...',
	rows:6,
	cols:60,
	cssclass  : 'editing',
	onblur	  : 'submit',
	submit    : 'Save',
	tooltip   : 'Click To Edit'
     });

	// Sortable fields
	$("#sortablelist").sortable({ 
		update : function () { 
			var order = $('#sortablelist').sortable('serialize'); 
 			$("#infowindow").load("/ajax/sortprofile/?"+order); 
    		} 
  	}); 

 });


function striphtml(html) {
	// Convert <br> to newline and strip HTML
	var retval = html.replace(/<br[\s\/]?>/gi, '\n');
	retval = retval.replace(/(<([^>]+)>)/ig,""); 
	return retval;
}

function loadPics(username) {
	$("#pics").load("/ajax/loadpics/", { user : username });
}
function loadTweets(username) {
	$("#tweets").load("/ajax/loadtweets/", { user : username });
}
function loadRSS(username) {
	$("#rss").load("/ajax/loadrss/", { user : username });
}
function saveSelect(field) {
	//var field = '\'' + field + '\'';
	var retval = document.getElementById(field).value;
	$("#infowindow").load("/ajax/saveselect/", { f : field , v : retval });
}

function saveDob(y,m,d) {
	document.getElementById('dob').value = y + '-' + m + '-' + d;
	$("#infowindow").load("/ajax/savedob/", { y : y , m : m , d : d });
	return true;
}
function showCalendar(defaultdate,divid) {
	var cal = new CalendarPopup(divid);
	cal.showNavigationDropdowns();
	cal.showYearNavigation();
}

////////////   Forms functions

function empty(obj,message){
    if(obj.value==''){
        alert(message);
        return false;
    }
    return true;
}

function int(obj,message){
    if(parseInt(obj.value)!=obj.value){
        alert(message);
        return false;
    }
    return true;
}
function email(obj,message){
    if(!/.+@.+\..+./.test(obj.value)){
        alert(message);
        return false;
    }
    return true;
}
//if(document.getElementById&&document.createElement&&document.getElementsByTagName){
//    document.getElementsByTagName('form')[0].onsubmit=function()
//{return validate()};
//}

