//Date validation function.
//var static_path = "http://192.168.1.111/phpdocs/pain-mds/";
var datePat = /^(\d{2})(-)(\d{2})(-)(\d{4})$/;
/*function isDate(dateStr) {
var datePat = /^(\d{2})(-)(\d{2})(-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter your birth date as mm-dd-yyyy");
//return false;
}
}*/

var remail=/^([_&a-zA-Z0-9-]+(\.[_&a-zA-Z0-9-]+)*@[&a-zA-Z0-9-]+\.+[&a-zA-Z0-9-]+)/;
/*function validatmail(fieldvalue){
	if(remail.test(fieldvalue))
		return false;
	else
		return true;
}*/
var rename	=	/^[a-zA-z\-\ ]+$/;
var telnoRegxp = /^([0-9]{10})$/;
var daymonthReg = /^([0-9]{2})$/;
var yearRegxp = /^([0-9]{4})$/;

//Function to show other textbox on select of the radio button
function showother1(){
		relationship = document.contact.relationship.value;
		//alert(relationship);
		if(relationship=='other'){
			document.getElementById("otherdiv").style.display ="inline";
		}else{
			document.getElementById("otherdiv").style.display ="none";
		}
		if(relationship!=0){
			document.contact.relRadio[1].checked=true;
		}else{
			document.contact.relRadio[1].checked=false;
		}
		
	}
function hide_changeto(){
	cancelapp = document.frmAppointment.cancelappt.checked;
	if(cancelapp==true)
	{
		document.getElementById("rescheduleto").style.display="none";
	}else{
		document.getElementById("rescheduleto").style.display="inline";
	}
	/*if(relationship=='other'){
		document.getElementById("otherdiv").style.display ="inline";
	}else{
		document.getElementById("otherdiv").style.display ="none";
	}
	if(relationship!=0){
		document.contact.relRadio[1].checked=true;
	}else{
		document.contact.relRadio[1].checked=false;
	}*/
}

//Check dateseleted or not.
function checkcurrdate(){
		if(document.frmAppointment.appointmentdate.value=="mm-dd-yyyy"){
			alert("\nSelection of appointment date is required first.");
		}

	}

function checkresdate(){
		if(document.frmAppointment.newdate.value=="mm-dd-yyyy"){
			alert("\nSelection of rescheduled appointment date is required first.");
		}

	}
//-------------Function to insert first form details in db--------------------------
function insert_PatientDetials()
{
	//alert(static_path);
	connect();
	var errmsg	= "";
	datePat = /^(\d{2})(-)(\d{2})(-)(\d{4})$/;
	//var brithdate = document.getElementById("birthdate").value;
	//alert(brithdate);
	
	if (document.getElementById("patientName").value=="")
	{
		/*alert("Please mention patient name");
		return false;*/
		errmsg	= errmsg + "\nPatient name required";
	}
	var valname	=	rename.test(document.getElementById("patientName").value);
	if(!valname){
		//alert("Please mention valid name");
		errmsg	= errmsg + "\nValid patient name required";
	}
	//Code to check valid birthdate
	if (document.getElementById("bmonth").value=="" || document.getElementById("bmonth").value=="mm")
	{
		/*alert("Please mention birth date");
		return false;*/
		errmsg	= errmsg + "\nBirth month required";

	}
	if (document.getElementById("bday").value=="" || document.getElementById("bday").value=="dd")
	{
		/*alert("Please mention birth month");
		return false;*/
		errmsg	= errmsg + "\nBirth date required";

	}
	if (document.getElementById("byear").value=="" || document.getElementById("byear").value=="yyyy")
	{
		/*alert("Please mention birth year");
		return false;*/
		errmsg	= errmsg + "\nBirth year required";
	}

	var matchday	=	daymonthReg.test(document.getElementById("bday").value);
	if(!matchday){
		/*alert("Please mention valid birth date");
		return false;*/
		errmsg	= errmsg + "\nValid birth date required";
	}
	var matchmon	=	daymonthReg.test(document.getElementById("bmonth").value);
	if(!matchmon){
		/*alert("Please mention valid birth month");
		return false;*/
		errmsg	= errmsg + "\nValid birth month required";
	}
	var matchyer	=	yearRegxp.test(document.getElementById("byear").value);
	if(!matchyer){
		errmsg	= errmsg + "\nValid birth year required";
		/*alert("Please mention valid birth year");
		return false;*/
	}
	
	//Check valid date( check month not > 12, day not >31)
	var brithdate	= document.getElementById("bmonth").value +"-"+document.getElementById("bday").value+"-"+document.getElementById("byear").value;
	var isvaldt	= validatebdate(brithdate);
	if(isvaldt){
		errmsg	= errmsg + "\nDate of birth required in the format mm-dd-yyyy";
		/*alert("Please mention valid date of birth as mm-dd-yyyy");
		return false;*/
	}
	//End of check valid date
	

	if (document.getElementById("appointmentdate").value=="" || document.getElementById("appointmentdate").value=="mm-dd-yyyy")
	{
		/*alert("Please mention appointment date.");
		return false;*/
		errmsg	= errmsg + "\nAppointment date required";

	}
	
	//Check validity of appointment date. Appointment date should be greater than todays date.

	var appmtdt		=	document.getElementById("appointmentdate").value;
	var isValidappDt=	chkValidapp(appmtdt);

	if(!chkValidapp(appmtdt)){
		
		errmsg	= errmsg + "\nValid Appointment Date required";

	}
	//End of check validity of appointment date


	if (document.getElementById("notifyReps").value=="")
	{
		/*alert("Please select appointment time.");
		return false;*/
		errmsg	= errmsg + "\nAppointment time required";
	}

	if(errmsg){
		alert(errmsg);
	}else{
		loginurl=static_path+"formsubmit/apptins_patient_details.php";
		var poststr = "patientName=" + encodeURI( document.getElementById("patientName").value ) +
						"&birthdate=" + encodeURI(brithdate) +
						"&appointmentdate=" + encodeURI(document.getElementById("appointmentdate").value ) +
						"&time=" + encodeURI( document.getElementById("notifyReps").value )+
						"&frmType=" + encodeURI( document.getElementById("frmType").value );
		//alert(poststr);
		//return false;
		//alert(loginurl);
		xmlhttp.open("POST",loginurl,true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(poststr);	
		
		xmlhttp.onreadystatechange=function()
		{
				if(xmlhttp.readyState == 3){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
			else if(xmlhttp.readyState==4)
			{
				var result=xmlhttp.responseText;
				//alert(result);
				if (result!="")
				{
					/*arrResult	=	result.split("+")
					document.getElementById('sheduleappointmet').innerHTML = arrResult[2];*/
					document.getElementById('sheduleappointmet').innerHTML = result;
				}
				//hiddenFloatingDiv('windowcontent');void(0);
			}
		}

	}
	//alert(brithdate);
	//alert("selected values-- " + document.getElementById("notifyReps").value);
	

}




// Function to Manage Professional. (4 options for 4 type of professionals)
function funinsProfessional(evt){
	connect();
	//alert(evt);
	loginurl=static_path+"formsubmit/addMedicalProf.php";
	if(evt==0){ // for next form functionality
		//alert(evt);
		var j,prof="";
		for(j=0;j<document.frmProfessionals.medicalProf.length;j++){
			if(document.frmProfessionals.medicalProf[j].checked	== true){
				prof	= document.frmProfessionals.medicalProf[j].value;
			}
		}

		if(prof == ""){
			alert("Medical professional required");
			return false;
		}
		
		var poststr	=	"profType=" + prof + "&actiontype=" + evt;
		xmlhttp.open("POST",loginurl,true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(poststr);
		xmlhttp.onreadystatechange=function()
		{
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}

			else if(xmlhttp.readyState==4)
			{
				var result=xmlhttp.responseText;
				if (result!="")
				{
					//alert(result);
					document.getElementById('sheduleappointmet').innerHTML = result;
				}
				
			}
		}
	}
	if(evt==1){ //For previous form functionality.
		parameter = "?actiontype=" + evt;
		xmlhttp.open('GET', loginurl +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{	
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
		 else if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 arrResult	=	result.split("+")
				 //alert(arrResult[1]);
				 document.getElementById('sheduleappointmet').innerHTML = arrResult[0];
				 if(arrResult[1]=="established"){
					 // alert(document.frmAppointment.notifyReps.value);
					 //alert(document.frmAppointment.appointmentdate.value);
					 SelectSubCat('appointmentdate','time');
					 var seltim		=	document.frmAppointment.notifyReps.value;
					 var backtime	=	seltim.split(",");
					 for(var t=0; t<backtime.length; t++){
						 //alert(backtime[t]);
						notifySelect(backtime[t]);
					 }

					 
				 }else{
					 if(arrResult[1]=="established"){
						//alert(document.frmAppointment.notifyReps.value);
						SelectSubCat('newdate','newtime');
						var seltim		=	document.frmAppointment.notifyReps.value;
						var backtime	=	seltim.split(",");
						for(var t=0; t<backtime.length; t++){
							 //alert(backtime[t]);
							notifySelect(backtime[t]);
						}
					 }
				 }
				 

			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}

	}
}
//End of function to manage Professional.

//Function to insert the doctors in db
function funinsDoctors(evt){
	//alert(evt);
	loginurl	=	static_path+"formsubmit/addDoctors.php";
	connect();
	if(evt=='next'){
		var docs	= "";
		postdata	= "";

		
		if(document.frmAppointment.doctors.length){
			for(var dcnt=0;dcnt<document.frmAppointment.doctors.length;dcnt++)
			{
				if(document.frmAppointment.doctors[dcnt].checked == true){
					docs = docs + ":" + document.frmAppointment.doctors[dcnt].value;
				}
				
			}
		}/*else{
			if(document.frmAppointment.doctors.checked == true){
				docs = docs + ":" + document.frmAppointment.doctors.value;
				docs = docs + ":";
			}
		}*/		
		
		
		if(docs!=""){
			docs = docs + ":";
			postdata	=	postdata + "doctors=" + docs;
			postdata	=	postdata + "&actiontype=" + evt;
		}else{

			alert("Professional required");
			return false;
		}
		
		if(document.frmAppointment.annualphy){
			if(document.frmAppointment.annualphy.checked==true)
			{
				//alert(document.frmAppointment.annualphy.value);
				postdata	=	postdata + "&annual="+encodeURI( document.frmAppointment.annualphy.value );
			}
		}

		//Ajax code
		
		

		xmlhttp.open("POST", loginurl, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(postdata);
		xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
			else if(xmlhttp.readyState==4){
				var result	= xmlhttp.responseText;
				//alert(result);
				document.getElementById('sheduleappointmet').innerHTML = result;
			}
		}
	}
	
	
	if(evt=='back'){ //For previous professional type form functionality.
		parameter = "?actiontype=" + evt;
		xmlhttp.open('GET', loginurl +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{		 
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}

		  else if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 document.getElementById('sheduleappointmet').innerHTML = result;

			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}

	}
	
}
//End of function to insert the doctors in db



//----------------Function to insert contact detail() form in db-------------
function insContactdetail(evt){
	
	connect();
	var rel;
	var relationship;
	loginurl	=	static_path+"formsubmit/addContactDtl.php";
	//alert(loginurl);
	if(evt=="next"){// code to get next form
		
		contactnm	= document.contact.contactnm.value;
		if(contactnm==""){
			alert("Contact name required");
			return false;
		}
		//alert(document.contact.relRadio.length);
		
		for(var rcnt=0;rcnt<document.contact.relRadio.length;rcnt++){
			if(document.contact.relRadio[rcnt].checked==true){
				rel	= document.contact.relRadio[rcnt].value;
			}
		}
		if(rel==1){
			relationship	= "Self";
		}else if(rel==2){
			relationship	= document.contact.relationship.value;
			if(relationship==""){
					alert("Relationship with patient required");
					return false;
			}else{
				if(relationship=='other'){
					relationship= document.contact.otherrel.value;
				}
			}

		}
		if(!relationship){
			alert("Relationship with patient required");
			return false;
		}
		phone	=	document.contact.phone.value;
		var validateno = checktelno(phone)
		if(phone==""){
			alert("Phone number required");
			return false;
		}else{
			
			if(validateno){
				alert("Telephone number required in the format xxx-xxx-xxxx");
				return false;
			}

		}

		email	=	document.contact.email.value;
		if(email==""){
			alert("Email address required");
			return false;
		}else{
			if(!remail.test(email)){
				alert("Valid email address required");
				return false;
			}
		}
		var postdata	= "contactnm=" + encodeURI(contactnm) + "&relationship=" + encodeURI(relationship) + "&phone=" + encodeURI(phone) + "&email=" + encodeURI(email) + "&actiontype=" + evt;
		//Ajax code
		//alert(postdata);
		xmlhttp.open("POST", loginurl, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(postdata);
		xmlhttp.onreadystatechange=function(){
			//alert(xmlhttp.readyState);
			if(xmlhttp.readyState==4){
				var result	= xmlhttp.responseText;
				 
				document.getElementById('sheduleappointmet').innerHTML = result;
				if(result.match("frmInsurance")){
				} else {
					sample();
				}
				 /*var x = "dsfsd fsdfsdf sdf sd";
				 y = result.split("frmInsurance");
				 y =y.length;
				 //alert (y);
				 if(y==1)
				 {
				 sample();
				 }*/
			}
		}
	}

	if (evt=='back') //code to get prevous form.
	{
		parameter = "?actiontype=" + evt;
		//alert(parameter);
		xmlhttp.open('GET', loginurl +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{		 
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
		 else if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 document.getElementById('sheduleappointmet').innerHTML = result;
				 //sample();
				
			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}
		
	}
	
}
function sample()
{
	var x,y;
		x = 300;
		y = 300;
		var url_is = 'thank-you-newpatient.html';
		window.open(url_is,this.target,'width='+x+',height='+y+',left=5,top=5,scrollbars, resizable=no');

	
}
function sample1()
{
	window.open("thank-you-established.html","Thank you","width=300, height=300, top=100, left=350, toolbar=1, menubar=1, location=1, status=1, scrollbars=1, resizable=0")
}
function sample2()
{
	window.open("thank-you-cancel.html","Thank you","width=300, height=300, top=100, left=350, toolbar=1, menubar=1, location=1, status=1, scrollbars=1, resizable=0")
}
//----------------End of Function to insert contact detail() form in db-------------



// --------------Function to check if insurance change -----------------------------
function isInsChange(evt){
	var insChange = "";
	connect();
	//alert(evt);
	url			=	static_path+"formsubmit/chkinsChange.php";
	//alert(url);
	if(evt == 'next'){ //Code to get next form
		for(inscnt=0;inscnt<document.frmInsurance.inschange.length;inscnt++){
			if(document.frmInsurance.inschange[inscnt].checked==true){
				insChange	=	document.frmInsurance.inschange[inscnt].value;
			}
		}
		if(!insChange){
			alert("Selection required");
			return false;
		}else{
			url			=	static_path+"formsubmit/chkinsChange.php";
			postdata	=	"insChange=" + encodeURI(insChange) + "&actiontype=" + evt;
			xmlhttp.open("POST", url, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.send(postdata);
			xmlhttp.onreadystatechange = function(){
					if(xmlhttp.readyState == 4){
						result	=	xmlhttp.responseText;
						//alert(result);
						if(result!=""){
							//alert(result);
							document.getElementById('sheduleappointmet').innerHTML = result;
							if(result.match("frmInschange") || result.match("frminsmoved")){
							} else {
								sample1();
							}
						}

					}
			}

		}
	}
	if (evt =='back') {//code to get prevous form.
		parameter = "?actiontype=" + evt;
		xmlhttp.open('GET', url +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{		 
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
		else if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 document.getElementById('sheduleappointmet').innerHTML = result;

			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}

	}





}
//---------------End of Function to check if insurance change ----------------------


//---------------Function to change address of insurance company------------------
function insAddresschange(evt){
	var errmsg	= "";
	connect();
	url			= static_path+"formsubmit/insAddresschange.php";
	if(evt == 'next'){

		if(document.frminsmoved.inschange.value=="noradio"){ //check if radio button is there or not.
			inschange = 0;
		}else{
			if(document.frminsmoved.inschange.checked==true){
			var inschg	= document.frminsmoved.inschange.value;
			}else{
				var inschg	= 0;
			}
		}
		
		if (document.frminsmoved.address.value=="")
		{
			errmsg	= errmsg + "Address required\n";
		}
		if(document.frminsmoved.telephone.value==""){
			errmsg	=  errmsg + "Telepehone number required\n";
		}else{
			var teleph = document.frminsmoved.telephone.value
			var validateno = checktelno(teleph);
			if (validateno)
			{
				errmsg = errmsg + "Telephone number required in the format xxx-xxx-xxxx\n";
			}
			/*if(!telnoRegxp.test(teleph)){
				alert("Invalid telephone number");
				return false;
			}*/
		}
		if(errmsg){
			alert(errmsg);
			return false;
		}else{
			postdata	= "address=" + encodeURI(document.frminsmoved.address.value) + 
						  "&telephone=" + encodeURI(document.frminsmoved.telephone.value) + 
						  "&inschange=" + encodeURI(inschg) + "&actiontype=" + evt;
			
			xmlhttp.open("POST", url, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.send(postdata);
			xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}

				else if (xmlhttp.readyState == 4)
				{
					result	= xmlhttp.responseText;
					document.getElementById('sheduleappointmet').innerHTML = result;
					if(result.match("frmInschange")){
					} else {
						sample1();
					}
				}
			}
		}
	
	}

	if(evt == 'back'){
		parameter = "?actiontype=" + evt;
		xmlhttp.open('GET', url +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{		 
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
		else  if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 document.getElementById('sheduleappointmet').innerHTML = result;

			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}
	}

	
}

//---------------End of Function to change address of insurance company-----------




//---------------Function to change insurance company itself------------------
function insCompanyChange(evt){
	//alert("hi hello");
	
	var errmsg	=	"";
	connect();
	url			=	static_path+"formsubmit/insCompanychange.php"
	
	if(evt=='next'){
		if(document.frmInschange.insmoved.value=="noradio"){
			var addressChange	= 0;
		}else{
			if(document.frmInschange.insmoved.checked==true){
				var addressChange = document.frmInschange.insmoved.value;
			}
		}
		//alert(addressChange);
		if (document.frmInschange.insCompany.value=="")
		{
			errmsg = errmsg + "\nInsurance company name required";
		}
		if(document.frmInschange.policyno.value==""){
			errmsg = errmsg + "\nInsurance Policy number required";
		}
		if(document.frmInschange.groupno.value==""){
			errmsg = errmsg + "\nGroup number required";
		}
		if (errmsg)
		{
			alert(errmsg);
			return false;
		}else{
			var insaddress;
			/*if(document.frmInschange.insmoved){
				insaddress = document.frmInschange.insmoved.value;
			}*/
			
			postdata	=	"comapnynm=" + encodeURI(document.frmInschange.insCompany.value) + "&policyno=" + encodeURI(document.frmInschange.policyno.value) + "&groupno=" + encodeURI(document.frmInschange.groupno.value) + "&addressChange=" + addressChange + "&actiontype=" + evt;
			
			//alert(postdata);
			xmlhttp.open("POST", url, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.send(postdata);
			xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
				else if (xmlhttp.readyState == 4)
				{
					result	= xmlhttp.responseText;
					//alert(result);
					document.getElementById('sheduleappointmet').innerHTML = result;
					if(result.match("frminsmoved")){
					} else {
						sample1();
					}
				}
				
		}
	}
	}
	
	if(evt=='back'){
		parameter = "?actiontype=" + evt;
		xmlhttp.open('GET', url +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{		 
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
		  else if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 document.getElementById('sheduleappointmet').innerHTML = result;

			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}


	}

	
}
//---------------End of Function to change insurance company itself-----------


//---------------Function for reschedule appointment--------------------------
function rescheduleapp(){
	connect();
	var patientnm	=	document.frmAppointment.patientname.value;
	//var birthdate	=	document.frmAppointment.birthdate.value;
	var appDate		=	document.frmAppointment.appointmentdate.value;
	var appTime		=	document.frmAppointment.notifyReps.value;
	var changeDate	=	document.frmAppointment.newdate.value;
	var changeTime	=	document.frmAppointment.notifyReps1.value;
	var appnewDate	=	document.frmAppointment.newdate.value;
	//alert(changeTime);
	var cancelapp	=	"";
	
	if(document.frmAppointment.cancelappt.checked==true){
		cancelapp	=	1;
	}else{
		cancelapp	=	0;
	}
	//alert(cancelapp);
		
	datePat = /^(\d{2})(-)(\d{2})(-)(\d{4})$/;
	//alert(patientnm + " " + birthdate+ " " +appDate + " " + appTime+ " " +changeDate + " " + changeTime);
	var errmsg	=	"";
	if(patientnm==""){
		errmsg = errmsg + "\nPatient name required";
	}
	var resname	=	rename.test(patientnm);
	if(!resname){
		//alert("Please mention valid name");
		errmsg	= errmsg + "\nValid patient name required";
	}
	/*if(birthdate=="" || birthdate=="mm-dd-yyyy"){
		errmsg = errmsg + "\nPlease enter birthdate";
	}*/
	
	/*var matchArray = birthdate.match(datePat); // is the format ok?
	if(matchArray == null){
		errmsg = errmsg + "\nPlease enter your birth date as mm-dd-yyyy";
	}*/
	var matchday	=	daymonthReg.test(document.getElementById("bday").value);
	var matchmon	=	daymonthReg.test(document.getElementById("bmonth").value);
	var matchyer	=	yearRegxp.test(document.getElementById("byear").value);
	//Check valid date( check month not > 12, day not >31)
	var brithdate	= document.getElementById("bmonth").value +"-"+document.getElementById("bday").value+"-"+document.getElementById("byear").value;
	var isvaldt	= validatebdate(brithdate);


	if (document.getElementById("bmonth").value=="" || document.getElementById("bmonth").value=="mm")
	{
		errmsg = errmsg + "\nBirth month required";
	}else if(!matchmon){
		errmsg = errmsg + "\nValid birth month required";
		
	}

	if (document.getElementById("bday").value=="" || document.getElementById("bday").value=="dd")
	{
		errmsg = errmsg + "\nBirth date required";
		
	}else if(!matchday){
		errmsg = errmsg + "\nValid birth date required";
		
	}
	if (document.getElementById("byear").value=="" || document.getElementById("byear").value=="yyyy")
	{
		errmsg = errmsg + "\nBirth year required";
		
	}else if(!matchyer){
		errmsg = errmsg + "\nValid birth year required";
		
	}
	if(isvaldt){
		errmsg = errmsg + "\nDate of birth required as mm-dd-yyyy";

	}
	//Validate appointment date.
	var isValidappDt=	chkValidapp(appDate);
	if (appDate=="" || appDate=="mm-dd-yyyy")
	{
		errmsg = errmsg +  "\nScheduled appointment date required";
		
		
	}else if(!chkValidapp(appDate)){
		
		errmsg	= errmsg + "\nValid scheduled appointment date required";

	}
	
	
	//alert(appDate);
//	if(!chkValidapp(appDate)){
//		
//		errmsg	= errmsg + "\nValid scheduled appointment date required";
//
//	}
	if (document.getElementById("notifyReps").value=="")
	{
		
		errmsg	= errmsg + "\nScheduled appointment time required";
	}
	var flagchk	= 0;
	var flagreschk = 0;
	var flagcanchk = 0;
	var flagchktime = 0;
	if(cancelapp!=1){
		flagcanchk=1;
	}
		var isValidnewappDt=	chkValidapp(appnewDate);
		if (appnewDate=="" || appnewDate=="mm-dd-yyyy")
		{
			//errmsg = errmsg +  "\nPlease mention new appointment date.";
			flagchk = 1;
		}else if(!isValidnewappDt){
			//errmsg = errmsg +  "\nPlease mention valid new appointment date.";
			flagreschk =1;
		}else{
			flagchktime=1;
		}
		
	
	/*if(flagchk==0){
		errmsg = errmsg +  "\nPlease select cancel or rescheduling appointment option.";
	}*/
	if(flagcanchk==1 && flagchk==1){
		errmsg = errmsg +  "\nCancellation or Rescheduling of appointment required";
	}
	if(flagreschk==1){
		errmsg = errmsg +  "\nValid rescheduled appointment date required";
	}
	if(flagchktime==1){
		if (document.getElementById("notifyReps1").value=="")
		{
			errmsg	= errmsg + "\nRescheduled appointment time required";
		}
	}

	if(errmsg != ""){
		alert(errmsg);
		return false;
	}else{
		
		postdata	=	"patientnm=" + encodeURI(patientnm) + "&birthdate=" + encodeURI(brithdate) + "&appDate=" + encodeURI(appDate) + "&appTime=" + appTime + "&changeDate=" + changeDate + "&changeTime=" + changeTime + "&cancelapp=" + cancelapp;
		//alert(postdata);
		//return false;
		url			=	static_path+"formsubmit/rescheduleappt.php";
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(postdata);
		xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}
			else if (xmlhttp.readyState == 4)
			{
				result	= xmlhttp.responseText;
				//alert(result);
				document.getElementById('sheduleappointmet').innerHTML = result;
				//sample();
			}
		
		}
	}


}
//---------------End of Function for reschedule appointment--------------------------

//--------------Function for cancel appointment form --------------------------------
function appCancel(evt){
	//alert(evt);
	connect();
	var rel;
	var relationship;
	loginurl	=	static_path+"formsubmit/cancelApp.php";
	if(evt=="next"){// code to get next form
		contactnm	= document.contact.contactnm.value;
		if(contactnm==""){
			alert("Contact name required");
			return false;
		}
		
		for(var rcnt=0;rcnt<document.contact.relRadio.length;rcnt++){
			if(document.contact.relRadio[rcnt].checked==true){
				rel	= document.contact.relRadio[rcnt].value;
			}
		}
		if(rel==1){
			relationship	= "Self";
		}else if(rel==2){
			relationship	= document.contact.relationship.value;
			if(relationship==""){
					alert("Relationship with patient required");
					return false;
			}else{
				if(relationship=='other'){
					relationship= document.contact.otherrel.value;
				}
			}

		}
		if(!relationship){
			alert("Relationship with patient required");
			return false;
		}
		phone	=	document.contact.phone.value;
		if(phone==""){
			alert("Phone number required");
			return false;
		}else{
			var validateno = checktelno(phone)
			if(validateno){
				alert("Telephone number required in the format xxx-xxx-xxxx");
				return false;
			}

		}

		email	=	document.contact.email.value;
		if(email==""){
			alert("Email address required");
			return false;
		}else{
			if(!remail.test(email)){
				alert("Valid email address required");
				return false;
			}
		}
		var postdata	= "contactnm=" + encodeURI(contactnm) + "&relationship=" + encodeURI(relationship) + "&phone=" + encodeURI(phone) + "&email=" + encodeURI(email) + "&actiontype=" + evt;
		//Ajax code
		
		xmlhttp.open("POST", loginurl, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.send(postdata);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				var result	= xmlhttp.responseText;
				
				document.getElementById('sheduleappointmet').innerHTML = result;
				sample2();
			}
		}
	}

	if (evt=='back') //code to get prevous form.
	{
		parameter = "?actiontype=" + evt;
		xmlhttp.open('GET', loginurl +parameter, true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function()
		{		 
				if(xmlhttp.readyState == 1){
						document.getElementById('sheduleappointmet').innerHTML = "Loading.....";
				}

		 else if (xmlhttp.readyState == 4) 
		  {			  
			 if (xmlhttp.status == 200) 
			 { 
				 var result=xmlhttp.responseText;
				 //alert(result);
				 //return false;
				 document.getElementById('sheduleappointmet').innerHTML = result;
				 //sample();
				 /*SelectSubCat('newdate','newtime');
				 var seltim		=	document.frmAppointment.notifyReps.value;
				 var backtime	=	seltim.split(",");
				 for(var t=0; t<backtime.length; t++){
				 //alert(backtime[t]);
					notifySelect(backtime[t]);
				 }*/
				SelectSubCat('appointmentdate','time');
				var seltim		=	document.frmAppointment.notifyReps.value;
				//alert(seltim);
				 var backtime	=	seltim.split(",");
				 for(var t=0; t<backtime.length; t++){
					//alert(backtime[t]);
					notifySelect(backtime[t]);
				 }
				SelectSubCat('newdate','newtime');
				 var seltim1		=	document.frmAppointment.notifyReps1.value;
				 //alert(seltim1);
				 var backtime1	=	seltim1.split(",");
				 for(var t1=0; t1<backtime1.length; t1++){
					 //alert(backtime[t]);
					notifySelect1(backtime1[t1]);
				 }
				 //alert(result);

			 }
			 else 
			 {
				alert('There was a problem with the request.');
			 }
		  }
		}
		
	}
	
}
//--------------End of Function for cancel appointment form --------------------------------

//--------------Function for reset appointment ---------------------------------------------
function resetapp(){
	//alert("here");
	var preload = document.getElementById("sheduleappointmet");
	connect();
	var postdata	= "";
	var loginurl	= static_path + "formsubmit/resetApp.php";
		//Ajax code
		xmlhttp.open('GET' , loginurl , true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==3)
					 {
						preload.innerHTML = "loading data..";
					}
			else if(xmlhttp.readyState==4){
				var result	= xmlhttp.responseText;
				//alert(result);
				document.getElementById('sheduleappointmet').innerHTML = result;
				//sample();
			}
		}
}
//--------------End of Function for reset appointment ---------------------------------------------

//---------------Function to get patient name for contact name field. -----------------------------
function getContactnm(){
	connect();
	var postdata	= "";
	var loginurl	= static_path+"formsubmit/getcontactnm.php";
		//Ajax code
		xmlhttp.open('GET', loginurl , true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				var result	= xmlhttp.responseText;
				//alert(result);
				if(result){
					document.contact.contactnm.value=result;
				}
			}
		}

}
//---------------End of Function to get patient name for contact name field. ----------------------