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

function toggle_different_address() {
	element = $('permanent_address');
	element.toggle()
	if (element.visible()) {
		
	}
}

function toggle_expertise(id) {
	element = $(id)
	element.toggle()
}

function select_usa() {
	$('united_states').show()
}	

function select_canada() {
	$('united_states').hide()
	$('camp_region').value = ""
}

function change_country(el, within_el) {
	if (el.value == "United States" || el.value == "Canada") {
		Selector.findChildElements($(within_el), ['.international_fields']).each(function(e) {e.hide();})
		Selector.findChildElements($(within_el), ['.us_and_canada_fields']).each(function(e) {e.show();})
		Selector.findChildElements($(within_el), ['.us_state']).each(function(e) {e.enable();})
		Selector.findChildElements($(within_el), ['.intl_state']).each(function(e) {e.disable();})
	} else if (el.value != ""){
		Selector.findChildElements($(within_el), ['.international_fields']).each(function(e) {e.show();})
		Selector.findChildElements($(within_el), ['.us_and_canada_fields']).each(function(e) {e.hide();})
		Selector.findChildElements($(within_el), ['.us_state']).each(function(e) {e.disable();})
		Selector.findChildElements($(within_el), ['.intl_state']).each(function(e) {e.enable();})
	}
}

function select_region(region) {
	$('camp_region').value = region
}

function enable_faceted_search() {
	$$('.search_facet').each(function(s) {
	  s.onclick = submit_search
	})
}

function enable_first_150() {
	$$('.first_150').each(function(s) {
	  if(s.text.length > 5) {
		alert( "text > 5");
		}
	})
}

// function get_current_section() {
// 	section = "camps"
// 	if (document.location.href.indexOf("/seekers") >= 0) {
// 		section = "seekers"
// 	}
// 	return section;
// }

function submit_search(element) {
	//section = get_current_section();
	//path = "/" + section + "/search"
	var path = $('search_form').action;

	auth_token = window.AUTH_TOKEN ? '&authenticity_token=' + encodeURIComponent(AUTH_TOKEN) : ""

	new Ajax.Updater({success:'results'}, path, {asynchronous:true, evalScripts:true, 
	  parameters: Form.serialize($('search_form')) + auth_token});
}

function run_saved_search(element) {
	//section = get_current_section();
	var path = $('search_form').action + "/saved/";
	document.location = path + element.value;
}
	
function add_expertise() {
	var el = $('expertise_selected');	
	var val = $('expertise_options').value;
	var i;
	for (i = 0; i < el.length; i++) {
		if (el.options[i].value == val) {
			return;
		}
	}
	
	if (el.length >= 10) {
		alert("You may only select 10 areas of expertise");
		return;
	}

	add_option_to_select(el, val, val);
	add_hidden_expertise(val);
	update_top_skills_lists();
}

function add_option_to_select(select, label, value) {
	var elOptNew = document.createElement('option');
	  elOptNew.text = label;
	  elOptNew.value = value;
	
	try {
	    select.add(elOptNew, null); // standards compliant; doesn't work in IE
	  }
	  catch(ex) {
	    select.add(elOptNew); // IE only
	  }
}

function add_hidden_expertise(val) {
	var f = $('registration')
	
	var newHidden = document.createElement('input');
	newHidden.name = "registration[profile_attributes][fields][expertise][]";
	newHidden.value = val;
	newHidden.type = 'hidden'
	
	f.appendChild(newHidden);
}

function update_top_skills_lists() {
	var el = $('expertise_selected')	
	var i;
	var selected = [];
	for (i = 0; i < el.length; i++) {
		selected[i] = el.options[i].value; 
	}
	
	for (i = 0; i < 3; i++) {
		update_top_skills_list(i, selected);		
	}
}

function update_top_skills_list(num, values) {
	var el = $('top_skills_title_' + num);
	var prev_val = el.value
	while (el.length > 0) {
		el.remove(0);
	}
	
	var i;
	for (i=0; i < values.length; i++) {
		var val = values[i];
		add_option_to_select(el, val, val);
	}
	el.value = prev_val;
	
	if ($('top_skills_desc_' + num).value == "") {
		el.value = values[num]
	}
}

function remove_all_expertise() {
	var el = $('expertise_selected');
	el.multiple = true;
	var i;
	var val = null;
	for (i = el.length - 1; i>=0; i--) {
		el.options[i].selected = true;
	}
	
	for (i = el.length - 1; i>=0; i--) {
	    if (el.options[i].selected) {
	      	val = el.options[i].value;
			remove_hidden_expertise(val);
			el.remove(i);
	    }
	  }
	
	update_top_skills_lists();
}

function remove_expertise() {
	var el = $('expertise_selected')
	var i;
	var val = null;
	for (i = el.length - 1; i>=0; i--) {
	    if (el.options[i].selected) {
	      	val = el.options[i].value;
			el.remove(i);
			remove_hidden_expertise(val);
	    }
	  }

	update_top_skills_lists();
}

function remove_hidden_expertise(val) {
	var inputs = $('registration').getElementsByTagName('input');
	var i;
		
	for (i = inputs.length - 1; i>=0; i--) {
	    if (inputs[i].type == "hidden" && inputs[i].value == val) {
			inputs[i].parentNode.removeChild(inputs[i]);
	    }
	  }
}

function show_all_text(el) {
	el.parentNode.hide();
	el.parentNode.parentNode.getElementsByClassName('all_text')[0].show();
}	

function show_first_150(el) {
	el.parentNode.hide();
	el.parentNode.parentNode.getElementsByClassName('first_150')[0].show();
}


function search_select_all() {
	$$('.seeker').each(function(s){s.checked=true;});
}

function search_clear_all() {
	$$('.seeker').each(function(s){s.checked=false;});
}

function show_preview() {
	$('email_preview').show();
}

function show_save_profiles() {
	$('save_profiles_popup').show();
}

function close_save_profiles() {
	$('save_profiles_popup').hide();
}


function show_message(el, event) {
	var messageNode = el.parentNode.select(".message")[0];
	messageNode.style.left = (event.pageX - 400) + "px";
	messageNode.style.top = (event.pageY - 50) + "px";
	messageNode.show();
}

function close_preview() {
	$('email_preview_email').hide();
}

function toggle_profiles(id,act) {
	element = $(id); 
	element2 = $(id+'edit'); 
	if (act=='save') {
		newval = element2.getElementsByTagName('textarea')[0].value;
		//element.getElementsByTagName('span')[0].innerHTML = newval;
		//element.getElementsByTagName('a')[0].innerHTML = (newval == '') ? ("Add "+id.replace(/profile\d+/,"").replace(/s$/,"(s)")) : "Edit";
	}
	if (act!='save') {
		element.toggle(); 
		element2.toggle();
	}
}

function select_payment_method(el) {
	if ($('payment_method_credit').checked) {
		$('credit_card_entry').show();
	} else {
		$('credit_card_entry').hide();
	}
}

function clear_search_selections() {
	$('search_form').reset();
	submit_search();
}

function update_tags() {
	$('update_tags').onsubmit();
}

function replace_erb(o) {
	o = o.replace(/&lt;%=/g, "<%=");
    o = o.replace(/%&gt;/g, "%>");
	return o;
}