// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var old_faction_id = "0";
var old_army_id = "0";
function update_force_icon() {
	new_faction_id = $('task_force_faction_id').value;
	new_army_id = $('task_force_army_list_id').value;
  if(( new_faction_id!=old_faction_id) || (new_army_id != old_army_id)){
    new Ajax.Request('/task_force/update_force_icon?faction_id=' + new_faction_id + '&army_id=' + new_army_id);
    old_faction_id = new_faction_id;
		old_army_id = new_army_id;
  }
}

function update_faction_select(){
	$('task_force_faction_id').value = army_list_factions[$('task_force_army_list_id').value];
}

function update_army_select(){
	if(army_list_factions[$('task_force_army_list_id').value] != $('task_force_faction_id').value) {
		$('task_force_army_list_id').value = '';
	}
}

var old_section_type = "";
function switch_section_type(section_id, section_type) {
  if(section_type != old_section_type) {
    new Ajax.Request('/section/switch_type/' + section_id + '?type=' + section_type);
    old_section_type = section_type;    
  }

}

function show_spinner(base_id) {
  if($(base_id+'_link')) { Element.hide(base_id+'_link'); }
  Element.show(base_id+'_spinner');
  // return false;
}

function hide_spinner(base_id) {
  Element.hide(base_id+'_spinner');
  if($(base_id+'_link')) { Element.show(base_id+'_link'); }
  if($(base_id+'_anchor')) {$(base_id+'_anchor').blur();}
  // return false;	
}

// Tip Jar Functions
function calc_amount() {
	//(g + .3)/(1 - p);
	if($('cover_fees').checked) {
		return Math.round((parseFloat($('donation_amount').value) + .3)/(1.0 - 0.029)*100.0)/100.0;
		// return Math.round((($('donation_amount').value * 0.029) + .30)*100.0)/100.0;
	} else {
		return parseFloat($('donation_amount').value);
	}
}

function update_total_donation() {
	$('total_donation').value = calc_amount();
}

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}


/* Cookie Management -- borrowed from
http://jason.pureconcepts.net/articles/javascript_cookie_object */
var Cookie = {
  data: {},
  options: {expires: 1, domain: "", path: "/", secure: false},

init: function(options, data) {
  			Cookie.options = Object.extend(Cookie.options, options || {});

  			var payload = Cookie.retrieve();

        if(payload) {
            Cookie.data = payload.evalJSON();
        }
        else {
            Cookie.data = data || {};
        }
        // Cookie.store();
    },
    getData: function(key) {
        return Cookie.data[key];
    },
    setData: function(key, value) {
        Cookie.data[key] = value;
        Cookie.store();
    },
    removeData: function(key) {
        delete Cookie.data[key];
        Cookie.store();
    },
    retrieve: function() {
        var start = document.cookie.indexOf(Cookie.options.name + "=");

        if(start == -1) {
            return null;
        }
        if(Cookie.options.name != document.cookie.substr(start, Cookie.options.name.length)) {
            return null;
        }

        var len = start + Cookie.options.name.length + 1;   
        var end = document.cookie.indexOf(';', len);

        if(end == -1) {
            end = document.cookie.length;
        } 
        return unescape(document.cookie.substring(len, end));
    },
    store: function() {
				alert('Here');
        var expires = '';

        if (Cookie.options.expires) {
            var today = new Date();
            expires = Cookie.options.expires * 86400000;
            expires = ';expires=' + new Date(today.getTime() + expires);
        }

        document.cookie = Cookie.options.name + '=' + escape(Object.toJSON(Cookie.data)) + Cookie.getOptions() + expires;
    },
    erase: function() {
        document.cookie = Cookie.options.name + '=' + Cookie.getOptions() + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    },
    getOptions: function() {
        return (Cookie.options.path ? ';path=' + Cookie.options.path : '/') + (Cookie.options.domain ? ';domain=' + Cookie.options.domain : '') + (Cookie.options.secure ? ';secure' : '');      
    }
};
