function setShips() {
	var lineList = document.getElementById('fLine');
	var shipList = document.getElementById('fShip');
	var theOptGroups 	= shipList.getElementsByTagName('optgroup'); 

	theSelectedLine = lineList.options[lineList.selectedIndex].value;
	for (i=0;i<theOptGroups.length;i++) {
		if (theSelectedLine!="Any") {
			if (theOptGroups[i].id == "line_"+theSelectedLine) {
				theOptGroups[i].style.display="";
			} else {
				theOptGroups[i].style.display="none";
			}
		} else {
			theOptGroups[i].style.display="";
		}
	}
	shipList.selectedIndex=0;
}

function setDate(whichOne) {
	var theYear;
	var theMonth;
	var theDay;
	var theField;

	if (whichOne==1) {
		var theYear 	= document.getElementById("fStartYear");
		var theMonth 	= document.getElementById("fStartMonth");
		var theDay = document.getElementById("fStartDay");
		var theField	= document.getElementById("fStartDate");
	}
	if (whichOne==2) {
		var theYear 	= document.getElementById("fEndYear");
		var theMonth 	= document.getElementById("fEndMonth");
		var theDay = document.getElementById("fEndDay");
		var theField	= document.getElementById("fEndDate");
	}

	// check for stupid dates
	var dV = theDay.options[theDay.selectedIndex].value;
	var mV = theMonth.options[theMonth.selectedIndex].value;
	var yV = theYear.options[theYear.selectedIndex].value;

	// January
	if (mV==2 && dV>29) {
		dV=28;
		theDay.selectedIndex=27;
	}

	if (dV>30) {
		switch(mV) {
			case "09":
			case "04":
			case "06":
			case "11":
				dV=30;
				theDay.selectedIndex=29;
			break;
		}
	}

	var theDate = yV;
	theDate += "-";
	theDate += mV;
	theDate += "-";
	theDate += dV;

	theField.value = theDate;
}


function checkCPSearchForm() {

	var dp	= document.getElementById("fDP");
	var price= document.getElementById("fPrice"); 
	var regionf 	= document.getElementById("fRegion"); 
	var linef = document.getElementById("fLine"); 
	var shipf = document.getElementById("fShip"); 

	region = regionf.options[regionf.selectedIndex].value; 
	line = linef.options[linef.selectedIndex].value; 
 	ship = shipf.options[shipf.selectedIndex].value; 

	var startDate 	= document.getElementById("fStartDate").value;
	var endDate   	= document.getElementById("fEndDate").value;

	// Save state of search form
	var cookieString = dp.selectedIndex+"|";
	cookieString += price.selectedIndex+"|";
	cookieString += regionf.selectedIndex+"|";
	cookieString += linef.selectedIndex+"|";
	cookieString += shipf.selectedIndex+"|";
	cookieString += startDate+"|";
	cookieString += endDate;
	document.cookie = "searchState="+cookieString;

	startDate = new Date(startDate.substring(0,4),startDate.substring(5,7)-1,startDate.substring(8));
	endDate   = new Date(endDate.substring(0,4),  endDate.substring(5,7)-1,  endDate.substring(8));

	startDate = startDate.getTime();
	endDate   = endDate.getTime();

	if ((endDate-startDate)<0) {
		alert("Second date cannot be before first date!");
		return false;
	}
	return true;

}

function restoreSearch() {
	var allCookies = document.cookie;
	var pos = allCookies.indexOf("searchState=");
	if (pos!=-1) {
		var start = pos+12;
		var end = allCookies.indexOf(";", start);
		if (end==-1) end=allCookies.length;
		var value=allCookies.substring(start,end);
	}
}


