function closeForm(url) {
    window.location=url;
}

function validentry() {
document.createappointment.button_value.value='create_app';	    
        var title=document.getElementById('title');
	var starthour=document.getElementById('starthour');
	var startminute=document.getElementById('startminute');
	var endhour=document.getElementById('endhour');
	var endminute=document.getElementById('endminute');
	var starthourtime=starthour.value*100;
	var endhourtime=endhour.value*100;
	var starttime=starthourtime+parseInt(startminute.value);
	var endtime=endhourtime+parseInt(endminute.value);
	var c=0;
	if(title.value=="") {
		alert('Appointment Title Cannot Be Null');
		c=1;
	}
	if(starttime>endtime) {
		alert('Start Time Cannot be After Than End Time');
		c=1;
	}
	if(c==0) {
		document.createappointment.submit();
       }
}

function validentryforedit() {
	document.editappointment.button_value.value='edit_app';	    
        var title=document.getElementById('title');
	var starthour=document.getElementById('starthour');
	var startminute=document.getElementById('startminute');
	var endhour=document.getElementById('endhour');
	var endminute=document.getElementById('endminute');
	var starthourtime=starthour.value*100;
	var endhourtime=endhour.value*100;
	var starttime=starthourtime+parseInt(startminute.value);
	var endtime=endhourtime+parseInt(endminute.value);
	var c=0;
	if(title.value=="") {
		alert('Appointment Title Cannot Be Null');
		c=1;
	}
	if(starttime>endtime) {
		alert('Start Time Cannot be After Than End Time');
		c=1;
	}
	if(c==0) {
		document.editappointment.submit();
       }
}


function deleteAppointment(action) {
 if(action=='delete_appointment') { 
 document.delete_appointment_form.button_value.value='delete_appointment';
 }
 else { 
 document.delete_appointment_form.button_value.value='';
 }
 document.delete_appointment_form.submit();
}


function deleteAttachmentAppointment(action) {
 if(action=='delete_attachment_appointment') { 
 document.delete_attachment_appointment_form.button_value.value='delete_attachment_appointment';
 }
 else { 
 document.delete_attachment_appointment_form.button_value.value='';
 }
 document.delete_attachment_appointment_form.submit();
}


function shareApp(action) {
    if(action=='save') { 
      document.share_form.button_value.value='share_appointment';
    }
    else if(action=='back') {
      document.share_form.button_value.value='back_to_appointment';
    }
    else { 
      document.share_form.button_value.value='';
    }
    document.share_form.submit();
}

function shareAppPopup(action) {
    if(action=='save') { 
      document.pop_share_form.button_value.value='share_appointment';
    }
    else if(action=='back') {
      document.pop_share_form.button_value.value='back_to_appointment';
    }
    else { 
      document.pop_share_form.button_value.value='';
    }
    document.pop_share_form.submit();
}

function showSearchResultDivision(Ourl) {
    var sdate=document.getElementById('sdate').value;
    var smonth=document.getElementById('smonth').value;
    var syear=document.getElementById('syear').value;
    fromDate=syear+'-'+smonth+'-'+sdate;
    var edate=document.getElementById('edate').value;
    var emonth=document.getElementById('emonth').value;
    var eyear=document.getElementById('eyear').value;
    toDate=eyear+'-'+emonth+'-'+edate;
    start=new Date(syear, smonth-1, sdate);
    endD=new Date(eyear, emonth-1, edate);
    if((smonth==2 || smonth==4 || smonth==6 || smonth==9 || smonth==11) && sdate>30) {
        alert("Invalid Start Date");
        return false;
    }
    if((emonth==2 || emonth==4 || emonth==6 || emonth==9 || emonth==11) && edate>30) {
        alert("Invalid End Date");
        return false;
    }       
    if(start>endD) { 
        alert("Start date can not be greater than the end date.");
        return false;
    }
    Ourl = Ourl+'?fromdate='+fromDate+'&todate='+toDate;
    
    httpReqAttach = new XMLHttpRequest();
    httpReqAttach.open("GET", Ourl, true);
    httpReqAttach.onreadystatechange = stateChangeSearchResults;
    httpReqAttach.send(null);
}

function stateChangeSearchResults(){
    if (httpReqAttach.readyState == 4) { 
       showSearchResults(httpReqAttach.responseText);
    }
}

function showSearchResults(result) {
    var  resultdiv = document.getElementById("SearchResultDivision");
    resultdiv.innerHTML = result;
}


function selectedUsers(rUrl) {
    var username=document.getElementById('username').value;
    var actionType=document.getElementById('actionType').value;
    //rUrl = rUrl+'/@@shareappointment_view?searchfor='+username;
    rUrl = rUrl+'/@@sharepopup_view?actionType='+actionType+'&searchfor='+username;
    showSharingDivision(rUrl);
    //window.location=rUrl; 
}

function popupClose() {
  window.opener.document.location.reload(true);
  window.close();
}

function redirectParentWindow(Ourl) {
  opener.location.href=Ourl;
  window.close();
}

function sendEmail(action) {
    if(action=='sendemail') { 
      document.sendemail.button_value.value='sendemail';
    }
    else { 
      document.sendemail.button_value.value='';
    }
    document.sendemail.submit();
}

function sendSMS(action) {
    if(action=='sendsms') { 
      document.sendsms.button_value.value='sendsms';
    }
    else { 
      document.sendsms.button_value.value='';
    }
    document.sendsms.submit();
}


