function toggle_queue(mode){
	var email_list_box = $("email_list"); 
	var email_queue_box = $("email_queue");
	
	var remove_list_box;
	if(mode == 0){
		// user clicked on tranfer to email queue	
		var exceeded_limit = 0;
		for (var i = email_list_box.options.length-1; i>=0; i--){
			if(email_list_box.options[i].selected){
				if(email_queue_box.options.length < 20){	
					email_queue_box.options[email_queue_box.options.length] = new Option(email_list_box.options[i].text, email_list_box.options[i].value);						
				}
				else{
					// continue to loop so we unselect the ones that was selected to move to queue
					email_list_box.options[i].selected = false;
					if(exceeded_limit == 0){
						window.alert("You cannot exceed more than 20 emails in the queue per submit");
						exceeded_limit = 1;
					}
				}
			}
		} // EO for loop
					
		remove_list_box = email_list_box;				
	}
	else{
		// user clicked on remove from queue
		remove_option_array = Array();
		for (var i = email_queue_box.options.length-1; i>=0; i--){
			if(email_queue_box.options[i].selected){
				email_list_box.options[email_list_box.options.length] = new Option(email_queue_box.options[i].text, email_queue_box.options[i].value);
			}
		} // EO for loop	
		
		remove_list_box = email_queue_box;		
	}
	
	//Remove it because done seperatly here due to ordering issues
	for (var i = remove_list_box.options.length-1; i>=0; i--){
		if(remove_list_box.options[i].selected){
			remove_list_box.options[i] = null;
		}
	} // EO for loop	
}

function queue_to_sent(){
	var email_sent_box = $("email_sent"); 
	var email_queue_box = $("email_queue");	

	for (var i = email_queue_box.options.length-1; i>=0; i--){
		email_sent_box.options[email_sent_box.options.length] = new Option(email_queue_box.options[i].text, email_queue_box.options[i].value);						
	} // EO for loop				
	//Remove it because done seperatly here due to ordering issues
	for (var i = email_queue_box.options.length-1; i>=0; i--){
		email_queue_box.options[i] = null;
	} // EO for loop						
}

function ajax_notify(){
	guest_id_array = new Array();
	var email_queue_box = $("email_queue");
	var email_added_array = new Array();
	for (var i = email_queue_box.options.length-1; i>=0; i--){
		email_queue_value = email_queue_box.options[i].value;
		if(isInteger(email_queue_value)){
			guest_id_array.push(email_queue_value);
		}
		else{
			temp_name =  email_queue_box.options[i].text;
			email_name = temp_name.split("(")[0];
			email_added_array.push(new Array(email_name,email_queue_value));	
		}
	}
	json_str = "";
	if(email_added_array.length){
		json_str = JSON.stringify(email_added_array);
	}
	email_count = guest_id_array.length + email_added_array.length;
	if(email_count > 20){
		window.alert("Error: You have exceeded the tranfer limit of 20 emails per submit");
		return;
	}
	
	if(email_count > 0){

		if (http==null){
		  alert ("Your browser does not support XMLHTTP!");
		  return;
		} 
				
		email_subject = $("subject").value;
		// this is how we can access the wysiwyg message content
		email_message = $jq("#message").val();
		
		if(isBlank(email_subject)){
			window.alert("Email subject cannot be empty");
			$("subject").focus();
			return;
		}
		if(isBlank(email_message)){
			window.alert("Email message cannot be empty");
			$("message").focus();
			return;
		}		
		
		send_self_copy_value = $("send_self_copy").checked ? 1 : 0;
		
		url = "AJAX/transmit_guest_emails.php";
		
		$("ajax_wait").innerHTML = "Processing email request ... please wait";
		//window.alert(url);
		//http.open("POST", url, true); 
		//http.onreadystatechange = handle_return_notify;
		params = "guest_id_list=" + encodeURIComponent(guest_id_array) 
		params += "&guest_added_emails=" + encodeURIComponent(json_str); 
		params += "&subject=" + encodeURIComponent(email_subject); 
		params += "&message=" + encodeURIComponent(email_message);
		params += "&SESSID=" + $("SESSID").value;
		params += "&send_self_copy=" + send_self_copy_value;
		
		//Send the proper header information along with the request
		//http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//http.setRequestHeader("Content-length", params.length);
		//http.setRequestHeader("Connection", "close");	
		//http.send(params);
		
		$jq.ajax({
		   type: "POST",
		   url: url,
		   data: params,
		   success: function(msg){
			 $jq("#log_frame").css("display","block");
			 $jq("#log_div").html(msg);
			 $jq("#ajax_wait").html("");
		   }
		 });		
		
		
	}
	else{
		window.alert("Error: No selected emails found in Email Queue!");
		$("email_list").focus();
	}
}

function handle_return_notify(){
	window.alert(result);
	if(http.readyState == 4 && http.status == 200) {
		result = http.responseText;
		window.alert(result);
		if(result == 'passed'){
			// now move the queue to the email_sent_box
			//window.alert("passed: " + result);
			$("ajax_wait").innerHTML = "";
			queue_to_sent();
			//window.alert('p:'+result);
		}
		else{
			window.alert(result);
		}
	}
}

function add_email(){
	// make sure the name and email is not empty and email is valid
	gname_elm = $("gname");
	gemail_elm = $("gemail");
	
	/*if(isBlank(gname_elm.value)){
		window.alert("Guest name cannot be an empty field");
		gname_elm.focus();
		return;
	}*/
	if(isBlank(gemail_elm.value)){
		window.alert("Guest Email cannot be an empty field");
		gemail_elm.focus();
		return;
	}
	if(!validate_email(gemail_elm.value)){
		window.alert("Guest Email is invalid");
		gemail_elm.focus();
		return;		
	}	

	var email_list_box = $("email_queue");
	text_info = gname_elm.value + " (" + gemail_elm.value+")";
	value = gemail_elm.value;
	email_list_box.options[email_list_box.options.length] = new Option(text_info, value);	
}
