﻿
//--------------------------------------------------------------------------------------------------------------------------------
//  functions.js
//  General javascript functions used across the site
//--------------------------------------------------------------------------------------------------------------------------------

//validate HTML for custom validator used with the free text box
function validateHTML(oSrc, args)
          {
            //---------------------------------------------------------------------------------------------------------------------------
            //Validator TURNED OFF because we cannot current display the WordClean button.
            //That's because the licence is not installed properly after architecture change
            //---------------------------------------------------------------------------------------------------------------------------
            args.IsValid = true;

//            args.IsValid = false;
//            if(args.Value)
//            var mytext = args.Value.toLowerCase();
//            {
//               if ((args.Value.indexOf("<font") > -1 ) || (args.Value.indexOf("class=") > -1 ) || (args.Value.indexOf("style=") > -1 ) || (args.Value.indexOf("lang=") > -1 ) || (args.Value.indexOf("<o:") > -1 ))
//               {
//                 args.IsValid = false;
//               }
//               else
//               {
//                args.IsValid = true;       
//               }
//            }

          }

//using prototype to check / uncheck all boxes...
function checkall(cssclass){
    $$('.'+cssclass+' input').each(function(s){s.disabled ? null : s.checked = true;});
}

function uncheckall(cssclass){
    $$('.'+cssclass+' input').each(function(s){s.disabled ? null : s.checked = false;});
}

//Check the format of an artwork upload
function validate_photoformat(oSrc, args)
  {
    args.IsValid = false;
    if(args.Value)
    {
       var length = args.Value.length;
       var endsWidth = args.Value.substr(length - 3, 3);
       if((endsWidth == "jpg") || (endsWidth == "gif") || (endsWidth == "tif") || (endsWidth == "png")|| (endsWidth == "JPG") || (endsWidth == "GIF") || (endsWidth == "TIF") || (endsWidth == "PNG"))
      {
         args.IsValid = true;
       }
    }
 } 

//ptype: clearing quicksearch
//document.observe('dom:loaded', function(){
// $('quicksearch') ?  $('quicksearch').observe('focus', function(ev){ Event.element(ev).clear();}) : null;
//});


//--------------------------------------------------------------------------------------------------------------------------------
//  Free Text Box related functions
//--------------------------------------------------------------------------------------------------------------------------------

//count characters in a freetextbox editor element
function FreetextboxCount(ftb) 
{
    var html = ftb.GetHtml();
    html = html.replace(/(<([^>]+)>)/ig,""); 
    html = html.replace("&nbsp;","");
    var count = 0;
    var output = "";        
    if ((1000 - html.length) > 0)
        { 
            document.getElementById('remainingProfileChars').value = 1000 - html.length; 
            document.getElementById('remainingProfileChars').style.color = "#000000";   
            document.getElementById('countProfileText').value = "characters left";
            document.getElementById('countProfileText').style.color = "#000000";  
        }
    else
        {    
            count = 1000 - html.length;
            output = count.toString();
            document.getElementById('remainingProfileChars').value = output.replace('-',''); 
            document.getElementById('remainingProfileChars').style.color = "#BB0000";     
            document.getElementById('countProfileText').value = "characters over"; 
            document.getElementById('countProfileText').style.color = "#BB0000";     
        }
window.status = html.length; 
}
    

function Counter(field,countfield,display,maxlimit)
{
    var output = "";
    var count = 0;
    
    if (field.value.length > maxlimit)
        {
            count = maxlimit - field.value.length;
            output = count.toString();
            countfield.value = output.replace('-','');
            countfield.style.color = "#BB0000"
            display.value = "characters over";
            display.style.color = "#BB0000"
        }
    else
        {
            countfield.value = maxlimit - field.value.length;
            countfield.style.color = "#555555";
            display.value = "characters left";  
            display.style.color = "#555555";
        }    
}
    
//--------------------------------------------------------------------------------------------------------------------------------
//popup functions : deprecated - DO NOT USE !!
//                                   only provided to support articles with old-style popupHelp links
//                                   INSTEAD: use class="lbOn" for the <a> tag
//--------------------------------------------------------------------------------------------------------------------------------

// all old popup functions call lightbox via this
function lightbox_popup(theElement)
{
    if(document.viewport.getHeight() > 550) {
        
        if(!window.lightboxmarkupadded){
            addLightboxMarkup();
            window.lightboxmarkupadded = true;        
            }
        lbox = new lightbox(theElement, true);
        
        } else {
            openPopUp("http://www.axisweb.org/"+theElement.href)    
        }
}

//function for backwards compatibility
function popup(pURL,pName,pWidth,pHeight) 
{
	if (pWidth == "") pWidth = 500;
	if (pHeight == "") pHeight = 400;
	if (pName == "") pName = "popup";
	if (pURL.indexOf('.htm') < 0) pURL = pURL+'&POP=1';
	var thiswin= open(pURL,pName,'width='+pWidth+',height='+pHeight+',scrollbars=yes,directories=no,location=no,menubar=no,status=no,toolbar=no,resizable=yes,left=100,top=100');
	if(navigator.appVersion.substring(0,1)!="2") thiswin.focus();
	if(thiswin.opener == null) thiswin.opener = self;
}
	
function openPopUp(linkURL) {
    window.open(linkURL, '','width=600,height=400,resizable=yes,scrollbars=yes,location=yes,status=yes,left=50,top=50')
}

//individual replacement functions
function popupHelp(popID){
    this.href = 'atATCL.aspx?AID='+popID;
    lightbox_popup(this);
}

//coloured popups - not coloured anymore.
function popupWorkColoured(pID,progID){
    this.href = 'seWORK.aspx?WORKID='+pID ;  
    lightbox_popup(this);
}
function popupVisualColoured(popID,visualID,progID){
    this.href = 'seWORK.aspx?WORKID='+popID+'&VISUALID='+visualID ;  
    lightbox_popup(this);
}
function popupEntryColoured(popID,progID){
    this.href = 'seCVEN.aspx?CVID='+popID;  
    lightbox_popup(this);
}
function popupAssetColoured(popID,progID){
    this.href = 'atASST.aspx?ASSETID='+popID+'&PID='+progID;  
    lightbox_popup(this);
}
function popupHelpColoured(popID,progID){
    this.href = 'atATCL.aspx?AID='+popID;
    lightbox_popup(this);
}

//old old old popups
function popupWork(popID)  { popupWorkColoured(popID,414); }
function popupEntry(popID) { popupEntryColoured(popID,414); }
function popupAsset(popID) { popupAssetColoured(popID,414); }



//--------------------------------------------------------------------------------------------------------------------------------
//Ajax timeout functions from jason@codejanitor.com, thanks!!
//--------------------------------------------------------------------------------------------------------------------------------

function callInProgress (xmlhttp) {
    switch (xmlhttp.readyState) {
        case 1: case 2: case 3:
            return true;
            break;
        default:          // Case 4 and 0
            return false;
            break;
    }
}

function showFailureMessage() {
    alert('Sorry, there is a problem accessing this page at present. Please try again.');
}

// Register global responders that will occur on all AJAX requests
//Ajax.Responders.register(
//    {
//        onCreate: function(request) {
//            request['timeoutId'] = window.setTimeout(
//                function() {
//                    // If we have hit the timeout and the AJAX request is active, abort it and let the user know
//                    if (callInProgress(request.transport)) {
//                        request.transport.abort();
//                        showFailureMessage();
//                        // Run the onFailure method if we set one up when creating the AJAX object
//                        if (request.options['onFailure']) {
//                            request.options['onFailure'](request.transport, request.json);
//                        }
//                    }
//                },
//                5000 // Five seconds
//            );
//        },
//        onComplete: function(request) {
//            // Clear the timeout, the request completed ok
//            window.clearTimeout(request['timeoutId']);
//        }
//    }
//);

//document.observe("dom:loaded", function(){

//        $$("input.entersubmit, textarea.entersubmit").each(function(element){
//            
//            element.observe("keydown", function(e){
//                   if((e.keyCode == Event.KEY_RETURN)){
//                     $$(".defaultbutton")[0].click();
//                     Event.stop(e);
//                   }                
//                });            
//        });
//});
    
function UpdateMainImage(id, type) {
    var sSrc, sAlt, thisimg, sTitle, sName, sDisplayName, sDisplayTitle;
    try
        {                       
            if(id !=undefined)
            {
                thisimg = document.getElementById(id);    
                sAlt = thisimg.alt;                                  
                
                if(type=='curator') {sSrc = thisimg.src.replace('/small/','/large/').toString();}
                else                    {sSrc = thisimg.src.replace('/thumbnail/','/full/').toString();}                            
                                      
                document.getElementById('mainImage').src = sSrc.toString(); 
                document.getElementById('mainImage').alt = sAlt.toString(); 
                document.getElementById('mainImage').title = sAlt.toString();        
                                     
                // Images provider
                sDisplayName = id+'-displayname';
                sName = document.getElementById(sDisplayName).innerHTML; 
                if(sName!='' && sName!=undefined)
                {document.getElementById('mainImage-displayname').innerHTML = sName;}
                
                // Works title
                sDisplayTitle = id+'-title';       
                sTitle = document.getElementById(sDisplayTitle).innerHTML;  
                if(sTitle!='' && sTitle!=undefined)
                {document.getElementById('mainImage-title').innerHTML = sTitle;}                            
            }                      
        }
    catch(err)
        {
            alert('Error processing the image [' + err.message + ']');
        }
}
            
//--------------------------------------------------------------------------------------------------------------------------------
// Flash related
//--------------------------------------------------------------------------------------------------------------------------------

// force a redraw to work around 'white-screen' bug in Firefox 2 on Mac
function forceSWFRedraw(element) {
  document.getElementById(element).style.display = 'block';
}

//--------------------------------------------------------------------------------------------------------------------------------
// Updates for image carousel on various homepages
//--------------------------------------------------------------------------------------------------------------------------------

function UpdateSelectionImage(id)
{
    var sSrc, sAlt;
    try
        {                       
            if(id !=undefined)
            { 
                document.getElementById('ctl00_maincontent_mainImage').src = '/images/loading.gif';                            
                thisimg = document.getElementById(id);    
                
                var inputs = document.getElementsByClassName('selectionprogrammes');
                var invalid = false;
                var match = false;
                //reset all styles
                for (var i=0;i<inputs.length;i++) 
                {
                    inputs[i].className ='selectionprogrammes';
                }
                //changed the current selection to be seleted
                inputs = document.getElementsByClassName('selectionprogrammesselected');
                invalid = false;
                match = false;
                
                for (i=0;i<inputs.length;i++) 
                {
                    inputs[i].className='selectionprogrammes';
                }
                var sSelection
                sSelection = 'selection-'+id; 
                 document.getElementById(sSelection).className='selectionprogrammesselected';
//                            document.getElementById(sSelection).style.backgroundColor = '#FFFFFF';   
//                            document.getElementById(sSelection).style.border = '#D3D3D3 2px solid';  
                                                        
                 //change image
                 var sImagelink;
                 var sImageTitle;
                sImagelink = 'imagelink-'+id;     
                sSrc = document.getElementById(sImagelink).innerHTML;
                sImageTitle = 'imagetitle-'+id; 
                sAlt = document.getElementById(sImageTitle).innerHTML; 
                if(sSrc!='' && sSrc!=undefined)
                document.getElementById('ctl00_maincontent_mainImage').src = sSrc.toString();                             
                document.getElementById('ctl00_maincontent_mainImage').alt = sAlt.toString(); 
                document.getElementById('ctl00_maincontent_mainImage').title = sAlt.toString();           
                }   
                var sDisplayName, sName, sDisplayTitle, sTitle, sInputLink, sLink;
                // displayname
                sDisplayName = 'displayname-'+id;
                sName = document.getElementById(sDisplayName).innerHTML; 
                if(sName!='' && sName!=undefined)
                {document.getElementById('ctl00_maincontent_mi_displayname').innerHTML = sName;}
 
                // Links
                sInputLink = 'selectionid-'+id;       
                sLink = document.getElementById(sInputLink).innerHTML;  
                document.getElementById('ctl00_maincontent_lbMainImage').href = sLink.toString();
                document.getElementById('ctl00_maincontent_lbCredit').href = sLink.toString();
                
                document.getElementById('ctl00_maincontent_mi_title').innerHTML = sAlt;
       
              }                    
     catch(err)
        {
            alert('Error processing the image [' + err.message + ']');
        }
}

  function DoNav(theUrl)
    {
       document.location.href = theUrl;
    }
//----------------------------------------------------------------------------------------------------------------------------------------
// Functions for slideshow on the Selected homepage
//----------------------------------------------------------------------------------------------------------------------------------------

function resize(img, max) 
{
      var elem = document.getElementById('image-'+img);
      if (elem == undefined || elem == null) return false;
      if (max == undefined) max = 100;
      if (elem.width > elem.height) 
      {
          if (elem.width > max) elem.width = max;
      } else {
          if (elem.height > max) elem.height = max;
      }
}

var t,c=1;
function loopthroughgrid()
{
    if (c>9) { c=1 };
    UpdateSelectedMainImage(c);
    c=c+1;
    t = setTimeout("loopthroughgrid()",4000);
}

function restartloop(id)
{
    c=id;
    loopthroughgrid();
}

function pausetoview(id)
{
   clearTimeout(t);
   UpdateSelectedMainImage(id);
}

function UpdateSelectedMainImage(id)
{
    var sSrc, sAlt, thisimg, sTitle, sName, sDisplayName, sDisplayTitle, sSlide,sSelectedslide , sSelection, sProgramme;
                       
    if(id !=undefined)
    { 
        document.getElementById('ctl00_maincontent_mainImage').src = 'http://www.axisweb.org/images/loading.gif';                            
        thisimg = document.getElementById('image-'+id);    
        
        sAlt = thisimg.alt;                                  
        sSrc = thisimg.src.replace('/thumbnail/','/halfscreen/').toString();       

        var inputs = document.getElementsByClassName('gridslide');
        var invalid = false;
        var match = false;
        
        for (var i=0;i<inputs.length;i++) 
        {
            inputs[i].style.backgroundColor='#eee';
            inputs[i].style.border='#D3D3D3 1px solid';
        }
        
        inputs = document.getElementsByClassName('gridslideselected');
        invalid = false;
        match = false;
        
        for (i=0;i<inputs.length;i++) 
        {
            inputs[i].style.backgroundColor='#eee';
            inputs[i].style.border='#D3D3D3 1px solid';
        }
        
        document.getElementById('ctl00_maincontent_mainImage').src = sSrc.toString();                             
        document.getElementById('ctl00_maincontent_mainImage').alt = sAlt.toString(); 
        document.getElementById('ctl00_maincontent_mainImage').title = sAlt.toString();     

        sSlide = 'gridslide-'+id; 
        document.getElementById(sSlide).style.backgroundColor = '#FFFFFF';   
        document.getElementById(sSlide).style.border = '#555 1px solid';       
        // Images provider
        sDisplayName = 'displayname-'+id;
        sName = document.getElementById(sDisplayName).innerHTML; 
        if(sName!='' && sName!=undefined)
            {document.getElementById('ctl00_maincontent_mi_displayname').innerHTML = sName;}
        
        // Selection
        sSelection = 'selection-'+id;
        sProgramme = document.getElementById(sSelection).innerHTML; 
        if(sProgramme!='' && sProgramme!=undefined)
            {document.getElementById('ctl00_maincontent_mi_programme').innerHTML = sProgramme;}
        
        // Works title
        sDisplayTitle = 'title-'+id;       
        sTitle = document.getElementById(sDisplayTitle).innerHTML;  
        if(sTitle!='' && sTitle!=undefined)
            {document.getElementById('ctl00_maincontent_mi_title').innerHTML = sTitle;}   
       
      }
  }

