
j(document).ready(function()
{
	
try
{
	urlOriginal=j("#original_url").val();

	if (urlOriginal.indexOf("both")>0)
		typx="both";
	else if (urlOriginal.indexOf("appt")>0)
		typx="appt";
	else if (urlOriginal.indexOf("calls")>0)
		typx="calls";
	else if (urlOriginal.indexOf("responder")>0)
		typx="responder";
	else if (urlOriginal.indexOf("datas")>0)
		typx="datas";

	j("#topmenu_"+typx).addClass("unselectedMenu");
	j("#topmenu_"+typx).addClass("selectedMenu");
	
}
catch (e)
{
}




	j("#dateTimeDiv").hide();

	j(function() {
		j("#tabs").tabs();
	});




	j("#advancedSearch").bind('click', function()
	{
		if (j('#advancedSearchDiv').is(':visible'))
		{
			j("#advancedSearchDiv").hide();
			j("#advancedSearchDiv").html("");
		}
		else
		{
			urlLoad="/leads/calls/ajax/m:get_search";
			j("#advancedSearchDiv").show();
			j("#advancedSearchDiv").load(urlLoad);
			
		}
		
	
	});



	j("#searchButton").bind('click', function()
	{
		searchTxt=j.trim(j('#searchTextBox').val());
		urlx="/leads/datas/index/0/search:"+searchTxt;
		if (searchTxt.length>=3)
			document.location=urlx;
		else
		{
			alert("Please enter at least 3 characters!")
			j('#searchTextBox').focus();
		}

	});

	j("#zipButton").bind('click', function()
	{
		zipval=j('#zipCodeLocal').val();
		//defval=j('input:radio[id=makeDefaultLocal]:checked').val();
		defCheck=document.getElementById('makeDefaultLocal').checked;
		defval=(defCheck)?1:0;
		urly=j("#original_url").val()+"/zip:"+zipval+":"+defval;
		document.location=urly;
		
	});

	j("#dateTimeShow").bind('click', function()
	{
		if (j('#dateTimeDiv').is(':visible'))
		{
			j("#dateTimeDiv").hide();
		}
		else
		{
			j("#dateTimeDiv").show();
		}


	});

	j("#selectFormsHeader").bind('click', function()
	{
		if (j('#selectForms').is(':visible'))
		{
			j("#selectForms").hide("slow");
			//j("#selectFormsHeader").html("Show Forms");
			//j("#selectFormsHeader").css("position","relative");
		}
		else
		{
			j("#selectForms").show("slow");
			//j("#selectFormsHeader").html("Hide Forms");
			//j("#selectFormsHeader").css("position","absolute");
		}


	});


	j("#searchComboList").bind('change', function()
	{
		selectItem=j("#searchComboList").val();		
		if (selectItem=="status")
		{
			j("#statusComboList").show();
			j("#searchTextBox").hide();
		}
		else
		{
			j("#statusComboList").hide();
			j("#searchTextBox").show();
		}
	});

	j("#filterButton").bind('click', function()
	{
		var nosubmit=0;
		startD=j("#startDate").val()
		endD=j("#endDate").val()
		filterTyp=j('#filter_type').val()
		filterScr=j('#filter_screen').val()

		if (!filterTyp)
		{
			filterTyp=filterScr;
			pageAddr=j("#pageUrl").val();
			if (nosubmit==0)
			{
				if (!startD && !endD)
					document.location="/leads/"+pageAddr+"/filter:"+filterTyp;
				else if (startD && endD)
					document.location="/leads/"+pageAddr+"/filter:"+filterTyp+"/"+startD+"/"+endD;
				else
				{
					if (startD)
						document.location="/leads/"+pageAddr+"/filter:"+filterTyp+"/"+startD+"/0";
					else
						document.location="/leads/"+pageAddr+"/filter:"+filterTyp+"/0/"+endD;
					
				}
				
			}

		
		}
		else
		{
			urlx=j("#url_base").val()+"&type="+filterTyp;
			window.open(urlx);


		}

			

	});


try
{
	

	//gmap functions
	gmapurl=j("#gmapUrl").val()
	dateAddNo="+"+j("#selectedDateAdd").val();

	j.ajax({ 
		type: "GET",
		url:  gmapurl,
		data: "",
		cache: false,
		async: false,
		success : function(data) { returnData=data;},
		complete : function() 
		{
			//alert(returnData)
			j("#gmapUrl").html(returnData);
		}
		});	

	j.datepicker.setDefaults({
	dateFormat : "yy-mm-dd"
	,defaultDate: dateAddNo
	});
	
	
	j(function() {

		
		j("#startDate").datepicker({
			maxDate: '0M 0D',
			firstDay: 1,
			numberOfMonths: 1
		});
		j("#endDate").datepicker({
			maxDate: '0M 0D',
			firstDay: 1,
			numberOfMonths: 1
		});



		j("#selectedDay").datepicker({
			maxDate: '12M 0D',
			minDate: '0M 0D',
			firstDay: 1,
			onSelect: function(dateText, inst) {changeApptDate(dateText) },
			numberOfMonths: 1
		});



	});
} catch (e) { }


	j("#apptSubmitButton").bind('click', function()
	{
		apptTime=j('input:checkbox[id=apptimes]:checked').val()
			
			try
			{
				if (apptTime>0)
				{
					;
				}
				else
				{
					;
					//alert("You must check appointment times!");
					//return false;
				}
	
			}
			catch (e) {	}

			j("#apptTimeForm").submit();

	});


});


function apptChangeButton(typx)
{
	dateText=j("#selectApptDate").val();
	start=j("#start").val();
	endt=j("#endt").val();
	formId=j("#formList").val();
	endt=j("#endt").val();
	start_day=j("#startDay").val();
	show_day=j("#showDay").val();
	applyType=j('input:radio[id=applyType]:checked').val()
	params=start+"-"+endt+","+start_day+","+show_day+","+applyType
	urlajax="/leads/calls/ajax/m:appt_save/params:"+params;
	//j("body").append(urlajax);
	if (typx==1)
	{
		//alert(urlajax)
		j.ajax({ 
		type: "GET",
		url:  urlajax,
		data: "",
		cache: false,
		async: false,
		success : function(data) { returnData=data;},
		complete : function() { } });	
		
		urlx="/leads/datas/appt/";
	}
	else
		urlx="/leads/datas/appt/appt:"+dateText+"/form:"+formId;

	
	
	//alert(urlx)
	location.href=urlx	


}

function changeApptDate(dateText)
{
	
	urlAjax="/leads/calls/ajax/m:get_week/date:"+dateText;
	j.ajax({ 
		type: "GET",
		url:  urlAjax,
		data: "",
		cache: false,
		async: false,
		success : function(data) {returnData=data;},
		complete : function() {	}
		});	
	if (returnData==0 || returnData==6)
	{
		alert("You can not select weekends")
		dateText=j("#selectApptDate").val();
	}


	formId=j("#formList").val();

	urlx="/leads/datas/appt/appt:"+dateText+"/form:"+formId;
	location.href=urlx

}

function checkAjaxFb(urlx)
{
	alert(urlx)
	j.ajax({ 
		type: "GET",
		url:  urlx,
		data: "",
		cache: false,
		async: false,
		success : function(data) {returnData=data;},
		complete : function() 
		{
			
		}
		});	

}

function gridCellEditNew(idSpan,idNo,width,fname,whereVal)
{
	spanId=idSpan+idNo;
	inputId=spanId+"Input";
	var spanObj=j("#"+spanId);
	var obj=j("#"+inputId);
	descValue=spanObj.html();
	chkVal=obj.attr("id");
	if (!chkVal)
	{
		submitBtn="<br><nobr><img src='/leads/img/yes.gif' title='OK' align=absmiddle class='ok_button' onclick=gridCellEditUpdate('"+idSpan+"','"+idNo+"','"+fname+"','"+whereVal+"','0')>";
		submitBtn+="&nbsp;<img src='/leads/img/no.gif' title='CLOSE' align=absmiddle class='close_button' onclick=gridCellEditUpdate('"+idSpan+"','"+idNo+"','"+fname+"','"+whereVal+"','1')>"
		submitBtn+="</nobr>";
		spanObj.html("<textarea cols="+width+" rows=3 name="+inputId+" id="+inputId+"  class=innerInput>"+descValue+"</textarea>"+submitBtn);
		j("#"+inputId).select();
	}
}


function gridCellEditUpdate(idSpan,idNo,fname,whereVal,closeS,tableName)
{
	spanId=idSpan+idNo;
	inputId=spanId+"Input";
	var spanObj=j("#"+spanId);
	var obj=j("#"+inputId);
	chkVal=obj.attr("id");

	if (chkVal && closeS!=1)
	{
		newVal=obj.val();
		urlxx="/leads/calls/comment_update/"+tableName+"|"+fname+"|"+newVal+"|"+whereVal
		j.get(urlxx);
		spanObj.html(newVal);		
	}
	else if (closeS==1)
	{
		newVal=obj.val();
		spanObj.html(newVal);		
	}

}

function changeReportType(typ)
{
	switch (typ)
	{
		case "both": 
			location.href="/leads/datas/index/both/filter:report"
		break;
		case "data": 
			location.href="/leads/datas/index/0/filter:report"
		break;
		case "call": 
			location.href="/leads/calls/index/0/filter:report"
		break;
	}

}

function closeAdvancedSearch()
{
	j("#advancedSearchDiv").hide();
	j("#advancedSearchDiv").html("");

}

function loadAdvancedDates()
{

}
