var LinePrice,LineChildrenPrice,LineLineSpecialPrice,LineStartDate,StartDateType=0,SelectLength=0;
//价格,儿童价格,特殊价格,发团时间,特殊价格类型
$(function(){
	SelectLength=10;//菜单长度
	if('天天发团'!=LineStartDate)
	{
		var Temp=LineStartDate+','; 
		if(!Temp.match(new RegExp('^(((0?[1-9])|(1[12]))-((0?[1-9])|([0-2][0-9])|(3[0-1])),)+$','i'))==false)
		{
			StartDateType = 1;
		}else if(!Temp.match(new RegExp('^([一二三四五六日],)+$','i'))==false){
			StartDateType = 2;
		}else if(!Temp.match(new RegExp('^(((0?[1-9])|([0-2][0-9])|(3[01])),)+$','i'))==false){
			StartDateType = 3
		}else{
			alert('发团时间格式错误！');return false;
		}
	}
	var D = new Date();
	D=new Date(Date.parse(D.getFullYear()+'/'+(D.getMonth()+1)+'/'+D.getDate())+86400000);
	CreateCalendar(D.getFullYear(),D.getMonth()+1);
	SelectPrice();
});
function CreateCalendar(Y,M)
{
	var td,Stop,S,D;
	td = $('tr:gt(1)','#Month_1').find('td');
	$(td).unbind().html('&nbsp;<br />&nbsp;').css('color','#CECFCE').attr('name','').attr('Price','').attr('ChildrenPrice','');
	S = (new Date(Y+'/'+M+'/1')).getDay()-1;
	Stop = (new Date(Y+'/'+(M+1)+'/0')).getDate();
	for(var i=1;i<=Stop;i++)
	{
		$(td[i+S]).html(i + '<br />');
		$(td[i+S]).attr('name',Y+'-'+M+'-'+i);
	}
	$('#Month_YM_1').html(Y+'年'+M+'月');
	D = new Date(Y+'/'+(M-1)+'/1');
	$('#Month_Move_1').html('< '+(D.getMonth()+1)+'月').attr('Y',D.getFullYear()).attr('M',D.getMonth()+1).unbind().bind("click",function(){
		CreateCalendar(parseInt($(this).attr('Y')),parseInt($(this).attr('M')));
	});
	td = $('tr:gt(1)','#Month_2').find('td');
	$(td).unbind().html('&nbsp;<br />&nbsp;').css('color','#CECFCE').attr('name','').attr('Price','').attr('ChildrenPrice','');
	D = new Date(Y+'/'+(M+1)+'/1');
	Y = D.getFullYear();
	M = D.getMonth()+1;
	S = D.getDay()-1;
	Stop = (new Date(Y+'/'+(M+1)+'/0')).getDate();
	for(var i=1;i<=Stop;i++){$(td[i+S]).html(i + '<br />').attr('name',Y+'-'+M+'-'+i);}
	$('#Month_YM_2').html(Y+'年'+M+'月');
	$('#Month_Move_2').html((M==12?1:M+1)+'月 >').attr('Y',Y).attr('M',M).unbind().bind("click",function(){
		CreateCalendar(parseInt($(this).attr('Y')),parseInt($(this).attr('M')));
	});
	D = new Date();
	$('td[name="'+D.getFullYear()+'-'+(D.getMonth()+1)+'-'+D.getDate()+'"]').css('color','').append('今天');
	FillPrice(Y,M);
	D = new Date(Y+'/'+(M-1)+'/1');
	FillPrice(D.getFullYear(),D.getMonth()+1);
}

function FillPrice(Y,M)
{
	var D = new Date(Y+'/'+(M+1)+'/0');
	var DayLength = D.getDate();
	var Week = ['日','一','二','三','四','五','六'];  
	var Year,Month,Day,MatchValue,TempStartDate,Td,IsStartDate,Price_1,ChildrenPrice_1;
	D = new Date();
	Year = D.getFullYear();
	Month = D.getMonth()+1;
	Day = D.getDate();
	TempStartDate = ','+LineStartDate+',';
	for(var j=1;j<=DayLength;j++)
	{
		D = new Date(Date.parse(Y+'/'+M+'/'+j));
		if(Y==Year && Month==M && Day==j){continue;}
		IsStartDate = true;
		if(StartDateType==1 && !TempStartDate.match(new RegExp(',(0?)'+M+'-(0?)'+j+',','i')))
		{
			IsStartDate = false;
		}else if(StartDateType==2 && TempStartDate.indexOf(Week[D.getDay()])==-1){
			IsStartDate = false;
		}else if(StartDateType==3 && TempStartDate.indexOf(','+j+',')==-1){
			IsStartDate = false;
		}
		Td = $('td[name="'+Y+'-'+M+'-'+j+'"]');
		if(StartDateType==0 || IsStartDate == true)
		{
			MatchValue = LineSpecialPrice.match(new RegExp('(\|)(0?)'+M+'-(0?)'+j+'\:([0-9]+)\:([0-9]+)(\|)','i'));
			if(MatchValue!=null)
			{
				Price_1=MatchValue[4];ChildrenPrice_1=MatchValue[5];
			}else{
				Price_1=LinePrice;ChildrenPrice_1=LineChildrenPrice;
			}
			if(Date.parse(Y+'/'+M+'/'+j)>Date.parse(new Date()))
			{
				Td.html(Td.html()+'<a href="javascript:;">'+Price_1+'元</a>'); 
				Td.attr('Price',Price_1).attr('ChildrenPrice',ChildrenPrice_1).css('color','').unbind().bind("click",function(){ClickCalendar($(this));});
			}else{
				Td.html(Td.html()+Price_1+'元'); 
			}
		}else{
			Td.html(Td.html()+'&nbsp;'); 
		}
	}
}
//o_date
function SelectPrice()
{
	var D = new Date();
	D=new Date(Date.parse(D.getFullYear()+'/'+(D.getMonth()+1)+'/'+D.getDate())+86400000);
	var Week = ['日','一','二','三','四','五','六'];  
	var Year,Month,Day,MatchValue;
	var TempStartDate;
	if(StartDateType != 0)
	{
		if(StartDateType==1)
		{
			TempStartDate = LineStartDate.split(',');
			var s=0;
			for(var j=0;j<TempStartDate.length;j++)
			{
				Year = D.getFullYear();
				D = new Date(Date.parse(Year + '/' + TempStartDate[j].replace('-','/')));
				if(Date.parse(D)>Date.parse(new Date()))
				{
					AddSelect(Year,D.getMonth()+1,D.getDate(),LinePrice,LineChildrenPrice,Week[D.getDay()])
					s++;
					if(s>=SelectLength)break;
				}
			}
		}else if(StartDateType==2){
			for(var j=0;j<SelectLength;j++)
			{
				Year = D.getFullYear();
				Month = D.getMonth()+1;
				Day = D.getDate();
				if(Date.parse(D)>Date.parse(new Date()) && LineStartDate.indexOf(Week[D.getDay()])!=-1)
				{
					AddSelect(Year,Month,Day,LinePrice,LineChildrenPrice,Week[D.getDay()])
				}else{
					j--;
				}
				D = new Date(Date.parse(Year + '/' + Month + '/' + (Day+1)));
			}
		}else if(StartDateType==3){
			TempStartDate = ','+LineStartDate+',';
			TempStartDate = TempStartDate.replace(/,0/g,',');
			for(var j=0;j<SelectLength;j++)
			{
				Year = D.getFullYear();
				Month = D.getMonth()+1;
				Day = D.getDate();
				if(Date.parse(D)>Date.parse(new Date()) && TempStartDate.indexOf(','+Day+',')!=-1)
				{
					AddSelect(Year,Month,Day,LinePrice,LineChildrenPrice,Week[D.getDay()])
				}else{
					j--;
				}
				D = new Date(Date.parse(Year + '/' + Month + '/' + (Day+1)));
			}
		}else{
			alert('出发时间格式错误！');return false;
		}
	}else{
		for(var j=0;j<SelectLength;j++)
		{
			Year = D.getFullYear();
			Month = D.getMonth()+1;
			Day = D.getDate();
			if(Date.parse(D)>Date.parse(new Date()))
			{
				AddSelect(Year,Month,Day,LinePrice,LineChildrenPrice,Week[D.getDay()])
			}else{
				j--;
			}
			D = new Date(Date.parse(Year + '/' + Month + '/' + (Day+1)));
		}
	}
}
function AddSelect(Year,Month,Day,Price,ChildrenPrice,Week)
{
	var MatchValue = LineSpecialPrice.match(new RegExp('(\|)(0?)'+Month+'-(0?)'+Day+'\:([0-9]+)\:([0-9]+)(\|)','i'))
	if(MatchValue!=null)
	{
		$('#o_date').append('<option value="'+Year+'-'+Month+'-'+Day+'" Price="'+MatchValue[4]+'" ChildrenPrice="'+MatchValue[5]+'">'+Month+'月'+Day+'日，周'+Week+'，成人'+MatchValue[4]+'元，儿童'+MatchValue[5]+'元</option>');
	}else{
		$('#o_date').append('<option value="'+Year+'-'+Month+'-'+Day+'" Price="'+Price+'" ChildrenPrice="'+ChildrenPrice+'">'+Month+'月'+Day+'日，周'+Week+'，成人'+Price+'元，儿童'+ChildrenPrice+'元</option>'); 
	}
}
function o_chengren_onchange(T)
{
	var v = parseInt($('#o_chengren').val());
	if(v==-1)
	{
		v = parseInt($('#o_chengren>option:selected').prev().val())+1;
		$('#o_chengren').children().remove();
		for(var i=v;i<v+10;i++)
		{
			$('#o_chengren').append('<option value="'+i+'">'+i+'</option>'); 
		}
		$('#o_chengren').append('<option value="-1">更多</option>'); 
		$('#o_chengren').val([v]);
	}
}
function o_ertong_onchange(T)
{
	var v = parseInt($('#o_ertong').val());
	if(v==-1)
	{
		v = parseInt($('#o_ertong>option:selected').prev().val())+1;
		$('#o_ertong').children().remove();
		for(var i=v;i<v+10;i++)
		{
			$('#o_ertong').append('<option value="'+i+'">'+i+'</option>'); 
		}
		$('#o_ertong').append('<option value="-1">更多</option>'); 
		$('#o_ertong').val([v]);
	}
}
function ClickCalendar(td)
{
	var Week = ['日','一','二','三','四','五','六'];
	if($('#o_date>option[value="'+td.attr('name')+'"]').length==0)
	{
		var D = new Date(td.attr('name').replace(/-/g,'/'));
		$('#o_date').append('<option value="'+D.getFullYear()+'-'+(D.getMonth()+1)+'-'+D.getDate()+'" Price="'+td.attr('Price')+'" ChildrenPrice="'+td.attr('ChildrenPrice')+'">'+(D.getMonth()+1)+'月'+D.getDate()+'日，周'+Week[D.getDay()]+'，成人'+td.attr('Price')+'元，儿童'+td.attr('ChildrenPrice')+'元</option>');
	}
	$('#o_date').val(td.attr('name'));
	$('#o_date').focus();
}