function slideSwitch() {
    var $active = $('#slider ul li.active');

    if ($active.length == 0) $active = $('#slider ul li:last');

    // use this to pull the divs in the order they appear in the markup
    var $next = $active.next().length ? $active.next()
        : $('#slider ul li:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function()
{
	//Add new list
    $("#test").click(function(event) {
        event.preventDefault();
        $("#clientbar").slideToggle();
    });

    /* == Adds the print page functionality == */
    $(".printpage span").click(function(event) { window.print(); });
});


// Form submit
function SubmitForm(formName)
{
    document.forms[formName].submit();
}
// End Form Submit


// Form validation
function validateForm(FormName)
{
    var validator = $('#' + FormName).validate();
    var isValid = validator.form();
          
      if (isValid) {
       
        if (FormName=='registerfrm') { // Register overlay
    
            RegisterUser();
                    
        } else if (FormName=='reminderfrm') { // Password Reminder Overlay
             
            SendPasswordReminder();
        
        } else { 
        
            SubmitForm(FormName);
        }
      }
}
// End form validation


/* Registration Overlay */
function ResetForm() {
    window.location = window.location;
}

function RegisterUser() {
    
    var stEmail = document.getElementById("stEmail").value;
    var stName = document.getElementById("stName").value;
    var stCompany = document.getElementById("stCompany").value;

    var sURL = '/SubmitTo/RegisterUser.aspx?stEmail=' + stEmail + '&stName=' + stName + '&stCompany=' + stCompany;
    
    GetContent(sURL, "ConfirmRegistration");           
}

function ConfirmRegistration(ret) {
    // Code 1 = Success
    // Code 2 = Error saving registration
    // Code 3 = Duplicate registration
    var stSplit = ret.split("_");         
    document.getElementById("div1").innerHTML = "<p>"+stSplit[1]+"</p>";
   
    if (stSplit[0]==1) {
        document.getElementById("ulReg").setAttribute("style","display:none;");
        document.getElementById("divOK").setAttribute("style","display:none;");
        document.getElementById("pText").innerHTML='';
        document.getElementById("divCancel").setAttribute("style","display:none;");
        document.getElementById("aRegisterOK").setAttribute("style","display:block;");
    }    
}

$(document).ready(function() {

    $("#slider").easySlider({
        auto: true,
        continuous: true
    });


    $('.expandablecontent').click(function() {

        var currentstate = $(this).children('h1:eq(0)').attr('class');

        if (currentstate != 'on') {
            $('.expanded').slideUp();
            $('.expandablecontent').children('h1').removeClass("on");
            $(this).children('h1').addClass("on");
            $(this).children('h1').removeClass("off");
            $(this).children('div').slideDown('200');
        }
        else {
            $('.expanded').slideUp();
            $('.expandablecontent h1').removeClass("on");
        }
    });

    var triggers = $("a.modalInput").overlay({

        // some expose tweaks suitable for modal dialogs
        expose: {
            color: '#333',
            loadSpeed: 200,
            opacity: 0.9
        },

        closeOnClick: false
    });

    $("#prompt form").submit(function(e) {

        // close the overlay
        triggers.eq(1).overlay().close();

        // do not submit the form
        return e.preventDefault();
    });

});
/* End Registration Overlay */



/* Password Reminder Overlay */

function SendPasswordReminder() {

    var stEmail = document.getElementById("stEmailReminder").value;
   
    var sURL = '/SubmitTo/PasswordReminder.aspx?stEmail='+stEmail+'&='+Date();
   
    GetContent(sURL, "ConfirmPasswordReminder");    
}

function ConfirmPasswordReminder(ret) {
    // Code 1 = Success
    // Code 2 = Error retreiving login details
    // Code 3 = Email does not exist
    var stSplit = ret.split("_");         
    document.getElementById("div2").innerHTML = "<p>"+stSplit[1]+"</p>";
   
    if (stSplit[0]==1) {
        document.getElementById("ulRem").setAttribute("style","display:none;");
        document.getElementById("divReminderOK").setAttribute("style","display:none;");
        document.getElementById("divReminderCancel").setAttribute("style","display:none;");
        document.getElementById("pRemText").innerHTML='';
        document.getElementById("aReminderOK").setAttribute("style","display:block;");        
    }    
}

function ResetReminderForm() {
    window.location = window.location;
}
/* End Password Reminder Overlay */



/* Client Login Overlay */

function LoginClient() {
    var sUsername = document.getElementById("uname").value;
    var sPassword = document.getElementById("pword").value;
    var sURL = '/SubmitTo/ClientLogin.aspx?stUname='+sUsername+'&stPword='+sPassword;
    
    GetContent(sURL, "ConfirmClientLogin");    
}

function ConfirmClientLogin(ret) {
    document.getElementById("pLogin").innerHTML = ret; 
}

/* End Client Login Overlay */




/* List module search */

// Called when a category is selected from category drop down list
function RefreshSearch(ctrlId) {

    var iSelectedCat;
    
    if (ctrlId==1) {
        iSelectedCat = document.getElementById("iListCategory1").value;        
    } else {
        iSelectedCat = document.getElementById("iListCategory2").value;        
    }

    var splitStr = document.location.href.split("?");
    var link = splitStr[0];    
    window.location = link+"?cid="+iSelectedCat;
}

// Called when a page is selected from page drop down list
function SelectPage(iFirstItem, catId, ctrlId, pageNo, pageItems) {

    var selectedPage;

    if (ctrlId==1) {
        selectedPage = document.getElementById("pageList1").selectedIndex+1;
    } else {
        selectedPage = document.getElementById("pageList2").selectedIndex+1;
    }
    
    var noOfPagesToChange;
    var noToIncrement;
    
    if (selectedPage > pageNo) { // Need to increment items
    
        noOfPagesToChange=selectedPage-pageNo;
        noToIncrement=noOfPagesToChange*pageItems;
    
        iFirstItem=iFirstItem+noToIncrement; 
        
    } else if (selectedPage < pageNo) { // Need to decrement items
    
        noOfPagesToChange=pageNo-selectedPage;
        noToIncrement=noOfPagesToChange*pageItems;
        
        iFirstItem=iFirstItem-noToIncrement; 
    }

    var splitURL = document.location.href.split("?");
    var sLink = splitURL[0];
    
    sLink+="?fi="+iFirstItem;
    sLink+="&cid="+catId;
    sLink+="&pno="+selectedPage;
   
    window.location = sLink;
}
/* End List module search */

/* List Module Search */

// Called when a category is selected from category drop down list
function RefreshSearch(iFirstItem, catId, ctrlId, pageNo, pageItems) {

    var iSelectedCat;
    var selectedPage;

    if (ctrlId == 1) {
        selectedPage = document.getElementById("pageList1").selectedIndex + 1;
    } else {
        selectedPage = document.getElementById("pageList2").selectedIndex + 1;
    }

    if (ctrlId == 1) {
        iSelectedCat = document.getElementById("iListCategory1").value;
    } else {
        iSelectedCat = document.getElementById("iListCategory2").value;
    }

    var splitStr = document.location.href.split("?");
    var sLink = splitStr[0];

    sLink += "?fi=" + iFirstItem;
    sLink += "&cid=" + iSelectedCat;
    sLink += "&pno=" + selectedPage;

    window.location = sLink;
}
