function $(id) {
	return document.getElementById(id);
}

function profileUpdater(user,profile,form){
	new Ajax.Request('/users/'+user+'/profiles/'+profile+'/improve', {asynchronous:true, evalScripts:true, method:'put', parameters:Form.serialize(form)});
}

function set_experience(years) {
	var id = 'y_' + years;
	$('filter_years').value = years;
	for (var i=0; i <= 20; i++) {
		$('y_'+i).style.backgroundImage = "";
		$('y_'+i).style.padding = "";
	};
	$(id).style.backgroundImage    = 'url(/images/account_creation/years_circle.gif)';
	$(id).style.backgroundRepeat   = 'no-repeat';
	$(id).style.backgroundPosition = (('' + years + '').length <= 1 ? '-5px' : '-1px') + ' 2px';
	$(id).style.padding = '10px';
	update_climber_count();
}

function set_industry(action,id,values,add_to_field) {
	var parent_div = $('industries_box');
	
	if (action == 'add') {
		var values   	 = values || $('industries').value
		var split_values = values.split("|");
		var to_add   = split_values[0];
		var elmnt_id = 'i_' + split_values[1];
		
		if (parent_div.hasChildNodes()) {
			for (var i = 0; i < parent_div.childNodes.length; i++) {
				if (parent_div.childNodes[i].attributes['id'].nodeValue == elmnt_id) {
					alert("The Industry \"" + to_add + "\" has already been added.")
					return false;
				}
			}
		}

		var link = document.createElement('a');
		    link.href =    "javascript:void(0)";
		    link.onclick = function() { set_industry('remove',''+elmnt_id+'',""+escape(values)+"") };
		    link.appendChild( document.createTextNode('[remove]') );

		var div  = document.createElement('div');
			div.id = elmnt_id;
			div.appendChild(document.createTextNode(to_add+' '));
			div.appendChild(link);
	
		parent_div.appendChild(div);
		occupation_select(split_values[1], 'filter_form');
		if (add_to_field) { $('filter_industries').value += values + "#" };
		update_climber_count();
	} 
	
	else if (action == 'remove' && id && values) {
		parent_div.removeChild($(id));
		$('filter_industries').value = $('filter_industries').value.replace(unescape(values)+"#","");
		update_climber_count();
	}
	
	else if (action == 'reset') { 
		all_values = $('filter_industries').value.split("#")
		for (var i=0; i < (all_values.length -1); i++) {
			set_industry('add','',all_values[i].replace("#",""))
		};
	}
}

function set_occupation(action,id,values,add_to_field,field_name_id) {
	var parent_div = $('occupations_box');
	
	if (!field_name_id) { field_name_id = 'filter_occupations'; };
	
	if (action == 'add') {
		var values   	 = values || $('occupations').value
		var split_values = values.split("|");
		var to_add   = split_values[0];
		var elmnt_id = 'o_' + split_values[1];
		
		if (!values) { 
			alert("Plese add some Industries first.");
			return false;
		};
		
		if (parent_div.hasChildNodes()) {
			for (var i = 0; i < parent_div.childNodes.length; i++) {
				if (parent_div.childNodes[i].attributes['id'].nodeValue == elmnt_id) {
					alert("The Occupation \"" + to_add + "\" has already been added.")
					return false;
				}
			}
		}

		var link = document.createElement('a');
		    link.href =    "javascript:void(0)";
		    link.onclick = function() { set_occupation('remove',''+elmnt_id+'',""+escape(values)+"") };
		    link.appendChild( document.createTextNode('[remove]') );

		var div  = document.createElement('div');
			div.id = elmnt_id;
			div.appendChild(document.createTextNode(to_add+' '));
			div.appendChild(link);
	
		parent_div.appendChild(div);
		if (add_to_field) { $(field_name_id).value += values + "#" }
		update_climber_count();
	} 
	
	else if (action == 'remove' && id && values) {
		parent_div.removeChild($(id));
		$(field_name_id).value = $(field_name_id).value.replace(unescape(values)+"#","");
		update_climber_count();
	}
	
	else if (action == 'reset') { 
		all_values = $(field_name_id).value.split("#")
		for (var i=0; i < (all_values.length -1); i++) {
			set_occupation('add','',all_values[i].replace("#",""))
		};
	}
}

function update_climber_count() {
	url_str = "";
	frm = document.forms['filter_form']
	if (frm) {
		frm = frm.elements;
		for(x=0; x<frm.length; x++) {
			if(frm[x].id.match(new RegExp(/filter_/))) {
				url_str += frm[x].name + "=";
				if (frm[x].name == "filter[roles][]") {
					value = getSelected(frm[x]);
				} else {
					value = escape($(frm[x].id).value);
				}
				url_str += value + "&";
			}
		}
		new Ajax.Updater('climber_count', '/filters/count/?' + url_str, { method: 'get' });
	}
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;
	var items = "";
	for (var intLoop=0; intLoop < opt.length; intLoop++) {
		if (opt[intLoop].selected) {
			index = selected.length;
			selected[index] = new Object;
			selected[index].value = opt[intLoop].value;
			selected[index].index = intLoop;
		}
	}
	for (var i=0; i < selected.length; i++) {
		if(selected[i].value) {items += selected[i].value + ","}
	};
	return items;
}

sfHover = function() {
  if (document.getElementById("menubar"))
  {
	  var sfEls = document.getElementById("menubar").getElementsByTagName("LI");
	  for (var i=0; i<sfEls.length; i++) {
	    sfEls[i].onmouseover=function() {
	      this.className+=" sfhover";
	    }
	    sfEls[i].onmouseout=function() {
	      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
	    }
	  }
  }
}
if (window.attachEvent && document.getElementById("menubar")) window.attachEvent("onload", sfHover);

function disable_buttons(form) {
	for(x=0; x<form.elements.length; x++) { 
		if(form.elements[x].type=='submit' || form.elements[x].type=='button' || form.elements[x].type=='image') {form.elements[x].disabled=true}		
	}

}

function check_max_length(obj,display_div){
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : "";
	$(display_div).innerHTML = obj.value.length;
	if (obj.getAttribute && obj.value.length>mlength) {obj.value=obj.value.substring(0,mlength)}
	
}

function stickynotes(action,id,name) {
	if (action == 'show') {
		Tips.hideAll();
		
		$('stickynote_handle').innerHTML = "<h2>Sticky Notes</h2>";
		new Ajax.Request("/postits/"+id, {method: 'get', asynchronous:true, evalScripts:true});
		$('stickynote').style.display = 'block';
		$('stickynote').style.visibility = 'visible';
		$('stickynote').style.zIndex = '999';
	}
	if (action == 'hide') {
		$('stickynote').style.display = 'none';
		$('stickynote').style.visibility = 'hidden';
	}
}

function CCollection() {
     var lsize = 0;
	 var fields = new Array();

     this.add = _add;
     this.remove = _remove;
     this.size = _size;
     this.get = _get;

     function _add(newItem) {
          if (newItem == null) return;

          lsize++;
          fields[(lsize - 1)] = newItem;
     }

     function _remove(index) {
          if (index < 0 || index > fields.length - 1) return;
          fields[index] = null;

          for (var i = index; i <= lsize; i++)
               fields[i] = fields[i + 1];

          lsize--;
     }
     function _size() { return lsize }
     function _get( index )
     {
         if (index < 0 || index > fields.length - 1) return null;
	     return fields[index];
	 }
}

////////////////////////////////////////////
// MAINLY REGISTRATION FUNCTIONS - BEGIN //
//////////////////////////////////////////

var ii = 0;
function swap_quote() {
	var quotes = Array("\"Thank you Climber.com.\" <br> - <em>Phil P., Accounting & Finance Recruiter</em>", 
					   "\"Finding candidates just got easier.\" <br> - <em>Mark G., IT Recruiter</em>", 
					   "\"I love the site, great job!\" <br> - <em>José H., Recruiter - Volt</em>", 
					   "\"Climber.com is a phenomenal product, it helps you find candidates that are NO WHERE else\" <br> - <em>Marie B. IT Recruiter</em>", 
					   "\"Climber.com has highly-qualified candidates that also appear to be a strong fit for our workplace environment.\" <br> - <em>Rodney Moses, VP of Talent Acquisition, Coca-Cola Enterprises</em>", 
					   "\"I loaded my jobs into Climber.com and within 24 hours I was interviewing quality Java Developers\" <br> - <em>Monty M. American Education Corporation</em>", 
					   "\"Climber.com works with clients to solve problems and offer value added advice and products.\" <br> - <em>James S., Talent Acquisition</em>");
	$('quotes').innerHTML = quotes[ii];
	ii ++;
    setTimeout("swap_quote()", 8000);
	if (ii >= quotes.length) { ii = 0 };
}

function update_plan_cost(){
	if (document.forms['choose_plan'] && document.forms['choose_plan'].elements['plan']){
		plans = document.forms['choose_plan'].elements['plan'];
		for (i=0; i< plans.length; i++) { 
			if (plans[i].checked==true) { 
				$('plan_cost').innerHTML = plans[i].value.split("|")[1];
				$('plan_id').value = plans[i].value.split("|")[0];
			} 
		}
	}
}

function togg(le, value){
    document.getElementById('select_plan_tab').className = '';
    document.getElementById('select_plan_tab').innerHTML = 'Select This Plan';
    document.getElementById('selectplus_plan_tab').className = '';
    document.getElementById('selectplus_plan_tab').innerHTML = 'Select This Plan';
    document.getElementById('pro_plan_tab').className = '';
    document.getElementById('pro_plan_tab').innerHTML = 'Select This Plan';
    document.getElementById('enterprise_plan_tab').className = '';
    document.getElementById('enterprise_plan_tab').innerHTML = 'Select This Plan';

    //document.getElementById('select_plan_content').className = 'inactive_block';
   // document.getElementById('selectplus_plan_content').className = 'inactive_block';
   // document.getElementById('pro_plan_content').className = 'inactive_block';
   // document.getElementById('enterprise_plan_content').className = 'inactive_block';

    document.getElementById(le + '_tab').className = 'selected';
    document.getElementById(le + '_tab').innerHTML = 'Selected';
    //document.getElementById(le + '_content').className = 'active_block';
 	document.getElementById('plan_id').value = value;
}

function togg2(le, value){
    document.getElementById('select_plan_tab').className = 'tab_inactive';
    document.getElementById('selectplus_plan_tab').className = 'tab_inactive';
    document.getElementById('pro_plan_tab').className = 'tab_inactive';
    document.getElementById('enterprise_plan_tab').className = 'tab_inactive';

    document.getElementById('select_plan_content').className = 'inactive_block';
    document.getElementById('selectplus_plan_content').className = 'inactive_block';
    document.getElementById('pro_plan_content').className = 'inactive_block';
    document.getElementById('enterprise_plan_content').className = 'inactive_block';

    document.getElementById(le + '_tab').className = 'tab_active';
    document.getElementById(le + '_content').className = 'active_block';
    document.getElementById('plan_id').value = value;
}

function planSelector(){
	var jobs      = parseInt(document.forms['questions'].elements['number_of_jobs'].value);
	var seo    	  = false;
	var templates = false;
	
	var seo_questions = document.forms['questions'].elements['seo_question'];
	for (i=0; i< seo_questions.length; i++) { if (seo_questions[i].checked==true && seo_questions[i].value == 'yes') { seo = true } }

	var template_questions = document.forms['questions'].elements['templates_q'];
	for (i=0; i< template_questions.length; i++) { if (template_questions[i].checked==true && template_questions[i].value == 'yes') { templates = true } }
	
	if (jobs == 3 && seo == false && templates == false) { togg('select_plan', '6'); };
	if (jobs == 3 && seo == true && templates == false) { togg('selectplus_plan', '7') };
	if (jobs == 3 && seo == true && templates == true) { togg('pro_plan', '8') };
	
	if (jobs == 6 && seo == false && templates == false) { togg('selectplus_plan', '7') };
	if (jobs == 6 && seo == true && templates == false) { togg('selectplus_plan', '7') };
	if (jobs == 6 && seo == true && templates == true) { togg('pro_plan', '8') };
	
	if (jobs == 10) { togg('pro_plan', '8') };
	if (jobs > 10) { togg('enterprise_plan', '9') };
	
}

//////////////////////////////////////////
// MAINLY REGISTRATION FUNCTIONS - END //
////////////////////////////////////////


function simpleTooltip(element,t,content){
  new Tip(element, unescape(content), {
	title: unescape(t),
    stem: 'leftMiddle',
    hook: { tip: 'leftMiddle', target: 'rightMiddle', mouse: false },
    width: 250
  });
  $(element).prototip.show();
}

function climberFlyout(element,t,content){
  new Tip(element, $(content).cloneNode(true), {
	title: 'Profile Actions',
    stem: 'leftMiddle',
    hook: { tip: 'leftMiddle', target: 'rightMiddle', mouse:false },
    width: '160',
    showOn: 'click',
	closeButton: true,
	hideOn: { element: 'closeButton', event: 'click'},
	offset: { x: 0, y: 10 }
  });
  $(element).prototip.show();
}

function autoSelectPlan(plan){
	if (plan.match(new RegExp(/\+/))) { togg('select_plan', '6') }
	else if (plan.match(new RegExp(/Select/))) { togg('selectplus_plan', '7') }
	else if (plan.match(new RegExp(/Pro/))) { togg('pro_plan', '8') }
	else if (plan.match(new RegExp(/Enterprise/))) { togg('enterprise_plan', '9') };
}

function autoSelectPlan2(plan){
	if (plan.match(new RegExp(/\+/))) { togg2('select_plan', '6') }
	else if (plan.match(new RegExp(/Select/))) { togg2('selectplus_plan', '7') }
	else if (plan.match(new RegExp(/Pro/))) { togg2('pro_plan', '8') }
	else if (plan.match(new RegExp(/Enterprise/))) { togg2('enterprise_plan', '9') };
}


//////////////////////////////////////////
//       SEARCH GOODIES - START        //
////////////////////////////////////////

function searchFlyout(element,title,id){
  new Tip(element, {
	title: unescape(title),
	target: $(element),
	ajax: {
		url: "/climbers/tooltip/" + id,
		options: {
			method: 'get'
		}
	},
    stem: 'leftMiddle',
    hook: { tip: 'leftMiddle', target: 'rightMiddle', mouse:false },
    width: '160',
    showOn: 'click',
	closeButton: true,
	hideOn: { element: 'closeButton', event: 'click'},
	offset: { x: 0, y: 10 },
	hideOthers: true
  });
}

function loadClimberStats(climbers){
	for (var i=0; i < climbers.length; i++) {
		new Ajax.Request("/climbers/stats/"+climbers[i], {method: 'get', asynchronous:true, evalScripts:true});
	};
}
