var bHasChanged = false;

function isValid(oForm)
{
	var sRegion;
	var sMessage = "";
	var dCurrentDate = new Date();
	var dSearchDate;
	var nDay = new Number(oForm.d.options[oForm.d.selectedIndex].value);
	var nMonth = new Number(oForm.m.options[oForm.m.selectedIndex].value);
	var nRange = new Number(oForm.range.options[oForm.range.selectedIndex].value);
	var nYear = new Number(oForm.y.options[oForm.y.selectedIndex].value);
	
	alert(nMonth + " " + nDay + " " + nYear);
	//set javascript test
	oForm.javascript.value="true";
	
	if ((oForm.location.options[oForm.location.selectedIndex].text=="- US and Canada Resorts -")
	 || (oForm.location.options[oForm.location.selectedIndex].text=="")
	 || (oForm.location.options[oForm.location.selectedIndex].text=="- Worldwide Resorts -"))
	{
		sMessage = "Please select a location.\n";
	}
	else if (oForm.location.options[oForm.location.selectedIndex].value==-99)
	{
		sMessage = "Please select a location within " + oForm.location.options[oForm.location.selectedIndex].text + ".\n";
	}
	
	if(nMonth < 2 && nYear < 2005) sMessage += "Please choose a current or future month.\n";
	
	if((nDay) < 8 && nMonth <= 4 && nYear == 2002) sMessage += "The selected date must be at least 2 days beyond today's date.\n";
	
	nDay = nDay + nRange;
	
	dSearchDate = new Date(nYear, nMonth-1, nDay);
	
	if (dSearchDate.valueOf() - dCurrentDate.valueOf() > 31536000000)
	{
		sMessage += "Search date must be within one calendar year of today's date.\n";
	}
	
	if(sMessage.length > 0)
	{
		alert(sMessage);
		return false;
	}
	else
	{
		return true;
	}
}

function changeDuration()
{
	bHasChanged = true;
}

function changeDurationToSevenDestination(oElement)
{
	if (!bHasChanged && (oElement.options[oElement.selectedIndex].value == 9 || oElement.options[oElement.selectedIndex].value == 14))
	{
		document.cruisesearch["dur"].selectedIndex = 2;
		bHasChanged = true;
	}
}

function changeDurationToSevenCL()
{
	if (!bHasChanged)
	{
		document.cruisesearch["dur"].selectedIndex = 2;
		bHasChanged = true;
	}
}

function checkCLDestination(oForm)
{
	var nDestination = new Number(oForm.destination.options[oForm.destination.selectedIndex].value);
	var nCruiseLine = new Number(oForm.cruiseline.options[oForm.cruiseline.selectedIndex].value);
	var nDuration = -99;
	var nPort = -99;
	var nRange = -99;

	var sDate = "";
	var sDates;
	var dCDate = new Date();
	var dSDate;
	var pattern = /\d{1,2}\/\d{1,2}\/\d{4}/;
	var pattern2 = /\D/;
	var sMessage = "";

	if (oForm.dport)
	{
		nDuration = new Number(oForm.dur.options[oForm.dur.selectedIndex].value);
		nPort = new Number(oForm.dport.options[oForm.dport.selectedIndex].value);

		if (oForm.prange)
		{
			nRange = new Number(oForm.prange.options[oForm.prange.selectedIndex].value);
		}

		if (oForm.calendar)
		{
			sDate = oForm.date.value;
			
			if (pattern.test(sDate))
			{
				sDates = sDate.split("/");
	            if (pattern2.test(sDates[0]) || pattern2.test(sDates[1]) || pattern2.test(sDates[2]))
	            {
	                sMessage += "Please enter a valid search date. \n";
	            }
				if (oForm.usdate)
				{
					dSDate = new Date(sDates[2], sDates[0]-1, sDates[1]);
				}
				else
				{
					dSDate = new Date(sDates[2], sDates[1]-1, sDates[0]);
				}
				if ((dCDate.valueOf() + 518400000) > dSDate.valueOf())
				{
					sMessage += "The search date must be at least 1 week in the future. \n";
				}
				if ((dCDate.valueOf() + 126144000000) < dSDate.valueOf())
				{
					sMessage += "The search date must not be more than 4 years in the future. \n";
				}
			}
			else
			{
				oForm.date.value = "";
				
				if (nCruiseLine == -99 && nDestination == -99 && nDuration == -99 && nRange == -99 && nPort == -99)
				{
					sMessage += "You must select at least one search item to use the Search.\n";
				}
			}
		}
	}

	if(sMessage.length > 0)
	{
		alert(sMessage);
		return false;
	}
	else
	{
		return true;
	}
}

function IG_FormRedirect(f)
{
	var nfindex;
	var sUrl = f.action;
	sUrl = sUrl + '?';
	
	if (!document.cruisesearch["specid"].value)
	{
		if (checkCLDestination(f))
		{
			for(nfindex=0; nfindex <= (f.length - 1); nfindex++)
			{
				var e = f.elements[nfindex];
				
				if (e.value == null)
				{
					sUrl = sUrl + e.name + '=' + e.options[e.selectedIndex].value;
				}
				else
				{
					sUrl = sUrl + e.name + '=' + e.value;
				}
				
				if ((nfindex+1) < (f.length - 1))
				{
					sUrl = sUrl + '\&';
				}
			}
			sUrl = sUrl + '#results';
			window.location = sUrl.replace(/amp;/g, "");
		}
	}
	else
	{
		return true;
	}
	return false;
}

function resetFormAdvanced()
{
	document.cruisesearch.reset();
	document.cruisesearch["specid"].value="";
}

function addSortToSearch(sortType)
{
	var sSortParm = "&amp;sort=" + sortType;
	sUrl = window.location.href;
	sUrl = sUrl.replace(/&sort=[0-9]/g, "");
	sUrl = sUrl.replace(/&amp;sort=[0-9]/g, "");
	sUrl = sUrl + sSortParm;
	window.location = sUrl.replace(/amp;/g, "");
}
