
//Function to get time drop down values.
function SelectSubCat(seldate,timenm){
	// ON selection of category this function will work
	var i;
	var j;
	var k,min,ap;
	min="00";
	//seldt = document.frmAppointment.appointmentdate.value;
	seldt = eval("document.frmAppointment." + seldate + ".value");
	month = seldt.substring(0,2);
	day = seldt.substring(3,5);
	year = (seldt.substring(6,10));
	dt = new Date(year,month-1,day);
	day = dt.getDay();
	/*
	AJAX Code for fetching half day time interval
	*/
	connect();
	loginurl=static_path+"includes/get_holiday_time.php";
	var poststr = "patientName=" + seldt;
	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==4)
		{
			var result=xmlhttp.responseText;
			var fldtimenm = eval("document.frmAppointment." + timenm);
			if(result == '')
			{
				
				if(day==0)
				{ //For weekdays
					alert("Its a sunday, please select another day");
					removeAllOptions(fldtimenm);
					addOption(fldtimenm, "", "Select Time", "");
					if(fldtimenm.name=='time')
					{
						document.frmAppointment.notifyReps.value = "";
					}
					if(fldtimenm.name=='newtime')
					{
						document.frmAppointment.notifyReps1.value = "";
					}
				}
				else
				{
					removeAllOptions(fldtimenm);
					addOption(fldtimenm, "", "Select Time", "");
					timeopt = new Array();
					timeopt	= eval("arrTimeList" + day);
					for(t=0;t<timeopt.length;t++)
					{
						addOption(fldtimenm,timeopt[t], timeopt[t]);
					}
				}
			}
			else
			{
				removeAllOptions(fldtimenm);
				addOption(fldtimenm, "", "Select Time", "");
				addOption(fldtimenm, "Anytime", "Anytime");
				var dates = result.split("#");
				var starttime = dates[0].split(" ");
				var endtime = dates[1].split(" ");
				var timeArr = new Array();
				if(starttime[1] == endtime[1])
				{
					if(starttime[1] == "AM")
					{
						hourStart = starttime[0].split(":");
						hourEnd = endtime[0].split(":");
						for(sths = Number(hourStart[0]);sths<= Number(hourEnd[0]); sths++)
						{
							if(sths  == 12)
								timeArr.push(sths+":00 Noon");
							else
								timeArr.push(sths+":00 AM");
						}
					}
					else
					{
						hourStart = starttime[0].split(":");
						hourEnd = endtime[0].split(":");
						for(sth = Number(hourStart[0]);sth <= Number(hourEnd[0]);sth++)
						{	
							timeArr.push(sth+":00 PM");
						}
					}
					for(ti=0;ti<timeArr.length;ti++)
					{
						addOption(fldtimenm,timeArr[ti], timeArr[ti]);
					}
				}
				else
				{
					if(starttime[1] == "AM")
					{
						hourStart = starttime[0].split(":");
						for(sth = hourStart[0];sth<=12;sth++)
						{
							if(sth == 12)
								timeArr.push(sth+":00 Noon");
							else
								timeArr.push(sth+":00 AM");
						}
					}
					if(endtime[1] == "PM")
					{
						hourEnd = endtime[0].split(":");
						for(eth = 1; eth <= hourEnd[0];eth++)
						{
							timeArr.push(eth+":00 PM");
						}
					}
					for(ti=0;ti<timeArr.length;ti++)
					{
						addOption(fldtimenm,timeArr[ti], timeArr[ti]);
					}
				}
			}
		}

	/*
	AJAX code end here
	*/
	}
}
//Function to remove the drop down options.
function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text ) //Fill up the time drop down
{
	var optn	=	document.createElement("OPTION");
	optn.text	=	text;
	optn.value	=	value;
	selectbox.options.add(optn);
}

//New code to apply multiple selection on the drop down
var selChr = "+";
function notifySelect(RepSelected)
{
	//alert(RepSelected);
	var selidx=0;
	var multidropdown = getObject("frmAppointment");
	if (RepSelected == "click to view reps") return false;
	var rep_str ="";
	for (i = 0; i < multidropdown.time.options.length; i++) 
	{
		if (multidropdown.time.options[i].value != '') 
		{
		   var dropdownItem = multidropdown.time.options[i].text;
		   if (dropdownItem == RepSelected || dropdownItem == selChr+RepSelected)
			{
			   if (dropdownItem.substring(0,1) == selChr) 
				{
					document.frmAppointment.time.options[i].text = RepSelected.substring(1);
				}
				else {
					document.frmAppointment.time.options[i].text = selChr + RepSelected;
				}
			}
			dropdownItem = multidropdown.time.options[i].text;
			if(dropdownItem == (selChr + RepSelected)){
				selidx	=	i;
			}
			if (dropdownItem.substring(0,1) == selChr)
			{
				rep_str = rep_str + multidropdown.time.options[i].value + ",";
				multidropdown.time.options[i].style.backgroundColor = "#FF3500";
				multidropdown.time.options[i].style.color = "#FFFFFF";
			}
			else {
				multidropdown.time.options[i].style.backgroundColor = "#FFFFFF";
				multidropdown.time.options[i].style.color = "#000000";
			}
		}
	}
 //end of search
	//alert(selidx);
	multidropdown.time.options[selidx].selected = true;
	document.frmAppointment.notifyReps.value = rep_str;  
}




function notifySelect1(RepSelected)
{
	//alert(RepSelected);
	var multidropdown = getObject("frmAppointment");
	if (RepSelected == "click to view reps") return false;
	var rep_str ="";
	var rselidx = 0;
	for (i = 0; i < multidropdown.newtime.options.length; i++) 
	{
		if (multidropdown.newtime.options[i].value != '') 
		{
			//alert(multidropdown.newtime.options[i].value);
		   var dropdownItem = multidropdown.newtime.options[i].text;
		   if (dropdownItem == RepSelected || dropdownItem == selChr+RepSelected)
			{
			   if (dropdownItem.substring(0,1) == selChr) 
				{
					document.frmAppointment.newtime.options[i].text = RepSelected.substring(1);
				}
				else {
					document.frmAppointment.newtime.options[i].text = selChr + RepSelected;
				}
			}
			dropdownItem = multidropdown.newtime.options[i].text;
			if(dropdownItem == (selChr + RepSelected)){
				rselidx	=	i;
			}
			if (dropdownItem.substring(0,1) == selChr)
			{
				rep_str = rep_str + multidropdown.newtime.options[i].value + ",";
				multidropdown.newtime.options[i].style.backgroundColor = "#FF3500";
				multidropdown.newtime.options[i].style.color = "#FFFFFF";
			}
			else {
				multidropdown.newtime.options[i].style.backgroundColor = "#FFFFFF";
				multidropdown.newtime.options[i].style.color = "#000000";
			}
		}
	}
 //end of search
//	multidropdown.newtime.options[0].selected = true;
	multidropdown.newtime.options[rselidx].selected = true;
	document.frmAppointment.notifyReps1.value = rep_str;  
}


//needed for mozilla compatibility
function getObject(objectId) 
{
	if (document.all && !document.getElementById) 
		return document.all(objectId);
	else 
		return document.getElementById(objectId);
}