var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) { return false; }
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) { returnString += c; }
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 === 0) && ( (!(year % 100 === 0)) || (year % 400 === 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
   } 
   return this;
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) { strDay=strDay.substring(1); }
	if (strMonth.charAt(0)=="0" && strMonth.length>1) { strMonth=strMonth.substring(1); }
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) { strYr=strYr.substring(1); }
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("You must select a day, month and year for flight date");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("You must select a day, month and year for flight date");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("You must select a day, month and year for flight date");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("You must select a day, month and year for flight date");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("You must select a day, month and year for flight date");
		return false;
	}
return true;
}

function ValidateEvent(){
	if (document.getElementById('newOrganizerID') !== null)
		if (document.getElementById('newOrganizerID').value !== '')
			if (!isInteger(document.getElementById('newOrganizerID').value)) {
				alert('membership number must be numeric');
				return false;
			}
			
	if (document.getElementById('newCOPAFlightNumber') !== null)
		if (document.getElementById('newCOPAFlightNumber').value !== '')
			if (!isInteger(document.getElementById('newCOPAFlightNumber').value)) {
				alert('copa flight number must be numeric');
				return false;
			}
				
	/* var oid=document.getElementById('newOrgID');
	var fdday = document.getElementById('FLIGHTDATE_DAY');
	var fdmonth = document.getElementById('FLIGHTDATE_MONTH');
	var fdyear = document.getElementById('FLIGHTDATE_YEAR');
	var fdate = fdday.options[fdday.selectedIndex].value + "/" + fdmonth.options[fdmonth.selectedIndex].value.toString() + "/" + fdyear.options[fdyear.selectedIndex].value.toString()
	var air=document.getElementById('newAirport');
	var nc=document.getElementById('newCity');
	var np=document.getElementById('newprovince');
	var binval = 0;
	if (oid != null) {
		if (oid.value=="") {
			binval = 1;
		}
		else if (isInteger(oid.value)==false) {
			alert('Organizer Member Number must be a number');
			return false;
		}
	}
	if (nc.value=="") {
		binval = 1;
	}
	if (air.value=="") {
		binval = 1;
	}
	if (np.value=="") {
		binval = 1;
	}
	if (binval == 1) {
		alert('all fields must be entered');
		return false;
	}
	if (isDate(fdate)==false){
		return false;
	} */
    return true;
 }
 
 function ValidateParticipant() {
	var pln=document.getElementById('partlastname');
	var pfn=document.getElementById('partfirstname');
	var pc=document.getElementById('partcity');
	var binval = 0;
	if (pln.value=="") {
		binval = 1;
	}
	if (pfn.value=="") {
		binval = 1;
		}
	if (pc.value=="") {
		binval = 1;
		}
	if (binval == 1) {
		alert('all fields must be entered for the Participant');
		return false;
	}
	return true;
 }
 
 function ValidatePilot() {
	/* ar pmn=document.getElementById('pilotmembernum');
	var pfcn=document.getElementById('pilotflightchapt');
	var pln=document.getElementById('pilotlastname');
	var pfn=document.getElementById('pilotfirstname');
	var pc=document.getElementById('pilotcity');
	var pat=document.getElementById('pilotatype');
	var paid=document.getElementById('pilotaid');
	var binval = 0;
	if (pfcn.value=="") {
		binval = 1;
	}
	if (pln.value=="") {
		binval = 1;
		}
	if (pfn.value=="") {
		binval = 1;
		}
	if (pmn.value=="") {
		binval = 1;
		}
	if (pc.value=="") {
		binval = 1;
		}
	if (pat.value=="") {
		binval = 1;
		}
	if (paid.value=="") {
		binval = 1;
		}
	if (binval == 1) {
		alert('all fields must be entered for the Pilot');
		return false;
	}
	if (isInteger(pmn.value)==false) {
		alert('COPA Member Number must be a number');
		return false;
	}
		if (isInteger(pfcn.value)==false) {
		alert('COPA Flight Chapter Number must be a number');
		return false;
	} */
	return true;
}
	
	function confirmDeletePart(){
		return confirm("This will permanantly delete the participant");
	}
	
	function confirmDeletePilot(){
		return confirm("This will permanantly delete the pilot");
 	}
	
	function ValidateLogin(){
		var pilnum = document.getElementById('userid');
		var passnum = document.getElementById('pass');
		if (pilnum.value=="") {
			alert('Please enter an ID');
			return false;
		}
		/* else if (passnum.value=="") {
			alert('Please enter a password');
			return false;
		} */
		else if (isInteger(pilnum.value)==false) {
			alert('Please enter a valid ID');
			return false;
		}
 	}
	
	function toggleVis(element) {
		if (document.getElementById(element).style.display=="none"){
			if (document.getElementById('popUpDiv') != null) {
			var h = document.getElementById('popUpDiv').style.height;
			if (h == "") {
				h = document.getElementById('heightmarker').style.height
			}
			h = h.substr(0,h.indexOf('px'));
			if (element == "newpar") { h = parseInt(h) + 220; }
			if (element == "newpil") { h = parseInt(h) + 260; }
			h = h + "px";
			document.getElementById('popUpDiv').style.height=h;
			}
			document.getElementById(element).style.display="block";
			}
		else {
		if (document.getElementById('popUpDiv') != null) {
			var h=document.getElementById('popUpDiv').style.height;
			h = h.substr(0,h.indexOf('px'));
			if (element == "newpar") { h = parseInt(h) - 220; }
			if (element == "newpil") { h = parseInt(h) - 260; }
			h = h + "px";
			document.getElementById('popUpDiv').style.height=h;
			}
			document.getElementById(element).style.display="none";
			}
	}
	
	function searchType() {
		if (document.forms[0].searchtype.value == "fl") {
				document.forms[0].searchText2.style.display="block";
				document.getElementById('fnLabel').style.display="block";
				document.getElementById('lnLabel').style.display="block";
		}
		else {
			document.forms[0].searchText2.style.display="none";
			document.getElementById('fnLabel').style.display="none";
			document.getElementById('lnLabel').style.display="none";
		}
	}
	
	function clickThis() {
		if (document.forms[0].searchtype.value == "da") {
			scwShow(document.forms[0].searchText,event);
		}
	}
	
	function pilotSearch(elem) {
		var butt = document.forms[0].btnPilotAdd;
		if (stripNonAlpha(elem.value) === "") {
			butt.disabled=true;
			document.getElementById('pilotSearchResults').innerHTML = "<hr /><hr />";
		}
		else {
			My_Request_No_Switch('event_search.cfm?type=pilot&text='+stripNonAlpha(elem.value)+'&itemid='+document.forms[0].itemid.value,'pilotSearchResults')
		}
	}
	
	function pilotSelectionMade() {
		document.forms[0].btnPilotAdd.disabled=false;
	}
	
	function participantSearch(elem) {
		var butt = document.forms[0].btnPartAdd;
		if (stripNonAlpha(elem.value) === "") {
			butt.disabled=true;
			document.getElementById('participantSearchResults').innerHTML = "<hr /><hr />";
		}
		else {
			My_Request_No_Switch('event_search.cfm?type=participant&text='+stripNonAlpha(elem.value)+'&itemid='+document.forms[0].itemid.value,'participantSearchResults')
		}
	}
	
	function participantSelectionMade() {
		document.forms[0].btnPartAdd.disabled=false;
	}
	
	function stripNonAlpha(input) {
		var regex=/^[A-Za-z]+$/;
		for(i=0; i<input.length; i++) {
			if (!regex.test(input.charAt(i)))
				return "";
		}
		return input;
	}
