/* begin section : add image for Profile Photo */

var previewsuspend = false;

function ajaxFileUpload()
    {

        $.ajaxFileUpload
        (
                {
                        url:'/a/uploadfile',
                        secureuri:false,
                        fileElementId:'fileToUpload',
                        dataType: 'json',
                        success: function (data, status)
                        {
                                alert(data.error);
				if(typeof(data.error) != 'undefined')
                                {
                                        if(data.error != '')
                                        {
                                                alert(data.error);
                                        }else
                                        {

                                        }
                                };

                        },
                        error: function (data, status, e)
                        {
                                alert(data.error);
                        }
                }
        )
        return false;
    }

/* end section : add image for Profile Photo */


    //used to load the package editing form
    function loadform()
    {
        $.get('/a/lf',{},function(data){ $('#tabs_packages').html(data); });
    }
    // resets the package section
    function cancelviewpackage()
    {
        $.get('/a/reloadpackages',{},function(data){ $('#tabs_packages').html(data); });
    }

    function edituserdetailsforms()
    {

        // load the forms from an ajax query
        $.get('/a/edituserdetailsforms',{},function(data){ $('#tabs_small_myaccount').html(data);  });
        // $('#tabs_small_myaccount').html("ok");
        // $.get('/a/edituserdetailsforms',{},function(data){ alert(data);  });
    }

    function deletePackage(id)
    {
        confirm('Are you sure you wish to delete this package for ever?', function()
        {
            $.get('/a/deletepackage',{'id':id},function(data){ $('#tabs_packages').html(data); });
            $.get('/a/reloadpackages',{},function(data){ $('#tabs_packages').html(data); });
        }
        );
    }

    // used to update the package details
    function savepackagedetails()
    {

        $.get('/a/savepackagedetails',{'id':$("#_id").attr("value"), 'title':$("#PackageTitle").attr("value"), 'description' : $("#PackageDescription").attr("value") },function(data){ $('#tabs_packages').html(data); });
    }

    function saverights(clip_id)
    {

        if ( $("#autoremove").attr("checked") == true)
        {
            confirm('Are you sure you?' ,function()
            {
                $.post('/a/saverights/', {'id':clip_id,
                                    'syn':$("#syndicatee").attr("value"),
                                    'rev1':$("#rev1").attr("value"),
                                    'rev2':$("#rev2").attr("value"),
                                    'comment':$("#comments").attr("value"),
                                    'thedate':$("#dateR").attr("value"),
                                    'autoremove':$("#autoremove").attr("checked"),
                                    'downloadable':$("#downloadupdate").attr("checked")},
                        function(data) {jAlert(data,'Result');  });
            });
        } else {
            $.post('/a/saverights/', {'id':clip_id,
                                    'syn':$("#syndicatee").attr("value"),
                                    'rev1':$("#rev1").attr("value"),
                                    'rev2':$("#rev2").attr("value"),
                                    'comment':$("#comments").attr("value"),
                                    'thedate':$("#dateR").attr("value"),
                                    'autoremove':$("#autoremove").attr("checked"),
                                    'downloadable':$("#downloadupdate").attr("checked")},
                        function(data) {jAlert(data,'Result');  });
        }
    }

    // used for
    function submitpackageform()
    {
        if ( (0 == $("#PackageTitle").attr("value").length) || (0 == $("#PackageDescription").attr("value").length) )
        {
            jAlert('Title and Description need to be entered','Message');
            return;
        }

        // post the information back and rerender the package block
        $.get('/a/cp/', {'title' : $("#PackageTitle").attr("value"), 'description' : $("#PackageDescription").attr("value") }
        ,function(data){ $('#tabs_packages').html(data);window.location.reload(); });
       // now rerender the select element so it has the new item

    }

    function previouspackagepage()
    {
        $.get('/a/ppp/',{},function(data){$('#tabs_packages').html(data);});
    }

    function nextpackagepage()
    {
        $.get('/a/npp/',{},function(data){$('#tabs_packages').html(data);});
    }

    function previouscirculatepage()
    {
        $.get('/a/pcp/',{},function(data){$('#tabs_packages').html(data);});
    }

    function nextcirculatepage()
    {
        $.get('/a/ncp/',{},function(data){$('#tabs_packages').html(data);});
    }



    function packageDropSelectorChange()
    {
        // let the server know which package has been selected
        // then update the current drop box from the server
        id = $('#package').attr("value");
        $.get('/a/pc/i/' +id,{},function(data){ $('#droparea').html(data); });
    }


    function categorychange(id)
    {
        $.get('/a/catc/i/' + id + '/v/' + $("#category" + id).attr("checked"),{},null) ;
    }

    function captionchange()
    {
        $.post('/a/capc/' ,{v:$("#caption").attr("value")},null);
    }

    function titlechange()
    {
        //$.get('/a/titc/v/' + $("#title").attr("value"),{},null);
        $.post('/a/titc/' ,{v:$("#title").attr("value")},null);
    }


    function keywordchange()
    {
        $.get('/a/keyc/v/' + $("#keywords").attr("value"),{},null);
    }

    function accesschange()
    {
        $.get('/a/acc/v/' + $("#access").attr("value"),{},null);
    }

    function packagechange()
    {
        $.get('/a/pac/v/' + $("#package").attr("value"),{},null);
    }

    function removeclip(clipid)
    {
        $.get('/a/dc/p/' +$('#package').attr("value") + '/c/' + clipid, {}, function(data) { $('#droparea').html(data); });
    }

    function removeClipFromPackage(packageid, clipid)
    {
       $.get('/a/dc/p/' + packageid + '/c/' + clipid, {}, function() { window.location.href=window.location.href; });
      // $.get('/a/dc/p/' + packageid + '/c/' + clipid, {}, function(data) { alert(data); });
    }

    function storerestriction()
    {
        // used by the view page for the restriction text
        $.get('/a/strres/c/' + clipid,{txt:$('#restrictiontext').attr("value")},function(){ $.get('/a/strres2/c/'+ clipid,{},function(data){ $("#restext").html(data);});$("#dim").fadeOut();});
        // now update the table

    }

    function showover () {
        $(this).attr("src","/img/buttons/"+this.name+"_on.gif");
    }
    function showout () {
        $(this).attr("src","/img/buttons/"+this.name+"_off.gif");
    }

    function revoke(id)
    {
        // the function to make a package revoked
        $.get('/a/revoke/p/' + id, {}, function(data) {jAlert(data,'Message');  });
        $('#p'+id).removeClass('revokePackage');
        $('#p'+id).addClass('makeLivePackage');
        $('#p'+id).attr('alt','Make Package Live');
        $('#p'+id).attr('href','javascript:live(' + id + ')');
    }

    function live(id)
    {
        // the function to make a package live
        $.get('/a/live/p/' + id, {}, function(data) {jAlert(data,'Message');  });
        // change the content
        $('#p'+id).removeClass('makeLivePackage');
        $('#p'+id).addClass('revokePackage');
        $('#p'+id).attr('alt','Revoke Package');
        $('#p'+id).attr('href','javascript:revoke(' + id + ')');
    }
    function editPackage(id)
    {
         $.get('/a/lpef/i/' + id,{},function(data){ $('#tabs_packages').html(data); });
    }

    function postuserdata()
    {
	// validate name
	if($('#UserScreenname').attr('value').length == 0){ jAlert('Please complete name','Message'); return;}
	// validate email
	$.ajax({
	    url: '/a/valemail',
	    data:{email:$('#UserEmail').attr('value') },
	    async:false,
	    dataType:'text',
	    success: function(data){ if(data == 'false'){jAlert('Bad Email Address','Message');return;}}});


	   // alert($('#UserPassword').attr('value'));
	   // alert($('#UserRepassword').attr('value'));
	// validate password (1)
	if($('#UserPassword').attr('value').length > 0 & $('#UserPassword').attr('value').length < 6){ jAlert('Password too short','Message'); return;}
	if($('#UserPassword').attr('value') != $('#UserRepassword').attr('value')){ jAlert("Passwords don't match",'Message'); return;}

	// file upload
	if ($('#fileToUpload').attr('value').length > 1){ajaxFileUpload()};



	//post the data
	 $.ajax({
	    url: '/a/newuserdetails',
	    data:{email:$('#UserEmail').attr('value'),
		name:$('#UserScreenname').attr('value'),
		phone:$('#UserPhone').attr('value'),
		mob1:$('#UserMobilePartI').attr('value'),
		mob2:$('#UserMobilePartII').attr('value'),
		password:$('#UserPassword').attr('value'),
	        dlnotify:$('#UserDownloadnotify').attr('checked'),
		ponotify:$('#UserUploadnotify').attr('checked'),
		wanotify:$('#UserSearchnotify').attr('checked')},
	    async:false,
	    dataType:'text',
	    success: function(data){jAlert(data,'Message'); }});

	// reload the basic form
	$.get('/a/userdetailssection',{},function(data){ $('#tabs_small_myaccount').html(data);  });


    }
    function reloaduserdetails()
    {
	$.get('/a/userdetailssection',{},function(data){ $('#tabs_small_myaccount').html(data);  });
    }

    function saveSearch()
    {
	// display the box
	$.get('/a/savesearchdialog/',{},function(data){ $('#dimcontent').html(data);});
	$("#dim").fadeIn();
    }

    function saveNewSearch()
    {
	// check there is text
	if($('#search_save_name').attr('value').length == 0){ jAlert('Please complete a name for this search','Message'); return;}
	// check one of the boxes has been selected
	if( ($('#email_notification').attr('checked') == false) & ($('#sms_notification').attr('checked') == false) ){ jAlert('Select an alert notification','Message'); }
	// send the information back to the server for processing

	$.get('/a/savenewsearch',{name:$('#search_save_name').attr('value'),email:$('#email_notification').attr('checked'), sms:$('#sms_notification').attr('checked')  },function(data){ $.get('/a/savedsearchnavigation', {}, function(data){ $('#tabs_small_mysearches').html(data)});$("#dim").fadeOut(); });
	// renew the saved search navigation
    }

    function changesavedsearchemail(id)
    {
	$.get('/a/togglesavedsearchemail', {id : id, value: $("#sse" + id).attr('checked')} , function(data){});
    }
    function changesavedsearchsms(id)
    {
	$.get('/a/togglesavedsearchsms', {id : id, value: $("#sse" + id).attr('checked')} , function(data){});
    }

    function removeSavedSearch(id)
    {
	confirm('Are you sure you wish to remove this search?', function(){$.get('/a/removesavedsearch', {id:id},function(data){$.get('/a/savedsearchnavigation', {}, function(data){ $('#tabs_small_mysearches').html(data)});});	    });
    }

    function makecliplive(id)
    {
	$.get('/a/makecliplive' , {id:id}, function(data){$('#tabs_circulationclips').html(data)});
    }
    function revokeclip(id)
    {
	$.get('/a/revokeclip' , {id:id}, function(data){$('#tabs_circulationclips').html(data)});
    }
    function deletecirculateclip(id)
    {
	confirm('Please confirm delete from circulation?', function(){$.get('/a/deleteclipcirculation' , {id:id}, function(data){$('#tabs_circulationclips').html(data); jAlert('Clip removed from circulation','Message');}); });
    }

    function deleteClip(id)
    {
	confirm('Are you sure you wish to delete this clip?', function(){$.get('/a/deleteclip' , {id:id}, function(data){ jAlert(data,'Message'); window.location = "/index/index";}); });
    }

    function removeAtClipDetailsClipFromPackage(clip_id, package_id)
    {
	confirm('Confirm removal from the package?',function(){$.get('/a/removeclipfrompackage' , {clip_id:clip_id,package_id:package_id}, function(data){$('#fragment-2').html(data); }); });
    }
    function updateClipOwner()
    {
	$.get('/a/changeclipowner', {owner:$('#clipowner').attr('value'),clip:clipid}, function(data){jAlert(data,'Message');});
    }
    // changes the access rights for a clip
    // called from the clip details page if the user is an administrator
    function accessrightschange()
    {
        $.get('/a/changeclipaccess',{clip:clipid,access:$('#visibility').attr('value')},function(data){jAlert(data,'Message');});
    }
    // used to add a clip to a package from the clip details
    // displayed by an owner or administrator
    function addtopackage()
    {
         $.get('/a/addcliptoapackage',{clip:clipid,packageid:$('#addtopackageselect').attr('value')},function(data){$('#fragment-2').html(data); });
    }
            function vis1()
        {
            $("#existingpackages").css("display", 'block');
            $("#newpackage").css("display", 'none');
            $.get('/a/existingpackage',{id:$('package').attr('value')},null);
        }
        function vis2()
        {
           $("#existingpackages").css("display", 'none');
           $("#newpackage").css("display", 'block');
           // inform the server
           $.get('/a/newpackage',{},null);
        }
        function newpackageentered()
        {
            $.get('/a/newpackagetext', {text:$('#newpackagetitle').attr('value')},null);
        }

        function logout()
        {
            confirm("Are you really sure you wish to logout?", function() {window.location='/index/logout;'});
        }

        function advancedon()
{
   if ($('#search_type').html() == "Advanced Search")
   {
   $('#search').css('height','auto');
   $('#search_type').html('Basic Search');
   $.get('/a/advanced/status/on',{},null);
   $('#basicsearch').attr("disabled", true);
   $.get('/a/includechange/v/' + $('#basicsearch').attr("value"), {},null);
   $('#basicsearch').attr("value", '');
   $('#togSea1').css('display','none');
   $('#togSea3').css('display','none');
   $('#togSea2').css('display','block');
   $('#togSea4').css('display','block');
   $('#searchtype').attr("value","advanced");


   }
   else
       {
            $('#search').css('height','42px');
            $('#search_type').html('Advanced Search');
            $.get('/a/advanced/status/off',{},null);
            $('#basicsearch').attr("disabled", false);
            $('#togSea2').css('display','none');
            $('#togSea4').css('display','none');

            $('#togSea1').css('display','block');
            $('#togSea3').css('display','block');
            $('#searchtype').attr("value","basic");
       }
}

function catclick(id)
{
    // send a request back to the server
    $.get('/a/catclick/v/' + id + '/checked/' + $('#category_id' + id).attr('checked'),{},null);
}

function includechange()
{

    $.get('/a/includechange/v/' + $('#includeterm').attr('value'), {},null);
}

function excludechange()
{
    $.get('/a/excludechange/v/' + $('#excludeterm').attr('value'), {},null);
}

function updatebasicsearch()
{
    $.get('/a/basicsearch', {s: $('#basicsearch').attr('value')}, null);
}

function user_id_update()
{
    $.get('/a/upusid', {i:$('#user_list').attr('value')}, function(data){alert(data,"Message");window.location.reload();});
}

function package_user_id_update()
{
    $.get('/a/uppausid', {i:$('#package_user_list').attr('value')}, function(data){alert(data,"Message");window.location.reload();});
}

function downloadupdate(id)
{
    $.get('/a/dlud/v/' + id + '/c/' + $('#downloadupdate').attr('checked'),{},function(data){jAlert(data,"Message");});
}
