


function saveLocations() {
//set up to save locations to a cookie
    //set up a maxCount - only ten allowed
	var maxCountReached = false;
	var maxCookieCount = 8;  //set a max count for number of cookies stored
	var i;
	var cookieExists = false;  //does cookie exist already? if not, don't store
	var addressString = "";
	var parseString = ":@@:";
	var storeFromCookie = false;
	var storeToCookie = false;
	
    //get data from form to create the cookie
    var startpoint = document.main.startpoint.value;
	var startcity = document.main.startcity.value;
	var startstate = document.main.startstate.value;
	var startzip = document.main.startzip.value;
      
	var endpoint = document.main.endpoint.value;
	var endcity = document.main.endcity.value;
	var endstate = document.main.endstate.value;
	var endzip = document.main.endzip.value;
	
	
    if (window.navigator.cookieEnabled) {
        //see how many past places the user has used
     
        var count = getCookie('count');
        var cookieCount = getCookie('cookieCount');
      
        if (!count) {
            count = 0;
        }//end if !count
   
        count = parseInt(count);
        cookieCount = parseInt(cookieCount);
        
        
        if (cookieCount == maxCookieCount) {
		    //maxCountReached is true - 10 saved
		    maxCountReached = true;
		    if (count == maxCookieCount) {
		        //start over
		        count = 0;
		    }//end if count = 0
        }//end if cookieCount == maxCookieCount
        
        if (document.main.elements['fromErrorCode'].value != "3") {
	        storeFromCookie = true;
	    }
	    if (document.main.elements['toErrorCode'].value != "3") {
	        storeToCookie = true;
	    }
	
        if (storeFromCookie) {
            if (startpoint != "") {
            //find out if startpoint exists already or not
            
            cookieExists = checkForExistingCookie(startpoint, cookieCount);
		
            //store startpoint info
            //if cookie does not exist store
           
            if (!cookieExists) {
                count++;
			    addressString = startpoint + parseString + startcity + parseString + startstate + parseString + startzip;
				
                if (maxCountReached) {
			        //if we have 10 stored, leave count at 10 - we've already overwritten the 
			        //previous ones
			        setCookie('cookieCount', maxCookieCount);
			    
			    
			        //move cookies down one spot to make room for new
			        moveCookiesDown(cookieCount);
			          
			        //now put current info in top spot
                    setCookie('address1',addressString); 
		    	}
		    	else {
		    	    //else just set to count and add cookies
		    	    setCookie('count',count);
                    setCookie('address'+count,addressString);
                    setCookie('cookieCount', count);
		    	}//end if maxCountReached
		    }//end if !cookieExists
            }//end if startpoint != ""
        }//end if storeFromCookie
        
        
        if (count == maxCookieCount) {
		    //start over
		    count = 0;
		}//end if count == maxCookieCount
		    
		if (storeToCookie){    
            if (endpoint != "") {
            //store endpoint info
            cookieExists = checkForExistingCookie(endpoint, cookieCount);
           
            if (!cookieExists) {
                count++; 
                
                addressString = endpoint + parseString + endcity + parseString + endstate + parseString + endzip;
                
                if (maxCountReached) {
			        //if we have 10 stored, leave count at 10 - we've already overwritten the 
			        //previous ones
			        setCookie('cookieCount', maxCookieCount);
		    	   
			    
			        //move cookies down one spot to make room for new
			        moveCookiesDown(cookieCount);
			    
			        //now put current info in top spot
                    setCookie('address1',addressString); 
			    }    
			    else {
			        //else just set to count and add cookies
			        setCookie('count',count);
                    setCookie('address'+count,addressString);
                    
                    //setCookie('city'+count,endpoint);
                    //setCookie('city'+count,endcity);
                    //setCookie('state'+count,endstate);
                    //setCookie('zip'+count,endzip);
                  
			        setCookie('cookieCount', count);
			    }//end if maxCountReached
            }//end if !cookieExists
            }//end if endpoint != ""
        }//end if storeToCookie
    }//end if window.navigator.cookieEnabled
    
    return true;
}



function checkForExistingCookie(cookieValue, cookieCount) {
	//if cookie exists, return false - we don't want to store the same cookie twice
    var i;
    for (i = 1; i <= cookieCount; i++) {
	    
      var address = getCookie('address'+i);
	  var addressArray = new Array();
      addressArray = address.split(':@@:');
      address = addressArray[0];
      
      
	    if (address) {
	        if (address.toUpperCase() == cookieValue.toUpperCase()) {
		        return true;
		    }
	    }	
    } 
    //ccokie does not exist
    return false;
}

function moveCookiesDown(cookieCount){
    //to make room for new cookies - these will appear at the top of the list, pushing old down one.
    for (i = cookieCount-1; i >= 1; i--) {
        //loop through and push the previous results down 1 so that new cookies
        //start from top of list box
	    j = i + 1;
        var address = getCookie('address'+i);
       // var city = getCookie('city'+i);
       // var state = getCookie('state'+i);
       // var zip = getCookie('zip'+i);
        
        if (address) {            
            setCookie('address'+j,address); 
            //setCookie('city'+j,city);
          //  setCookie('state'+j,state);
           // setCookie('zip'+j,zip); 	              	
        } 
     }
}

