// JavaScript Document

$(document).ready(function() {
	//$(".help").tipTip();
	
	//Set up breadcrumbs	
	$(".breadcrumb").html($(".asa\\.page\\.breadcrumb").html());
	
	//Set up page title and icons
	if ($(".pageiconurl").length) {
		$("#pageTitle").addClass($(".pageiconurl").html()).html($(".asa\\.page\\.title").html());
	}
	
	$( "#tabs" ).tabs({
   		select: function(event, ui) {
   		getDefaultTabContentView(ui.tab.id);
 	}
	});
	
	// creates physician tab containers with jqueryUIok
	$( "#physicianTabs" ).tabs({
		select: function(event, ui) {
		getPhysicianWelcomeContentView(ui.tab.id);
	}
	}); 

	// creates patient tab containers with jqueryUIok
	$( "#patientTabs" ).tabs({
   		select: function(event, ui) {
   	    getPatientWelcomeContentView(ui.tab.id);
 	}
	});

	// validate create Account
	//$("#createAccount").validate({
	//	rules: {
	//		firstName: "required",
	//		lastName: "required",
	//		DOB: "required",
	//		preferedName: "required",
	//		zipcode: "required",
	//	},
	//	messages: {
	//		
	//	}
	//});
	
	// select all desired input fields and attach tooltips to them
	//$("input.message").tooltip({
	// place tooltip on the right edge
	//	position: "center right",
	// a little tweaking of the position
	//	offset: [-2, 30],
	// use the built-in fadeIn/fadeOut effect
	//	effect: "fade",
	// custom opacity setting
	//	opacity: 0.9
	//});
	
	//hides and shows Status bar
	$(".statusBody").hide();
		$('.status').click(function() {
			var st = $(this).attr("class");
			if (st == "status open"){
			$(this).removeClass("open");
			$(this).next(".statusBody").slideToggle(300);
			
			} else {
			$(this).addClass("open");
			$(this).next(".statusBody").slideToggle(300);	
			}
		});
	
		
	// validate signup form on keyup and submit
	//$("#changePassword").validate({
	//	rules: {
	//		challenge: "required",
	//		newPassword: {
	//			required: true,
	//			minlength: 5
	//		},
	//		confirmPassword: {
	//			required: true,
	//			minlength: 5,
	//			equalTo: "#newPassword"
	//		},
	//	},
	//	messages: {
	//		challenge: "This field must be completed.",
	//		newPassword: {
	//			required: "Please provide a password",
	//			minlength: "Your password must be at least 5 characters long"
	//		},
	//		confirmPassword: {
	//			required: "Please provide a password",
	//			minlength: "Your password must be at least 5 characters long",
	//			equalTo: "Please enter the same password as above"
	//		},
	//	}
	//});
	
});

function getProfileView(view) {

	url = $("#profile").attr("url");

		$.ajax({
		url: url,		
		data: "view=" + view,
		success: function(data) {
			$(".profileSettings").html(data);
		}
	});
}

function getProfileCompleteness() {
	url = $("#profile").attr("url");

		$.ajax({
		url: url,		
		data: "view=ProfileCompletenessView",
		success: function(data) {
			$(".profileComplete").html(data);
		}
	});
}

function profilePost(data, success_view) { 
url = $("#profile").attr("url");

	$.ajax({
		url: url,		
		data: data + "&view=" + success_view,
		success: function(data) {
			$(".profileSettings").html(data);
			getProfileCompleteness();
		}
	});
}

function showProfile(){
    $("#profile").dialog({
	  	width: 975,
	  	height: 800,
		modal: true,
	    resizable: false
	  });
	  $(".ui-dialog-titlebar").hide();
	  getProfileCompleteness();
	  getProfileView("DefaultProfileModalView") //Default view
    
}

function showDialog(title,dclass,height,width, scroll){
	$("." + dclass).css("overflow", scroll);
	$("." + dclass).dialog({
	width: width,
	height: height,
	title: title,
	modal: "true"
	});
} 

$(function() {
		$( "#datepicker" ).datepicker({
			showOn: "both",
			buttonImage: "img/theme/jqueryUI/datePickerIcon.png",
			buttonImageOnly: true,
			yearRange: '1935:2015',
			changeMonth: true,
			changeYear: true
		});
	});

$(function() {
		$( "#accordion" ).accordion();
	});
	
//fadeIn function 
function fadeInContent(element) {
	$(element).fadeIn(500);
}

//load external content into profileBody section
function getContent(newURL) {
	$('#profileBody').fadeToggle(500, function() {			
		$('#profileBody').load(newURL, function() {
		  fadeInContent('#profileBody');
		});
	});
};

//END OF DOCUMENT.READY

//This function is used to call the photo upload servlet from people and permissions
function callFileUploadServlet(portletdata, success_view) {
	var servletLink = document.getElementById("servletLink").value;

	$("#accountSettings").ajaxSubmit({
		url : servletLink,
		cache : false,
		success : function(data) {
			profilePost(portletdata, success_view);
		}
	});
}


/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

//Dropdown DOB - BEGIN

function populatedropdown(savedDay, savedMonth, savedYear){
	var today=new Date();
	var dayfield=document.getElementById("daydropdown");
	var monthfield=document.getElementById("monthdropdown");
	var yearfield=document.getElementById("yeardropdown");

	var monthcaption=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

	for (var m=0; m<12; m++){
		monthfield.options[m]=new Option(monthcaption[m],m);
	}
	$("#monthdropdown option:eq(0)").attr("selected", "selected");
	if (savedMonth!=null){
		 var selectedMonthJS= savedMonth;
		 selectedMonthJS--;
	     $("#monthdropdown").val(selectedMonthJS).attr("selected", "selected");
	}
	for (var i=0; i<31; i++){
		dayfield.options[i]=new Option(i+1, i);
	}
	$("#daydropdown option:eq(0)").attr("selected", "selected");
	if (savedDay!=null){
		 var selectedDayJS= savedDay;
		 selectedDayJS--;
	     $("#daydropdown").val(selectedDayJS).attr("selected", "selected");
	}
	var thisyear=today.getFullYear();
	for (var y=0; y<76; y++){
		yearfield.options[y]=new Option(thisyear, thisyear);
		thisyear-=1;
	}
	yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true); //select today's year
	if (savedYear!=null){
	     var selectedYearJS= savedYear;
	     $("#yeardropdown").val(selectedYearJS).attr("selected", "selected");
	}	
}	



function updateDropdown(){
	
	selectedDay =$("#daydropdown option").index($("#daydropdown option:selected"));
	selectedMonth =$("#monthdropdown option").index($("#monthdropdown option:selected"));
	selectedYear =$('#yeardropdown').val();
    
	var maxDay="31";
	switch(selectedMonth){
		case 0:
		maxDay="31";
		break;
		case 1:
		maxDay="28";
		break;
		case 2:
		maxDay="31";
		break;
		case 3:
		maxDay="30";
		break;
		case 4:
		maxDay="31";
		break;
		case 5:
		maxDay="30";
		break;
		case 6:
		maxDay="31";
		break;
		case 7:
		maxDay="31";
		break;
		case 8:
		maxDay="30";
		break;
		case 9:
		maxDay="31";
		break;
		case 10:
		maxDay="30";
		break;
		case 11:
		maxDay="31";
		break;
	}
	//Feb - special case
	if (selectedMonth=="1"){
		if (selectedYear%4=="0"){
			maxDay="29";
		}
	}
	var dayfield=document.getElementById("daydropdown");
	$('#daydropdown').children().remove();
	for (var i=0; i<maxDay; i++){
		dayfield.options[i]=new Option(i+1, i);
	}
	if (selectedDay < maxDay){
		$("#daydropdown").val(selectedDay).attr("selected", "selected");
	}else{
		$("#daydropdown").val(maxDay-1).attr("selected", "selected");
	}

}

function updateDOB(){

	selectedMonth =$("#monthdropdown option").index($("#monthdropdown option:selected"));
	selectedMonth++;
    if (selectedMonth<10){
    	selectedMonth="0"+selectedMonth;
    }
    
    selectedDay =$("#daydropdown option").index($("#daydropdown option:selected"));
    selectedDay++;
    if (selectedDay<10){
    	selectedDay="0"+selectedDay;
    }
    
    selectedYear =$('#yeardropdown').val();
    
	dob = selectedMonth +'\/'+ selectedDay +'\/'+ selectedYear;
	return dob;
}

function calculate_age(birth_month,birth_day,birth_year){

    today_date = new Date();
    today_year = today_date.getFullYear();
    today_month = today_date.getMonth();
    today_day = today_date.getDate();
    age = today_year - birth_year;
    if ( today_month < (birth_month - 1)){
      age--;
    }
    if (((birth_month - 1) == today_month) && (today_day < birth_day)){ 
      age--;
    }

  	return age;

}

//Dropdown DOB - END

// to display the url in an IFrame
function getIFrameView(view, childRelID) {
	url = $("#profile").attr("url");
	$.ajax({
		url : url,
		data : "view=" + view + "&childRelID=" + childRelID,
		success : function(data) {
			$(".iframeSettings").html(data);
		}
	});
}

function showIframeModal(title, url) {
	$("#contentModal").html(
	'<iframe id="contentIframe" name="contentIframe" src="' + url +'" frameborder="0" scrolling="auto" width="100%" height="500"marginheight="5">'
	);
	$("#contentModal").dialog({
	width : 950,
	height: 600,
	title : title,
	modal : true
	});
}

//show Iframe
function showIFrame(title, view, childRelID) {
	$("#iframe").css("overflow","none");
	$("#iframe").dialog({
		width : 500,
		height: 600,
		title : title,
		modal : true
	});
	getIFrameView(view, childRelID);
}

function getModalView(view) {
	url = $("#modal").attr("url");
	$.ajax({
		url : url,
		data : "view=" + view,
		success : function(data) {
			$("#modal").html(data);
		}
	});
}

function showModal(title, viewName) {
	$("#modal").css("overflow","scroll");
	$("#modal").dialog({
		width : 950,
		height: 600,
		title : title,
		modal : true
	});
	getModalView(viewName) //Default view      
}

function showCSNModal(title, viewName) {
	$("#modal").css("overflow","auto");	
	$("#modal").dialog({
		width : 970,
		height: 300,
		autoResize: true,
		title : title,
		modal : true,
		resizable: false
	});
	getModalView(viewName) //Default view      
}

var toJoinCSN;
function getjoinCSNInfoFromModal() {
	toJoinCSN = document.getElementById("joinCSNSettings").MODAL_JOIN_CSN.checked;
	$("#modal").dialog("close");
	updatejoinCSNInfoFromModal();
}
function updatejoinCSNInfoFromModal() {
	document.getElementById("accountSettings").JOIN_CSN.checked = toJoinCSN;
}
function getjoinCSNInfo() {
	return toJoinCSN;
}

function getjoinCSNInfoFromView() {
	return document.getElementById("accountSettings").MODAL_JOIN_CSN.checked;
}

// On question change call this only after emptying the answer
function questionFieldChangedResetAnswer(resetField){
	document.getElementById(resetField).value="";
	securityFieldsQuestionsChanged();
}

function contactFieldsChanged(){
	document.getElementById("VALIDATE_CONTACT").value="changed";

}

function securityFieldsNewPasswordChanged(){
	document.getElementById("VALIDATE_SECURITY_NEWPASSWORD").value="changed";
}

function securityFieldsQuestionsChanged(){
	document.getElementById("VALIDATE_SECURITY_QUESTIONS").value="changed";
}


function findKidstoAllowAccess(form) {
	var allowAccessKids = "";
	var noOfKids = 0;
	if (form.ALLOW_MYCHART_ACCESS.length != undefined) {
		noOfKids = form.ALLOW_MYCHART_ACCESS.length;
	} else {
		noOfKids = 1;
	}
	if (noOfKids > 1) {
		for ( var i = 0; i < form.ALLOW_MYCHART_ACCESS.length; i++) {
			if (form.ALLOW_MYCHART_ACCESS[i].checked) {
				allowAccessKids += form.ALLOW_MYCHART_ACCESS[i].value + ",";
			}
		}
	} else if (noOfKids == 1) {
		if (form.ALLOW_MYCHART_ACCESS.checked) {
			allowAccessKids = form.ALLOW_MYCHART_ACCESS.value;
		}
	}
	return allowAccessKids;
}

function getDefaultChildChosen(form) {
if(form.DEFAULT_CHILD_MRN != undefined){
	for ( var i = 0; i < form.DEFAULT_CHILD_MRN.length; i++) {
		if (form.DEFAULT_CHILD_MRN[i].checked) {
			return form.DEFAULT_CHILD_MRN[i].value;
		}
	}
	}
	return "none";		
}

function isNumberKey(evt) {
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
}
