﻿String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}


var marqueeColor = 0;
var titleColor = 0;
var zipColor = 0;
var myColors = ["White", "#ffdead", "#a4302c", "#722f2c", "#56290c", "#FFCC33", "Yellow", "Teal"];

$(function() { initjs(); });

function initjs() {

    selectCC();

    $('#testmimonials').scrollFollow({ speed: 700, offset: 40, killSwitch: 'exampleLink',
        onText: 'Disable Follow', offText: 'Enable Follow'
    });

    $('table#weightAndMoversTable td').not('.weightAndMoversNote').hover(
        function() { $(this).parent().addClass('highlightRow') },
        function() { $(this).parent().removeClass('highlightRow') }
    );
    $('table#weightAndMoversTable td').not('.weightAndMoversNote').click(function() {
        $('.prebookTableTopSpace select').val($(this).parent().children('td:eq(2)').text());
        $('.prebookTableWeight').val($(this).parent().children('td:eq(1)').text());
        checkForPiano();
    });

    //This code is duplicated because two weight/men tables exist in 'quote-book-from-email' 
    //and the second one has id QweightAndMoversTable
    $('table#QweightAndMoversTable td').not('.weightAndMoversNote').hover(
        function() { $(this).parent().addClass('highlightRow') },
        function() { $(this).parent().removeClass('highlightRow') }
    );
    $('table#QweightAndMoversTable td').not('.weightAndMoversNote').click(function() {
        $('.prebookTableTopSpace select').val($(this).parent().children('td:eq(2)').text());
        $('.prebookTableWeight').val($(this).parent().children('td:eq(1)').text());
        checkForPiano();
    });



    $('.prebookTableTopSpace select').click(checkForPiano);
    $('marquee').click(switchMarqueeColors);
    $('#states h1 big').click(switchTitleColors);
    /*    $('#bookingZipCodeBox').click(switchZipColors); */
    checkForPiano();
    $('.FAQsDiv td:even').css('padding-top', '10px');
    $('.FAQsDiv td:even a').prepend('&bull;&nbsp;');
    $('.FAQsDiv td:odd').css('padding-top', '0px').css('padding-left', '17px');
    $('.Visa').click(ClickVisa);
    $('.MC').click(ClickMC);
    $('.Amex').click(ClickAMEX);
    $('.QMWeightMen').css('cursor', 'pointer');
    $('.QMWeightMen').click(function() {
        $('.weightAndMoversTable').toggleClass('invisible');
    });

    $('#RadioVisa').click(ClickVisa);
    $('#RadioMC').click(ClickMC);
    $('#RadioAmex').click(ClickAMEX);
}

function selectCC() {
    whichCard = $('input:radio[name=ctl00$Main$CardType]:checked').val();
    if (whichCard == 1) ClickVisa();
    else if (whichCard == 2) ClickMC();
    else if (whichCard == 3) ClickAMEX();
}

function ClickVisa() {
    $('#CCVlabel').text('CVV2');
    $('#CCVsubfield').text('back: 3 digits');
    VisaMCmask();
}

function ClickMC() {
    $('#CCVlabel').text('CVC2');
    $('#CCVsubfield').text('back: 3 digits');
    VisaMCmask();
}

function ClickAMEX() {
    $('#CCVlabel').text('CID');
    $('#CCVsubfield').text('front: 4 digits');
    AmexMask();
}

function VisaMCmask() {
    cc = $('.Card2').val();
    if (cc.length > 0) {
        $('.Card1').val(formatVisaMC(cc));
        $('.Card2').val('');
    }
    ccv = $('.CCV2').val();
    if (ccv.length > 0) {
        $('.CCV1').val(ccv.substring(0, 3));
        $('.CCV2').val('');
    }
    $('.CreditCardNumber1').css('display', 'inline');
    $('.CreditCardNumber2').css('display', 'none');
    $('.CCVNumber1').css('display', 'inline');
    $('.CCVNumber2').css('display', 'none');
    $('.Card1').focus();
}

function AmexMask() {
    cc = $('.Card1').val();
    if (cc.length > 0) {
        $('.Card2').val(formatAMEX(cc));
        $('.Card1').val(''); 
    }
    ccv = $('.CCV1').val();
    if (ccv.length > 0) {
        $('.CCV2').val(ccv);
        $('.CCV1').val('');
    }
    $('.CreditCardNumber1').css('display', 'none');
    $('.CreditCardNumber2').css('display', 'inline');
    $('.CCVNumber1').css('display', 'none');
    $('.CCVNumber2').css('display', 'inline');
    $('.Card2').focus();
}

function formatVisaMC(occ) {
    ncc = occ.replace(/ /g, "");
    return (ncc.substring(0, 4) + " "
        + ncc.substring(4, 8) + " "
        + ncc.substring(8, 12) + " "
        + ncc.substring(12)).trim();
}

function formatAMEX(occ) {
    ncc = occ.replace(/ /g, "");
    return (ncc.substring(0, 4) + " "
        + ncc.substring(4, 10) + " "
        + ncc.substring(10, 15)).trim();   
}

function checkForPiano() {
    if ('2' == $('.prebookTableTopSpace select').val()) {
        $('.moversNote').bind('click', function() { $('.prebookTableTopSpace select').val("3"); unbindPiano(); });
        $('.moversNote').bind('mouseover', function() { $(this).addClass('highlightRow').css('cursor', 'pointer'); });
        $('.moversNote').bind('mouseout', function() { $(this).removeClass('highlightRow').css('cursor', 'default'); });
    }
    else unbindPiano();
}

function unbindPiano() {
    $('.moversNote').removeClass('highlightRow').css('cursor', 'default');
    $('.moversNote').unbind('click');
    $('.moversNote').unbind('mouseover');
    $('.moversNote').unbind('mouseout');
}

function switchMarqueeColors() {
    status = myColors[marqueeColor];
    $(this).css('color', myColors[marqueeColor++]);
    if (marqueeColor >= myColors.length) marqueeColor = 0;
}

function switchTitleColors() {
    status = myColors[titleColor];
    $(this).css('color', myColors[titleColor++]);
    if (titleColor >= myColors.length) titleColor = 0;
}

function switchZipColors() {
    status = myColors[zipColor];
    $(this).css('background-color', myColors[zipColor++]);
    if (zipColor >= myColors.length) zipColor = 0;
}

function CreditCardRequired1(oSrc, args) {
    whichCard = $('input:radio[name=ctl00$Main$CardType]:checked').val();
    if (whichCard == 3) {
        args.IsValid = true;
        return;
    }
    card = $('.Card1').val().trim();
    if (card.length > 0) args.IsValid = true;
    else args.IsValid = false;
}

function CreditCardRequired2(oSrc, args) {
    whichCard = $('input:radio[name=ctl00$Main$CardType]:checked').val();
    if (whichCard != 3) {
        args.IsValid = true;
        return;
    }
    card = $('.Card2').val().trim();
    if (card.length > 0) args.IsValid = true;
    else args.IsValid = false;    
}

function CCVRequired1(oScr, args) {
    whichCard = $('input:radio[name=ctl00$Main$CardType]:checked').val();
    if (whichCard == 3) {
        args.IsValid = true;
        return;
    }
    ccv = $('.CCV1').val().trim();
    if (ccv.length > 0) args.IsValid = true;
    else args.IsValid = false;
}

function CCVRequired2(oScr, args) {
    whichCard = $('input:radio[name=ctl00$Main$CardType]:checked').val();
    if (whichCard != 3) {
        args.IsValid = true;
        return;
    }
    ccv = $('.CCV2').val().trim();
    if (ccv.length > 0) args.IsValid = true;
    else args.IsValid = false;
}

function PhoneRequired(oScr, args) {
    if ($('#PhoneValidationTD span#ctl00_Main_PhoneValid1').css('display') == 'inline') args.IsValid = true;
    else if ($('#PhoneValidationTD span#ctl00_Main_PhoneValid2').css('display') == 'inline') args.IsValid = true;
    else if ($('#PhoneValidationTD span#ctl00_Main_PhoneValid3').css('display') == 'inline') args.IsValid = true;
    else if ($('.Phone1').val().trim().length > 0) args.IsValid = true;
    else if ($('.Phone2').val().trim().length > 0) args.IsValid = true;
    else if ($('.Phone3').val().trim().length > 0) args.IsValid = true;
    else args.IsValid = false;
}
