var spi_deposit_base_path = "/";

function prepare_date_select_box (discount_allowed,discounted_price)
{	
	// grab select box
	var select = document.getElementById("edit-attributes-1");
	var options = select.options;
	//var length = options.length;
	
	// grab today's date
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	
	
	
	// go through each option in the select box, if the option is before today's date, remove it
	
	var option_date;
	var option_date_year;
	var option_date_month;
	var option_date_day;
	var option_remove = false;
	
	for (var i = 0; i < options.length; i++)
	{
		option = options[i].text;
		
		if (option != "Please select")
		{
			option_remove = false;
			
			// drop any leading zeros for integer comparrison from the current option's text
			option_split = option.split("-");
			option_date_year = option_split[0];
			option_date_month = option_split[1]; if (option_date_month.length == 2 && option_date_month.substr(0,1) == "0") { option_date_month = option_date_month.substr(1,1); }
			option_date_day = option_split[2]; if (option_date_day.length == 2 && option_date_day.substr(0,1) == "0") { option_date_day = option_date_day.substr(1,1); }
			
			if (option_date_year > year)
			{
				// date is ok
			}
			else if (option_date_year == year)
			{
				if (option_date_month > month)
				{
					// date is ok
				}
				else if (option_date_month == month)
				{
					if (option_date_day >= day)
					{
						// date is ok
					}
					else
						option_remove = true;		
				}
				else
					option_remove = true;		
			}
			else
				option_remove = true;
				
			if (option_remove)
			{
				select.remove(i);
				// make up for lost index...
				i--;
			}
		}
	}
	
	if (discount_allowed)
	{
		// are the remaining dates in the select box 10 days before today? if they are, display the discounted price
		option_date;
		option_date_year = "";
		option_date_month = "";
		option_date_day = "";
		option_remove = false;
		
		//var today_in_milliseconds = date.getTime();
		var today_in_milliseconds = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
		//var today_in_milliseconds_backup = today_in_milliseconds;
		//alert(today_in_milliseconds);
		// remove the hours , minutes and seconds from today_in_milliseconds
		//today_in_milliseconds -= (date.getSeconds() + 1)*1000;
		//alert((date.getSeconds() + 1) + " " + (date.getSeconds() + 1)*1000);
		//today_in_milliseconds -= (date.getMinutes() + 1)*60*1000;
		//alert((date.getMinutes() + 1) + " " + (date.getMinutes() + 1)*60*1000);
		//today_in_milliseconds -= (date.getHours() + 1)*60*60*1000;
		//alert((date.getHours() + 1) + " " + (date.getHours() + 1)*60*60*1000);
		
		var ten_days_from_now_in_milliseconds = today_in_milliseconds + (1000*60*60*24*10);
		
		var option_in_milliseconds;
		
		for (var i = 0; i < options.length; i++)
		{
			option = options[i].text;
			
			if (option != "Please select")
			{
				// convert this option to milliseconds
				option_split = option.split("-");
				option_date_year = option_split[0];
				option_date_month = option_split[1]; if (option_date_month.length == 2 && option_date_month.substr(0,1) == "0") { option_date_month = option_date_month.substr(1,1); }
				option_date_day = option_split[2]; if (option_date_day.length == 2 && option_date_day.substr(0,1) == "0") { option_date_day = option_date_day.substr(1,1); }
				option_in_milliseconds = Date.UTC(option_date_year, option_date_month-1, option_date_day);
				
				//alert("backu = " + today_in_milliseconds_backup + "\ntoday = " + today_in_milliseconds + "\ntenda = " + ten_days_from_now_in_milliseconds + "\noption = " + option_in_milliseconds);
				//alert("today = " + today_in_milliseconds + "\ntenda = " + ten_days_from_now_in_milliseconds + "\noption = " + option_in_milliseconds);
				
				if (option_in_milliseconds >= ten_days_from_now_in_milliseconds)
				{
					options[i].text += " ( " + discounted_price + " ) ";
				}
				else
				{
					// this option can't be discounted
					// if this is the only option left, then hide the discount announcement
					if (options.length == 2)
						document.getElementById("ftatv_discount").style.display = "none";
				}
				
			}
		}
	}
}

// after a user clicks the make a deposit button, the button grabs all the form data and uses 
// javascript to redirect the user to the deposit product page with all the form data appended in the url.
// this function is executed after the deposit page loads
function spi_deposit_form_prepopulate ()
{	
	if (document.referrer.indexOf("deposit") >= 0)
		return;
	
	var something_strange = false;
	
	var url_gets = location.href.split("&");
	var length = url_gets.length;
	
	var null_value_present = false;
	
	var url_get = "";
	for (var i = 1; i < length; i++)
	{
		url_get = url_gets[i];
		url_get_split = url_get.split("=");
		
		if (url_get_split.length != 2)
		{
			something_strange = true;
			break;
		}
		else
		{
		
			url_key = url_get_split[0];
			url_value = spi_url_decode(url_get_split[1]);
			
			switch (url_key)
			{
				case "class_date":
					if (!spi_deposit_form_set_drop_down("edit-attributes-1",url_value))
					{
						window.location = spi_deposit_base_path + "cart";
					}
					break;
				case "class":
					if (!spi_deposit_form_set_drop_down("edit-attributes-2",url_value.replace(/_/g," ")))
					{
						window.location = spi_deposit_base_path + "cart";
					}
					break;
				case "name":
					document.getElementById("edit-attributes-3").value = url_value;
					break;
				case "age":
					document.getElementById("edit-attributes-13").value = url_value;
					break;
				case "a1":
					document.getElementById("edit-attributes-4").value = url_value;
					break;
				case "a2":
					document.getElementById("edit-attributes-5").value = url_value;
					break;
				case "city":
					document.getElementById("edit-attributes-6").value = url_value;
					break;
				case "state":
					spi_deposit_form_set_drop_down("edit-attributes-7",url_value);
					break;
				case "zip":
					document.getElementById("edit-attributes-8").value = url_value;
					break;
				case "email":
					document.getElementById("edit-attributes-10").value = url_value;
					break;
				case "phone":
					document.getElementById("edit-attributes-9").value = url_value;
					break;
				case "fax":
					document.getElementById("edit-attributes-11").value = url_value;
					break;
				case "occupation":
					document.getElementById("edit-attributes-12").value = url_value;
					break;
				default:
					break;
			}
			
		}
	}
	
	if (!something_strange)
	{
		document.getElementById("edit-submit-105").click();
	}
	else
	{
		window.alert('There was a problem preparing your deposit form, please try again. If the problem persists, please contact support@signalpoint.net, thanks!');
		window.location = window.location = spi_deposit_base_path + "cart";
	}
}

function spi_deposit_form_set_drop_down (select_id,text)
{
	var select = document.getElementById(select_id);
	
	var save_value;
	var save_text;
	
	var found = false;
	var found_index = 0;
	
	var options;
	
	if (select != null)
	{
		options = select.options;
		
		for (var i = 0; i < options.length; i++)
		{
			if (options[i].text.toLowerCase() == text.toLowerCase())
			{
				found = true;
				
				// auto select state
				if (select_id == "edit-attributes-7")
					select.selectedIndex = i;
				
				save_value = options[i].value;
				save_text = options[i].text;
			}
		}
	}
	
	// don't remove options from state drop down menu
	if (found && select_id != "edit-attributes-7")
	{
		options.length = 0;
		option = document.createElement('option');
		option.value = save_value;
		option.text = save_text;
		
		try 
		{
      		select.add(option, null); // standards compliant; doesn't work in IE
    	}
    	catch (ex) 
    	{
      		select.add(option, 0); // IE only
    	}
    	
    	return true;
	}
	else
		return false;
	
}

function spi_init_deposit_drop_down ()
{	
	// class date
	//listen("change",document.getElementById("edit-attributes-1"),spi_update_deposit_link);	
}

function spi_update_deposit_link ()
{
	// e.g. url deposit&class_date=2008-08-23&class=advanced_carbine
	
	var link = document.getElementById("ftatv_deposit_link");
	
	var select = document.getElementById("edit-attributes-1");
	var option = select.options[select.selectedIndex];
	var option_text = option.text;
	
	if (option_text == "Please select")
	{
		link.value = "";
		return;
	}
	
	// does the option have yyyy-mm-dd ( $xx.xx ) in it?
	if (option_text.indexOf("(") != -1)
	{
		// parse out the date
		option_text = option_text.substring(0,option_text.indexOf("(") - 1);
		//alert(option_text);
	}
	
	link.value = spi_deposit_base_path + "deposit&class_date=" + option_text + "&class=" + spi_deposit_class_name;
	
	//alert(link.value);
	
}

function spi_deposit_button_click ()
{
	var link = document.getElementById("ftatv_deposit_link");
	
	// verify all form input for deposit
	var error = false;
	var msg = "";
	
	// class date
	var class_date = document.getElementById("edit-attributes-1");
	var option = class_date.options[class_date.selectedIndex];
	var option_text = option.text;
	
	if (option_text == "Please select")
	{
		error = true;
		msg += "Please select a class date.\r\n";
	}
	else if (option_text.indexOf("(") != -1)
	{
		// the option has yyyy-mm-dd ( $xx.xx ) in it
		// parse out the date
		option_text = option_text.substring(0,option_text.indexOf("(") - 1);
	}
	class_date = option_text;
	
	// name
	var name = document.getElementById("edit-attributes-3").value;
	if (name == "")
	{
		error = true;
		msg += "Please enter your name.\r\n";
	}
	
	// age
	var age = document.getElementById("edit-attributes-13").value;
	if (age == "")
	{
		error = true;
		msg += "Please enter your age.\r\n";
	}
	
	// address line 1
	var a1 = document.getElementById("edit-attributes-4").value;
	if (a1 == "")
	{
		error = true;
		msg += "Please enter your address.\r\n";
	}
	
	// address line 2
	var a2 = document.getElementById("edit-attributes-5").value;
	
	// city
	var city = document.getElementById("edit-attributes-6").value;
	if (city == "")
	{
		error = true;
		msg += "Please enter your city.\r\n";
	}
	
	// state
	var state = document.getElementById("edit-attributes-7");
	var option = state.options[state.selectedIndex];
	var option_text = option.text;
	
	if (option_text == "Please select")
	{
		error = true;
		msg += "Please select your state.\r\n";
	}
	state = option_text;
	
	// zip
	var zip = document.getElementById("edit-attributes-8").value;
	if (zip == "")
	{
		error = true;
		msg += "Please enter your zip code.\r\n";
	}
	
	// email
	var email = document.getElementById("edit-attributes-10").value;
	
	// phone
	var phone = document.getElementById("edit-attributes-9").value;
	if (phone == "")
	{
		error = true;
		msg += "Please enter your phone number.\r\n";
	}
	
	// fax
	var fax = document.getElementById("edit-attributes-11").value;
	
	// occupation
	var occupation = document.getElementById("edit-attributes-12").value;
	
	if (error)
	{
		alert(msg);
		return;
	}
	else
	{
		var deposit_url = spi_deposit_base_path + "deposit";
		
		deposit_url += "&class_date=" + class_date;
		deposit_url += "&class=" + spi_deposit_class_name;
		deposit_url += "&name=" + name;
		deposit_url += "&age=" + age;
		deposit_url += "&a1=" + a1;
		deposit_url += "&a2=" + a2;
		deposit_url += "&city=" + city;
		deposit_url += "&state=" + state;
		deposit_url += "&zip=" + zip;
		deposit_url += "&email=" + email;
		deposit_url += "&phone=" + phone;
		deposit_url += "&fax=" + fax;
		deposit_url += "&occupation=" + occupation;
		
		window.location = deposit_url;
	}
}




// Cross-browser implementation of element.addEventListener()
function listen(evnt, elem, func) 
{

	if (elem.addEventListener) 
	{
		// W3C DOM
		elem.addEventListener(evnt,func,false);
	}
	else if (elem.attachEvent) 
	{ 
		// IE DOM
		var r = elem.attachEvent("on"+evnt, func);
		return r;
	}
	else 
		window.alert('Please upgrade your browser, it is not compatable with our website. Please contact support@signalpoint.net if you have any questions, thanks!');

}

function exec_register_student_again_link (id)
{
	try
	{
		var text_box = document.getElementById(id);
		text_box.disabled = false;
		var qty = parseInt(text_box.value);
		var new_qty = qty + 1;
		text_box.value = new_qty;
		document.getElementById('edit-update').click();
	}
	catch (ex)
	{
		alert("Please upgrade your internet browser! Please contact us if you have any questions, thanks!");
	}
}

function spi_url_decode (input)
{
	var output = input;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;

}




