//<!--
//
//  saveInput (v2)
//
//  Purpose: To save form input
//
//  Description: This script saves user input to a cookie so that they don't
//  have to re-enter their data on an unsuccessful submission (I.e., error pages
//  that ask a user to hit the back button to add missing data). The script
//  supports saving text, textarea, password, radio, checkbox, select-one and
//  select-multiple input types.
//
//  Usage: To use this script you must do three things:
//
//    1. Ensure that all your form controls are uniquely named. This includes
//       button, submit and reset controls.
//    2. Add the following line of code to the source of your article's body:
//       <script type="text/javascript" src="http://libraries.ucsd.edu/_files/javascript/ucsdlib_saveInput_v2.js"></script>
//    3. Call "saveInput(this)" from your form's onSubmit event.
//       - E.g., <form onSubmit="saveInput(this)">
//
//-->

var cookie_name = "UCSD_" + encodeURI(window.location.pathname);
var cookie_lifetime_mins = 15;

var delimiter_inner = "::";
var delimiter_outer = "||";

//---
// saveInput
//---

var saveInput = function(form){

	var form_control_list = [];

	for(var i = 0; i < form.elements.length; i++){
		form_control_list = form_control_list.concat(form.elements[i].name);
	}

	form_control_list.unique();

	var cookie_data = "";

	for (var i = 0; i < form_control_list.length; i++){
		
		var control_collection = document.getElementsByName(form_control_list[i]);
		var control = control_collection[0];
		var values = "";

		if (control.type == "text" || control.type == "password" || control.type == "textarea" || control.type == "select-one"){
			if (control.value != "" && control.name != "recaptcha_response_field"){
				cookie_data += control.name + delimiter_inner + encodeURI(control.value) + delimiter_outer;
			}
		}
		else if (control.type == "radio" || control.type == "checkbox"){
			for (var x = 0; x < control_collection.length; x++){
				(control_collection[x].checked) ? values += "1" : values += "0";
			}
			cookie_data += control.name + delimiter_inner + encodeURI(values) + delimiter_outer;
		}
		else if (control.type == "select-multiple" && control.value != ""){
			for (var x = 0; x < control.length; x++){
				(control[x].selected) ? values += "1" : values += "0";
			}
			cookie_data += control.name + delimiter_inner + encodeURI(values) + delimiter_outer;
		}
	}

	saveInput.setCookie(cookie_name, cookie_data, cookie_lifetime_mins);
}

//---
// saveInput: Get Cookie Value
//---

saveInput.getCookie = function(name){
	var re = new RegExp(name + "=[^;]+", "i");
	if (document.cookie.match(re)){
		return document.cookie.match(re)[0].split("=")[1];
	}
	return "";
}

//---
// saveInput: Set Cookie Value
//---

saveInput.setCookie = function(name, data, lifetime_mins){
	var expiration_date = new Date();
	expiration_date.setTime(expiration_date.getTime() + (lifetime_mins * 60 * 1000));
	document.cookie = name + "=" + data + "; expires=" + expiration_date.toGMTString() + ";";
}

//---
// restoreInput
//---

var restoreInput = function(){

	var my_cookie = saveInput.getCookie(cookie_name).split(delimiter_outer);

	for (var i = 0; i < my_cookie.length; i++){
	
		var field_name  = my_cookie[i].split(delimiter_inner)[0];
		var field_value = my_cookie[i].split(delimiter_inner)[1];
		var control_collection = document.getElementsByName(field_name);
		var control = control_collection[0];

		if (control != null){
			if (control.type == "text" || control.type == "password" || control.type == "textarea" || control.type == "select-one"){
				control.value = decodeURI(field_value);
			}
			else if (control.type == "radio" || control.type == "checkbox"){
				var values = decodeURI(field_value);
				for (var x = 0; x < control_collection.length; x++){
					(values.charAt(x) == "1") ? control_collection[x].checked = true : control_collection[x].checked = false;
				}
			}
			else if (control.type == "select-multiple"){
				var values = decodeURI(field_value);
				for (var x = 0; x < control.length; x++){
					(values.charAt(x) == "1") ? control[x].selected = true : control[x].selected = false;
				}
			}
		}
	}
}

//---
// Main: Restore form data on page load
//---

if (window.addEventListener){
	window.addEventListener("load", restoreInput, false);
}
else if (window.attachEvent){
	window.attachEvent("onload", restoreInput);
}
else if (document.getElementById){
	window.onload = restoreInput;
}

//----------------------------------------------------------
// 3rd party functions
//----------------------------------------------------------

// .unique: Removes duplicate items from an array.
//   Usage:
//     var myArray = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
//     myArray.unique();
//   Source: http://www.martienus.com/code/javascript-remove-duplicates-from-array.html (free to use and redistribute)

Array.prototype.unique = function(){
	var skip = 0;
	o:for(var i = 0, n = this.length; i < n; i++){
		for(var x = 0; x < i; x++){
			if(this[x]==this[i]){
				skip++;
				continue o;
			}
		}
		this[i - skip] = this[i];
	}
	this.length -= skip;
}

