//
// "Internal" function to return the decoded value of a cookie
//

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) {
    var j = i + alen;

    if (document.cookie.substring(i, j) == arg)
      return getCookieVal(j);

    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }

  return null;
}


//
//  Function to create or update a cookie.
//
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//

function SetCookie (name, value, expires, path, domain, secure) {

  document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") 
                    + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") 
                    + ((secure) ? "; secure" : "");

}



//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//

function DeleteCookie (name, path, domain) {

  if (GetCookie(name)) {
    document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }

}

function detectCookieSetting()
{
  var random_num = Math.floor(Math.random() * 1000000);

  SetCookie("cb", escape(random_num), null, "/");

  var cookieData = GetCookie("cb");

  if (cookieData == escape(random_num)) {
    return "true";
  } else {
    return "false";
  }
}

function formatDate(number) {
  var result;

  if (number < 10) {
    result = "0" + number;
  } else {
    result = number;
  }
  
  return result;
}

function getDateStr() {
  var today = new Date();
  var year = formatDate(today.getYear() % 100);
  var month = formatDate(today.getMonth() + 1);
  var date = formatDate(today.getDate());

  var todayStr = month + "." + date + "." + year;

  return todayStr;
}

function formatDateAlphabet(number) {
    var result;

    number = number % 60;

    if (number == 1) {          result = "A";
    } else if (number == 2) {   result = "B";
    } else if (number == 3) {   result = "C";
    } else if (number == 4) {   result = "D";
    } else if (number == 5) {   result = "E";
    } else if (number == 6) {   result = "F";
    } else if (number == 7) {   result = "G";
    } else if (number == 8) {   result = "H";
    } else if (number == 9) {   result = "I";
    } else if (number == 10) {  result = "J";
    } else if (number == 11) {  result = "K";
    } else if (number == 12) {  result = "L";
    } else if (number == 13) {  result = "M";
    } else if (number == 14) {  result = "N";
    } else if (number == 15) {  result = "O";
    } else if (number == 16) {  result = "P";
    } else if (number == 17) {  result = "Q";
    } else if (number == 18) {  result = "R";
    } else if (number == 19) {  result = "S";
    } else if (number == 20) {  result = "T";
    } else if (number == 21) {  result = "U";
    } else if (number == 22) {  result = "V";
    } else if (number == 23) {  result = "W";
    } else if (number == 24) {  result = "X";
    } else if (number == 25) {  result = "Y";
    } else if (number == 26) {  result = "Z";
    } else if (number == 27) {  result = "a";
    } else if (number == 28) {  result = "b";
    } else if (number == 29) {  result = "c";
    } else if (number == 30) {  result = "d";
    } else if (number == 31) {  result = "e";
    } else if (number == 32) {  result = "f";
    } else if (number == 33) {  result = "g";
    } else if (number == 34) {  result = "h";
    } else if (number == 35) {  result = "i";
    } else if (number == 36) {  result = "j";
    } else if (number == 37) {  result = "k";
    } else if (number == 38) {  result = "l";
    } else if (number == 39) {  result = "m";
    } else if (number == 40) {  result = "n";
    } else if (number == 41) {  result = "o";
    } else if (number == 42) {  result = "p";
    } else if (number == 43) {  result = "q";
    } else if (number == 44) {  result = "r";
    } else if (number == 45) {  result = "s";
    } else if (number == 46) {  result = "t";
    } else if (number == 47) {  result = "u";
    } else if (number == 48) {  result = "v";
    } else if (number == 49) {  result = "w";
    } else if (number == 50) {  result = "x";
    } else if (number == 51) {  result = "y";
    } else if (number == 52) {  result = "z";
    } else if (number == 53) {  result = "1";
    } else if (number == 54) {  result = "2";
    } else if (number == 55) {  result = "3";
    } else if (number == 56) {  result = "4";
    } else if (number == 57) {  result = "5";
    } else if (number == 58) {  result = "6";
    } else if (number == 59) {  result = "7";
    } else {                    result = "0";
    } 
 
    return result;
}

function getCurrentDate() {
  var today = new Date();
  var year = today.getYear();
  var month = today.getMonth() + 1;
  var date = today.getDate();

  if (month < 10) {
    month = '0' + month;
  }

  if (date < 10) {
    date = '0' + date;
  }

  var todayStr = year + '-' + month + "-" + date;

  return todayStr;
}

function ValidateGetPaidForm(theForm)
{
  var error = "";
  var email = theForm.email.value;
  var referrer = theForm.referrer.value;
  var offer = theForm.creditcard.value;
  var tid = getTid();

  if (email != "") {
    if (validateEmail(email) == false) {
      error += "Please verify that your email address is correct.\n";
    }
  }

  // if (referrer != "") {
  //   if (validateEmail(referrer) == false) {
  //     error += "Please verify that your friend's email address is correct.\n";
  //   }
  // }

  if (error == "") {
    // var expdate = new Date();
    // expdate.setTime(expdate.getTime() + (30 * 60 * 1000)); // 30 minutes from now 

    var card = document.getpaidform.creditcard.value;
    var cid = GetCid(card);
    if (cid == null) {
      cid = "";
    }

    var queryStr = "email=" + email + "&referrer=" + referrer + "&offer=" + offer + "&tid=" + tid + "&cid=" + cid;
    theForm.track.src="/cgi-bin/cb/track.pl?" + queryStr;

    show(offer, offer, tid);

    return false;
  } else {
    alert(error);
    return false;
  }
}

function ValidateLoginForm(theForm)
{
  var error = "";
  var email = theForm.email.value;
  var password = theForm.password.value;

  if (email != "") {
    if (validateEmail(email) == false) {
      error += "Please verify that your email address is correct.\n";
    } else {
      if (password.length < 6) {
        error += "Your password must have at least 6 characters.\n";
      } else if (password.length > 16) {
        error += "Your password may not exceed 16 characters.\n";
      }
    }
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function ValidateRegistrationForm(theForm)
{
  var error = "";
  var email = theForm.email.value;
  var password = theForm.password.value;
  var confirmpwd = theForm.confirmpwd.value;

  if (email != "") {
    if (validateEmail(email) == false) {
      error += "Your email address is incorrect.\n";
    }
    if (password != confirmpwd) {
      error += "Your password entries do not match.\n";
    } else {
      if (password.length < 6) {
        error += "Your password must have at least 6 characters.\n";
      } else if (password.length > 16) {
        error += "Your password may not exceed 16 characters.\n";
      }
    }
  } else {
    error += "Please enter a valid email address.\n";
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function ValidateProfileForm(theForm)
{
  var error = "";
  var errorFlag = 0;

  var firstname = theForm.firstname.value;
  var lastname  = theForm.lastname.value;
  // var ssn       = theForm.ssn.value;
  var addr1     = theForm.addr1.value;
  var city      = theForm.city.value;
  var state     = theForm.state.value;
  var zip       = theForm.zip.value;
  var phone     = theForm.phone.value;

  if (firstname.length < 1 || lastname.length < 1 || addr1.length < 4 || city.length < 1 || state.length < 2 || zip.length < 5) {
      error += "Please complete the following required fields:\n\n";
      
      if (firstname.length < 1) {
          error += "    *  First Name;\n";
      }
      if (lastname.length < 1) {
          error += "    *  Last Name;\n";
      }
      if (addr1.length < 4) {
          error += "    *  Address;\n";
      }
      if (city.length < 1) {
          error += "    *  City;\n";
      }
      if (state.length < 2) {
          error += "    *  State;\n";
      }
      if (zip.length < 5) {
          error += "    *  Zip Code;\n";
      }    
  } else {
      var chars1 = "-0123456789";
      var chars2 = "0123456789";
      var chars3 = "-() 0123456789";

      // count = 0;
      // for (i=0;i<ssn.length;i++) {
      //    if (chars1.indexOf(ssn.charAt(i)) == -1) {
      //        errorFlag = 1;
      //        break;
      //    }

      //    if (chars2.indexOf(ssn.charAt(i)) != -1) {
      //        count++;
      //    }
      // }
      // if (errorFlag == 1 || count != 9) {
      //    error += "Please enter a valid social security number.\n";
      // }

      errorFlag = 0;
      phone_len = 0;
      for (i=0;i<phone.length;i++) {
         if (chars3.indexOf(phone.charAt(i)) == -1) {
             errorFlag = 1;
             break;
         }
         if (chars2.indexOf(phone.charAt(i)) != -1) {
             if (phone_len != 0 || phone.charAt(i) != '1') {
                 phone_len = phone_len + 1;
             }
         }
      }
      if (phone_len != 10) {
         errorFlag = 1;
      }

      if (errorFlag == 1) {
         error += "Please enter a valid phone number.\n";
      }
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function ValidatePasswordForm(theForm)
{
  var error = "";
  var oldpwd  = theForm.oldpwd.value;
  var newpwd1 = theForm.newpwd1.value;
  var newpwd2 = theForm.newpwd2.value;

  if (newpwd1 != newpwd2) {
      error += "Please verify that your entries for new password match.\n";
  } else {
      if (oldpwd.length < 6 || newpwd1.length < 6 || newpwd2 < 6) {
          error += "Your password must have at least 6 characters.\n";
      } else if (oldpwd.length > 16 || newpwd1.length > 16 || newpwd2 > 16) {
          error += "Your password may not exceed 16 characters.\n";
      }
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function ValidateReminderForm(theForm)
{
  var error = "";
  var email = theForm.email.value;

  if (email != "") {
    if (validateEmail(email) == false) {
      error += "Please verify that your email address is correct.\n";
    }
  } else {
      error += "Please provide your email address.\n";
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function ValidateRefCodeForm(theForm)
{
  var error = "";
  var email = theForm.email.value;

  if (email != "") {
    if (validateEmail(email) == false) {
      error += "Please verify that your email address is correct.\n";
    }
  } else {
      error += "Please provide your email address.\n";
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function ValidateFriendsForm(theForm)
{
  var error = "";

  var name = theForm.senderName.value;
  var email = theForm.senderEmail.value;

  var recipientName = theForm.recipientName.value;
  var recipientEmail = theForm.recipientEmail.value;

  // alert(email);

  if (name == "") {
    error += "Please enter your name.\n";
  }

  if (email != "") {
    if (validateEmail(email) == false) {
      error += "Your email address is incorrect.\n";
    }
  } else {
    error += "Please enter your valid email address.\n";
  }

  if (recipientName == "") {
    error += "Please enter your friend's name.\n";
  }

  if (recipientEmail != "") {
    if (validateEmail(recipientEmail) == false) {
      error += "The email address of your friend is incorrect.\n";
    }
  } else {
    error += "Please enter the valid email address of your friend.\n";
  }

  if (error == "") {
    return true;
  } else {
    alert(error);
    return false;
  }
}

function validateEmail(email) {

  var text = email;

  // The Field Shouldn't be Empty
  if (text == "") {
    return false;
  }

  // Check For Bad Chars
  var badchar = " ()<>\/[]{}*|;,'\"";

  for (i=0;i<text.length;i++) {
    for (j=0;j<badchar.length;j++) {
      if (text.charAt(i) == badchar.charAt(j)) {
        return false;
      }
    }
  }

  // There should be at least a dot (.) Symbol
  if (text.indexOf(".") == -1) {
    return false;
  }

  // There should be an @ Symbol
  if (text.indexOf("@") == -1) {
    return false;
  } else {
    // Only one @ symbol should be found
    if (text.indexOf("@") != text.lastIndexOf("@")) {
      return false;
    }
  }

  // Check For Syntax Errors Such as .@  or   @.  or  ..
  if (text.indexOf("@.") != -1 || text.indexOf(".@") != -1 || text.indexOf("..") != -1) {
    return false;
  }

  // The first character shouldn't be @ or .
  if (text.charAt(0) == "@" || text.charAt(0) == ".") {
    return false;
  }

  // The last two characters shouldn't contain @ or .
  if (text.charAt(text.length-1) == "@" || text.charAt(text.length-1) == "." || text.charAt(text.length-2) == "@" || text.charAt(text.length-2) == ".") {
    return false;
  }

  // The '@' Symbol should be found before the last '.'
  var foundchar1 = text.indexOf("@");
  var foundchar2 = text.lastIndexOf(".");
  if (foundchar1 > foundchar2) {
    return false;
  }
  
  // Any characters after the last '.' symbol should only be letters;
  // Any characters after the '@' symbol should only be letters, numbers, '-', '.'
  var posAt = text.indexOf("@");
  var posLastDot = text.lastIndexOf(".");

  var validLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.";

  for (i = posAt + 1; i < text.length; i++) {
    if (validChars.indexOf(text.charAt(i)) == -1) {
      return false;
    }     
  }

  for (i = posLastDot + 1; i < text.length; i++) {
    if (validLetters.indexOf(text.charAt(i)) == -1) {
      return false;
    }
  }

  return true;
}

function CB_goTo(url) {
  window.location.href = url;
}

function CB_navBar(tableCellRef, hoverFlag) {
  if (hoverFlag) {
    tableCellRef.style.backgroundColor = '#00CC33';
  } else {
    tableCellRef.style.backgroundColor = '#336600';
  }
}

function CB_navBarClick(tableCellRef, url) {
  CB_navBar(tableCellRef, 0);
  CB_goTo(url);
}

function CB_sub_navBar(tableCellRef, hoverFlag, color) {
  if (color == "lightblue") {
    if (hoverFlag) {
      tableCellRef.style.backgroundColor = '#4169e1';
    } else {
      tableCellRef.style.backgroundColor = '#333366';
    }
  }
}

function CB_sub_navBarClick(tableCellRef, url, color) {
  CB_sub_navBar(tableCellRef, 0, color);
  CB_goTo(url);
}

function showInvitation(name)
{
  var href = "/cgi-bin/cb/invitation.pl?name=" + name;

  var newWindow;
  newWindow = window.open(href, 'Invitation', 'width=800,height=600,directories=no,location=yes,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes');

  newWindow.focus();

  return false;
}

function view(link)
{
  var newWindow;

  var width = 800;
  var height = 600;

  if (window.screen.width >= 1400) {
    width = 1280;
    height = 1024;
  } else if (window.screen.width >= 1152) {
    width = 1024;
    height = 768;
  }

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=yes,menubar=yes,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes';

  newWindow = window.open(link, 'vw', windowProperties);

  newWindow.focus();

  return false;
}

function view2(link, width, height)
{
  var newWindow;

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=no,menubar=yes,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no';
  // var windowProperties = 'width=480,height=360,top=100,left=100,directories=no,location=yes,menubar=yes,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes';

  newWindow = window.open(link, 'vw2', windowProperties);

  newWindow.focus();

  return false;
}

function profileTip(name)
{
  var href = "/report/tip1.htm";
  if (name == "tip2") {
    href = "/report/tip2.htm";
  } else if (name == "tipbbs") {
    href = "/report/tipbbs.htm";
  }

  var newWindow;

  var width = 480;
  var height = 360;

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no';

  newWindow = window.open(href, 'tip', windowProperties);

  newWindow.focus();

  return false;
}

function showGiftCards()
{
  var href = "/ccards/giftcards.htm";

  var newWindow;

  var width = 800;
  var height = 600;

  if (window.screen.width >= 1400) {
    width = 1280;
    height = 1024;
  } else if (window.screen.width >= 1152) {
    width = 1024;
    height = 768;
  }

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=yes,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes';

  newWindow = window.open(href, 'GiftCard', windowProperties);

  newWindow.focus();

  return false;
}

function showGiftCards2()
{
  var href = "/ccards/giftcards2.htm";

  var newWindow;

  var width = 800;
  var height = 600;

  if (window.screen.width >= 1400) {
    width = 1280;
    height = 1024;
  } else if (window.screen.width >= 1152) {
    width = 1024;
    height = 768;
  }

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=yes,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes';

  newWindow = window.open(href, 'GiftCard2', windowProperties);

  newWindow.focus();

  return false;
}

function show(link, windowname, subid, params)
{
  if (!window.focus)
    return true;

  var keyword;
  var offerType = "default";
  var href;
  var dataType;

  keyword = windowname;

  var inSubid = subid;
  if (subid == "0" || escape(subid) == "undefined") {
    subid = getTid();
  }

  var bankName = GetBankName(keyword);
  if (bankName == "citi" || bankName == "amex" || bankName == "usbank") {
    var cookieEnabled = detectCookieSetting();
    if (cookieEnabled == "true") {
      if (GetCid(keyword) == null) {
        SetCid(keyword, subid);
      }
    }
  }

  if (escape(params) == "undefined" || params.length < 3) {
    if (inSubid == "0" || escape(inSubid) == "undefined") {
      var queryStr = "&offer=" + windowname + "&tid=" + subid;
      document.click.src="/cgi-bin/cb/click.pl?" + queryStr;
    }
  }

  var newWindow;

  var width = 800;
  var height = 600;

  if (window.screen.width >= 1400) {
    width = 1280;
    height = 1024;
  } else if (window.screen.width >= 1152) {
    width = 1024;
    height = 768;
  }

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=yes,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes';

  if (escape(params) == "undefined" || params.length < 3) {
    href = 'http://cardbenefit.com/php/offer.php?offer=' + windowname + '&subid=' + subid;
  } else {
    href = 'http://cardbenefit.com/php/offer.php?offer=' + windowname + '&subid=' + subid + '&' + params;
  }

  newWindow = window.open(href, windowname, windowProperties);

  newWindow.focus();

  return false;
}

function GetBankName(keyword)
{
  var bankName = "";

  if (keyword == "CTP" || keyword == "CDP" || keyword == "AUR" || keyword == "CPF" || keyword == "CCR") {
    bankName = "citi";
  } else if (keyword == "CDS" || keyword == "CPP" || keyword == "CDV" || keyword == "HHH" || keyword == "CDI") {
    bankName = "citi";
  } else if (keyword == "CBA" || keyword == "MTV" || keyword == "CHR" || keyword == "CUC" || keyword == "CPA") {
    bankName = "citi";
  } else if (keyword == "CPS" || keyword == "CPV" || keyword == "ATT" || keyword == "CAA" || keyword == "CBC") {
    bankName = "citi";
  } else if (keyword == "CBT" || keyword == "CTB" || keyword == "CAD" || keyword == "CBZ") {
    bankName = "citi";
  } else if (keyword == "BCS" || keyword == "BCB" || keyword == "HHP" || keyword == "SPG" || keyword == "GDS") {
    bankName = "amex";
  } else if (keyword == "DSB" || keyword == "JBC" || keyword == "BPF") {
    bankName = "amex";
  } else if (keyword == "NYC" || keyword == "LAC" || keyword == "CHI" || keyword == "BGR") {
    bankName = "amex";
  } else if (keyword == "ONE" || keyword == "SKY" || keyword == "BAM" || keyword == "PBC" || keyword == "BGC") {
    bankName = "amex";
  } else if (keyword == "JBB" || keyword == "PRG" || keyword == "PRW" || keyword == "SPB" || keyword == "NCC") {
    bankName = "amex";
  } else if (keyword == "KCC" || keyword == "RAE" || keyword == "SBC" || keyword == "CAM") {
    bankName = "amex";
  } else if (keyword == "AEA") {
    bankName = "amex";
  } else if (keyword == "WPV") {
    bankName = "usbank";
  } 

  return bankName;
}

function SetCid(keyword, subid)
{
  var days = 0;

  if (GetBankName(keyword) == "citi") {
    days = 30;    
  } else if (GetBankName(keyword) == "amex") {
    days = 90;
  } else if (GetBankName(keyword) == "usbank") {
    days = 1;
  }

  var expdate = new Date();
  expdate.setTime(expdate.getTime() + (days * 24 * 60 * 60 * 1000)); 

  SetCookie(keyword, subid, expdate, "/");
}

function GetCid(keyword)
{
  var subid = GetCookie(keyword);
  return subid;
}

function popup(link)
{
  if (!window.focus)
    return true;

  var href;

  href = link.href;

  var newWindow;
  newWindow = window.open(href, 'rewards', 'width=800,height=600,directories=no,location=yes,menubar=yes,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes');

  newWindow.focus();

  return false;
}

function displayAd(link)
{
  if (!window.focus)
    return true;

  var href;

  href = link.href;

  var newWindow;
  newWindow = window.open(href, 'advertisement', 'width=800,height=600,directories=no,location=yes,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=yes');

  newWindow.focus();

  return false;
}

function validate()
{ 
  if ((document.guestform.realname.value=="") || (document.guestform.email.value=="")) {
    return false;
  } else {
    return true;
  }
}

function getTid()
{
  var today = new Date();

  var month = formatDateAlphabet(today.getMonth() + 1);
  var date = formatDateAlphabet(today.getDate());
  var zone = getTimeZoneSymbol();

  var hour = formatDateAlphabet(today.getHours());
  var minute = formatDateAlphabet(today.getMinutes());
  var second = formatDateAlphabet(today.getSeconds());

  var random_num = Math.floor(Math.random() * 1000);
  while (random_num % 100 == 0) {
	random_num = Math.floor(Math.random() * 1000);
  }

  var text;

  if (random_num < 10) {
    text = month + date + zone + hour + minute + second + "00" + random_num;
  } else if (random_num < 100) {
    text = month + date + zone + hour + minute + second + "0" + random_num;
  } else {
    text = month + date + zone + hour + minute + second + random_num;
  }

  return text;
}

function getTimeZoneSymbol()
{
  var timezone;
  var daylightSummertime = 1;
  // var daylightSummertime = 0;
  var d = new Date();
  var gmtHours = d.getTimezoneOffset() / 60;

  gmtHours = gmtHours + daylightSummertime;

  if (gmtHours == 0) {
    // Greenwich
    timezone = "G";
  } else if (gmtHours == 1) {
    // Azores
    timezone = "Z";
  } else if (gmtHours == 2) {
    // MidAtlantic
    timezone = "D";
  } else if (gmtHours == 3) {
    // Greenland
    timezone = "Q";
  } else if (gmtHours == 4) {
    // Atlantic
    timezone = "T";
  } else if (gmtHours == 5) {
    // EST
    timezone = "E";
  } else if (gmtHours == 6) {
    // CST
    timezone = "C";
  } else if (gmtHours == 7) {
    // MST
    timezone = "M";
  } else if (gmtHours == 8) {
    // PST
    timezone = "P";
  } else if (gmtHours == 9) {
    // Alaska
    timezone = "A";
  } else if (gmtHours == 10) {
    // Hawaii
    timezone = "H";
  } else if (gmtHours == 11) {
    // MidwayIsland
    timezone = "I";
  } else if (gmtHours == 12) {
    // Eniwetok
    timezone = "W";
  } else if (gmtHours == -1) {
    // Paris
    timezone = "R";
  } else if (gmtHours == -2) {
    // Cairo
    timezone = "O";
  } else if (gmtHours == -3) {
    // Moscow
    timezone = "S";
  } else if (gmtHours == -4) {
    // Baku
    timezone = "K";
  } else if (gmtHours == -5) {
    // Islamabad
    timezone = "L";
  } else if (gmtHours == -6) {
    // Astana
    timezone = "N";
  } else if (gmtHours == -7) {
    // Bangkok
    timezone = "B";
  } else if (gmtHours == -8) {
    // Beijing
    timezone = "J";
  } else if (gmtHours == -9) {
    // Tokyo
    timezone = "Y";
  } else if (gmtHours == -10) {
    // Sydney
    timezone = "V";
  } else if (gmtHours == -11) {
    // SolomonIsland
    timezone = "U";
  } else if (gmtHours == -12) {
    // Fiji
    timezone = "F";
  } else {
    // timezone = "GMT - " + gmtHours;
    timezone = "X";
  }

  return timezone;
}

function detectDaylightSavings()
{
    var Date1 = new Date();
    Date1.setFullYear(2007, 2, 11);
    Date1.setHours(2, 0, 0, 0);

    var Date2 = new Date();
    Date2.setFullYear(2007, 10, 4);
    Date2.setHours(2, 0, 0, 0);

    var Date3 = new Date();
    Date3.setFullYear(2008, 2, 9);
    Date3.setHours(2, 0, 0, 0);

    var Date4 = new Date();
    Date4.setFullYear(2008, 10, 2);
    Date4.setHours(2, 0, 0, 0);

    var Date5 = new Date();
    Date5.setFullYear(2009, 2, 8);
    Date5.setHours(2, 0, 0, 0);

    var Date6 = new Date();
    Date6.setFullYear(2009, 10, 1);
    Date6.setHours(2, 0, 0, 0);

    var Date7 = new Date();
    Date7.setFullYear(2010, 2, 14);
    Date7.setHours(2, 0, 0, 0);

    var Date8 = new Date();
    Date8.setFullYear(2010, 10, 7);
    Date8.setHours(2, 0, 0, 0);

    var today = new Date();
    if (today >= Date1 && today < Date2) {
        return 1;
    } else if (today >= Date2 && today < Date3) {
        return 0;
    } else if (today >= Date3 && today < Date4) {
        return 1;
    } else if (today >= Date4 && today < Date5) {
        return 0;
    } else if (today >= Date5 && today < Date6) {
        return 1;
    } else if (today >= Date6 && today < Date7) {
        return 0;
    } else if (today >= Date7 && today < Date8) {
        return 1;
    } else if (today >= Date8 || today < Date1) {
        return detectGenericDaylightSavings();
    }
}

function detectGenericDaylightSavings()
{
    var testDate1 = new Date(2007, 0, 1, 10, 30, 00, 1);
    var dateDiff1 = (testDate1.getHours() - testDate1.getUTCHours());

    var testDate2 = new Date();
    var dateDiff2 = (testDate2.getHours() - testDate2.getUTCHours());

    if (dateDiff1 + 1 == dateDiff2) {
        return 1;
    } else {
        return 0;
    }
}

function getTimeZone()
{
  var timezone;
  var daylightSummertime = detectDaylightSavings();
  var d = new Date();

  if (navigator.appName == "Netscape") {
    var s = d.toString();
    if (s.indexOf("GMT-0400") > 0 && s.indexOf("Eastern") > 0) {
      timezone = "EDT";
    } else if (s.indexOf("GMT-0500") > 0 && s.indexOf("Eastern") > 0) {
      timezone = "EST";
    } else if (s.indexOf("GMT-0500") > 0 && s.indexOf("Central") > 0) {
      timezone = "CDT";
    } else if (s.indexOf("GMT-0600") > 0 && s.indexOf("Central") > 0) {
      timezone = "CST";
    } else if (s.indexOf("GMT-0600") > 0 && s.indexOf("Mountain") > 0) {
      timezone = "MDT";
    } else if (s.indexOf("GMT-0700") > 0 && s.indexOf("Mountain") > 0) {
      timezone = "MST";
    } else if (s.indexOf("GMT-0700") > 0 && s.indexOf("Pacific") > 0) {
      timezone = "PDT";
    } else if (s.indexOf("GMT-0800") > 0 && s.indexOf("Pacific") > 0) {
      timezone = "PST";
    } else if (s.indexOf("GMT-0800") > 0 && s.indexOf("Alaska") > 0) {
      timezone = "AKDT";
    } else if (s.indexOf("GMT-0900") > 0 && s.indexOf("Alaska") > 0) {
      timezone = "AKST";
    } else if (s.indexOf("GMT-1000") > 0 && s.indexOf("Hawaii") > 0) {
      timezone = "HST";
    } else if (s.indexOf("GMT-0400") > 0 && s.indexOf("Atlantic") > 0) {
      timezone = "ATC";
    } else {
      timezone = s.substr(s.indexOf("("));
    }
  } else if (navigator.appName == "Microsoft Internet Explorer") {
    var gmtHours = d.getTimezoneOffset() / 60;
    var adjusted_gmtHours = gmtHours + daylightSummertime;

    if (adjusted_gmtHours == 4) {
      timezone = "ATC";
    } else if (adjusted_gmtHours == 5) {
      if (daylightSummertime == 1) {
        timezone = "EDT";
      } else {
        timezone = "EST";
      }
    } else if (adjusted_gmtHours == 6) {
      if (daylightSummertime == 1) {
        timezone = "CDT";
      } else {
        timezone = "CST";
      }
    } else if (adjusted_gmtHours == 7) {
      if (daylightSummertime == 1) {
        timezone = "MDT";
      } else {
        timezone = "MST";
      }
    } else if (adjusted_gmtHours == 8) {
      if (daylightSummertime == 1) {
        timezone = "PDT";
      } else {
        timezone = "PST";
      }
    } else if (adjusted_gmtHours == 9) {
      if (daylightSummertime == 1) {
        timezone = "AKDT";
      } else {
        timezone = "AKST";
      }
    } else if (adjusted_gmtHours == 10) {
      timezone = "HST";
    } else if (adjusted_gmtHours == 11) {
      timezone = "Samoa";
    } else {
      if (gmtHours > 0) {
        timezone = "GMT - " + gmtHours;
      } else {
        var tmp = 0 - gmtHours;
        timezone = "GMT + " + tmp;
      }
    }
  } else {
    timezone = "";
  }

/*
  var timezone;
  var daylightSummertime = 1;
  // var daylightSummertime = 0;
  var d = new Date();
  var gmtHours = d.getTimezoneOffset() / 60;

  gmtHours = gmtHours + daylightSummertime;

  if (gmtHours == 0) {
    timezone = "Greenwich";
  } else if (gmtHours == 1) {
    timezone = "Azores";
  } else if (gmtHours == 2) {
    timezone = "MidAtlantic";
  } else if (gmtHours == 3) {
    timezone = "Greenland";
  } else if (gmtHours == 4) {
    timezone = "Atlantic";
  } else if (gmtHours == 5) {
    timezone = "EST";
  } else if (gmtHours == 6) {
    timezone = "CST";
  } else if (gmtHours == 7) {
    timezone = "MST";
  } else if (gmtHours == 8) {
    timezone = "PST";
  } else if (gmtHours == 9) {
    timezone = "Alaska";
  } else if (gmtHours == 10) {
    timezone = "Hawaii";
  } else if (gmtHours == 11) {
    timezone = "MidwayIsland";
  } else if (gmtHours == 12) {
    timezone = "Eniwetok";
  } else if (gmtHours == -1) {
    timezone = "Paris";
  } else if (gmtHours == -2) {
    timezone = "Cairo";
  } else if (gmtHours == -3) {
    timezone = "Moscow";
  } else if (gmtHours == -4) {
    timezone = "Baku";
  } else if (gmtHours == -5) {
    timezone = "Islamabad";
  } else if (gmtHours == -6) {
    timezone = "Astana";
  } else if (gmtHours == -7) {
    timezone = "Bangkok";
  } else if (gmtHours == -8) {
    timezone = "Beijing";
  } else if (gmtHours == -9) {
    timezone = "Tokyo";
  } else if (gmtHours == -10) {
    timezone = "Sydney";
  } else if (gmtHours == -11) {
    timezone = "SolomonIsland";
  } else if (gmtHours == -12) {
    timezone = "Fiji";
  } else {
    timezone = "GMT - " + gmtHours;
  }
*/

  return timezone;
}

function setCard(card)
{
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + (30 * 60 * 1000)); // 30 minutes from now 

  SetCookie ("cardName", card, expdate, "/");

  return true;
}

function selectCard()
{
  var listbox = document.getpaidform.creditcard;
  var card = GetCookie("cardName");
  var selectedOne = 0;

  for (var i=0; i < listbox.options.length; i++){
    if (listbox.options[i].value == card) {
      listbox.options[i].selected = true;
      selectedOne = 1;
    } else {
      listbox.options[i].selected = false;
    }
  }

  if (selectedOne == 0) {
    listbox.options[0].selected = true;
  }

}

function showApprovalReport(data)
{
  var tableStr = "";

  var pos = data.indexOf(";");
  while (pos != -1) {
    var oneRecord = data.substring(0, pos);
    data = data.substring(pos + 1);

    if (oneRecord.length > 0 && oneRecord.indexOf(",") != -1) {
      var applyDate, confirmedDate, card, commission;
      pos = oneRecord.indexOf(",");
      if (pos != -1) {
        applyDate = oneRecord.substring(0, pos);
      }

      oneRecord = oneRecord.substring(pos + 1);
      pos = oneRecord.indexOf(",");
      if (pos != -1) {
        confirmedDate = oneRecord.substring(0, pos);
      }

      oneRecord = oneRecord.substring(pos + 1);
      pos = oneRecord.indexOf(",");
      if (pos != -1) {
        card = oneRecord.substring(0, pos);
      }

      commission = oneRecord.substring(pos + 1);

      tableStr = tableStr + "<tr class=\"LabelRow\" align=\"center\"><td>" + applyDate + "</td><td>" + confirmedDate + "</td><td>" + card + "</td><td>" + commission + "</td></tr>";
    }
    pos = data.indexOf(";");
  }  

  var href = "#";
  var newWindow;

  var width = 560;
  var height = 200;

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no';

  var random_num = Math.floor(Math.random() * 1000);
  newWindow = window.open(href, 'report' + random_num, windowProperties);
  
  newWindow.focus();

  var content = "<html><head><title>CardBenefit - Report of Application Approvals</title><script type=\"text/javascript\" language=\"JavaScript1.2\">  window.focus();</script><link rel=\"stylesheet\" href=\"/css/main.css\" type=\"text/css\"></head>";
  content = content + "<body leftmargin=\"0\" topmargin=\"2\" marginheight=\"0\" marginwidth=\"0\" bgcolor=\"#ffffff\" text=\"#000000\"><br><br><center>";
  content = content + "  <table><tr><td width=12>&nbsp;</td><td><table cellSpacing=0 cellPadding=1 border=1 bordercolorlight=\"#6699CC\" bordercolordark=\"#E1E1E1\" class=\"LabelHeader\">    <tr class=\"LabelHeader\" align=center bgcolor=\"#eeeeee\"><th>Application Date</th><th>Approval Confirmed</th><th>Credit Card</th><th>Commission</th></tr>";
  content = content + tableStr;
  content = content + "  </table></td><td width=12>&nbsp;</td></tr></table></center></body></html>";

  newWindow.document.write(content);

  return false;
}

function showReferralReport(data)
{
  var tableStr = "";

  var pos = data.indexOf(";");
  while (pos != -1) {
    var oneRecord = data.substring(0, pos);
    data = data.substring(pos + 1);

    if (oneRecord.length > 0 && oneRecord.indexOf(",") != -1) {
      var email, card, applyDate, confirmedDate;
      pos = oneRecord.indexOf(",");
      if (pos != -1) {
        email = oneRecord.substring(0, pos);
      }

      oneRecord = oneRecord.substring(pos + 1);
      pos = oneRecord.indexOf(",");
      if (pos != -1) {
        card = oneRecord.substring(0, pos);
      }

      oneRecord = oneRecord.substring(pos + 1);
      pos = oneRecord.indexOf(",");
      if (pos != -1) {
        applyDate = oneRecord.substring(0, pos);
      }

      confirmedDate = oneRecord.substring(pos + 1);

      tableStr = tableStr + "<tr class=\"LabelRow\" align=\"center\"><td>" + email + "</td><td>" + card + "</td><td>" + applyDate + "</td><td>" + confirmedDate + "</td></tr>";
    }
    pos = data.indexOf(";");
  }  

  var href = "#";
  var newWindow;

  var width = 560;
  var height = 200;

  var winleft = (window.screen.width - width) / 2;
  var wintop = 0;
  if (window.screen.height > (height + 140)) {
    wintop = (window.screen.height - height - 140) / 2;
  }

  var windowProperties = 'width=' + width + ',height=' + height + ',top=' + wintop + ',left=' + winleft + ',directories=no,location=no,menubar=no,personalbar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no';

  var random_num = Math.floor(Math.random() * 1000);
  newWindow = window.open(href, 'report' + random_num, windowProperties);
  
  newWindow.focus();

  var content = "<html><head><title>CardBenefit - Report of Referrals</title><script type=\"text/javascript\" language=\"JavaScript1.2\">  window.focus();</script><link rel=\"stylesheet\" href=\"/css/main.css\" type=\"text/css\"></head>";
  content = content + "<body leftmargin=\"0\" topmargin=\"2\" marginheight=\"0\" marginwidth=\"0\" bgcolor=\"#ffffff\" text=\"#000000\"><br><br><center>";
  content = content + "  <table><tr><td width=12>&nbsp;</td><td><table cellSpacing=0 cellPadding=1 border=1 bordercolorlight=\"#6699CC\" bordercolordark=\"#E1E1E1\" class=\"LabelHeader\">    <tr class=\"LabelHeader\" align=center bgcolor=\"#eeeeee\"><th>Applicant whom you referred</th><th>Credit Card</th><th>Application Date</th><th>Approval Confirmed</th></tr>";
  content = content + tableStr;
  content = content + "  </table></td><td width=12>&nbsp;</td></tr></table></center></body></html>";

  newWindow.document.write(content);

  return false;
}

