function Banner_NextClick(BannerIDNum)
{
	var currentBanner = Math.round($("txt_CurrentBnr"+BannerIDNum).value) + 1;
	if (currentBanner >= $("txt_BnrTotal"+BannerIDNum).value)
		currentBanner = 0;
	Banner_Update(BannerIDNum,currentBanner);
}

function Banner_PrevClick(BannerIDNum)
{
	var currentBanner = Math.round($("txt_CurrentBnr"+BannerIDNum).value) - 1;
	if (Math.round(currentBanner) == -1)
		currentBanner = Math.round($("txt_BnrTotal"+BannerIDNum).value) - 1;
	Banner_Update(BannerIDNum,currentBanner);
}

function Banner_Update(BannerIDNum,BannerNum)
{
	//Reset button classes
	for (i=0;i<=($("txt_BnrTotal"+BannerIDNum).value - 1);i=i+1) 
	{
		$("dv_BnrBtn" + BannerIDNum + "_" + i).className = "Banner_Button";
	}
	//Set the new active button
	$("dv_BnrBtn" + BannerIDNum + "_" + BannerNum).className = "Banner_Button Banner_ButtonSelected";
	$("txt_CurrentBnr"+BannerIDNum).value = BannerNum;
	$("img_bnr" + BannerIDNum).src = $("txt_BnrImageURL" + BannerIDNum + "_" + BannerNum).value;
	$("a_bnr" + BannerIDNum).href = $("txt_BnrNavURL" + BannerIDNum + "_" + BannerNum).value;
	
	if ($("txt_BnrNewWindow" + BannerIDNum + "_" + BannerNum).value == "True")
	{
	    $("a_bnr" + BannerIDNum).target = "_blank";
	}
    else
    {
        $("a_bnr" + BannerIDNum).target = "_self";
    }
}
    
/*============================================================================
This routine checks the credit card number. The following checks are made:

1. A number has been provided
2. The number is a right length for the card
3. The number has an appropriate prefix for the card
4. The number has a valid modulus 10 number check digit if required

If the validation fails an error is reported.

The structure of credit card formats was gleaned from a variety of sources on 
the web, although the best is probably on Wikepedia ("Credit card number"):

  http://en.wikipedia.org/wiki/Credit_card_number

Parameters:
            cardnumber           number on the card
            cardname             name of card as defined in the card list below

Author:     John Gardner
Date:       1st November 2003
Updated:    26th Feb. 2005      Additional cards added by request
Updated:    27th Nov. 2006      Additional cards added from Wikipedia
Updated:    18th Jan. 2008      Additional cards added from Wikipedia
Updated:    26th Nov. 2008      Maestro cards extended
*/

function ValidCCNumber(cardnumber, cardname) {
     
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [2] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,622,64,65",
               checkdigit: true};
                              
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}

/*============================================================================*/

/// Formatting Extensions
String.prototype.htmlEncode = function() {
    var div = document.createElement('div');
    if (typeof (div.textContent) == 'string')
        div.textContent = this.toString();
    else
        div.innerText = this.toString();
    return div.innerHTML;
}
String.prototype.trimEnd = function(c) {
    return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function(c) {
    return this.replace(/^\s+/, '');
}
String.repeat = function(chr, count) {
    var str = "";
    for (var x = 0; x < count; x++) { str += chr };
    return str;
}
String.prototype.padL = function(width, pad) {
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) return this.substr(0, width);

    return (String.repeat(pad, length) + this).substr(0, width);
}
String.prototype.padR = function(width, pad) {
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) this.substr(0, width);

    return (this + String.repeat(pad, length)).substr(0, width);
}
String.startsWith = function(str) {
    if (!str) return false;
    return this.substr(0, str.length) == str;
}
String.format = function(frmt, args) {
    for (var x = 0; x < arguments.length; x++) {
        frmt = frmt.replace("{" + x + "}", arguments[x + 1]);
    }
    return frmt;
}
String.prototype.format = function() {
    var a = [this];
    $.merge(a, arguments);
    return String.format.apply(this, a);
}
String.prototype.isNumber = function() {   

    if (this.length == 0) return false;
    if ("0123456789".indexOf(this.substr(0,1)) > -1)
        return true;
    return false;
}
var _monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
Date.prototype.formatDate = function(format) {
    var date = this;
    if (!format)
        format = "MM/dd/yyyy";

    var month = date.getMonth();
    var year = date.getFullYear();

    if (format.indexOf("yyyy") > -1)
        format = format.replace("yyyy", year.toString());
    else if (format.indexOf("yy") > -1)
        format = format.replace("yy", year.toString().substr(2, 2));

    format = format.replace("dd", date.getDate().toString().padL(2, "0"));

    var hours = date.getHours();
    if (format.indexOf("t") > -1) {
        if (hours > 11)
            format = format.replace("t", "pm")
        else
            format = format.replace("t", "am")
    }
    if (format.indexOf("HH") > -1)
        format = format.replace("HH", hours.toString().padL(2, "0"));
    if (format.indexOf("hh") > -1) {
        if (hours > 12) hours -= 12;
        if (hours == 0) hours = 12;
        format = format.replace("hh", hours.toString().padL(2, "0"));
    }
    if (format.indexOf("mm") > -1)
        format = format.replace("mm", date.getMinutes().toString().padL(2, "0"));
    if (format.indexOf("ss") > -1)
        format = format.replace("ss", date.getSeconds().toString().padL(2, "0"));

    if (format.indexOf("MMMM") > -1)
        format = format.replace("MMMM", _monthNames[month]);
    else if (format.indexOf("MMM") > -1)
        format = format.replace("MMM", _monthNames[month].substr(0, 3));
    else
        format = format.replace("MM", month.toString().padL(2, "0"));

    return format;
}
Number.prototype.formatNumber = function(format, option) {
    var num = this;
    if (format.substr(0, 1) == "c") {
        var dec = 2;
        if(format.length > 1) dec = parseInt(format.substr(1));
        if (typeof (dec) != "number") dec = 2;
        num = num.toFixed(dec);
        num = num.toString();
        var s = num.split(".");
        var p = s.length > 1 ? s[1] : '';
        
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(s[0]))
            s[0] = s[0].replace(rgx, '$1,$2');
            
        if(num < 0) 
            return "($" + Math.abs(s[0]) + "." + p.padR(dec, '0') + ")";
        return "$" + s[0] + "." + p.padR(dec, '0');
    }
    if (format.substr(0, 1) == "n") {
        if (format.length == 1)
            return num;
        var dec = format.substr(1);
        dec = parseInt(dec);
        if (typeof (dec) != "number")
            return num.toString();
        num = num.toFixed(dec);
        var x = num.split(".");
        var x1 = x[0];
        var x2 = x.length > 1 ? "." + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1))
            x1 = x1.replace(rgx, '$1,$2');
        if(num < 0) 
            return "(" + x1 + x2 + ")";
        return x1 + x2;
    }
    if (format.substr(0, 1) == "f") {
        if (format.length == 1)
            return num.toString();
        
        var dec = 0;
        if(format.length > 1) dec = parseInt(format.substr(1));
        if (typeof (dec) != "number") dec = 0;
        
        num = num.toString();
        var s = num.split(".");
        var p = s.length > 1 && dec > 0 ? s[1] : '';
        if(num < 0) 
            return "(" + Math.abs(s[0]) + (dec > 0 ? "." + p.padR(dec, '0') : '') + ")";
        return s[0] + (dec > 0 ? "." + p.padR(dec, '0') : '');
                  
    }
    return num.toString();
}

/*====================================================================================
Used on v2_EmpireNewsletterPanel and v3_EmpireNewsletterPanel for newsletter signup. =
====================================================================================*/
function SubmitEmail() 
{
    new Ajax.Request('/AJAXFunctions.aspx', {
        method: 'post',
        asynchronous: true,
        parameters: {
            action: 'newslettersignup',
            email: $('txt_email').value,
            type: $('hid_nlType').value
        },
        onSuccess: function(transport) 
        {
            if(transport.responseText == '1')
            {
                $('pnl_ThankYou').show();
                $('pnl_SignUp').hide();
            }
            else
            {
              $('lbl_SignUpMsg').update('Please enter a valid e-mail address.');
            }     
        },
        onFailure: function(transport)
        {
              $('lbl_SignUpMsg').update('Your request was not processed. Please try again.');
        }
    });
}

function clearField(obj)
{
    obj.value="";
}

/*====================================================================================
Used on AddToFavoritesLink.ascx
====================================================================================*/
function AddtoFavoriteCategory(elem, customerID, categoryID, refID, mediaID)
{
    new Ajax.Updater(elem, '/AJAXFunctions.aspx?action=AddtoFavoriteCategory&categoryID=' + categoryID + '&custid=' + customerID + '&refID=' + refID + '&mediaID=' + mediaID, {asynchronous:true} );
}

function AddtoFavoriteStudio(elem, customerID, studioID)
{
    new Ajax.Updater(elem, '/AJAXFunctions.aspx?action=AddtoFavoriteStudio&studioID=' + studioID + '&custid=' + customerID, {asynchronous:true} );
}

function AddtoFavoriteSeries(elem, customerID, seriesID)
{
    new Ajax.Updater(elem, '/AJAXFunctions.aspx?action=AddtoFavoriteSeries&seriesID=' + seriesID + '&custid=' + customerID, {asynchronous:true} );
}

function AddtoFavoritePerformer(elem, customerID, perfomerID)
{
    new Ajax.Updater(elem, '/AJAXFunctions.aspx?action=AddtoFavoritePerformer&performerID=' + perfomerID + '&custid=' + customerID, {asynchronous:true} );
}

function MarkItemAsErrant(elem, itemID)
{
    new Ajax.Updater(elem, '/AJAXFunctions.aspx?action=MarkItemAsErrant&itemID=' + itemID, {asynchronous:true} );
}




