﻿/// <reference path="jquery/jquery.min.js">

if (typeof jQuery == 'undefined') {
    //load jquery
    var jqEle = document.createElement("script");
    jqEle.setAttribute("type", "text/javascript");
    jqEle.setAttribute("src", ResolveUrl("~/Scripts/jquery/jquery.min.js"));

    if (document.body) {
        //add to bottom of the body tag
        document.body.appendChild(jqEle);
    }
    else {
        document.getElementsByTagName("head")[0].appendChild(jqEle);
    }
}

//cross page postback using jQuery
if (typeof crossPagePostBack == 'undefined') {
    function crossPagePostBack(page, formFieldIds) {
        if (page) {
            var child = null;
            var form = document.createElement("form");
            form.setAttribute("action", ResolveUrl(page));
            form.setAttribute("method", "post");
            form.setAttribute("id", new Date().getTime());

            if (!formFieldIds) {
                throw new Error("Missing field ids");
            }

            for (i = 0; i < formFieldIds.length; i++) {
                child = document.createElement("input");
                child.setAttribute("type", "hidden");                

                if (formFieldIds[i].charAt(0) == "*") {
                    //by checkbox or radio
                    key = $('input[name=' + formFieldIds[i].substr(1) + ']');
                    
                    if (key) {
                        for (k = 0; k < key.length; k++) {
                            if ($(key[k]).attr('checked')) {
                                child.setAttribute("value", $(key[k]).val());
                                child.setAttribute("name", $(key[k]).attr('name'));

                                //append to form
                                form.appendChild(child);
                            }
                        }
                    }                    
                }
                else if (formFieldIds[i].charAt(0) == "@") {
                    //by name attr
                    key = $('input[name=' + formFieldIds[i].substr(1) + ']');

                    if (key) {
                        child.setAttribute("value", $(key).val());
                        child.setAttribute("name", $(key).attr('name'));

                        //append to form
                        form.appendChild(child);
                    }
                }
                else {
                    var input = $('#' + formFieldIds[i]);

                    if (input.length > 0) {
                        child.setAttribute("value", $('#' + formFieldIds[i]).val());
                        child.setAttribute("name", $('#' + formFieldIds[i]).attr('name'));

                        //append to form
                        form.appendChild(child);
                    } else {
                        input = $('input[name=' + formFieldIds[i] + ']');

                        child.setAttribute("value", $(input).val());
                        child.setAttribute("name", $(input).attr('name'));

                        //append to form
                        form.appendChild(child);
                    }                    
                }
            }

            //add to document
            document.body.appendChild(form);

            //submit the form
            form.submit();

            return false;
        }
    }
}

function ResolveUrl(page) {
    if (typeof ccfcWebsiteRootPath == 'undefined') {
        window.ccfcWebsiteRootPath = '/';
    }

    if (page) {
        return page.replace(/~\//, ccfcWebsiteRootPath);
    }
}


function SetFocus(elementId) {
    var element = document.getElementById(elementId);

    if (element != null) {
        element.focus();
    }
}


function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

//add header and footer if available
$(function () {
    var alertHeader = $('#alertHeader');
    var alertFooter = $('#alertFooter');

    if (alertHeader.length > 0) {
        //add below body tag
        $(alertHeader).show().prependTo('body');
    }

    if (alertFooter.length > 0) {
        $(alertFooter).show().appendTo('body');
    }

    //add google analytics tracking to pdf links
    $('a').click(function () {
        //add link tracking if pdf
        var ext = ['pdf'],
            link = $(this).attr('href');

        if (link == null || link.length == 0) {
            return;
        }

        for (i = 0; i < ext.length; i++) {            
            if (link.indexOf('.' + ext[i]) > 0) {
                //google track
                try {
                    var gaLink = '/PDF' + link.substr(link.lastIndexOf('/'));
                    //console.log(link + '. Index of: ' + link.indexOf('.' + ext[i]) + '. GA Link: ' + gaLink);
                    
                    _gaq.push(['_trackPageview', gaLink]);
                } catch (err) { }
            }
        }

        return true;
    });
});
