// JavaScript Document

// this function capitalizes alpha characters 
function caps(id){
		var str = document.getElementById(id).value;
		document.getElementById(id).value = str.toUpperCase();
}

// this function provides a confirmation box
function confirmation(link, verb, subject){
	var answer = confirm("Are you sure you want to "+verb+" this "+subject+"?");
	if(answer) {
		window.location = link;
	}
	else {
	}
}

// this function auto-tabs over to the next input field when multiple fields are used for a phone number
// MODDED FOR 3 PHONE NUMBERS!!
function autoFocusPhoneField(id){
	if(id == "phone1" || id == "phone2"){
		var field_name = document.getElementById(id).value;
		if(field_name.length == 3){
			if(id == "phone1"){
				document.form1.phone2.focus();
			}
			else {
				document.form1.phone3.focus();
			}
		}
	}
	else if(id == "phone4" || id == "phone5"){
		var field_name = document.getElementById(id).value;
		if(field_name.length == 3){
			if(id == "phone4"){
				document.form1.phone5.focus();
			}
			else {
				document.form1.phone6.focus();
			}
		}
	}
	else if(id == "phone8" || id == "phone9"){
		var field_name = document.getElementById(id).value;
		if(field_name.length == 3){
			if(id == "phone8"){
				document.form1.phone9.focus();
			}
			else {
				document.form1.phone10.focus();
			}
		}
	}
}

// this function checks the current value of the date field and adjusts the next one accordingly
function manageNextDate(id){
	// javascript month array
	var months = new Array(12);
	months[0] = "January";
	months[1] = "February";
	months[2] = "March";
	months[3] = "April";
	months[4] = "May";
	months[5] = "June";
	months[6] = "July";
	months[7] = "August";
	months[8] = "September";
	months[9] = "October";
	months[10] = "November";
	months[11] = "December";
	
	var selected_year = document.getElementById("year").value; // get the user-selected year
	var now = new Date();  // get current date
	var cur_year = (now.getYear()+1900);  // extract the year from the current date
	var cur_month = now.getMonth();  // extract the current month
	var cur_day = now.getDate();     // extract the current day of the month
	var months_left = (12-cur_month);
	var counter = 1;
	if(id == "year"){
		document.form1.day.options.length = 0;  // reset the day select list everytime year is changed
		document.form1.day.disabled = true;  // disable day select list
		if(selected_year == "blank"){
			document.form1.month.disabled = true;
			document.form1.day.disabled = true;
		}
		else if(selected_year.length == 4){
			document.form1.month.disabled = false;   // enable the month select list
			if(selected_year > cur_year){
				document.form1.month.options.length = 0;  // reset the month select list to having no options, then repopulate
				document.form1.month.options[0] = new Option("", "blank",false,false);
				document.form1.month.options[1] = new Option("January","1",false,false);
				document.form1.month.options[2] = new Option("February","2",false,false);
				document.form1.month.options[3] = new Option("March","3",false,false);
				document.form1.month.options[4] = new Option("April","4",false,false);
				document.form1.month.options[5] = new Option("May","5",false,false);
				document.form1.month.options[6] = new Option("June","6",false,false);
				document.form1.month.options[7] = new Option("July","7",false,false);
				document.form1.month.options[8] = new Option("August","8",false,false);
				document.form1.month.options[9] = new Option("September","9",false,false);
				document.form1.month.options[10] = new Option("October","10",false,false);
				document.form1.month.options[11] = new Option("November","11",false,false);
				document.form1.month.options[12] = new Option("December","12",false,false);
			}
			else if(selected_year == cur_year){
				document.form1.month.options.length = 0;  // reset the month select list to having no options, then repopulate
				document.form1.month.options[0] = new Option("","blank",false,false);
				for(i=cur_month; i<12; i++){
					document.form1.month.options[counter] = new Option(months[i],i+1, false, false);
					counter++;
				}
			}
		}
	}
	else if(id == "month"){
		document.form1.day.disabled = false;  //  enable day select list
		var selected_month = document.getElementById(id).value;  // get the user-selected month
		var now = new Date();  // get the current date
		var now_year = now.getYear()+1900;  // extract year from current date and add 1900 to make it right
		var now_month = now.getMonth()+1;  // extract current month from current date;
		var now_day = now.getDate()-1;  // extract current day of the month from current date
		if(now_month == selected_month && now_year == selected_year){
			var setSelectedDate = new Date(selected_year, (selected_month-1), 1);
			var test = setSelectedDate.getYear()+1900
			var counter = 0;;
			var dateTest = 32 - new Date(test, selected_month-1, 32).getDate();
			document.form1.day.options.length = 0;
			for(d=now_day; d<dateTest; d++){
				document.form1.day.options[counter] = new Option((d+1), (d+1), false,false);
				++counter;
			}
		}
		else{
			var setSelectedDate = new Date(selected_year, (selected_month-1), 1);
			var test = setSelectedDate.getYear()+1900;
			var dateTest = 32 - new Date(test, selected_month-1, 32).getDate();
			document.form1.day.options.length = 0;
			for(d=0; d<dateTest; d++){
				document.form1.day.options[d] = new Option((d+1), (d+1), false,false);
			}
		}
	}
}


// this function dynamically populates the sub categories
function populateSubs( id, sub_arr, arr_cnt ){
	var p_value = document.getElementById(id).value;
	var counter = 0;   // counter - must start at zero and reset at 2
	var len = 0;
	var arr_cnt = (arr_cnt*3);
	var home_arr = new Array(0);
	var auto_arr = new Array(0);
	var garden_arr = new Array(0);
	var personal_arr = new Array(0);
	var pro_arr = new Array(0);
	var other_arr = new Array(0);

	if(p_value != "b"){
		document.form1.subs.disabled = false;
	}
	else if(p_value == 'b'){
		document.getElementById("subs").disabled = true;
	}
	
	var j = 0;
	var counter = 0;
	while( j < arr_cnt ){
		if(sub_arr[j] == 1){
			len = home_arr.length;
			home_arr[len] = sub_arr[j];
			++j;
			++len;
			home_arr[len] = sub_arr[j];
			++j;
			++len;
			home_arr[len] = sub_arr[j];
			++j;
			++len;	
		}
		else if(sub_arr[j] == 2){
			len = auto_arr.length;
			auto_arr[len] = sub_arr[j];
			++j;
			++len;
			auto_arr[len] = sub_arr[j];
			++j;
			++len;
			auto_arr[len] = sub_arr[j];
			++j;
			++len;	
		}
		else if(sub_arr[j] == 3){
			len = garden_arr.length;
			garden_arr[len] = sub_arr[j];
			++j;
			++len;
			garden_arr[len] = sub_arr[j];
			++j;
			++len;
			garden_arr[len] = sub_arr[j];
			++j;
			++len;	
		}
		else if(sub_arr[j] == 4){
			len = personal_arr.length;
			personal_arr[len] = sub_arr[j];
			++j;
			++len;
			personal_arr[len] = sub_arr[j];
			++j;
			++len;
			personal_arr[len] = sub_arr[j];
			++j;
			++len;	
		}
		else if(sub_arr[j] == 5){
			len = pro_arr.length;
			pro_arr[len] = sub_arr[j];
			++j;
			++len;
			pro_arr[len] = sub_arr[j];
			++j;
			++len;
			pro_arr[len] = sub_arr[j];
			++j;
			++len;	
		}
		else if(sub_arr[j] == 6){
			len = other_arr.length;
			other_arr[len] = sub_arr[j];
			++j;
			++len;
			other_arr[len] = sub_arr[j];
			++j;
			++len;
			other_arr[len] = sub_arr[j];
			++j;
			++len;	
		}
	}
	
	// find out which main category was chosen
	var k = 0;
	var m = 1;
	var h = 2;
	document.form1.subs.options.length = 0;  // reset the month select list to having no options, then repopulate
	if(p_value == 1){
		len = home_arr.length;
		if(len > 2){
			for(k=0; k<(len/3); k++){
				document.form1.subs.options[k] = new Option(home_arr[m], home_arr[h], false,false);
				m = m+3;
				h = h+3;
			}
		}
		else { 
			document.form1.subs.options[0] = new Option("None", "none",false,false);
		}
	}
	else if(p_value == 2){
		len = auto_arr.length;
		if(len > 2){
			for(k=0; k<(len/3); k++){
				document.form1.subs.options[k] = new Option(auto_arr[m], auto_arr[h], false,false);
				m = m+3;
				h = h+3;
			}
		}
		else { 
			document.form1.subs.options[0] = new Option("None", "none",false,false);
		}
	}
	else if(p_value == 3){
		len = garden_arr.length;
		if(len > 2){
			for(k=0; k<(len/3); k++){
				document.form1.subs.options[k] = new Option(garden_arr[m], garden_arr[h], false,false);
				m = m+3;
				h = h+3;
			}
		}
		else { 
			document.form1.subs.options[0] = new Option("None", "none",false,false);
		}
	}
	else if(p_value == 4){
		len = personal_arr.length;
		if(len > 2){
			for(k=0; k<(len/3); k++){
				document.form1.subs.options[k] = new Option(personal_arr[m], personal_arr[h], false,false);
				m = m+3;
				h = h+3;
			}
		}
		else { 
			document.form1.subs.options[0] = new Option("None", "none",false,false);
		}
	}
	else if(p_value == 5){
		len = pro_arr.length;
		if(len > 2){
			for(k=0; k<(len/3); k++){
				document.form1.subs.options[k] = new Option(pro_arr[m], pro_arr[h], false,false);
				m = m+3;
				h = h+3;
			}
		}
		else { 
			document.form1.subs.options[0] = new Option("None", "none",false,false);
		}
	}
	else if(p_value == 6){
		len = other_arr.length;
		if(len > 2){
			for(k=0; k<(len/3); k++){
				document.form1.subs.options[k] = new Option(other_arr[m], other_arr[h], false,false);
				m = m+3;
				h = h+3;
			}
		}
		else { 
			document.form1.subs.options[0] = new Option("None", "none",false,false);
		}
	}
}






