//Check www

if (window.location.href.indexOf("constructseer.com") != -1) //only for prod
{
    if (window.location.href.indexOf("https://") == -1)
    {
        if (window.location.href.indexOf("www") == -1)
        {
           window.location = window.location.href.replace("http://", "https://www.");
        }
        else
        {
           window.location = window.location.href.replace("http://", "https://");
        }
    }
    else
        if (window.location.href.indexOf("www") == -1)
        {
           window.location = window.location.href.replace("https://", "https://www.");
        }
}

//Move owner list when page is scroll
/*
$(window).scroll(function () {
    var pageY = window.pageYOffset + 40;
    if ((/MSIE (\d+\.\d+);/.test(navigator.userAgent))) {
        pageY = document.documentElement.scrollTop + 40;
    }
    $(".enrollNowSection").css("position", "absolute");
    $(".enrollNowSection").stop(true, true).delay(250).animate({ "top": pageY + "px" }, { "easing": "linear", "duration": 400 });
});
*/

//DOCUMENT READY
$(document).ready(function () {

    $(".enrollNowSection").stop(true, true).delay(100).animate({ "top": 40 + "px" }, { "easing": "linear", "duration": 1000 });

    //focus to the login text field
    $("#LogInUserName").focus();
    /*
    $('#LogInUserName').keypress(function (e) {
        if (e.which == 13) {
            TryLogin(); // enter
            return false;
        }
    });
    $('#LogInUserPass').keypress(function (e) {
        if (e.which == 13) {
            TryLogin(); // enter
            return false;
        }
    });
    */
    $(document).keyup(function (e) {
        if (e.keyCode == 27) { ClosePopups(); }   // esc
    });

    $('#EmailAddress').keypress(function (e) {
        if (e.which == 13) {
            SendUserName(); // enter
            return false;
        }
    });



    /*Contact US*/

    $('#firstName').keypress(function (e) {
        if (e.which == 13) {
            SendMessage(); // enter
            return false;
        }
    });
    $('#lastName').keypress(function (e) {
        if (e.which == 13) {
            SendMessage(); // enter
            return false;
        }
    });
    $('#email').keypress(function (e) {
        if (e.which == 13) {
            SendMessage(); // enter
            return false;
        }
    });

    $('#mobile').keypress(function (e) {
        if (e.which == 13) {
            SendMessage(); // enter
            return false;
        }
    });

    $('#company').keypress(function (e) {
        if (e.which == 13) {
            SendMessage(); // enter
            return false;
        }
    });

});

function Enroll() {
    var domainName = $($('input[name=ownerList]:checked')[0]).val();
    var contractorTypeID = $($('input[name=ownerList]:checked')[0]).attr('contractorTypeID');
    if (domainName) {
        window.location = rc_hostUrl + "/" + domainName + '/Enroll?contractorTypeID=' + contractorTypeID;
    }
    else {
        CSMessageBox('Please select owner');
    }
}

function TryLogin() {
    
    var userName = $('#LogInUserName').val();
    var password = $('#LogInUserPass').val();
    $.ajax({
        url: rc_hostUrl + "/Login",
        global: false,
        type: "POST",
        data: ({ userName: userName, password: password }),
        dataType: "json",
        async: true,
        success: function (data) {

            //If result > 0
            if (data.success == "true") {
                window.location = data.redirectUrl;
            }
            else {
                CSMessageBox(data.error, 'ok', '$("#LogInUserName").focus();');
            }
        }
    });
}

function ForgetUserName() {
    TogglePopup('#ForgetUserNamePopup', true, false);
    $('#EmailAddress').val('');
    $('#EmailAddress').focus();
}

function SendUserName() {
    $.ajax({
        url: rc_hostUrl + "/ForgetUserName",
        global: false,
        type: "POST",
        data: ({ email: $('#EmailAddress').val() }),
        dataType: "json",
        async: true,
        success: function (data) {
            if (data.success == "true") {
                ClosePopups();
                CSMessageBox(rc_email_was_sent, 'ok', '$("#UserName").focus();');
            }
            else {
                CSMessageBox(data.error, 'ok', '$("#EmailAddress").focus();');
            }
        }
    });
}

//load fucntions
//AuthenticationCheck();

ie4 = ((navigator.appVersion.indexOf("MSIE")>0) && (parseInt(navigator.appVersion) >= 4));

var count = 0, add = 2, timerID;
var countF = 0, addF = 0.01;

var count2 = 0, add2 = 2, timerID2;
var countF2 = 0, addF2 = 0.01;

var indexTestim = 0;

function fadeInOut(obj, arrayText, obj, index, delayMain)
{
    var currObj = document.getElementById(obj);
    if(currObj != null)
    {
        if (ie4)
        {
            count += add;
            var delay = 1;
            if(count >= 0 && count <= 100)
            {
                currObj.style.filter = "alpha(opacity="+count+")";
                timerID = setTimeout(function() { fadeInOut(currObj.id, arrayText, currObj.id, index, delayMain); }, delay);
            }
            if(count >= 100)
            {
                count = 100;
                add = -2;
                clearTimeout(timerID);
                timerID = setTimeout(function() { fadeInOut(currObj.id, arrayText, currObj.id, index, delayMain); }, delayMain-100);
            }
            if(count <= 0)
            {
                clearTimeout(timerID);
                timerID = setTimeout(function() { rotateText(arrayText, currObj.id, index, delayMain); }, 100);
            }
        }
        else
        {
            var delay = 1;
            countF += addF;
            if(countF >= 0 && countF <= 1)
            {
                currObj.style.opacity = countF;
                timerID = setTimeout(function() { fadeInOut(currObj.id, arrayText, currObj.id, index, delayMain); }, delay);
            }
            if(countF >= 1)
            {
                countF = 1;
                addF = -0.01;
                clearTimeout(timerID);
                timerID = setTimeout(function() { fadeInOut(currObj.id, arrayText, currObj.id, index, delayMain); }, delayMain-100);
            }
            if(countF <= 0)
            {
                clearTimeout(timerID);
                timerID = setTimeout(function() { rotateText(arrayText, currObj.id, index, delayMain); }, 100);
            }
        }
    }
}

function fadeInOut2(obj2, arrayText2, obj2, index2, delayMain2)
{
    var currObj2 = document.getElementById(obj2);
    if(currObj2 != null)
    {
        if (ie4)
        {
            count2 += add2;
            var delay2 = 4;
            if(count2 >= 0 && count2 <= 100)
            {
                currObj2.style.filter = "alpha(opacity="+count2+")";
                timerID2 = setTimeout(function() { fadeInOut2(currObj2.id, arrayText2, currObj2.id, index2, delayMain2); }, delay2);
            }
            if(count2 >= 100)
            {
                count2 = 100;
                add2 = -2;
                clearTimeout(timerID2);
                timerID2 = setTimeout(function() { fadeInOut2(currObj2.id, arrayText2, currObj2.id, index2, delayMain2); }, delayMain2-100);
            }
            if(count2 <= 0)
            {
                clearTimeout(timerID2);
                timerID2 = setTimeout(function() { rotateText2(arrayText2, currObj2.id, index2, delayMain2); }, 100);
            }
        }
        else
        {
            var delay2 = 3;
            countF2 += addF2;
            if(countF2 >= 0 && countF2 <= 1)
            {
                currObj2.style.opacity = countF2;
                timerID2 = setTimeout(function() { fadeInOut2(currObj2.id, arrayText2, currObj2.id, index2, delayMain2); }, delay2);
            }
            if(countF2 >= 1)
            {
                countF2 = 1;
                addF2 = -0.01;
                clearTimeout(timerID2);
                timerID2 = setTimeout(function() { fadeInOut2(currObj2.id, arrayText2, currObj2.id, index2, delayMain2); }, delayMain2-100);
            }
            if(countF2 <= 0)
            {
                clearTimeout(timerID2);
                timerID2 = setTimeout(function() { rotateText2(arrayText2, currObj2.id, index2, delayMain2); }, 100);
            }
        }
    }
}

function rotateText(arrayText, obj, index, delayMain)
{
    var currObj = document.getElementById(obj);
    if(arrayText != null && currObj != null && index != null)
    {
        currObj.innerHTML = arrayText[index];
        if(index < arrayText.length-1)
        {
            index++;
        }
        else
        {
            index = 0;
        }
        countF = 0;
        addF = 0.01;
        count = 0;
        add = 2;
        fadeInOut(currObj.id, arrayText, currObj.id, index, delayMain);
    }
}

function rotateText2(arrayText2, obj2, index2, delayMain2)
{
    var currObj2 = document.getElementById(obj2);
    if(arrayText2 != null && currObj2 != null && index2 != null)
    {
        currObj2.innerHTML = arrayText2[index2];
        if(index2 < arrayText2.length-1)
        {
            index2++;
        }
        else
        {
            index2 = 0;
        }
        indexTestim = index2;
        countF2 = 0;
        addF2 = 0.01;
        count2 = 0;
        add2 = 2;
        fadeInOut2(currObj2.id, arrayText2, currObj2.id, index2, delayMain2);
    }
}

function nextTestim()
{
    clearTimeout(timerID2);
    rotateText2(arrayTestimonialsText, 'testimonials', indexTestim, 1);
}

function setZeroOpacity(obj)
{
    var currObj = document.getElementById(obj);
    if(currObj != null)
    {
        currObj.style.opacity = 0;
        currObj.style.filter = 'alpha(opacity=0)';
        currObj.style.display = 'block';
    }
}

function showSelMenu(showFlag, obj)
{
    var currObj = document.getElementById(obj);
    if(showFlag && currObj != null)
    {
        currObj.className = "headerMenuItemSelected";
    }
    else
    {
        if(!showFlag && currObj != null)
        {
            currObj.className = "headerMenuItem";
        }
    }
}

var arrayRotateText = new Array(3);
arrayRotateText[0] = "Designed at Harvard, now the Construction Industry Standard for Safety Assessment";
arrayRotateText[1] = "Improve Injury Performance and OCIP Costs on Your Construction Projects";
arrayRotateText[2] = "Reduce the Amount of Time to do Safety Prequalification of Contractors";

var arrayTestimonialsText = new Array(4);
arrayTestimonialsText[0] = "- \"Just a note of congratulations on an awesome on-line qualification process... never seen anything like this... and I do quite a few, but since it is Harvard, I am not surprised!\"";
arrayTestimonialsText[0] += "<br>Cheryl Hunsberger, Flagship PDG";
arrayTestimonialsText[1] = "- \"The Harvard CSAP has the ability to help those involved move in a positive direction with their safety programs. After submitting our information, we discovered that our soft tissue injury prevention program needed improvement so we took the steps to make that part of our program better.\"";
arrayTestimonialsText[1] += "<br>Matthew Schroyer, ";
arrayTestimonialsText[1] += "<a href=\"http://www.bovislendlease.com/\" class=\"testimonialsLinkBt\">Bovis Lend Lease<&/a>";
arrayTestimonialsText[1] = arrayTestimonialsText[1].replace('&/','/');
arrayTestimonialsText[1] += "  Office H&S Director";
arrayTestimonialsText[2] = "- \"Our BuildSafe partners, Skanska USA Building, Gilbane Building Company, and Limbach Company LLC, all of whom have embraced IIF/IFE, were the top three among companies achieving the PLATINUM designation \"by demonstrating outstanding performance in the Contractors Safety Assessment Program administered by ConstructSecure, Inc.\"";
arrayTestimonialsText[2] += "<br>Mary Vogel, ";
arrayTestimonialsText[2] += "<a href=\"http://builtbest.org/buildsafe-0\" class=\"testimonialsLinkBt\">Build Safe Boston<&/a>";
arrayTestimonialsText[2] = arrayTestimonialsText[2].replace('&/','/');
arrayTestimonialsText[3] = "<b>Garrett Burke Recognized for Safety Achievements</b> - During his tenure at Harvard, Garrett's progressive approach to construction and occupational safety has resulted in major improvements and advancements in these disciplines";

function changeText(obj, objValue)
{
    var currObj = document.getElementById(obj);
    if(objValue != null && currObj != null)
    {
        currObj.innerHTML = objValue;
    }
}

function generalContractorsClick(flag)
{
    var genCon1 = document.getElementById('genContr1');
    var genCon2 = document.getElementById('genContr2');
    if(genCon1 != null && genCon2 != null)
    {
        if(flag)
        {
            //changeText('mainUpTitle', 'Advantages of CSAP for General Contractors and Owners');
            genCon1.style.display = "none";
            genCon2.style.display = "";
        }
        else
        {
            //changeText('mainUpTitle', 'Why Use CSAP?');
            genCon1.style.display = "";
            genCon2.style.display = "none";
        }
    }
}

function independentContractorsClick()
{
    changeText('mainUpTitle', 'Advantages of CSAP Contractors');
    changeText('independentContrSect','<div class=indContentWidth><&div>Coming soon'.replace('&','/'));
}

function loadFunctions()
{
    setZeroOpacity('centerUpText');
    setZeroOpacity('testimonials');
    rotateText(arrayRotateText, 'centerUpText', 0, 10000);
    rotateText2(arrayTestimonialsText, 'testimonials', 0, 15000);
   
}

function showCertificate()
{
    win = window.open("", "", "width=1024, height=856, top="+((screen.height - 856)/2)-50+", left="+(screen.width  - 1024)/2+", scrollbars=yes, resizable=yes");
    win.document.writeln("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    win.document.writeln("<html>");
    win.document.writeln("<head>");
    win.document.writeln("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
    win.document.writeln("</head>");
    win.document.writeln("<body>");
    win.document.writeln("<a href=\"javascript:window.close();\">");
    win.document.writeln("<center>");
    win.document.writeln("<p>");
    win.document.writeln("<img id=\"LargeCertificate\" src=\"" + rc_hostUrl + "/Content/Images/Home/certificate.jpg\" border=\"0\">");
    win.document.writeln("</p>");
    win.document.writeln("<p>");
    win.document.writeln("<b>Close</b>");
    win.document.writeln("</p>");
    win.document.writeln("</center>");
    win.document.writeln("</a>");
    win.document.writeln("</body>");
    win.document.writeln("</html>");
}

function GetQueryParam(parameter){
    var p = escape(unescape(parameter));
    var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
    var match = regex.exec(window.location.search);
    var value = null;
    if( match != null ){
    value = match[1];
    }
    return value;
}


function SendMessage()
{
    //Validate
    var valid = true;
    $('#firstName').removeClass('input-validation-error');
    $('#lastName').removeClass('input-validation-error');
    $('#email').removeClass('input-validation-error');
    $('#mobile').removeClass('input-validation-error');
    $('#comments').removeClass('input-validation-error');
    $('#company').removeClass('input-validation-error');

    if ($.trim($('#firstName').val()) == "") {
        $('#firstName').addClass('input-validation-error');
        valid = false;
    }
    if ($.trim($('#lastName').val()) == "") {
        $('#lastName').addClass('input-validation-error');
        valid = false;
    }
    if ($.trim($('#email').val()) == "") {
        $('#email').addClass('input-validation-error');
        valid = false;
    }

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = $.trim($('#email').val());
   if (reg.test(address) == false) {
       $('#email').addClass('input-validation-error');
       valid = false;
   }

    if ($.trim($('#mobile').val()) == "") {
        $('#mobile').addClass('input-validation-error');
        valid = false;
    }
    if ($.trim($('#mobile').val()) == "") {
        $('#mobile').addClass('input-validation-error');
        valid = false;
    }
    if ($.trim($('#comments').val()) == "") {
        $('#comments').addClass('input-validation-error');
        valid = false;
    }
    if ($.trim($('#company').val()) == "") {
        $('#company').addClass('input-validation-error');
        valid = false;
    }

    if (valid) {
        $.ajax({
            url: rc_hostUrl + "/ContactUS",
            global: false,
            type: "POST",
            data: ({ firstName: $('#firstName').val(), lastName: $('#lastName').val(), email: $('#email').val(), mobile: $('#mobile').val(), companyName: $('#company').val(),
                subject: $('#subject').val(), body: $('#comments').val()
            }),
            dataType: "json",
            async: true,
            success: function (data) {

                //If result > 0
                if (data.success == "true") {
                    CSMessageBox('Email was succesfully sent');
                }
                else {
                    CSMessageBox(data.error, 'ok', '$("#LogInUserName").focus();');
                }
            }
        });
    }
    else {
        CSMessageBox(rc_please_enter_fields_marked_with_red);
    }
}
