﻿addLoadEvent(makeAllLinksAbsolute);
addLoadEvent(wirePrintLinks);

String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, "");}

//http://www.webreference.com/programming/javascript/onloads/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        };
    }
}
function wirePrintLinks() {
    if (!document.getElementsByTagName) return;
    var Anchors = document.getElementsByTagName('a');
    if (!Anchors || Anchors.length == 0) return;
    var printLinks = new Array();
    for (var i = 0; i < Anchors.length; i++) {
        if (Anchors[i].href && (Anchors[i].href.indexOf('www.jaybuckley.com') < 0 && Anchors[i].href.indexOf('jaybuckley.com') < 0 && Anchors[i].href.indexOf('jaybuckley.sites.vendiadvertising.com') < 0 && Anchors[i].href.indexOf('mailto') != 0 && Anchors[i].href.indexOf('tel') != 0)) {
            printLinks.push(Anchors[i].href);
            Anchors[i].innerHTML += '<sup class="print-link-id">' + printLinks.length + '</sup>';
        }
    }
    if (printLinks.length > 0) {
        var div = document.createElement('div');
        div.id = 'print-links';
        var ul = document.createElement('ol');
        var li;
        for (var i = 0; i < printLinks.length; i++) {
            li = document.createElement('li');
            li.innerHTML = printLinks[i];
            ul.appendChild(li);
        }
        div.appendChild(ul);
        document.getElementsByTagName('body')[0].appendChild(div);
    }
}
function makeAllLinksAbsolute() {
    if (!document.getElementsByTagName) return;
    var Anchors = document.getElementsByTagName('a');
    if (!Anchors || Anchors.length == 0) return;
    for (var i = 0; i < Anchors.length; i++) {
        if ((Anchors[i].className && Anchors[i].className.indexOf('noabs') >= 0) || (Anchors[i].href && Anchors[i].href == '#')) {

        } else {
            Anchors[i].href = relLinkToAbs(Anchors[i].href, document.location.href);
            //alert(Anchors[i].href);
        }
    }
}

//http://www.phpied.com/relative-to-absolute-links-with-javascript/
function relLinkToAbs(link, host) {
    var lparts = link.split('/');
    if (/mailto:|http:|https:|ftp:|#/.test(lparts[0])) {
        // already abs, return
        return link;
    }

    var i, hparts = host.split('/');
    if (hparts.length > 3) {
        hparts.pop(); // strip trailing thingie, either scriptname or blank 
    }

    if (lparts[0] === '') { // like "/here/dude.png"
        host = hparts[0] + '//' + hparts[2];
        hparts = host.split('/'); // re-split host parts from scheme and domain only
        delete lparts[0];
    }

    for (i = 0; i < lparts.length; i++) {
        if (lparts[i] === '..') {
            // remove the previous dir level, if exists
            if (typeof lparts[i - 1] !== 'undefined') {
                delete lparts[i - 1];
            } else if (hparts.length > 3) { // at least leave scheme and domain
                hparts.pop(); // stip one dir off the host for each /../
            }
            delete lparts[i];
        }
        if (lparts[i] === '.') {
            delete lparts[i];
        }
    }

    // remove deleted
    var newlinkparts = [];
    for (i = 0; i < lparts.length; i++) {
        if (typeof lparts[i] !== 'undefined') {
            newlinkparts[newlinkparts.length] = lparts[i];
        }
    }

    return hparts.join('/') + '/' + newlinkparts.join('/');
}

function valueAppForm() {
    if (!checkTextbox('txtFirstName', 'First Name') ||
        !checkTextbox('txtLastName', 'Last Name') ||
        !checkTextbox('txtAddress', 'Address') ||
        !checkTextbox('txtCity', 'City') ||
        !checkTextbox('txtState', 'State') ||
        !checkTextbox('txtZip', 'Zip') ||
        !checkTextbox('txtNumberOfPersons', 'number of persons') ||
        !checkTextbox('txtEmail', 'Email')
        ) {
        return false;
    }
    
    if (!checkMultiTextbox('txtHomePhone', 'txtWorkPhone', 'txtCellPhone')) {
        alert('Please enter at least 1 phone number');
        document.getElementById('txtHomePhone').focus();
        return false;
    }

    if (!checkRadio('rdoLodgingDouble', 'rdoLodgingTriple', 'rdoLodgingQuad', 'rdoLodgingPrivate')) {
        document.getElementById('rdoLodgingDouble').focus();
        alert('Please select a lodging type');
        return false;
    }

    if (!checkTextbox('txtNames', 'Names of party memebers')) {
        return false;
    }
    
    if (!checkMultiSelect('ddlTrips', 'Trip')) {
        return false;
    }
    
    if (!checkRadio('rdoCreditCardVisa', 'rdoCreditCardMastercard', 'rdoCreditCardDiscover', 'rdoCreditCardAmex')) {
        document.getElementById('rdoCreditCardVisa').focus();
        alert('Please select a credit card type');
        return false;
    }

    if (!checkRadio('rdoPaymentDeposit', 'rdoPaymentFull')) {
        document.getElementById('rdoPaymentDeposit').focus();
        alert('Please select a payment type');
        return false;
    }

    if (!checkTextbox('txtCCNumber', 'Credit Card Number') ||
        !checkTextbox('txtCV2', 'Credit Card Security Code')
        ) {
        return false;
    }
    if (!document.getElementById('txtCCNumber').value.replace(/-/g, '').match(/^\d{15,16}$/)) {
        alert('Please enter a valid credit card number');
        return false;
    }
    
    if (!document.getElementById('txtCV2').value.replace(/-/g, '').match(/^\d{3,4}$/)) {
        alert('Please enter a valid credit card security code');
        return false;
    }

    if (!checkRadio('rdoTravelYes', 'rdoTravelNo')) {
        document.getElementById('rdoTravelYes').focus();
        alert('Please select an insurance option');
        return false;
    }
    
    if (!checkTextbox('txtShoeSize', 'words in the last box')) {
        return false;
    }


    return true;
}
function checkMultiSelect(id, fieldName) {
    var ddl = document.getElementById(id);
    if (!ddl) {
        alert('Cannot find form field : ' + ddl);
        return false;
    }
    for (var i = 0; i < ddl.options.length; i++) {
        if (ddl.options[i].selected) return true;
    }
    alert('Please select at least one ' + fieldName);
    return false;
}
function checkRadio(){
    var rdo;
    for (var i = 0; i < arguments.length; i++) {
        rdo = document.getElementById(arguments[i]);
        if (!rdo) {
            alert('Cannot find form field : ' + id);
            return false;
        }
        if (rdo.checked) return true;
    }
    return false;
}
function checkTextbox(id, fieldName) {
    var txt = document.getElementById(id);
    if (!txt) {
        alert('Cannot find form field : ' + id);
        return false;
    }
    if (!txt.value || txt.value.trim().length == 0) {
        txt.focus();
        alert('Please enter your ' + fieldName);
        return false;
    }
    return true;
}
function checkMultiTextbox() {
    var txt;
    for (var i = 0; i < arguments.length; i++) {
        txt = document.getElementById(arguments[i]);
        if (!txt) {
            alert('Cannot find form field : ' + id);
            return false;
        }
        if (txt.value && txt.value.trim().length > 0) {
            return true;
        }
    }
    return false;
}

