var needToConfirm = false;
  
function confirmExit()
{	
	if (needToConfirm)
    {
		return "You have attempted to leave this page with unsaved data. ";		
    }
}

window.onbeforeunload = confirmExit;

function confirmExitForTabs(pageName,tabId,tab)
{	
	if (needToConfirm)
    {
		if (confirm("You have attempted to leave this page with unsaved data. Do you wish to continue?"))
		{
			hideShow(pageName,tabId,tab);
			needToConfirm = false;
		}		
    }
	else
	{
		hideShow(pageName,tabId,tab);
	}
}

function dataChanged(imageId)
{
	needToConfirm = true;
	var image = document.getElementById(imageId);
	if (image!=null){
		image.src = "/appt/images/disk.gif";
	}
}

function getAttribute(element, attributeName)
{
  var attribute = null;
  var attributes = element.attributes;

  try
  {
    if(attributes.getNamedItem)
    {
      attribute = attributes.getNamedItem(attributeName).nodeValue;
    }
    else
    {
      for(var i = 0 ; i < attributes.length && (attribute == null) ; i++)
      {
        if(attributes[i].nodeName == attributeName)
        {
          attribute = attributes[i].nodeValue;
        }
      }
    }
  }
  catch(e)
  { 
  }
  return attribute;
}

function getId(element)
{
  return getAttribute(element, 'id');
}

function getType(element)
{
  return getAttribute(element, 'type');
}

function hideShow(id,menuId,listId)
{	
  var row = document.getElementById(id);	
  var table = row.parentNode;  
  var anchor = document.getElementById(menuId);
  var list = document.getElementById(listId);  
  
  for (var i=0; i < list.childNodes.length; i++){
	  list.childNodes[i].childNodes[0].setAttribute("class","");
	  list.childNodes[i].childNodes[0].setAttribute("className","");
  }
   
  anchor.setAttribute("class", "selected");
  anchor.setAttribute("className", "selected");
  var style;
  for (var j=0;j<table.childNodes.length;j++)
  {
	  style = table.childNodes[j].style;
	  style.display = "none";	 
  }
  
  style = row.style;
  if (style.display=="block"){
	  style.display = "none";
  }
  else{
	  style.display = "block";
  }   
}

function showIt(item, display)
{
  if(display == null) {
	  display = 'block';
  }
  try
  {
    hideShow(item, 'visible', display);
  }
  catch(e)
  {
    hideShow(item, 'visible', 'block');
  }
}

function changeVisibility(id){
	var el = document.getElementById(id);
	if (el!=null){
		if (el.style.visibility == 'visible'){
			el.style.visibility = 'hidden';
		}
		else{
			el.style.visibility = 'visible';
		}
	}
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g,'');
}

function isEmpty(str)
{
  return (trim(str).length == 0);
}

function delay(millis)
{
  var end = new Date().getTime() + millis;
  while(new Date().getTime() < end){}
}

function copyField(form,fieldWithValue,fieldToChange){
	for(var i = 0 ; i < form.elements.length ; i++ )
	{
		if(form.elements[i].name.indexOf( fieldWithValue ) != -1)
	     {
			var copy = form.elements[i].value;
			for(var j = 0 ; j < form.elements.length ; j++ )
			{
				if(form.elements[j].name.indexOf( fieldToChange ) != -1)
	     		{
	     			form.elements[j].value = copy;
	     		}
			}
	     }
	}
}

function submitForm(id)
{			
	needToConfirm = false;
	document.scheduleForm.scheduleDayId.value = id; 
	document.scheduleForm.submit();
}

function submitReportForm(id)
{					
	needToConfirm = false;
	var reportForm = document.getElementById(id);
	var suffixList = reportForm.suffix;
	for (var i=0;i<suffixList.length;i++){
		if (suffixList[i].checked){
			reportForm.setAttribute("action","/appt/secure/admin/viewReport." + suffixList[i].value);
		}
	}	
	reportForm.submit();
}

function submitFormById(id)
{					
	needToConfirm = false;
	document.getElementById(id).submit();
}

function submitApptFormById(id)
{					
	needToConfirm = false;
	var selectedServices = document.getElementById("selectedServices"); 
	selectedServices.value = "";
	var services = document.getElementById("dd2-ct").childNodes;
	for (var i=0;i<services.length;i++){
		label = services[i].id;
		if (label!="draghelp"){
			var serviceId = label.substring(0,label.indexOf("|")); 
			selectedServices.value += serviceId;
			if (i<(services.length-1)){
				selectedServices.value += ",";
			}
		}
	}	
	document.getElementById(id).submit();
}

function checkClientSelected(){
	var client = document.getElementById("clientSelect");
	if (client.value=="none"){
		Ext.Msg.alert('No Client Selected', 'Please choose a client.').setIcon(Ext.Msg.WARNING);
		return false;
	}
	return true;
}


function checkServiceSelected(id,formId){
	var container = document.getElementById("dd2-ct");
	if(container.childNodes.length > 0 && container.childNodes[0].id !== "draghelp"){
		document.getElementById(id).value = 'true';
		submitApptFormById(formId);			
	}
	else{		
		Ext.Msg.alert('No Service', 'Please choose a service.').setIcon(Ext.Msg.WARNING);
	}	
}

function checkTerms(id,formId)
{		
	var value = document.getElementById(id).value;	
	if (value === 'staff' || value === true)
	{
		if (formId==='updateEvent' || checkClientSelected()){
			checkServiceSelected(id,formId);
		}
	}
	else{
		Ext.MessageBox.confirm('Confirm', 'Please ensure that you have read the terms and conditions.<br/>Click <a href="/appt/displayText.jsp" target="_blank">here</a> to view. Do you agree to the terms?',
				function(btn, text){
		    		if (btn == 'yes'){
		    			checkServiceSelected(id,formId);	    				
		    		}
				});		
	}	
}

function preview(what){
	var fileTypes=["bmp","gif","png","jpg","jpeg","tif","tiff","jif","jfif","jp2","jpx" ,"j2k","j2c","fpx","pcd" ];
	var source=what.value;	
	var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
	
	for (var i=0; i<fileTypes.length; i++){
		if (fileTypes==ext){ 
			break;
		}
	}
	var globalPic=new Image();
	if (!(i==fileTypes.length)){
		alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
		what.fileUpload.focus();
		return false;
	}
	else{
		return true;
	}
}

function createCookie(name,value,days) {
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function disableBeanToggle(form, beanProperty)
{
  for(var i = 0 ; i < form.elements.length ; i++ )
   {
     if(form.elements[i].name.indexOf( beanProperty ) != -1)
     {
       form.elements[i].disabled = !form.elements[i].disabled;
     }
   }
}

function updateScheduleChanges(){
	if (document.getElementById("updated").value=="false"){
		document.getElementById("updateScheduleChages").style.display = "block";
	}
}