function actionOnURL(action, url_id)
{
    filename = '/ajax/boxy_url.php';	
    $.ajax({
        type:"post",
        url:filename,
        data:"a="+action+"&urlid="+url_id,
		success:function(msg){
			Boxy.alert(msg);
			
			if(msg.substring(0,7) == "Success")
			{ 
               refreshURLs($("#ajax_return_URL_container"));
            } 

		}
    });
    
}

function boxy_URLs(category)
{	
    $.include("/js/jquery.form.js");
    $.include("/js/jquery.validate.js");
	var action    = "pending";
	var bxy_title = "Pending URL";
	switch(category)
	{
		case "add":
			action    = "add";
			bxy_title = "Submit URL";
			break;
		case "approved":
			action    = "viewApproved";
			bxy_title = "Approved URL";			
			break;
	}
	
    Boxy.load("/ajax/boxy_urls.php?a="+action,{
        title:bxy_title,
		unloadOnHide:true,
        afterShow:function(){
            validateSubmitURLForm(); 
        }
    });
}

function validateSubmitURLForm()
{
	$("#submitURLForm").validate({
		errorElement: "em",
		errorPlacement: function(error, element) {
			error.appendTo( $("#messageFeedback") );
		},
        submitHandler: function(form) {
            jQuery(form).ajaxSubmit({
                success:function(msg){
                    var pending_url_num = Number($("#pending_URL_num").text());
					pending_url_num++;
					$("#pending_URL_num").text(pending_url_num);
					
					$(".close").click();
                    //refreshURLs();
					
                    Boxy.alert(msg);
                }
            });
        }
	});
}

function validateEditURLForm()
{
	$("#editURLForm").validate({
		errorElement: "em",
		errorPlacement: function(error, element) {
			error.appendTo( element.closet('td').next('info') );
		},
        submitHandler: function(form) {
            jQuery(form).ajaxSubmit({
                success:function(msg){
					$(".close").click();
                    refreshURLs($("#ajax_return_URL_container"));
					
                    Boxy.alert(msg);
                }
            });
        }
	});
}

function refreshURLs(htmlContainer)
{
    uid = $("#uid").val();

    $.ajax({
        type:"post",
        url:"/ajax/section_urls.php",
        data:"u="+uid,
        success:function(msg){
            htmlContainer.html(msg);            
        }
    });
}

function validateURLForm()
{
	$("#editURLForm").validate({
		errorElement: "em",
		errorPlacement: function(error, element) {
			error.appendTo( element.closest("td.check_field").next("td.info") );
		},
        submitHandler: function(form) {
            jQuery(form).ajaxSubmit({
                success:function(msg){
                    $(".close").click();
                    if(msg.substring(0,7) == "Success")
                    { 
                        refreshURLs($("#ajax_return_URL_container"));
                    }
                    
                    Boxy.alert(msg);
                }
            });
        }
	});
}

function bxy_rejectURL(url_id)
{
    $.include("/js/jquery.form.js");
    Boxy.load('/ajax/boxy_url.php?a=reject&urlid='+url_id,
              { title:"Reject URL",
                afterShow:function(){
                    validateURLForm();
                }
              });
}

function bxy_editURL(url_id)
{
    $.include("/js/jquery.form.js");
    Boxy.load('/ajax/boxy_url.php?a=edit&urlid='+url_id,
              { title:"Edit URL",
                afterShow:function(){
                    validateEditURLForm();
                }
              });
}




function boxy_appSelURLs()
{
    var selectedIds = "";

    $("#my_urls .url_checkbox:checked").each(function(){
        tpId = $(this).attr("id").substr(4);//url_
        if(tpId.length >0) selectedIds +=tpId+",";
    });
    
    if(selectedIds.length > 0)
    {
        actionOnURL('approve',selectedIds);
    }
}


