//image swap
function imgswap(img,ref){
  if (document.images) {
        document.images[img].src = ref;
  }
}   

// open window function
function openWindow(url, name) {
	popupWin = window.open(url, name, "width=540,height=500,resizable=yes,scrollbars=yes");
}


//agree to join conditions
function AgreeConditions() {
	agreeCon = document.editForm.agreeCon.checked;
	
	if (!agreeCon) {
		openWindow("results/error-agreeconditions.shtml", "confirm");
		return false;
	}
	else return true;
}


 




//old stuff 

pageLoad = 0;

weekdayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

fRunOrder = '';

function openHelpWindow(url, name) {
	help = window.open(url, name, "width=300,height=400,resizable=no,scrollbars=yes");
	help.focus();
}

function compareDates() { 
	fRunOrder += 'compareDates, ';

	var firstDateInMillis = datesToMillis(earliestDate);
	var lastDateInMillis = datesToMillis(latestDate);

	var desiredFirst = datesToMillis(document.newReports.firstDate.value);
	var desiredLast = datesToMillis(document.newReports.lastDate.value);

	//alert(firstDateInMillis+' vs '+desiredFirst);
	//alert(lastDateInMillis+' vs '+desiredLast);
	
	if((firstDateInMillis > desiredFirst) || (lastDateInMillis < desiredLast)) {
		alert('Sorry, your selected date range is larger than the available data.');
		return false;
	}
	else return true;
}

function datesToMillis(aDate) {
	fRunOrder += 'datesToMillis, ';

	c_date_array = aDate.split("/");

	c_month = c_date_array[0];
	c_day = c_date_array[1];
	c_year = c_date_array[2];
	
	c_month = Math.round(c_month-1);
	
	f_date = new Date(c_year,c_month,c_day);
	f_date_in_millis = f_date.getTime();
	return f_date_in_millis;
}


function setDatePopups() {
	fRunOrder += 'setDatePopups, ';

	buildYears(earliestDate,latestDate);
	//setTimeout("setYear(\'"+earliestDate+"\','first')",10);
	//setTimeout("setYear(\'"+latestDate+"\','last')",11);

	//setTimeout("setMonth(\'"+earliestDate+"\','first')",30);
	//setTimeout("setMonth(\'"+latestDate+"\','last')",31);

	setMonth(earliestDate,'first');
	setMonth(latestDate,'last');

	//setTimeout("setMonthDays("+getNumVal('month',earliestDate)+",'first')",40);
	//setTimeout("setMonthDays("+getNumVal('month',latestDate)+",'last')",41);

	//setTimeout("setDay(\'"+earliestDate+"\','first')",300);
	//setTimeout("setDay(\'"+latestDate+"\','last')",301);

	//setTimeout("glomAllDays()",350);
	//setTimeout("alert(fRunOrder)",3000);

}


function getNumVal(mode,theDate){
	fRunOrder += 'getNumVal, ';

	// enter month, day, or year, and a commander date
	// returns the javascript number for the request

	var theDateString = '' + theDate;
	var date_array = theDateString.split("/");
	
	if(mode == 'month') {
		return Math.round(date_array[0] - 1);
	}
	else if(mode == 'day') {
		return Math.round(date_array[1]);
	}
	else if(mode == 'year') {
		return Math.round(date_array[2]);
	}

}

function buildYears(first,last) {
	fRunOrder += 'buildYears, ';

	//alert(first+" "+last);

	var first_date_array = first.split("/");
	var first_year = Math.round(first_date_array[2]);
	//alert(first_year);

	var last_date_array = last.split("/");
	var last_year = Math.round(last_date_array[2]);
	//alert(last_year);

	var totalYears = Math.round((last_year - first_year) + 1);
	//alert(totalYears);
	
	var firstOpts = document.newReports.firstYear.options;
	var lastOpts = document.newReports.lastYear.options;

	// builds new years
	for(var g=0; g < totalYears; g++) {
		eval("firstOpts[g] = new Option(first_year,first_year)");
		eval("lastOpts[g] = new Option(first_year,first_year)");
		first_year++;
	}

	setYear(earliestDate,'first');
	setYear(latestDate,'last');

}


function setYear(theDate,edge) {
	fRunOrder += 'setYear, ';

	var theDateString = '';
	theDateString = theDateString + theDate;
	//alert(theDateString);

	var date_array = theDateString.split("/");
	var year = Math.round(date_array[2]);

	var opts = document.newReports[edge+"Year"].options;

	// sets popup to that year
	for (var h=0; h < opts.length; h++) {
		if(opts[h].value == year) {
			opts[h].selected = true;
			break;
		}
		else opts[h].selected = false;
	}

}


function setMonth(theDate,edge) {
	fRunOrder += 'setMonth, ';

	var theDateString = '';
	theDateString = theDateString + theDate;
	var date_array = theDateString.split("/");
	var month = Math.round(date_array[0] - 1);

	var opts = document.newReports[edge+"Month"].options;

	// sets popup to that month
	for (var h=0; h < opts.length; h++) {
		if(opts[h].value == month) {
			opts[h].selected = true;
			break;
		}
		//else opts[h].selected = false;
	}

	setMonthDays(theDate,month,edge);

}


function setDay(theDate,edge) {
	fRunOrder += 'setDay, ';

	var theDateString = '' + theDate;
	var date_array = theDateString.split("/");
	var theDay = Math.round(date_array[1]);

	var opts = document.newReports[edge+"Day"].options;

	// sets popup to that day
	for (var q=0; q < opts.length; q++) {
		if(opts[q].value == theDay) {
			opts[q].selected = true;
			//alert(opts[q].value+' and '+theDay)
			//alert(opts.selectedIndex);
			break;
		}
		// else opts[q].selected = false;
	}

	glomAllDays();
}


function getDateInMillis(theDate) {
	fRunOrder += 'getDateInMillis, ';

	c_date_array = firstD.split("/");

	c_month = c_date_array[0];
	c_day = c_date_array[1];
	c_year = c_date_array[2];
	
	c_month = Math.round(c_month-1);

	f_date = new Date(c_year,c_month,c_day);
	f_date_in_millis = f_date.getTime();
	
}


function setMonthDays(cDate,monthNumber,edge) {
	fRunOrder += 'setMonthDays, ';

	// stuff to determine days in that month
	var type1 = ',0,2,4,6,7,9,11,';
	var type2 = ',1,3,5,8,10,';
	var type3 = ',1,';
	
	if(edge.indexOf('first') != -1) {
		thePoint = "firstDay";
	}
	else thePoint = "lastDay";

	if(type3.indexOf(','+monthNumber+',') != -1) {
		days = 28;
	}
	else if(type2.indexOf(','+monthNumber+',') != -1) {
		days = 30;
	}
	else if(type1.indexOf(','+monthNumber+',') != -1) {
		days = 31;
	}

	var opts = document.newReports[thePoint].options;
	
	// cleans up extra trailing days from previous display
	while(days < opts.length) {
		opts[opts.length-1] = null;
	}

	// sets new days
	for(var g=0; g < days; g++) {
		//alert(g);
		eval("opts[g] = new Option(g+1,g+1)");
	}

	// sets the selected item of the pulldown to the first item, otherwise the form will contain null or undefined when submit, which is bad.
	opts.selectedIndex = 0;

	if(pageLoad<2) {
		setTimeout("setDay(\'"+cDate+"\',\'"+edge+"\')",30);
	}
	else glomAllDays();
	
	//alert(pageLoad);
	
	pageLoad++;

	//setTimeout("setDay(cDate,edge)",1);
	
	//setTimeout("glomAllDays()",350);

}



reportPartA = '';
reportPartB = '';


function glomAndGo() {
	fRunOrder += 'glomAndGo, ';

	var earlyDate = document.reports.startDate.value;
	var lateDate = document.reports.endDate.value;
	if((reportPartA != '' && reportPartB != '')) location.href = reportPartA + reportPartB + "&startDate="+escape(earlyDate)+"&endDate="+escape(lateDate);
}

function setVals(newVal) {
	fRunOrder += 'setVals, ';

	if(newVal != '') {
		valArray = newVal.split(',');
	
		reportPartA = 'report'+valArray[0]+'.shtml?pfl=rpt'+valArray[0]+'.param&op1.dfl='+theUser+'\/current/';
		reportPartB = valArray[1]+'.db&line-specs='+theUser+'/current/'+valArray[2]+'.db,0x0000FF,2px&rpt='+newVal;
	}
	else {
		reportPartA = '';
		reportPartB = '';
	}

}

function setReports(theSet,theState) {  
	fRunOrder += 'setReports, ';

	if(theSet == 'set1') {
		if(theState) {
			document.newReports.set2.checked = false;
			for(var x=0;x<document.forms["newReports"].elements.length;x++) {
				if(document.forms["newReports"].elements[x].type == 'checkbox' && document.forms["newReports"].elements[x].name == 'report') {
					document.forms["newReports"].elements[x].checked = true;
				}
			}
		}
		else if(!theState) {
			document.newReports.set2.checked = true;
			for(var x=0;x<document.forms["newReports"].elements.length;x++) {
				if(document.forms["newReports"].elements[x].type == 'checkbox' && document.forms["newReports"].elements[x].name == 'report') {
					document.forms["newReports"].elements[x].checked = false;
				}
			}
		}
	}	


	else if(theSet == 'set2') {
		if(theState) {
			document.newReports.set1.checked = false;
			for(var x=0;x<document.forms["newReports"].elements.length;x++) {
				if(document.forms["newReports"].elements[x].type == 'checkbox' && document.forms["newReports"].elements[x].name == 'report') {
					document.forms["newReports"].elements[x].checked = false;
				}
			}
		}
		else if(!theState) {
			document.newReports.set1.checked = true;
			for(var x=0;x<document.forms["newReports"].elements.length;x++) {
				if(document.forms["newReports"].elements[x].type == 'checkbox' && document.forms["newReports"].elements[x].name == 'report') {
					document.forms["newReports"].elements[x].checked = true;
				}
			}
		}
	}	

}

function OLDcustomChoice() {  
	fRunOrder += 'OLDcustomChoice, ';

	for(var x=0;x<reportArray.length;x++) {
		if(document.forms["newReports"].elements[reportArray[x]].checked == false) {
			document.newReports.set1.checked = false;
			document.newReports.set2.checked = true;
			break;
		}
	}

}

function customChoice() {  
	fRunOrder += 'customChoice, ';

	for(var x=0;x<document.forms["newReports"].elements.length;x++) {
		if(document.forms["newReports"].elements[x].type == 'checkbox') {
			if(document.forms["newReports"].elements[x].name == 'report' && document.forms["newReports"].elements[x].checked == false) {
				document.newReports.set1.checked = false;
				document.newReports.set2.checked = true;
				break;
			}
		}
	}
}

function setReportField(theName, theStatus) {
	fRunOrder += 'setReportField, ';

	alert(theName);
	alert(theStatus);
}


var reportArray = new Array('r1','r2','r3','r4','r5','r6','r7','r8','r9','r10','r11','r12');


var asIndex;
function setPop(popup,currentval) {
	fRunOrder += 'setPop, ';

	for(g=0;g<document.forms.reports[popup].length;g++) {
		if(document.forms.reports[popup].options[g].value == currentval) {
			asIndex = g;
			break;
		}
		else asIndex = 0;
	}

	document.forms.reports[popup].selectedIndex = asIndex;

}


function glomAllDays() {
	fRunOrder += 'glomAllDays, ';

	var firstD = '';
	firstD = firstD + Math.round(Math.round(document.newReports.firstMonth.options[document.newReports.firstMonth.selectedIndex].value) + Math.round(1)) + '/';
	firstD = firstD + document.newReports.firstDay.options[document.newReports.firstDay.selectedIndex].value + '/';
	firstD = firstD + document.newReports.firstYear.options[document.newReports.firstYear.selectedIndex].value;

	var lastD = '';
	lastD = lastD + Math.round(Math.round(document.newReports.lastMonth.options[document.newReports.lastMonth.selectedIndex].value) + Math.round(1)) + '/';
	lastD = lastD + document.newReports.lastDay.options[document.newReports.lastDay.selectedIndex].value + '/';
	lastD = lastD + document.newReports.lastYear.options[document.newReports.lastYear.selectedIndex].value;

	document.newReports.firstDate.value = firstD;
	document.newReports.lastDate.value = lastD;

	document.newReports.startDate.value = firstD;
	document.newReports.endDate.value = lastD;

	//alert(document.newReports.firstDate.value + ',' + document.newReports.lastDate.value);

}


function showVals(theForm) {
	fRunOrder += 'showVals, ';

	var valString = '';
	var d = document.forms[theForm];
	for(x=0;x<d.elements.length;x++) {
		if(d.elements[x].type == 'checkbox' || d.elements[x].type == 'radio') {
			valString = valString + d.elements[x].name + ' = ' + d.elements[x].value + '  (' + d.elements[x].checked + ');';
		}
		else if(d.elements[x].type.indexOf('select') != -1) {
			//alert(d.elements[x].selectedIndex);
			valString = valString + d.elements[x].name + ' = ' + d.elements[x].options[d.elements[x].selectedIndex].value + ';';
		}
		else {
			valString = valString + d.elements[x].name + ' = ' + d.elements[x].value + ';';
		}
	}
	valString = valString.substr(0,valString.length-1);
	valString = escape(valString);
	
	openWindow('debug.html?v='+valString, 'values');

}

/*
Syntax: P3_setPop('nameofpopup','value')
*/

var asIndex;
function P3_setPop(popup,currentval) {

	for(g=0;g<document.forms.editForm[popup].length;g++) {
		if(document.forms.editForm[popup].options[g].value == currentval) {
			asIndex = g;
			break;
		}
		else asIndex = 0;
	}

	document.forms.editForm[popup].selectedIndex = asIndex;

}

