/*
|---------------------------------------------------------------
| LOAD CMS FUNCTIONS & PLUGINS
|---------------------------------------------------------------
|
| the function that loads the essential CMS funtions and plugins
|
| The esential plugins:
|
| @list_items - embed in the body
| @edit_listed - a popup when you click the button
| @load_form - embed in the body
| @post_form & @process_posted - related to @edit_listed & @load_form on submit
| @delete_item & @process_deleted - embed in the body, applied to listed items and uploaded items
|
*/
this.load_cms = function(){	
	
	if (($('#cms').length > 0)&&($('#menu-admin-header').length == 0)) 
	{
		// Attach the login plugin.
		$.fn.get_login_admin();
	}
	
	if ($('#invite').length > 0) 
	{
		// Attach load_form_account plugin.
		$.fn.load_form_account({popupWidth:800});
		//load_invite_account_accept();
		//$(document).load_form_account({popupWidth:800});
	} 
	
	if ($('#reset').length > 0) 
	{
		// Attach load_form_account plugin.
		$.fn.load_form_account({popupWidth:600});
		//load_reset_account_action();
		//$(document).load_form_account({popupWidth:600});	
	} 
	
	if ($('#cms').length == 0)
	{
		$("#menu-admin-header").remove();
		//load_edit_embed();
	} 
	else 
	{
		
		$("#container, #supersize, #tab-background,#tab-contact-me").remove();
		
		$("html,body").css({
			fontFamily:'Arial, Helvetica, sans-serif',
			fontWeight:'normal',
			fontSize: '12px',
			height: '100%',
			background:'#ffffff',
			color:'#676764',
			textAlign: 'left'
		});
		
		if($('#menu-admin-header').length > 0)
		{
			$(document.body).append("<div id=\"container-cms\"><div id=\"body-cms\" class=\"align-center\"></div></div><div id=\"footer-cms\"></div>");
			var target_body = $('#body-cms');
			var target_footer = $('#footer-cms');
		
			target_body.html('<div><p><img src="'+http_root+rp_global_image+image_loader+'"/> loading</p></div>');
			target_body.load( http_root+rp_cms+"page_summary.php", function(){
				target_footer.load( http_root+rp_cms+"menu_admin_mysql_footer.php", function(){
					reset_element_admin_menu();
					get_guide();
					toggle_see_more_profile();
					load_approval();
					toggle_nextparent();
					
					$('#menu-admin-header > ul > li > a:first,#menu-admin-footer > ul > li > a:first').get_dashboard();
					$('.list-item').list_item({container:'#body-cms'});
					$('.load-form').load_form({container:'#body-cms'});
					//load_edit_embed();
				});	
			});
			//$("#menu-admin-header").css({display:'block'});
		}
		
	}
}

/*
|---------------------------------------------------------------
| RESET ADMIN MENU ELEMENT FUNCTIONS
|---------------------------------------------------------------
|
*/

this.reset_element_admin_menu = function(){	
			
	/**
	do not return the drop-down:level 1 when you click on it, accept the last item.
	
	$('#menu-admin-header > ul > li:not(:last) > a,#menu-admin-footer > ul > li:not(:last) > a').click(function(){
		return false;
	});
	
	$('.menu-admin > ul > li > a').click(function(){
		return false;
	});**/
	
	$('.menu-admin a').each(function () {
		if ( $(this).siblings().size() > 0 ) 
		{
			$(this).click(function(){
				return false;
			});
		}
	});
	
	/**
	loop through each <a>, if it has a sibling, then append the class of 'indicator'.
	reset the style of which has a sibling.
	**/
	$("#menu-admin-header a").each(function () {
		if ( $(this).siblings().size() > 0 ) 
		{
			$(this).append("<span class='menu-admin-sub-indicator'></span>");
			
			//get the width of each current <a>
			var width = $(this).width();
			//alert(width);
			
			$(this).css({
				//if you set a width or height on current <a>, the z-index won't work in IE
				//so you have to take out the width below.
				//use padding instead.
				//width: width+8+'px'
				padding: '2px 16px 1px 8px'
			});
		}
	});
	
	/**
	reset the style of the drop-down:level 1 when you hover it.
	**/
	$("#menu-admin-header > ul > li").hover(function () {
		if ( $('a',this).siblings().size() > 0 ) 
		{
			$('a',this).css({
				zIndex: '100',
				padding: '2px 16px 2px 8px'
			});				
		}
	},
	function(){
		if ( $('a',this).siblings().size() > 0 ) 
		{
			$('a',this).css({
				zIndex: '',
				padding: '2px 16px 1px 8px'
			});
		}
	});
	
	/**
	reset the style of all drop-down:level 2 & level 3.
	**/
	$('#menu-admin-header > ul > li > ul > li:last-child > a, #menu-admin-header > ul > li > ul > li > ul > li:last-child > a').css({
		borderBottom: '0px solid #666'
	});
	
	/**
	reset the style of the last item with the class of admin-menu-singleton, which is the log out button.
	**/
	$('.admin-menu-singleton:last').css({
		padding: '2px 8px 1px 8px'
	});
	
	/**
	reset style of the menu-admin when you hover on the sub menus.
	**/	
	$("#menu-admin-header > ul > li > ul").hover(function () {
		$(this).parent().addClass("hover-grandparent");
	  },
	function(){
		$(this).parent().removeClass("hover-grandparent");
	});
	
	$("#menu-admin-header ul ul ul").hover(function () {
		$(this).parent().addClass("hover-parent");
		$(this).children().addClass("hover-children");
	  },
	function(){
		$(this).parent().removeClass("hover-parent");
		$(this).children().removeClass("hover-children");
	});
	
	
	/**
	control the style of the third level of drop down of last two <li> tags
	**/
	//$('#menu-admin-header > ul:last li:last').prev('li').andSelf().css({background:"red"});
	var last_two = $('#menu-admin-header > ul:last li:last').prev('li').andSelf();
	
	if ($('ul ul',last_two).size() > 0)
	{
		last_two.find('ul ul').css({left:'-102%'});
	}
}

/*
|---------------------------------------------------------------
| AJAX UPLOAD FUNCTIONS
|---------------------------------------------------------------
|
*/

this.get_ajax_upload_cms = function(index){

	//alert(index);
	index = String(index);
	
	if(index) 
	{
		var index = ':eq('+index+')';
	}
	else
	{
		var index = '';
	}
	
	var total_items = ($('.items-uploaded'+index+' > li').length);
	var btn_upload = $('.btn-upload'+index+'');
	var path = btn_upload.attr("rel");
	var cat =  btn_upload.attr("name");
	var status = $('.status-upload'+index+'');
	
	if(total_items >= 1)
	{
		$('.upload'+index+' > .binder-background > .binder-row:first-child').css({
			borderBottom: '1px solid #999', 
			padding: '0px 0px 15px 0px'
		});
	}
	
	//alert(cat);
	//alert(path);
	//alert(total_items);
	
	new AjaxUpload(btn_upload, {
		action: path,
		name: cat,
		onSubmit: function(file, ext){
		
			// Check if the session is expired.
			$.fn.get_authentication();
	
			if(cat == 'image')
			{
				if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
					// extension is not allowed 
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
			}
			else if(cat == 'document')
			{
				if (! (ext && /^(doc|pdf|zip)$/.test(ext))){
					// extension is not allowed 
					status.text('Only DOC, PDF or ZIP files are allowed');
					return false;
				}
			}
			else if(cat == 'video')
			{
				if (! (ext && /^(avi|mov|swf)$/.test(ext))){
					// extension is not allowed 
					status.text('Only AVI, MOV or SWF files are allowed');
					return false;
				}
			}
			status.html('<img src="'+http_root+rp_global_image+image_loader+'"/>');
		},
		onComplete: function(file, response){
			//On completion clear the status
			status.text('');
			//Add uploaded file to list
			if(response){
			
				$("error", response).each(function(){
					var message = $(this).attr('message');
					alert(message);
				});
				
				$("result", response).each(function(){
				
					var itemid = $(this).attr('itemid');
					var itemcat = $(this).attr('itemcat');
					var itemtitle = $(this).attr('itemtitle');
					var itempath = $(this).attr('itempath');
					var message = $(this).attr('message');
					//alert(itempath);
					
					$('.upload'+index+' > .binder-background > .binder-row:first-child').css({
						borderBottom: '1px solid #999', 
						padding: '0px 0px 15px 0px',
						margin: '0px'
					});
					//alert(itemcat);
					
					if(cat == 'image')
					{
						alert(message+' The id of this image is '+itemid+'.');
					$('<li id="item_'+itemid+'" class="item-uploaded"></li>').appendTo('.items-uploaded-image'+index+'').html('\
					<div class="item-uploaded-image">\
						<img src="'+itempath+'"/>\
					</div>\
					<div class="item-uploaded-info">\
						<ul class="menu-micro">\
							<li><a href="'+http_root+rp_cms+'form_image_edit.php?image_id='+itemid+'" class="edit-uploaded">EDIT</a><span class="separator">&#124;</span></li>\
							<li><a href="'+http_root+rp_cms+'form_image_delete.php?image_id='+itemid+'" class="delete-uploaded">DELETE</a><span class="separator">&#124;</span></li>\
							<li><a href="'+http_root+rp_cms+'form_image_rescale.php?image_id='+itemid+'" class="rescale-uploaded">RESCALE</a><span class="separator">&#124;</span></li>\
							<li><a href="'+http_root+rp_cms+'form_image_crop.php?image_id='+itemid+'" class="crop-uploaded">CROP</a><span class="separator">&#124;</span></li>\
							<li><a href="'+http_root+rp_cms+'form_image_createthumb.php?image_id='+itemid+'" class="createthumb-uploaded">CREATE THUMBNAIL</a></li>\
						</ul>\
						<h4>'+itemtitle+'</h4>\
						<p></p>\
					</div>\
					').addClass('success');

					}
					
					else if(cat == 'document')
					{
						alert(message+' The id of this document is '+itemid+'.');
						$('<li id="item_'+itemid+'" class="item-uploaded"></li>').appendTo('.items-uploaded'+index+'').html('\
						<div>\
							<img src="'+itempath+'"/>\
						</div>\
						<div>\
							<ul class="menu-micro">\
								<li><a href="'+http_root+rp_cms+'form_document_edit.php?document_id='+itemid+'" class="edit-uploaded">EDIT</a><span class="separator">&#124;</span></li>\
								<li><a href="'+http_root+rp_cms+'form_document_delete.php?document_id='+itemid+'" class="delete-uploaded">DELETE</a></li>\
							</ul>\
							<h4>'+itemtitle+'</h4>\
							<p></p>\
						</div>\
						').addClass('success');
					}
					
					else if(cat == 'video')
					{
						alert(message+' The id of this video is '+itemid+'.');
						$('<li id="item_'+itemid+'" class="item-uploaded"></li>').appendTo('.items-uploaded'+index+'').html('\
						<div>\
							<img src="'+itempath+'"/>\
						</div>\
						<div>\
							<ul class="menu-micro">\
								<li><a href="'+http_root+rp_cms+'form_video_edit.php?video_id='+itemid+'" class="edit-uploaded">EDIT</a><span class="separator">&#124;</span></li>\
								<li><a href="'+http_root+rp_cms+'form_video_delete.php?video_id='+itemid+'" class="delete-uploaded">DELETE</a></li>\
							</ul>\
							<h4>'+itemtitle+'</h4>\
							<p></p>\
						</div>\
						').addClass('success');
					}
					
					// Attach the Micro Menu plugin for uploaded files: images, documents and videos.
					$(".edit-uploaded").micro_menu_uploaded();
					$(".rescale-uploaded").micro_menu_uploaded({popupEdit:true});
					$(".crop-uploaded").micro_menu_uploaded({popupEdit:true,crop:true});
					$(".createthumb-uploaded").micro_menu_uploaded({popupEdit:true,thumbnail:true});
					
					// Attach other plugins.
					$('.delete-uploaded').delete_item({
						deleteItem:		'.item-uploaded', // The item for deletion, such as item held in li 
						deleteParent:	'.items-uploaded', // The parent that hold delete item, such as ul > li
						wrapperParent:	'.upload' // The wrapper that hold the parent, such as div > ul > li
					});
					
					
				});

			}
		}
	});
}

/*
|---------------------------------------------------------------
| SORT ORDERS FUNCTIONS
|---------------------------------------------------------------
|
*/

this.sortOrders = function(path){ //function sortOrders(){
	
	$(".items-uploaded-image,.items-uploaded-document,.items-uploaded-video,.sort-menu").sortable({
		opacity: 0.8, 
		cursor: 'move', 
		update: function(){
			
			//$("#items > .item").removeClass("last");
			//$("#items > .item:last").addClass("last");
			
			//$(this).data("title", $(this).attr("title")).removeAttr("title");
			//var path = $(this).data("title");
			
			var path = $(this).parent().find('input').val();
			var order = $(this).sortable("serialize");
			//alert(path);
			
			$.post(http_root+path, order, function(theResponse){
				//alert(order);
				$('#popup_result').remove();
				$(document.body).append("<div id=\"popup_result\" class=\"popup\"></div>");
				var popup = $('#popup_result');
				var scrollTop = $(window).scrollTop();
				var scrollLeft = $(window).scrollLeft();
				var width = 400;
				var top = 250;
				var marginLeft = "-"+ ((scrollLeft + width)/2);
				
				popup.css({
					top:(scrollTop + top) + "px", 
					left:"50%",
					marginLeft:marginLeft + "px",
					width:width + "px",
					zIndex:"100",
					display: "none"
				});
						
				popup.load( http_root+rp_cms+"result.php", {}, function(){
					$(".result").html(theResponse);
					popup.fadeIn('slow', function(){	
						setTimeout(function(){
							$("#popup_result").fadeOut("slow");
						},2000);
						close_popup(popup);
					});	
				})

				//$(".status-sort").html(theResponse);
				//$(".status-sort").slideDown('slow');
			}); 															 
		}								  
	});
}


/*
|---------------------------------------------------------------
| PARENT & CHILDREN
|---------------------------------------------------------------
|
| to find children in the list
|
*/

this.toggle_nextparent = function(){

	$(".toggler").click(function(){
		var parent = $(this).parent();
		$('.target-toggler',parent).slideToggle("slow");
		$(this).toggleClass("active");
		return false;
	});
	
}

this.toggle_children = function(){

	/**
	loop through each <li>, if it has a children of <ul>, then append the class of 'indicator'.
	reset the style of which has a children.
	**/
	$(".items-listed li").each(function () {
		if ( $(this).children('ul').size() > 0 ) 
		{
			$('h1:first', this).append("<span class='sub-indicator'></span>");
			$('h1:first', this).css({cursor:'pointer'});
			$(this).children('ul').css({display:'none'});
			//$(this).addClass('toggle-my-children');
		}
	});
	
	/**
	toggle the subs and the subsubs
	**/
	$('.sub-indicator').toggle(
		function (){
			var parent = $(this).parentsUntil('ul');
			parent.children('ul').slideDown();
		},
		function (){
			var parent = $(this).parentsUntil('ul');
			parent.children('ul').slideUp();
	});
}

/*
|---------------------------------------------------------------
| DISABLE/RESTORE/RESTORE ALL FUNCTIONS
|---------------------------------------------------------------
|
| apply to where the buttons has a restore/ disable/ restore-all class
|
*/

this.disable = function(){
	
	$(".disable").click(function(){
		
		var path = $(this).attr('href');
		var original = $(this).text();
		//alert(original);
		
		if(original == 'DISABLE')
		{
			$.post(path+'1',function(data){
				//alert(data);
			});
			$(this).text('ENABLE');
			$(this).parent().append('<span class="message-setting">&nbsp; Disabled successfully.</span>');
			$('.message-setting').fadeOut(2000);
		}
		else
		{
			$.post(path+'0',function(data){
				//alert(data);
			});
			$(this).text('DISABLE');
			$(this).parent().append('<span class="message-setting">&nbsp; Enabled successfully.</span>');
			$('.message-setting').fadeOut(2000);
		}
		
		return false;
	});
	
}

this.restore = function(){
	
	$(".restore").click(function(){
		
		var original = $(this).attr('rel');
		var parent = $(this).parentsUntil('form');
		var target = $('input',parent).val();

		//var original = $(this).text();
		//alert(original);
		//alert(target);
		//$(this).parent().parent().prev().css({background:'red'});
		
		if(original == target)
		{
			// Append a message box.
			$(this).parent().append('<span class="message-setting">&nbsp; Nothing was changed.</span>');
			
			// Fade out the message box.
			$('.message-setting').fadeOut(2000,function(){
				
				// Remove this message box.
				$(this).remove();
			});
		}
		else
		{
			$(this).css({color: ''});
			
			// Focus on the current input field and change its value.
			//parent.focus(); this causing problems before, now it seems to be fine.
			$('input',parent).focus().val(original);
			$('form *[title]').inputHint();
		}
		return false;
	});
	
	/**
	loop each of the input field
	change the color of its parent's restore button's colour to blue, when the keyup accurs on that input field.
	**/
	$("input").each(function () {
		$(this).keyup(function () {
			var parent = $(this).parentsUntil('form');
			$(".restore", parent).css({color:'#3374d0'});
		});
	});
}

this.restoreAll = function(target){	
	
	$(".restore-all").click(function(){
	
		// Loop each of the input field accept the submit inputs which are 'update' and 'cancel'
		$("input:not(:submit)").each(function () {
			var parent = $(this).parentsUntil('form');
			var target = $(this).val();
			var original = parent.find('.restore').attr('rel');
			
			if(original == target)
			{
				parent.find('.left-restore').append('<span class="message-setting">&nbsp; Nothing was changed.</span>');
				$('.message-setting').fadeOut(2000,function(){
					
					// Remove this message box.
					$(this).remove();
				});
				$('form *[title]').inputHint();
			}
			else
			{
				parent.find('.restore').css({color: ''});
				$(this).focus().val(original);
				$('form *[title]').inputHint();
			}
		});
		return false;
	});
}

/*
|---------------------------------------------------------------
| TINY MCE EDITOR
|---------------------------------------------------------------
|
*/

this.load_mce_basic = function(){
	
	tinyMCE.init({
		// General options
		mode : "exact",
		theme : "advanced",
		elements : "content-1-mce, content-2-mce, content-3-mce, content-4-mce", //id
		plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
		
		//plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

		// Theme options
		theme_advanced_buttons1 : "newdocument,pastetext,|,bold,italic,underline,strikethrough,|,bullist,numlist,|,undo,redo,|,link,unlink,image,media,styleselect,formatselect,|,help,code,|,fullscreen",
		theme_advanced_buttons2 : "",
		
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		
		// Specifying an Absolute Image Path
		//document_base_url : "http://localhost/art_on_your_doorstep_2009/", 
		document_base_url : http_root,
    	relative_urls : false,
        remove_script_host : false,
		
		// Example content CSS (should be your site CSS)
		content_css : http_root+rp_local_style+"tinymce.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : http_root+rp_cms+"mce_template_list.js",
		external_link_list_url : http_root+rp_cms+"mce_link_list.php",
		external_image_list_url : http_root+rp_cms+"mce_image_list.php",
		media_external_list_url : http_root+rp_cms+"mce_media_list.php",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
	
}

/*
initEditor: function () {
	tinyMCE.init({
		language: "zh-cn",
		mode: "exact",
		theme: "advanced",
		height: 500,
		plugins: "emotions,spellchecker,advhr,insertdatetime,preview",


		// Theme options - button# indicated the row# only
		theme_advanced_buttons1: "newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect,formatselect",
		theme_advanced_buttons2: "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,code,preview,|,forecolor,backcolor",
		theme_advanced_buttons3: "insertdate,inserttime,|,spellchecker,advhr,,removeformat,|,sub,sup,|,charmap,emotions",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_statusbar_location: "bottom",
		theme_advanced_resizing: true
	});

	tinyMCE.execCommand('mceAddControl', true, txtDetails);
	//dnn_ctr392_Product_txtDetails
},

enableEditor: function () {
	//console.log(tinyMCE.activeEditor);
	if (tinyMCE.activeEditor != null || tinyMCE.activeEditor != undefined) {
		//console.log(tinyMCE.activeEditor);

		for (var i = 0; i < tinyMCE.editors.length; i++) {
			tinyMCE.remove(tinyMCE.editors[i]);
		}

		setTimeout(function () {
			//tinyMCE.execCommand('mceAddControl', true, txtDetails);
			Product.initEditor();
		}, 1000);

	} else {
		Product.initEditor();
	}
},
	
*/


this.load_mce_complete = function(){
	tinyMCE.init({
		// General options
		mode : "exact",
		theme : "advanced",
		elements : "content-1-mce, content-2-mce, content-3-mce, content-4-mce", //id
		plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		
		// Specifying an Absolute Image Path
		//document_base_url : "http://localhost/art_on_your_doorstep_2009/", 
		document_base_url : http_root,
    	relative_urls : false,
        remove_script_host : false,
		
		// Example content CSS (should be your site CSS)
		content_css : http_root+rp_local_style+"tinymce.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : http_root+rp_cms+"mce_template_list.js",
		external_link_list_url : http_root+rp_cms+"mce_link_list.php",
		external_image_list_url : http_root+rp_cms+"mce_image_list.php",
		media_external_list_url : http_root+rp_cms+"mce_media_list.php",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
}

/*
|---------------------------------------------------------------
| APPROVAL FUNCTIONS
|---------------------------------------------------------------
|
| apply to where the buttons has a load-approval class
|
*/

this.load_approval = function(){
	
	$('.load-approval').click(function(){
		
		/* set the variable and store the value */
		var $this = $(this);
		var $this_path = $this.attr('rel');
		var $this_parent = $this.parents('.right');
		var target_parent_item = $this.parents('.item-form');
		var target_parent_list = $this.parents('.form');
		var parent_html = $this_parent.html();

		//alert(target_html);
		//alert('1');
		
		/* display the loading */
		$this_parent.html('<img src="'+http_root+rp_global_image+image_loader+'" style="float:none; border:0px solid #000; margin:0px;"/> loading');
		
		$this_parent.load($this_path, function(){
			
			/* get the post_approval function and pass in the required variables */
			post_approval($this_parent,parent_html,target_parent_item,target_parent_list);

			/* when you click the no button if there is any no button */
			$("input[type=button][name=no]",$this_parent).click(function(){

				$this_parent.html(parent_html);
				load_approval();

			});
		});
		
		
		return false;
		
	});
	
}

this.post_approval = function($this_parent,parent_html,target_parent_item,target_parent_list){
	
	$(".form-admin",$this_parent).submit(function(){
		
		/* set the variable */
		var $this = $(this);
		var $this_path = $this.attr('action');
		//alert($this.serialize());
		
		/* display the loading */
		$this_parent.html('<img src="'+http_root+rp_global_image+image_loader+'" style="float:none; border:0px solid #000; margin:0px;"/> loading');
		
		/* post the form */
		$.post($this_path, $this.serialize(), function(xml){
			
			if($(xml).find("result").length > 0)
			{
				var message = $("result", xml).attr('message');
				//var path_image = $("result", xml).attr('pathimage');
				//alert(message);
				
				/* then append a fresh popup */
				$(document.body).prepend("<div id='popup-approve-result' class='popup-outer'><div class='popup-inner'><div class='close'><a href='#' class='button-close'>x close</a></div><div id='wrapper-result'>"+message+"</div></div></div>");
				
				/* set the popup target and hide it first */
				var target_result = $('#popup-approve-result').width(400).hide();
				
				/* set the popup to the center of the page */
				set_center(target_result);
				
				/* slide up the target parent and remove it */
				target_parent_item.slideUp(function(){
					
					/* remove this */
					$(this).remove();
					
					/* check the number of .member-profile, if it is 0, remove the list parent */
					if($('.member-profile',target_parent_list).length == 0) target_parent_list.remove();
					
					/* fade out the target after 1 sec */
					setTimeout(function(){ 
						target_result.fadeOut(function(){
					
						/* remove this */
						$(this).remove();
						
						});
					}, 1000 ); 
					
				})
				
			}
			
			/* check if the xml return result has confirmation node */
			if($(xml).find("confirmation").length > 0)
			{
				$('#popup-form-confirmation').remove();
				
				var path = $("confirmation", xml).attr('path');
				var message = $("confirmation", xml).attr('message');
				
				/* then append a fresh popup */
				$(document.body).prepend("<div id='popup-form-confirmation' class='popup-outer'><div class='popup-inner'><div class='close'><a href='#' class='button-close'>x close</a></div><div id='wrapper-form'></div></div></div>");
				
				//alert(path);
				//alert(message);
				
				/* set the popup target and hide it first */
				var target_confirmation = $('#popup-form-confirmation').width(500).hide();
				
				/* load the form to the div */
				$("#wrapper-form").load(path, {}, function(){
				
					/* set back the reject and approve buttons again */
					$this_parent.html(parent_html);
					
					/* set the popup to the center of the page */
					set_center(target_confirmation);
					
					/* load the load_approval function to the button */
					load_approval();
					
					$(".form-admin", $(this)).submit(function(){
						
						var $this = $(this);
						var $this_path = $this.attr('action');
						var $this_processing = $('.processing', $this);
						//alert($this.serialize());
						
						/* these are needed when you submit the form with error and error repeatedly */
						$this_processing.show();
						$('.error').html('').andSelf().removeClass('error');
						//alert($('.error').length);
						
						/* display the loading */
						$this_processing.html('<img src="'+http_root+rp_global_image+image_loader+'" style="float:none; border:0px solid #000; margin:0px 0px 0px 10px;"/> processing');
						/* disable the submit button so that you won't click it twice while the ajax is processing the form */	
						$('input[type=submit]',$this).attr('disabled', 'disabled').css({opacity:0.4});
						
						/* post the form */
						$.post($this_path, $this.serialize(), function(xml){
							
							/* hide the proccesing */
							$this_processing.hide();
							
							/* disable the submit button so that you won't click it twice while the ajax is processing the form */	
							$('input[type=submit]',$this).attr('disabled', '').css({opacity:1.0});
							
							/* process each of the error returns from the xml */
							$("error", xml).each(function(){
			
								/* set the local variable */
								var elementid = $(this).attr('elementid');
								var message = $(this).attr('message');
								
								/* add an error class to each error element */
								$("."+elementid+"_label span:last-child").html(message).addClass('error');
								
							});
							
							/* check if the xml result returns 'result' in its node */
							if($(xml).find("result").length > 0)
							{
								$('#popup-form-confirmation').remove();
								
								var message = $("result", xml).attr('message');
								//var path_image = $("result", xml).attr('pathimage');
								//alert(message);
								
								/* then append a fresh popup */
								$(document.body).prepend("<div id='popup-reject-result' class='popup-outer'><div class='popup-inner'><div class='close'><a href='#' class='button-close'>x close</a></div><div id='wrapper-result'>"+message+"</div></div></div>");
								
								/* set the popup target and hide it first */
								var target_result = $('#popup-reject-result').width(400).hide();
								
								/* set the popup to the center of the page */
								set_center(target_result);
								
								/* slide up the target parent and remove it */
								target_parent_item.slideUp(function(){
									
									/* remove this */
									$(this).remove();
									
									/* check the number of .member-profile, if it is 0, remove the list parent */
									if($('.member-profile',target_parent_list).length == 0) target_parent_list.remove();
									
									/* fade out the target after 1 sec */
									setTimeout(function(){ 
										target_result.fadeOut(function(){
									
										/* remove this */
										$(this).remove();
										
										});
									}, 1000 ); 
									
								})
								
							}
							
						});
						
						return false;
					});
					
				})
				
				
			}
			
		});
				
		return false;
	});
}

/*
|---------------------------------------------------------------
| GENERAL FUNCTIONS
|---------------------------------------------------------------
|
*/

this.popup_window = function(target, name, sizes){
//function popup_window(target, name, sizes) {
	newwindow = window.open(target, name, sizes);
	if (window.focus) {newwindow.focus()}
	return false;
}

this.get_guide = function(){	

	$(".guide").click(function(){
		var path = $(this).attr("href");
		popup_window(path,'guide','width=400,height=600, scrollbars=yes'); 
		return false;
	});
}

this.close_popup = function(target){	
	
	$(".close",target).click(function(){
		
		//alert('ie-drop-shadow');
		
		target.fadeOut('fast', function(){
			target.remove();
		});
		
	return false;
	});
}

this.locationBack = function(target){	
	
	$(".back").click(function(){
		var path = $(this).attr("href");
		$(".popup").remove();
		document.location = path;
		return false;
	});
}
