var button_state = 1;
var global_help_dialog_holder;
document.onclick = clickme;
var synchronous_call_values = null;
/** Code added by mayura for onclick **/
function clickme(e) {
	//hide_plans();
	//this.stopPropagation();
	var target = (e && e.target) || (event && event.srcElement); 
	//alert(target.id);
	var obj2 = document.getElementById('saved-plans');

	if(target.id != "")
		hide_plans();
	/*if ( target != "")
	{
		if(target.id != "")
			hide_plans();
	}//if*/
	//alert(target.parentNode.id);
	//else
		//alert("not saved");
	
	//alert(this.style.zIndex);
    //if(this.style.zIndex == 999) 
    	//alert ( "hi");
    //alert("mayura");
}

 

/** End of Code added by mayura for onclick **/

function ajax_execute_script(t) {
	if(is_ie()) {
		if(!is_ie6()) {
			window.execScript(t.responseText);
		}
	}
	else {
		eval(t.responseText);
	}
}

/** DISPLAY FUNCTIONS **/
function open_info_box() {
	$('light').style.display='block';
	$('fade').style.display='block';
	fade_in('fade');
	info_box_loading();
}
function info_box_loading() {
	$('white_content_inner').innerHTML = '<img width="24px;" height="24px;" src="/images/ajax-loader.gif">&nbsp;Loading....';
}
function close_info_box() {
	$('light').style.display='none';
	$('fade').style.display='none';
	if($('vidTutorial')){
		
		$('vidTutorial').remove();
	}
}
function populate_info_box(t) {
	var response_arr = new Array();
	var response = t.responseText;
	response_arr = response.split("~|~");
	$('white_content_inner').innerHTML = "<div class='close' onclick='close_info_box();'><img src='/images/delete.gif'></div>" + response_arr[0] ;
	/*if(is_ie()) {
		if(!is_ie6()) {
			window.execScript(response_arr[1]);
		}
	}
	else {
		eval(response_arr[1]);
	}*/
	var dj_global = this; // global scope reference
	if (window.execScript) {

	    window.execScript(response_arr[1]); // eval in global scope for IE
	    return null; // execScript doesn’t return anything
	}
	return dj_global.eval ? dj_global.eval(response_arr[1]) : eval(response_arr[1]);
}


function left_box_loading() {
	$('box-content').innerHTML = '<img width="24px;" height="24px;" src="/images/ajax-loader.gif">&nbsp;Loading....';
}
function left_box_success(t) {
	var response_arr = new Array();
	var response = t.responseText;
	response_arr = response.split("~|~");
	$('box-content').innerHTML = response_arr[0];
	if(is_ie()) {
		if(!is_ie6()) {
			window.execScript(response_arr[1]);
		}
	}
	else {
		eval(response_arr[1]);
	}
}

function right_box_loading() {
	$('box-content-right').innerHTML = '<img width="24px;" height="24px;" src="/images/ajax-loader.gif">&nbsp;Loading....';
}

function process_right_box_success(t, performlastsearch, trigger_synchronous) {
	if(performlastsearch == null || performlastsearch == undefined || performlastsearch == '') {
		// Set default
		performlastsearch = false;
	}
	if(trigger_synchronous == null || trigger_synchronous == undefined || trigger_synchronous == '') {
		// Set default
		trigger_synchronous = false;
	}
	
	var response = t.responseText;

	if(response != '') {
		
		var response_arr = new Array();
	
		response_arr = response.split("~|~");
		
		if(response_arr[0] == 'notready') {
			$('box-content-right').innerHTML = response_arr[1];
			if(synchronous_call_values != null && synchronous_call_values['rel_type'] == 'qualification_id') {
				reset_level_images('');
				close_levels();
				reset_cat_images('occ');
			}
			if(synchronous_call_values != null && synchronous_call_values['rel_type'] == 'occupation_id') {
				reset_cat_images('qual');
			}
		}
		else {
			//turn off all the buttons when you start a plan.
			reset_level_images('');
			close_levels();
			reset_cat_images('');
			fade_out('search-buttons1');
			fade_out('search-buttons2');
			
			button_state = 0;
			if(performlastsearch) {
				perform_last_search();
			}
			$('box-content-right').innerHTML = response_arr[0];
			if(is_ie()) {
				if(!is_ie6()) {
					window.execScript(response_arr[1]);
				}
			}
			else {
				eval(response_arr[1]);
			}
		}
		if(trigger_synchronous) {
			//synchronous call ajax for left box
			rel_type = synchronous_call_values['rel_type'];
			rel_id = synchronous_call_values['rel_id'];
			page = synchronous_call_values['page'];
			search_text = synchronous_call_values['search_text'];
			version_id = synchronous_call_values['version_id'];
			clear_synchronous_variables();
			var action = null;
			if(rel_type == 'occupation_id') {
			
				action = 'SelectOccupation';
			}
			else if(rel_type == 'qualification_id') {
				
				action = 'SelectQualification';
			}
			var params = 'ajax_action='+action+'&'+rel_type+'=' + rel_id + '&page=' + page + '&search_text=' + search_text;
			if(version_id != null && version_id != 'undefined') {
				params += '&version_id=' + version_id;
			}
			new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
		}
	}
	else {
		$('box-content-right').innerHTML = 'empty';
	}
}

/**
 * processes ajax response for the right-hand box and also reloads the left-hand box.
 * @param t
 * @return
 */
function right_box_success(t) {
	process_right_box_success(t, true, false);
}

/**
 * processes ajax response for the right-hand box but doesn't reload the left-hand box. 
 * @param response text
 * @return
 */
function right_box_success_simple(t) {
	process_right_box_success(t, false, false);
}


function right_box_success_simple_force_synchronous(t) {
	process_right_box_success(t, false, true);
}
function print_plans() {
	var params = 'ajax_action=PrintPlans';
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:plans_success});
}
function plans_success(t) {
	$('saved-plans-content').innerHTML = t.responseText;
}

function show_tutorial(display_type){
	var type = 'menu';
	if(display_type == 'automatic'){
		type = display_type;
	}
	open_info_box();
	$('white_content_inner').innerHTML = "<div class='close' onclick='close_info_box();'><img src='/images/delete.gif'></div>" + '<iframe id="vidTutorial" name="vidTutorial" border="none" frameborder="0" src="/video.php?display_type='+type+'" scrolling="no" style="border:none;width:100%;height:100%;"></iframe>';
}

function showBorkenTutorial( ){
	open_info_box();
	$('white_content_inner').innerHTML = "<div class='close' onclick='close_info_box();'><img src='/images/delete.gif'></div>" + '<iframe id="vidTutorial" name="vidTutorial" border="none" frameborder="0" src="/video_broken.php" scrolling="no" style="border:none;width:100%;height:100%;"></iframe>';
}
function tutorial_status(checked) {
	var params = 'ajax_action=TutorialStatus&show=' + checked;
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:plans_success});
}
/** DISPLAY FUNCTIONS **/



/** SEARCH FUNCTIONS **/
function select_occupation(occupation_id, page, search_text, version_id) {
	if(button_state == 0) {
		new_plan();
		return;
	}
	
	close_levels();
	//reset images
	reset_level_images(''); 
	reset_cat_images('occ'); 
	
	if(occupation_id != '') {
		
		right_box_loading();
		left_box_loading();
		set_synchronous_variables('occupation_id', occupation_id, page, search_text, version_id);
		//load the sample plan
		var params = 'ajax_action=SelectOccupationPlan&occupation_id=' + occupation_id + '&page=' + page + '&search_text=' + search_text;
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success_simple_force_synchronous});
	}
	else {
		// IF not occupation ID, need to call this here.
		left_box_loading();
		
		var params = 'ajax_action=SelectOccupation&occupation_id=' + occupation_id + '&page=' + page + '&search_text=' + search_text;
		if(version_id != null && version_id != 'undefined') {
			params += '&version_id=' + version_id;
		}
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
	}
	
}

function set_synchronous_variables(rel_type, rel_id, page, search_text, version_id) {
	synchronous_call_values = new Array();
	synchronous_call_values['rel_type'] = rel_type;
	synchronous_call_values['rel_id'] = rel_id;
	synchronous_call_values['page'] = page;
	synchronous_call_values['search_text'] = search_text;
	synchronous_call_values['version_id'] = version_id;
}

function clear_synchronous_variables() {
	synchronous_call_values = null;
}

function select_category(category_id, page, search_text) {
	if(button_state == 0) {
		new_plan();
		return;
	}
	
	close_levels();
	//turn search button on and off
	reset_level_images(''); 
	reset_cat_images('cat'); 
	
	//from add button - switch to occupation results
	if(category_id != '') {
		reset_level_images(''); 
		reset_cat_images('occ'); 
	}
	
	left_box_loading();
	var params = 'ajax_action=SelectCategory&category_id=' + category_id + '&page=' + page + '&search_text=' + search_text;
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
}
function checkQualNeedOccupation(qualification_id) {
	
	var params = 'ajax_action=checkQualNeedOccupation&qualification_id=' + qualification_id;
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess: function(transport) {
		var result = transport.responseText;
		return result;
	}});
	
}
function checkOccupationSelected() {
	var result = false;
	var params = 'ajax_action=checkOccupationSelected';
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess: function(transport) {
		result = transport.responseText;
		return result;
	}});
	
}
function checkQualificationSelected() {
	var result = false;
	var params = 'ajax_action=checkQualificationSelected';
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess: function(transport) {
		result = transport.responseText;
		return result;
	}});
	
}
function select_qualification(qualification_id, page, search_text,version_id) {
	if(button_state == 0) {
		new_plan();
		return;
	}

	if(qualification_id == '') {
		open_levels();
		reset_level_images(''); 
		reset_cat_images('qual'); 
		
		left_box_loading();
		var params = 'ajax_action=SelectQualification&qualification_id=' + qualification_id + '&page=' + page + '&search_text=' + search_text;
		
		if(version_id != null && version_id != 'undefined') {
			params += '&version_id=' + version_id;
		}
		
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
	}
	else {
		
		right_box_loading();
		left_box_loading();
		
		set_synchronous_variables('qualification_id', qualification_id, page, search_text, version_id);
		//load the sample plan
		var params = 'ajax_action=SelectQualificationPlan&qualification_id=' + qualification_id + '&page=' + page + '&search_text=' + search_text;
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success_simple_force_synchronous});
		
	}
		
}

function select_skillset(skillset_id, page, search_text, version_id) {
	if(button_state == 0) {
		new_plan();
		return;
	}
	
	if(skillset_id == '') {
		close_levels();
		reset_level_images(''); 
		reset_cat_images('skill'); 
	}
	else {
		
		right_box_loading();
		//load the sample plan
		var params = 'ajax_action=SelectQualificationPlan&qualification_id=' + skillset_id + '&page=' + page + '&search_text=' + search_text;
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success_simple});
	}
	
	left_box_loading();
	var params = 'ajax_action=SelectSkillset&skillset_id=' + skillset_id + '&page=' + page + '&search_text=' + search_text;
	if(version_id != null && version_id != 'undefined') {
		params += '&version_id=' + version_id;
	}
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});	
}

//acts as a search and an add to plan function.
function select_unit(unit_id, page, search_text, use_qual, rule_id, green_only) {	
	var params = 'ajax_action=SelectUnit&unit_id=' 	+ unit_id + '&page=' + page + '&search_text=' + search_text + '&use_qual=' + use_qual;
	
	if(rule_id != null && rule_id != 'undefined') {
		params += '&rule_id=' + rule_id;
	}
	if(green_only != null && green_only != 'undefined' && green_only == true) {
		params += '&green_only=' + green_only;
	}
	
	//adding to the plan
	if(unit_id != '') {
		
		right_box_loading();
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success});
		
	}
	//searching for units.
	else {
		left_box_loading();
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
	}
}

function filter_green_unit(on) {
	var selected_rule = $('rule_filter').value;
	select_unit('',1,'','',selected_rule,on);
}

function add_custom_unit(rule_id) {
	
	if(rule_id != null && rule_id != '') {
		//var name = prompt("Please type in the unit name");
		//var code = prompt("Please type in the unit code (optional)");
		
		var title = $('custom_unit_title_' + rule_id).value;
		var code = $('custom_unit_code_' + rule_id).value;
		
		if(title != null && title != "") {
			var params = 'ajax_action=AddCustomUnit&unit_title='+ title +'&unit_code='+ code + '&rule_id=' + rule_id;
			
			//adding to the plan
			right_box_loading();
			new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success_simple});
		}
		else {
			alert("Please specify unit title (required)");
		}
	}
}

function remove_custom_unit(custom_unit_id) {
	if(custom_unit_id != null && custom_unit_id != "") {
		var params = 'ajax_action=RemoveCustomUnit&custom_unit_id='+ custom_unit_id;
		
		//remove from plan
		right_box_loading();
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success_simple});
	}
}

function perform_last_search() {
	left_box_loading();
	var params = 'ajax_action=PerformLastSearch';
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
}

function select_level(level_id, page, search_text,version_id) {
	if(button_state == 0) {
		new_plan();
		return;
	}
	left_box_loading();
	var params = 'ajax_action=SelectLevel&level_id=' + level_id + '&page=' + page + '&search_text=' + search_text;
	if(version_id != null && version_id != 'undefined') {
		params += '&version_id=' + version_id;
	}
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:left_box_success});
}

//returns a value used in the unit seach
//need to know if the search should search the qualification or search for global units.
function use_qualification_search() {
	var use_qual = document.getElementsByName('search_qualification');
	var uq = 0;
	
	if(use_qual[0].checked && use_qual[0].value == 1) {
		uq = 1;
	}
	else if (use_qual[0].checked && use_qual[0].value == 0) {
		uq = 0;
	}
	else if (use_qual[1].checked && use_qual[1].value == 1) {
		uq = 1;
	}
	else if (use_qual[1].checked && use_qual[1].value == 0) {
		uq = 0;
	}
	return uq;
}
/** SEARCH FUNCTIONS **/


/** LOAD/SAVE FUNCTIONS **/
function remove_unit(unit_id) {
	right_box_loading();
	var params = 'ajax_action=RemoveUnit&unit_id=' + unit_id;
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success});
}

function print_current_plan() {
	var params = 'ajax_action=GetCurrentPlanId';
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:print_current_success});
}

function print_current_success(t) {
	var plan_id = t.responseText;
	var is_int = parseInt(plan_id);
	
	if(is_int == 0) {
		alert('Nothing to print');
	}
	else {
		window.location = '/user/plan.php?plan_id=' + plan_id;
	}
}

function load_plan(plan_id) {
		
	right_box_loading();
	var params = 'ajax_action=LoadPlan&plan_id=' + plan_id;
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:right_box_success_simple});
	
	
	button_state = 0;
	open_close_plans();
}

function copy_plan(plan_id, title) {
	
	if (title != null && title != "") {
		var params = 'ajax_action=SavePlanAs&plan_id=' + plan_id + '&plan_title=' + title;
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:populate_info_box});
		$('save_button').disabled = 'true';
	}
}

function save_plan() {
	var plan_form = $('SavePlan');
	var params = plan_form.serialize();
    new Ajax.Request(plan_form.action, {method:'post', postBody:params, onSuccess:populate_info_box});
    info_box_loading();
    print_plans();
}
function save_current_plan() {
	var params = 'ajax_action=GetCurrentPlanId';
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:save_current_plan_success});
}
function save_current_plan_success(t) {
	var plan_id = t.responseText;
	var is_int = parseInt(plan_id);
	
	if(is_int == 0) {
		alert('Nothing to save');
	}
	else {
		show_info('plan_id', is_int);
		print_plans();
	}
}
function new_plan() {
	if(confirm('Are you sure you wish to create a new plan. You will lose any changes you have made since last saving.')) {
		window.location = '/user/index.php?new=1';
	}
}

function delete_plan(plan_id) {
	if(confirm('Are you sure you wish to delete this plan?')) {
		$('plan_row_' + plan_id).innerHTML = '<img width="24px;" height="24px;" src="/images/ajax-loader.gif">';
		var params = 'ajax_action=DeletePlan&plan_id=' + plan_id;
		new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:ajax_execute_script});
	}
}

/** LOAD/SAVE FUNCTIONS **/

function recommend_friend() {
	var email_form = $('Recommend');
	var params = email_form.serialize();
    new Ajax.Request(email_form.action, {method:'post', postBody:params, onSuccess:populate_info_box});
    info_box_loading();
}

/** INFO FUNCTIONS **/
function show_info(rel_type, rel_id) {
	open_info_box();
	var params = 'ajax_action=ShowInfo&rel_type=' + rel_type + '&rel_id=' + rel_id;
	new Ajax.Request('/includes/processes/user_ajax_handler.php', {method:'post', postBody:params, onSuccess:populate_info_box});
}
/** INFO FUNCTIONS **/


/** EFFECTS **/
function open_close_plans() {
	
	if($('saved-plans-content').style.display == 'none') {
		open_plans();
	}
	else {
		hide_plans();
	}
}
function showdiv(div) {
	var temp = $(div);
	if(temp.style.display == 'none'){
		temp.style.display = 'block';
		return false;
	}
}
function hidediv(div) {
	var temp = $(div);
	if(temp.style.display = 'block') {
		temp.style.display = 'none';
		return false;
	}
}
function show_hide(div) {
	var temp = $(div);
	if(temp.style.display == 'none'){
		temp.style.display = 'block';
		return false;
	}else{
		temp.style.display = 'none';
		return false;
	}
}

function show_hide_animated(div){
	var temp = $(div);
	if(temp.style.display == 'none'){
		Effect.BlindDown(div);
		return false;
	}else{
		Effect.BlindUp(div);
		return false;
	}
}

function open_plans() {
	if($('saved-plans-content').style.display == 'none') { 
		Effect.BlindDown('saved-plans-content', { duration: 0.3 });  
		return false; 
	}
}

function overwrite_hide_plans() {
	clearTimeout(timeout); 
	$('saved-plans-content').show();
}

function hide_plans() {
	if($('saved-plans-content') != null) {
		timeout = setTimeout('$(\'saved-plans-content\').hide();', 500);
	}
}

function open_levels() {
	if($('search-buttons2').style.display == 'none') { 
		Effect.BlindDown('search-buttons2', { duration: 0.5 });  
		return false; 
	}
}
function close_levels() {
	if($('search-buttons2').style.display != 'none') { 
		Effect.BlindUp('search-buttons2', { duration: 0.5 });  
		return false; 
	}
}

function fade_out(div_id){
	Effect.Fade(div_id, {from: 1, to: 0.2});
}

function fade_in(div_id){
	Effect.Fade(div_id, {from: 0, to: 0.3});
}

/*
function showHelpDialog( ){	
	removeHelpTimeout( );
	if(document.getElementById('help-dialog').style.display == 'none'){
		Effect.BlindDown('help-dialog', { duration: 0.3 });
	}
}

function hideHelpDialog( ){
	alert('hide');
	global_help_dialog_holder = setTimeout("hideHelpDialogNext", 1000); 	
}

function hideHelpDialogNext( ){
	alert('close');
	removeHelpTimeout( );
	Effect.BlindUp('help-dialog', { duration: 0.3 });
}

function removeHelpTimeout( ){
	//clearTimeout(global_help_dialog_holder);
}
*/
/* Credits: Stu Nicholls */
/* URL: http://www.stunicholls.com/menu/pro_dropdown_2/stuHover.js */



/** EFFECTS **/
