/*
=============
FORM AUTOFILL SCRIPT
VERSION: 0.99 BETA
LAST UPDATED: 12/07/09
AUTHOR: RYAN SCHWARTZ
COMPANY: ARCSIGHT
=============
*/

function saveFormValues(frm){
	/* Save the form fields */
	
	// Expire cookie in 999 days.
	var today = new Date();
	var exp   = new Date(today.getTime()+999*24*60*60*1000);
	
	// Loop through form elements and save values
	var n = frm.length;
	for(var i = 0; i < n; i++){
		
		// Skip the field types we do not need to save
		if(frm[i].type == "submit" || frm[i].type == "checkbox" || frm[i].type == "radio" || frm[i].type == "hidden"){
			continue;
		}
		
		// Save the cookie of current form value
		setCookie("ARST_" + frm[i].name, frm[i].type + "||" + frm[i].value, exp); 
	}
	
	return true;
}

function loadFormValues(frm){
	/* Load the form values */
	
	// Loop through form elements and load cookies (if found)
	var n = frm.length;
	for(var i = 0; i < n; i++){
		
		// Get cookie
		var c = getCookie("ARST_" + frm[i].name);
		if(c != null){
			var split = c.split("||");
			var type = split[0];
			var val = split[1];
			
			// Load text values
			if(frm[i].type == "text" || frm[i].type == "select-one" || frm[i].type == "textarea"){
				frm[i].value = val;
			}
		}
	}
	
}

function setCookie(name, value, expires) {
	/* Set cookie */
	document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

function getCookie(name){
	/* Get the cookie */
	var cname = name + "=";               
	var dc = document.cookie;             
	    if (dc.length > 0) {              
	    begin = dc.indexOf(cname);       
	        if (begin != -1) {           
	        begin += cname.length;       
	        end = dc.indexOf(";", begin);
	            if (end == -1) end = dc.length;
	            return unescape(dc.substring(begin, end));
	        } 
	    }
	return null;
}
