//set options
$.ajaxSetup({
    error:function(x,e){
	if(x.status==0){
	    alert('You are receiving this error because there was a problem with your web browser connecting to the application.\nPlease check your internet connection, then close and re-open your browser and try loading the site again.');
	}else if(x.status==404){
	    alert('Oops..\n The page wasn\'t found.\n Please let us know about this error.');
	}else if(x.status==500){
	    alert('Oops..\n Internal server error.\n Please let us know about this.');
	}else if(x.status==503){
	    alert('We\'re sorry, the server is too busy right now.\n Please wait a few minutes and try again.');
	}else if(x.status==401){
	    alert('Access denied. Please logout and log in again.');
	}else if(e=='parsererror'){
	    alert('Oops..\n Parsing JSON request failed.\n Please let us know about this error.');
	}else if(e=='timeout'){
	    alert('You are receiving this error because there was a problem (timeout) with your web browser connecting to the application.\nPlease check your internet connection, then close and re-open your browser and try loading the site again.');
	}else {
	    alert('Unknown error:\n'+x.responseText+'\n Please try again later.');
	}
    },
    success:function(){
	$("#siteLoading").hide('slow');
    },
    complete:function(){
	$("#siteLoading").hide('slow');
    },
    beforeSend:function(){
	$("#siteLoading").show('fast');
    }
});

function trim(string) {
    string = string.replace(/^\s+/, '');
    for (var i = string.length; i > 0; i--) {
	if (/\S/.test(string.charAt(i))) {
	    string = string.substring(0, i);
	    break;
	}
    }
    return string;
}

function getElementLeft(Elem) {
    var elem = Elem;
    xPos = elem.offsetLeft;
    tempEl = elem.offsetParent;
    while (tempEl != null) {
	xPos += tempEl.offsetLeft;
	tempEl = tempEl.offsetParent;
    }
 		
    return xPos;
}


function getElementTop(Elem) {
    var elem = Elem;
    yPos = elem.offsetTop;
    tempEl = elem.offsetParent;
    while (tempEl != null) {
	yPos += tempEl.offsetTop;
	tempEl = tempEl.offsetParent;
    }
	
    return yPos;
}

$(document).ready(function(){
    $.fn.clearForm = function() {
	return this.each(function() {
	    var type = this.type, tag = this.tagName.toLowerCase();
	    if (tag == 'form')
		return $(':input',this).clearForm();
	    if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
	    else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
	    else if (tag == 'select')
		this.selectedIndex = -1;
	});
    };
});
$(document).ready(function(){
 
    //start account settings
    jQuery(".accountSettings").click( showADialog );
    //variable to reference account settings window
    $myAWindow = jQuery('#dialogA');
    //instantiate the dialog
    $myAWindow.dialog({
	height: 400,
	width: 600,
	modal: true,
	position: 'center',
	autoOpen:false,
	overlay: {
	    opacity: 0.5,
	    background: 'black'
	}
    });
    //end account settings
	
    //start global settings
    jQuery(".globalSettings").click( showGDialog );
    //variable to reference global settings window
    $myGWindow = jQuery('#dialogG');
    //instantiate the dialog
    $myGWindow.dialog({
	height: 614,
	width: 501,
	modal: true,
	position: 'center',
	autoOpen:false,
	overlay: {
	    opacity: 0.5,
	    background: 'black'
	}
    });
    //end global settings
	
    //start new appointment
    jQuery(".newApp").click( showNewAppDialog );
    //variable to reference new appointment window
    $myNewAppWindow = jQuery('#dialogNewApp');
    //instantiate the dialog
    $myNewAppWindow.dialog({
	height: 580,
	width: 470,
	modal: true,
	position: 'center',
	autoOpen:false,
	overlay: {
	    opacity: 0.5,
	    background: 'black'
	}
    });
//end new appointment
	
});

//function to show account settings dialog   
var showADialog = function() {

    //if the contents have been hidden with css, you need this
    $myAWindow.show();
    //open the dialog
    $myAWindow.dialog("open");

    var url = "controller.php?module=account_settings&action=edit";
	
    $.ajax({
	url: url,
	cache: false,
	success: function(html){
	    $("#dialogA").html(html);
	    $("#account_settings").validate({
		rules: {
		    first_name: "required",
		    last_name: "required",
		    username: "email",
		    userpass: {
			required: true,
			minlength: 6
		    },
		    userpasscheck: {
			equalTo: "#userpass",
			required: true,
			minlength: 6
		    }
		}
	    });
	}
    });
	
};

//function to close account settings dialog, probably called by a button in the dialog
var closeADialog = function() {

    $myAWindow.dialog("close");
	
};

//function to show global settings dialog   
var showGDialog = function() {

    //if the contents have been hidden with css, you need this
    $myGWindow.show();
    //open the dialog
    $myGWindow.dialog("open");

    var url = "controller.php?module=global_settings&action=edit";
	
    $.ajax({
	url: url,
	cache: false,
	success: function(html){
	    $("#dialogG").html(html);
	    $("#global_settings").validate({
		rules: {
		    avoid_admin_app_within_h: {
			required: true,
			minlength: 1,
			range: [0, 365],
			digits: true
		    },
		    avoid_admin_app_within_d: {
			required: true,
			minlength: 1,
			range: [-365, 365]
		    },
		    avoid_app_within: {
			required: true,
			minlength: 1,
			range: [0, 365],
			digits: true
		    },
		    sms_cap: {
			required: true,
			minlength: 1,
			range: [0, 99999],
			digits: true
		    },
		    pre_app_mail_value: {
			required: true,
			minlength: 1,
			range: [0, 365],
			digits: true
		    },
		    pre_app_sms_value: {
			required: true,
			minlength: 1,
			range: [0, 365],
			digits: true
		    },
		    full_image_width: {
			required: true,
			minlength: 1,
			range: [0, 999],
			digits: true
		    },
		    full_image_height: {
			required: true,
			minlength: 1,
			range: [0, 999],
			digits: true
		    },
		    thumb_image_width: {
			required: true,
			minlength: 1,
			range: [0, 999],
			digits: true
		    },
		    thumb_image_height: {
			required: true,
			minlength: 1,
			range: [0, 999],
			digits: true
		    }
		}
	    });
	}
    });
		
};

//function to close global settings dialog, probably called by a button in the dialog
var closeGDialog = function() {

    $myGWindow.dialog("close");
	
};

//function to show new appointment dialog   
var showNewAppDialog = function(patientId) {

    //if the contents have been hidden with css, you need this
    $myNewAppWindow.show();
    //open the dialog
    $myNewAppWindow.dialog("open");
	
    if(patientId == undefined) patientId = "";

    var url = "controller.php?module=newappointment&patient_id="+patientId;
	
    $.ajax({
	url: url,
	cache: false,
	success: function(html){
	    $("#dialogNewApp").html(html);
		    
	}
    });
		
};

//function to close new appointment dialog, probably called by a button in the dialog
var closeNewAppDialog = function() {

    $myNewAppWindow.dialog("close");
	
};