function iBookSubmit(form){

	if(document.getElementById("TB_overlay") !== null)
	{
		return;
	}

	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			jQuery("body","html").css({height: "100%", width: "100%"});
			jQuery("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				jQuery("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				jQuery("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				jQuery("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			jQuery("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			jQuery("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}

		TB_WIDTH = 900;
		TB_HEIGHT = 600;
		ajaxContentW = TB_WIDTH - 30;
		ajaxContentH = TB_HEIGHT - 45;

		jQuery("#TB_window").append("<div id='TB_title' style='background: #f90;'><div id='TB_ajaxWindowTitle'></div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close' style='color: #ffffff;text-decoration: none'><img src='" + tb_closeImage + "' align='absmiddle' border='0' /></a></div></div><iframe frameborder='0' hspace='0' id='TB_iframeContent' name='TB_iframeContent' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");

		jQuery("#TB_closeWindowButton").click(tb_remove);

		jQuery("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
		if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
			jQuery("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
		}
		jQuery("#TB_window").css({display:"block"});
		//var AVP = document.getElementById("AVP");
		form.target = "TB_iframeContent";
		//AVP.submit();

	//jQuery("#blockMember").html("<br /><span style=\"color: #0000ff;\">Thank you for your subscription.</span><br /><br />");
	return true;
	
}

var maxDuration=30;
function GetFormElementByName(form, name)
{
	for(var i = 0; i < form.length; i ++)
	{
		var em = form.elements[i];
		if(em.name == name)
		{
			return em;
		}
	}

	return null;
}

function appendOption(l,t,v)
{
	var e=document.createElement('option');
	e.text=t;
	e.value=v;
	try
	{
		l.add(e,null);
	}
	catch(ex)
	{
		l.add(e);
	}	
}

function getDaysInMonth(y,m)
{
	var days=[31,28,31,30,31,30,31,31,30,31,30,31];
	var d=days[m-1];
	if(d==28){if(isLeapYear(y))d=29;}
	
	return d/1;
}

function isLeapYear(y)
{
	if(y%400==0||(y%4==0&&y%100!=0))
		return true;
	
	return false;
}

function InitAForm(form)
{
	var d=new Date();
	var y=d.getFullYear();
	var lm=GetFormElementByName(form, 'AVP_list_months');
	var ld=GetFormElementByName(form, 'AVP_list_days');
	var ln=GetFormElementByName(form, 'AVP_list_nights');
	
	for(i=0;i<12;i++)
	{
		var m=(d.getMonth()+i)%12;
		if(m==0&&i!=0)y++;
		appendOption(lm,months[m]+' '+y,y+'-'+(m+1));
	}
	
	for(i=1;i<=31;i++)
	{
		appendOption(ld,i,i);
		if(i<=maxDuration)appendOption(ln,i,i);
	}
	
	ld.selectedIndex=d.getDate()-1;
	
	setDays(form);
}

function setDays(form)
{
	var ld=GetFormElementByName(form, 'AVP_list_days');
	var lm=GetFormElementByName(form, 'AVP_list_months');
	var ym=lm.options[lm.selectedIndex].value;
	var y=eval(ym.substring(0,4));
	var m=eval(ym.substring(5,ym.length));
	var d=getDaysInMonth(y,m);
	
	if(d>ld.length){for(i=ld.length;i<d;i++)appendOption(ld,i+1,i+1);}
	if(d<ld.length){if(ld.selectedIndex>d-1)ld.selectedIndex=d-1;ld.length=d;}
}

function validate(form)
{
	var lm=GetFormElementByName(form, 'AVP_list_months');
	var ld=GetFormElementByName(form, 'AVP_list_days');
	var ym=lm.options[lm.selectedIndex].value;
	var y=eval(ym.substring(0,4));
	var m=eval(ym.substring(5,ym.length));
	var d=new Date();
	var t=new Date(d.getFullYear(),d.getMonth(),d.getDate());
	d=new Date(y,m-1,eval(ld.options[ld.selectedIndex].value));
	if(d<t){alert(message);return false;}

	var ln=GetFormElementByName(form, 'AVP_list_nights');
	GetFormElementByName(form, 'arrivalDate').value = lm.options[lm.selectedIndex].value+'-'+ld.options[ld.selectedIndex].value;
	GetFormElementByName(form, 'nights').value = ln.options[ln.selectedIndex].value;
	//alert("target: " + form.target);
	form.submit();

	//alert("arr: " + GetFormElementByName(form, 'arrivalDate').value);
	//alert("nights: " + GetFormElementByName(form, 'nights').value);
	//return false;
}

function SubmitForm(ind)
{
	for(var i = 0; i < document.forms.length; i ++)
	{
		var form = document.forms[i];
		if(form.id == "AVP")
		{
			if(ind <= 0)
			{
				iBookSubmit(form);
				validate(form);

				return;
			}

			ind --;
		}
	}
}

function SubmitForm2(ind, frameId)
{
	for(var i = 0; i < document.forms.length; i ++)
	{
		var form = document.forms[i];
		if(form.id == "AVP")
		{
			if(ind <= 0)
			{
				form.target = frameId;
				validate(form);

				return;
			}

			ind --;
		}
	}
}

var start = 0;
var box = null;
function InitForms()
{
	for(; start < document.forms.length; start ++)
	{
		var form = document.forms[start];
		if(form.id == 'AVP')
		{
			InitAForm(form);
			//validate(form);
		}
	}

	/*jQuery(".AVP_btn_check").each(function(i){
		jQuery(this).click(function(){
			SubmitForm(i);
		});
	});*/
	
}

jQuery(document).ready(function (){
	InitForms();

	if(box == null)
	{
		box = new MultiBox('AVP_btn_check');
	}
});

