//Check www
if (window.location.href.indexOf("www.") == -1)
{
    if (window.location.href.indexOf("https://") == -1)
    {
        window.location = window.location.href.replace("http://", "https://www.");
    }
    else
    {
        window.location = window.location.href.replace("https://", "https://www.");
    }
}

//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);
    
    //Login check
    LoginCheck();
   
}

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 src=\"images/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 redirectEnroll()
{
    var ownerID = -1;
    if (document.getElementById("ownerListItem1").checked)
    {
        ownerID = 1;
    }
    if (document.getElementById("ownerListItem2").checked)
    {
        ownerID = 2;
    }
    if (document.getElementById("ownerListItem3").checked)
    {
        ownerID = 3;
    }
    if (document.getElementById("ownerListItem4").checked)
    {
        ownerID = 2451;
    }
    if (document.getElementById("ownerListItem5").checked)
    {
        ownerID = 2541;
    }
    if (document.getElementById("ownerListItem6").checked)
    {
        ownerID = 2462;
    }
    
    if (document.getElementById("ownerListItem7").checked)
    {
        ownerID = 2684;
    }
    
    if (document.getElementById("ownerListItem8").checked)
    {
        ownerID = 2782;
    }
    
    if (document.getElementById("ownerListItem9").checked)
    {
        ownerID = 2915;
    }
    
    if (document.getElementById("ownerListItem10").checked)
    {
        ownerID = 2988;
    }
    
    
    if (ownerID == -1)
    {
        alert("Please choose a Primary Owner");
    }else
    {    
        window.location = '../../home?ownerID=' + ownerID;
    }
}

function ForgetPassword()
{
    var emailAddress = window.prompt("Please enter your email address", "");
    
    if (emailAddress)
    {
    
        var http = false;

        if (navigator.appName == "Microsoft Internet Explorer") {

            http = new ActiveXObject("Microsoft.XMLHTTP");

        } else {

            http = new XMLHttpRequest();

        }

        var today = new Date();

        http.open("GET", "Pages/ForgetPassword.ashx?forgetPassword=" + emailAddress + "&nochache=" + today, true);

        http.onreadystatechange = function() {

            if (http.readyState == 4) {

                if (http.status == 200) {

                    try {

                        var response = http.responseText;
                        alert(response);

                    }
                    catch (Error) {

                    }


                }
            }
        }

        http.send(null);
    }
   
}

function AuthenticationCheck()
{
     var http = false;

    if (navigator.appName == "Microsoft Internet Explorer") {

        http = new ActiveXObject("Microsoft.XMLHTTP");

    } else {

        http = new XMLHttpRequest();

    }

    var today = new Date();

    http.open("GET", "Pages/AuthenticationCheck.ashx?nochache=" + today, true);

    http.onreadystatechange = function() {

        if (http.readyState == 4) {

            if (http.status == 200) {

                try {

                    var response = http.responseText;
                    if (response.length > 0)
                    {
                        window.location = response;
                    }

                }
                catch (Error) {

                }


            }
        }
    }

    http.send(null);
}

function LoginCheck()
{
    // get the current URL
     try {
        if (GetQueryParam("login").toString() == "incorrect");
        {
            document.getElementById("lblIncorrectLogin").style.display = "block";
        }
    }
     catch (Error) {}
     
}

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()
{
    var http = false;

    if (navigator.appName == "Microsoft Internet Explorer") {

        http = new ActiveXObject("Microsoft.XMLHTTP");

    } else {

        http = new XMLHttpRequest();

    }

    var today = new Date();
    
    var prevBreakIndex = 0;
    var strBody = document.getElementById("comments").value;
    while(prevBreakIndex != -1)
    {
        prevBreakIndex = strBody.indexOf("\n", prevBreakIndex);
        strBody = strBody.replace("\n", "<br />")
    }
    
    
    var sendValues = "&firstName=" + document.getElementById("firstName").value;
    sendValues += "&lastName=" + document.getElementById("lastName").value;
    sendValues += "&email=" + document.getElementById("email").value;
    sendValues += "&mobile=" + document.getElementById("mobile").value;
    sendValues += "&comments=" + strBody;
    sendValues += "&company=" + document.getElementById("company").value;
    sendValues += "&subject=" + document.getElementById("subject").value;
    sendValues += "&mailto=" + document.getElementById("mailto").value;
    sendValues += "&mailfrom=" + document.getElementById("mailfrom").value;
    
    http.open("GET", "Pages/SendMail.aspx?nochache=" + today + sendValues, true);
    
    
//    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //http.setRequestHeader("Content-length", sendValues.length);

    //http.setRequestHeader("Connection", "close");

    //http.send(sendValues);

    http.onreadystatechange = function() {

        if (http.readyState == 4) {

            if (http.status == 200) {

                try {

                    alert("Email was successfully sent");

                }
                catch (Error) {

                }


            }
        }
    }
    
    http.send(null);
}