var catName = '';
var startX, socialSliderVal, distanceSliderVal;
var currentCity = "";
var currentLL = "";



//var ckSocial = getCookie("BB_SS");
//var ckDist = getCookie("BB_DS");
//var defaultDistanceSetting = 1;
//var hiddenIFrameHeight = 0;
//获取还原点的ID号
function setpid(pid)
{
	document.getElementById("pid").value=pid;
}
function usaDateTime(strDate)
{
		var stime=strDate.match(/[012]{0,1}[\d]{1}\:[\d]{1,2}\:[\d]{1,2}/);
		//var tdate=sdate[0];
		var ttime=stime[0];
		var dArr=new Array();
		var tArr=new Array();
		var returnvalue;
		tArr=ttime.split(":");
		var returnvalue="";
		for(var i=0;i<tArr.length;i++)
		{
		   if(tArr[i]<10)
		  {
		     returnvalue+="0"+tArr[i]+":";
		  } 
		  else
		  {
		      returnvalue+=tArr[i]+":";
		  }
		}
//			if (tArr[0]*1<12) 
//			{
//				returnvalue=tArr[0]+":"+tArr[1]+":"+tArr[2];//+" AM";
//			}
//			else
//			{
//				returnvalue=tArr[0]+":"+tArr[1]+":"+tArr[2];//+" PM";	
//			}
		returnvalue=returnvalue.substring(0,returnvalue.length-1);
		//alert(returnvalue);
		return returnvalue;
}

////--- This part needs to be snipped out into a resource folder
var llastobj=null;
//change style when click
function test_b(obj)
{
	//alert(obj);
	try
	{
		if(llastobj==null)
		{
			document.getElementById(obj).className="selectedList";
			//obj.style.whiteSpace="nowrap";
			
		}
		else
		{
			//alert(lastobj.innerHTML);
			llastobj.className="noselectedList";
			//llastobj.style.whiteSpace="nowrap";
			document.getElementById(obj).className="selectedList";
			//llastobj.firstChild.innerHTML="<image src='images/recy1.gif' />"
			//obj.style.whiteSpace="nowrap";
		}
		//alert(document.getElementById(obj).innerHTML);
		//document.getElementById(obj).firstChild.innerHTML="<image src='images/recy.gif' />";
		var temp = obj.substring(2,obj.length);
		//alert(temp);
		setpid(temp);
	}
	catch(e)
	{
		//alert(e);
	}
	llastobj=document.getElementById(obj);
	
}
//color when mouseover

var calendar_DayNames = getWeekDayNameFromLanguage(langType)
var calendar_MonthNames = getMonthNameFromLanguage(langType)
//var socArray = new Array("Messenger Buddies Only", "E-mail Groups Only", "Buddies and E-mail Groups", "Everyone");
//var dstArray = new Array(25,50,75,100,150,250,500,-1);
//var dstTextArray = new Array(" +25 miles"," +50 miles"," +75 miles"," +100 miles"," +150 miles"," +250 miles"," +500 miles","United States"); 
function setcb(dDay)
{
	if(document.getElementById(dDay)!=null)
	{
	test(document.getElementById(dDay));
	}
}
var keytargetID;
var keytargetURL;
var lkeydMonth;
var lkeydYear;
var lkeydDay;
var rkeydMonth;
var rkeydYear;
var rkeydDay;
var maxid;
var keyarray; 
var llcd;
var rrcd;
var ddMonth;
var ddYear;
function showCalendar1(targetID, targetURL, dMonth, dYear, dDay)
{
	//dDay=getlastDate(dYear,dMonth);
	if(dDay==90)
	{
		dDay=1;
	
	}
	else
	{
        setcb(dDay);
	}
	keytargetID=targetID;
   keytargetURL=targetURL;
	var thisDate=new Date();
	var lastDay=31;
	var leftListType="";
	var testDate=new Date(dYear,dMonth,31)
	var firstDayOfTheMonth=new Date(dYear,dMonth,1)
	testDate.setMonth(firstDayOfTheMonth.getMonth())
	testDate.setDate(lastDay);
     
	
	//showCalendar("myok", targetURL, dMonth, dYear, dDay,"1");
	while(testDate.getMonth()!=firstDayOfTheMonth.getMonth())
	{
		lastDay--;
		testDate.setMonth(firstDayOfTheMonth.getMonth())
		testDate.setDate(lastDay);
	}
	//alert(testDate);//total day of the month
	var firstDateDay=firstDayOfTheMonth.getDay();
	var lineCount=Math.floor((lastDay+firstDateDay-1)/7)+1  //Get the month calendar line number
	if (lineCount==5)
	{
		leftListType=("short");//Get the size of IBP list table
	}
	
	
	document.getElementById("pid").value=""
	//document.getElementById('myok1').height=document.getElementById('myok').height;
	//alert(document.getElementById('myok').height);
	
	var permonth=dMonth;
	var nextmonth=dMonth;
	var pnewyear=dYear;
	var nnewyear=dYear;
	var pday=getperydate(dYear,dMonth,dDay);
	var nday=getnextdate(dYear,dMonth,dDay);
	
	//var okd = dYear+"-"+(dMonth+1)+"-"+dDay;
	//var nobj=document.getElementById(dDay);
	if(pday==false)
	{
		
	    if(getperyMonth(dYear,dMonth)!=false)
		{
	    permonth=getperyMonth(dYear,dMonth);
		//alert(permonth);
		
		pday=getlastDate(dYear,permonth);
		
		//showCalendar("myok","123.htm", permonth, newyear, pday,"1")
		}
		else{
			
		  if(getperYear(dYear)!=false)
		  {
		    pnewyear=getperYear(dYear);
			permonth=getlsatmonth(pnewyear);
			pday=getlastDate(pnewyear,permonth);
			//showCalendar("myok","123.htm", permonth, newyear, pday,"1")
		  }
		  else{
		  pday=dDay;
		 
		  }
		}
		
	}
	
	if(nday==false)
	{
		
	   if(getNextMonth(dYear,dMonth)!=false)
		{
	    nextmonth=getNextMonth(dYear,dMonth);
		//alert(nextmonth);
		nday=getfirstDate(dYear,nextmonth);
		//showCalendar("myok","123.htm", nextmonth, newyear, nday,"1")
		}
		else{
		  if(getNextyear(dYear)!=false)
		  {
		    nnewyear=getNextyear(dYear);
			nextmonth=getfirstmonth(nnewyear);
			nday=getfirstDate(nnewyear,nextmonth);
			//showCalendar("myok","123.htm", nextmonth, newyear, nday,"1")
			
		  }
		  else{
		  nday=dDay;
		   
		  }
		}
	}
	
	if (lastDay>thisDate.getDate())
	{
		if (dDay=="") dDay=thisDate.getDate();
		if (pday=="") pday=thisDate.getDate();
		if (nday=="") nday=thisDate.getDate();
	}
	else
	{
		if (dDay=="") dDay=lastDay;
		if (pday=="") pday=lastDay;
		if (nday=="") nday=lastDay;	
	}
    if(dMonth < 0)
    {       
        dMonth = 11;
        dYear--;
    }
    if(dMonth > 11)
    {
        dMonth = 0;
        dYear++;
    }
    var xq = new Date(dYear,dMonth,dDay);
    var dNow = new Date();
    if(dDay == 90 && dMonth == dNow.getMonth() && dYear == dNow.getYear()) dDay = dNow.getDate();
    var dCurFD = new Date(dYear, dMonth, 1);
    var dGivenDate = new Date(dYear, dMonth, dDay);  
    var dCurrentMonthFirstSunday = dCurFD.getDay();
    var dCurrentViewDate = new Date(dCurFD);
    var iDays = 0;
    var weekMilliseconds = 1000*60*60*24*7;
    if(dCurrentMonthFirstSunday > 0) iDays = -dCurrentMonthFirstSunday;
    
    var fTrgt = targetURL;
    while (fTrgt.indexOf("'") >= 0) fTrgt = fTrgt.replace("'", "`");
    while (fTrgt.indexOf("`") >= 0) fTrgt = fTrgt.replace("`", "\\'");
	 lkeydYear=pnewyear;
	 lkeydDay=pday;
	 lkeydMonth=permonth;
	 rkeydDay=nday;
	 rkeydMonth=nextmonth;
	 rkeydYear=nnewyear;
	 ddMonth=dMonth;
	 ddYear=dYear;
    var oBuf = '<TABLE BORDER="0" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" style="border:none;background:#ECF2F7;white-space:nowrap;">';       
    oBuf += '<TR><td></td><TD CLASS="calendarhd" border=0><A id="lkey" onkeydown="keySup();" href="#" onclick="javascript:showCalendar1(\'' + targetID + "','" + fTrgt + "'," + (permonth) + ',' + pnewyear + ','+pday+');Setfocus(0);';
	if(permonth!=dMonth || pnewyear!=dYear)
	{
		llcd=true;
		oBuf+="showCalendar('myok','"+ fTrgt + "'," + (permonth) + ',' + pnewyear + ','+pday+',1);';
		
	}
	else
	{
		llcd=false;
	}
	oBuf+='"><img src="pic/leftArrow.jpg" /></A></TD>';
	//alert(oBuf);
    oBuf += '<TD COLSPAN="3" CLASS="calendarhd" style="overflow:hidden;">' +formatDayStringByLanguage(dMonth,dYear,dDay,calendar_DayNames[xq.getDay()],langType)+ '</TD>';
    oBuf += '<TD CLASS="calendarhd" border=0><A id="rkey" onkeydown="keySup();" href="#" onclick="javascript:showCalendar1(\'' + targetID + "','" + fTrgt + "'," + (nextmonth) + ',' + nnewyear + ','+nday+');Setfocus(1);';
	if(nextmonth!=dMonth || nnewyear!=dYear)
	{
		oBuf+="showCalendar('myok','"+ fTrgt + "'," + (nextmonth) + ',' + nnewyear + ','+nday+',1);';
		rrcd=true;
	}
	else
	{
		rrcd=false;
	}
	oBuf+='"><img src="pic/rightArrow.jpg" /></A></TD><td></td></TR></table>';
	oBuf +='<span style="padding-left:5px;overflow:auto;width:230px;height:';
	if (leftListType=="short")
	{
		oBuf +="173";
	}
	else
	{
		oBuf +="203";
	}
	oBuf +='px;" id="ibpListSpan"><TABLE BORDER="0" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" style="border:none;background:#ECF2F7;white-space:nowrap;">'
  //oBuf+='<tr><TD COLSPAN="7"><select name="select" size="'+statetmp+'">';
  var i=pointArray.length-1;
  var j=0;
  var undoLogoHTML="";
  while(i>-1)
  {
   var sobj=pointArray[i];
   
   var newmydate=sobj.Date;
   var testdate = usaDateFormat(newmydate);
   var stime=usaDateTime(newmydate);
   var timeSplitArr=stime.split(":");
   if (timeSplitArr.length!=3)
   {
	   	i--;
		continue;   
   }
   else
   {
		var timeString12=timeFormat12(timeSplitArr)
   }
   //alert(testdate.getFullYear()+"\n"+testdate.getMonth()+"\n"+testdate.getDate());
   //alert(_year+"\n"+_month+"\n"+_date);
   if(testdate.getFullYear()==dYear)
   {
	   if(testdate.getMonth()==dMonth)
	   {
		   if(testdate.getDate()==dDay)
		   {
			   if(j==0)
               {
	               maxid=sobj.ID;
				   keyarray= new Array();
			   }
			   keyarray[j]=sobj.ID;
			   j++;
			   //alert(sobj.IsUndo);
			   if(parseInt(sobj.IsUndo))
			   {
					undoLogoHTML="<div style='padding-right:3px; float:left;'><image src='images/recy1.gif' /></div>";
			   }
			   else
			   {
					undoLogoHTML="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			   }
			   //oBuf+='<option value="'+sobj.ID+'">'+stime+'  '+sobj.Describe+'</option>';
			  	oBuf+='<tr style="border-left:1px solid #000000;"><TD onkeydown="keySup(\''+sobj.ID+'\');" nowrap COLSPAN="7" id="la'+sobj.ID+'" onClick="test_b(\'la'+sobj.ID+'\');" onDblClick="openwin()" style="cursor:hand;color=#000000;white-space:nowrap;" align="left"><span style="white-space:nowrap;">'+undoLogoHTML+timeString12+'&nbsp;&nbsp;'+sobj.Describe+'</span></td></tr>';	  
				//oBuf+='<tr style="border-left:1px solid #000000;"><TD  nowrap COLSPAN="7" id="\"'+sobj.ID+'\"" onClick="test_b(\"'+sobj.ID+'\");setpid(\''+sobj.ID+'\');" onDblClick="openwin()" style="cursor:hand;color=#000000;white-space:nowrap;" align="left"><span style="white-space:nowrap;">'+timeString12+'&nbsp;&nbsp;'+sobj.Describe+'</span></td></tr>';	
			//alert(oBuf);
		   }
	   }
   }
   
   i--;
 }
    oBuf += '</TABLE></span>';
    document.getElementById(targetID).innerHTML = oBuf;
}

function Setfocus(id)
{
	if(id==0)
	{
		document.getElementById("lkey").focus();
	}
	if(id==1)
	{
		document.getElementById("rkey").focus();
	}
}
function getnextid(_value)
{
	//alert(keyarray.length);
	for(var i=0;i<keyarray.length;i++)
	{
		if(keyarray[i]==_value)
		{
			//alert(_value+"+++^^^^^^"+keyarray[i+1]);
			if(i+1<=keyarray.length)
			{
				return keyarray[i+1];
			}
			else
			{
				return keyarray[keyarray.length-1];
			}
		}
		//else
//		{
//			return keyarray[keyarray.length-1];
//		}
//alert(keyarray[i]);
	}
}
function getperid(_value)
{
	for(var i=keyarray.length-1;i>-1;i--)
	{
		if(keyarray[i]==_value)
		{
			if(i-1>=0)
			{
				//alert(_value+"^^^^^^"+keyarray[i-1]);
				return keyarray[i-1];
			}
			else
			{
				return keyarray[0];
			}
		}
		//else
//		{
//			return keyarray[0];
//		}
	}
}

function keySup(id)
{
	var key=event.keyCode;
	if(key==37)
	{
		//showCalendar1(keytargetID,keytargetURL,lkeydMonth,lkeydYear,lkeydDay);
//		//alert(lkeydMonth+"^^^^"+ddMonth);
//		var pnewyear=getperYear(lkeydYear);
//		var permonth=getperyMonth(lkeydYear,lkeydMonth);
//		if(permonth!=ddMonth || pnewyear!=ddYear)
//		{
//			showCalendar('myok',keytargetURL,lkeydMonth,lkeydYear,lkeydDay,1);
//		}
        document.getElementById("lkey").click();
		document.getElementById("lkey").focus();
	}
	else if(key==39)
	{
		//showCalendar1(keytargetID,keytargetURL,rkeydMonth,rkeydYear,rkeydDay);
//		//alert(rkeydMonth+"^^^^"+ddMonth);
//		if(rkeydMonth!=ddMonth || rkeydYear!=ddYear)
//		{
//			//showCalendar('myok',keytargetURL,rkeydMonth,rkeydYear,rkeydDay,1);
//		}
        document.getElementById("rkey").click();
		document.getElementById("rkey").focus();
	}
	else if(key==38)
	{
		if(id==null)
		{
			//document.getElementById(maxid).focus();
			test_b(maxid)
		}
		else
		{
			var temp =  getperid(id)//parseInt(id,10)+1;
			//alert(id+"----------"+temp);
			try
			{
				test_b("la"+temp+"");
				document.getElementById("la"+temp+"").focus();
			}
			catch(e)
			{
				temp=keyarray[0];//getperid(id+1);
				document.getElementById("la"+temp+"").focus();
				test_b("la"+temp+"");
			}
		}
	}
	else if(key==40)
	{
		//alert(id);
		if(id==null)
		{
			document.getElementById("la"+maxid).focus();
			test_b("la"+maxid);
			//alert(maxid);
		}
		else
		{
			
			var temp =  getnextid(id)//parseInt(id,10)-1;
			//alert(id+"----------"+temp);
			try
			{
				test_b("la"+temp+"");
				document.getElementById("la"+temp+"").focus();
			}
			catch(e)
			{
				temp=keyarray[keyarray.length-1];//getnextid(id-1);
				//alert(temp);
				//alert(keyarray.length);
				document.getElementById("la"+temp+"").focus();
				test_b("la"+temp+"");
			}
		}
	}
	else if(key==13 || key==32)
	{
		if(document.getElementById("pid").value!="")
		{
			openwin();
		}
	}
	
}
function timeFormat12(arr)
{
	arr[0]=parseInt(arr[0],10);
	arr[1]=parseInt(arr[1],10);
	arr[2]=parseInt(arr[2],10);
	
	var temp=arr[0];
	var isAmPm=arr[0]>=12?GS("Time_format_PM"):GS("Time_format_AM");
	var hour=temp+"";
	if(arr[0]==0)
	{
		hour=12;
	}
	else
	{
		if(temp>12)
		{
			hour=temp-12+"";
			temp=temp-12;
		}
		if(temp<10)
		{
			hour="0"+temp;
		}
	}
	
	var minutes=arr[1]+"";
	if(arr[1]<10)
	{
		minutes="0"+arr[1];
	}
	var seconds=arr[2]+"";
	if(arr[2]<10)
	{
		seconds="0"+arr[2];
	}
	//return hour+":"+minutes+":"+seconds+" "+isAmPm;
	return isAmPm.replace(/\{\$time\_str\}/g,hour+":"+minutes+":"+seconds+" ");
}
