//----------- COOKIE FUNCTIONS ---------------------------------------------
function SetCookie (name,value,expires) {
  var lifetime = 360; //-- DEFAULT, IN DAYS
  if (typeof(expires))  lifetime=expires;
  var todayDate = new Date();
  var expireDate = new Date(todayDate.getTime() + (lifetime*24*3600*1000)); //-- MILLISECONDS
  document.cookie=(name + '=' + escape(value) + '; expires=' + expireDate.toGMTString());
}

function EraseCookie (name) {
  if ( document.cookie && ((document.cookie.indexOf(name))>-1) ) {
      var lifetime = -1; //-- IN DAYS
      var todayDate = new Date();
      var expireDate = new Date(todayDate.getTime() + (lifetime*24*3600*1000)); //-- MILLISECONDS
      document.cookie=(name + '=' + ' ' + '; expires=' + expireDate.toGMTString());
  }
}

function GetCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

//               __   _   _____  _        __
//------------- |  \ | | |  ___|\ \  /\  / / ----------------------------------------------------------------------------------
//------------- | |\\| | |  __|  \ \//\\/ / -----------------------------------------------------------------------------------
//------------- |_| \__| |_____|  \_/  \_/ -------    NEW    FUNCTIONS    ---------------------------------------------
//----------- SAVE FORM DATA TO COOKIE ------------------------------------------
//--CONSTRUCT A STRING IN THE FORMAT Plan_Name=Fairmont&Roof=metal ...
function SaveForm (theForm) { 
	var formName = theForm.name;
	var string = "";
	var numFields = theForm.length;
	for (i = 0; i < numFields; i++) {
		fieldName     = theForm[i].name;
		fieldId     = theForm[i].id;
		fieldType    = theForm[i].type;
		fieldData = "";
		
		// TYPICAL FIELDS:
		if ((fieldType == "text") || (fieldType == "textarea") || (fieldType == "hidden") || (fieldType == "select-one")) {
	    	fieldData = theForm.elements[fieldName].value;
		}

		// CHECKBOX
		if (fieldType == "checkbox") {
			if (theForm.elements[fieldName].checked) {var setvalue = "1";} else {var setvalue = "0";}
		    fieldData = setvalue;
		}

		// RADIO BUTTON
		if (fieldType == "radio") {
			if (theForm[i].checked) fieldData = theForm[i].value;  //-- ONLY SET IF A SELECTED RADIO BUTTON
		}

		if (fieldData) {  //-- fieldData WILL BE EMPTY IF THIS WAS A NON-SELECTED RADIO BUTTON
            if (string.length > 0) string += '&';  //--SEPARATOR
    		string += fieldName + "=" + fieldData;
		}
	}
    SetCookie(formName, string, 360); //--EXPIRE IN 360 DAYS
}


//----------- READ DATA FROM COOKIE INTO AN ARRAY --------------------------------------------------------------------
//----------- WILL RETURN EG: cookieElements['Player_Select'] = 'mov' ETC ------------
function ArrayFromCookie (formName) {
	//if (typeof(theForm) == 'string') {var formName = theForm;} else {var formName = theForm.name}
    var cString = GetCookie(formName);
    if ( !cString ) return false;
    var fieldArray = cString.split("&",200); //-- | SEPARATES FORM FIELDS
    var cookieElements = new Array();
    var fieldName = "";
    var fieldValue = "";
    for (i in fieldArray) {
        var temp = fieldArray[i].split("=",2); //-- = SEPARATES FIELD NAME FROM VALUE
        fieldName = temp[0];
        fieldValue = temp[1];
        cookieElements[fieldName] = fieldValue;
    }
    return cookieElements;
}


//----------- LOAD DATA INTO FORM FROM ARRAY; SKIP SOME FIELDS ------
function FillFormExcept (theForm,cookieElements,skipNames) {
    for (thisFieldName in cookieElements) {
        var doThis = true;
        var selectFlag = false;
        var thisCookieValue = cookieElements[thisFieldName]; 
        for (n in skipNames) {
            if (skipNames[n] == thisFieldName) doThis = false;
        }
        var thisFormElement = theForm.elements[thisFieldName];
        if ( thisCookieValue && thisFormElement && doThis ) {
            if (thisFormElement.length) {  //--  RADIO BUTTON OR SELECT MENU:
                //-- NOTE!: IF thisFormElement IS A COLLECTION OF RADIO BUTTONS, IT DOESN'T HAVE A type PROPERTY ITSELF...
                //-- ...BUT EACH ELEMENT (=BUTTON) DOES.  BUT IF IT'S A SELECT MENU, THE OPPOSITE IS TRUE!
                if (thisFormElement.type == 'select-one') selectFlag = true; 
                for (x=0; x < thisFormElement.length; x++) {
                    if ( selectFlag ) { //--THEN thisCookieValue CONTAINS THE VALUE OF THE OPTION WHICH NEEDS TO BE selected 
				        if (thisFormElement[x].value==thisCookieValue) thisFormElement[x].selected=true;
				    } else {
                        if (thisFormElement[x].type == 'radio') { //-- EACH RADIO BUTTON DOES HAVE A type PROPERTY:
                            //--THEN thisCookieValue CONTAINS THE VALUE OF THE BUTTON WHICH NEEDS TO BE checked
        				    if (thisFormElement[x].value==thisCookieValue) thisFormElement[x].checked=true;
        				}
				    }
				}   //-- END if (thisFormElement.length)
            } else if (thisFormElement.type == 'checkbox') {
                thisFormElement.checked = thisCookieValue;
            } else { 
                //-- SO fieldData CONTAINS THE VALUE OF THE FIELD i
                thisCookieValue = thisCookieValue.replace(/\\/g, "");
                thisFormElement.value = thisCookieValue;
            }
        }   //-- END if ( thisCookieValue && thisFormElement && doThis
    }   //-- END for (thisFieldName in cookieElements)
}


