//This file includes the core javascript required for the portal. 
//It also sets up HTTP headers that force visitors browsers to cache the javascript and will gzip compress the javascript if the zlib library is available in PHP
//The files included are:
// prototype.js
// scriptaculous/effects.js
// DynamicOptionList.js (used for drill down custom fields)
// jscalendar/calendar_stripped.js (used for date custom fields)
// jscalendar/lang/calendar-en.js (used for date custom fields)
// jscalendar/calendar-setup.js (used for date custom fields)
document.write('<script type="text/javascript" src="http://helpdesk.keymetricsoft.com/js/c_portaljs_2.7.0.php"></script>');

//Function that shows appropriate custom fields for each category
function ShowCategoryCustomFields(category_id){

	//Find the ID of the currently selected category
	var selected_category = category_id ? category_id : ($("xCategory") ? $F("xCategory") : 0);
	
	//Create javascript array of custom field ID's, excludes always visible custom fields
	var custom_field_ids = new Array("2","3","4");
	var field_count = custom_field_ids.length;
	
	//Create javascript array of custom fields each category should display
	var category_custom_fields = new Array();
		category_custom_fields[4] = new Array("");
		category_custom_fields[3] = new Array("");
		category_custom_fields[2] = new Array("");
	
	//Hide all fields on category change
	for(i=0;i < field_count;i++){
		if($("Custom" + custom_field_ids[i] + "_wrapper")) $("Custom" + custom_field_ids[i] + "_wrapper").hide();
	}
		
	//Show fields which are for this category
	if(selected_category != 0){			
		var custom_field_len = category_custom_fields[selected_category] ? category_custom_fields[selected_category].length : 0;
		for(i=0;i < custom_field_len;i++){
			if($("Custom" + category_custom_fields[selected_category][i] + "_wrapper")){
				$("Custom" + category_custom_fields[selected_category][i] + "_wrapper").show();
			}
		}
	}	
}
 
//Function that resets a portal login password
function ChangePortalLoginPassword(){

	//Find the new password they've set
	var password_new = $F('new_password');
	
	//Find the password confirmation they've set
	var password_confirm = $F('new_password_confirm');
	
	//Check if the password and the password confirm field match and that the password is not empty.
	if(password_new != password_confirm || password_new.empty()){
		//Popup an alert to notify the user that the passwords must match
		show_feedback("Passwords must match. Please try again.","error");
	}else{
		//Everything is OK so send the new password to the server
		new Ajax.Request("index.php?pg=password.change", {
			method: "post",
			parameters: {password: password_new},
			onSuccess: function(transport) {
				show_feedback("Password saved","success");
				
				//Hide the password box and clear the form fields
				$("change_password_box").hide();
				$("new_password").value = "";
				$("new_password_confirm").value = "";
			},
			onFailure: function(transport){
				show_feedback("Could not save password","error");
			}			
		});
		
	}
}

//Function that sends the retrieve password email
function RetrievePortalLoginPassword(){
	//If there's no email in the box show feedback that an email needs to be entered
	if($F("login_email").empty()){
		show_feedback("Please enter your email","error");
		return;
	}else{
		//Change link text to loading
		$("retrievePortalPasswordLink").update('<span class="sending_note">Sending...</span>');
		
		//An email is available so send the password email
		new Ajax.Request("index.php?pg=password.retrieve", {
			method: "post",
			parameters: {login_email: $F("login_email")},
			onSuccess: function(transport) {
				show_feedback("Password sent. Please check your email account.","success");
			},
			onFailure: function(transport){
				show_feedback("Please enter a valid email","error");
			},
			onComplete: function(){
				//Remove sending text
				$("retrievePortalPasswordLink").update();			
			}
		});
	}
}

//Function to create a feedback box at the top of the right column. 
function show_feedback(message,type){
	//Style the feedback box as appropriate for each type of feedback
	if(type == "error"){
		$("feedback_box").addClassName("feedback_box_error");
	}else{ //By default shows positive feedback
		$("feedback_box").addClassName("feedback_box_positive");
	}
	
	//Show the box
	$("feedback_box").show();
	
	//Insert message into the feedback box
	$("feedback_box").update(message);
}