﻿var redirectURL = "";
var redirectSection = "";
var redirectionExpiration = "";
var redirectionDesc = "";
var redirectDuration = "";
var todayDate=new Date();

window.load = handleRedirect();

function handleRedirect() {

    var strIDValue = getQuerystring("redirectid", "0");
    //strIDValue Validation,
    //if it is not numric, set to ZERO
    //If strIDValue is greater than arrRedirection length, set to ZERO

    for (var intCount = 0; intCount < arrRedirection.length; intCount++) {
        if (arrRedirection[intCount]['RedirectId'] == strIDValue) {
            redirectURL = arrRedirection[intCount]['TargetUrl'];
            redirectSection = arrRedirection[intCount]['Section'];
            redirectionExpiration = arrRedirection[intCount]['Expiration'];
            redirectionDesc = arrRedirection[intCount]['Description'];
            redirectDuration = arrRedirection[intCount]['Duration'];
        }
    }
    //Build the contents......
    DrawthePage(strIDValue, redirectionExpiration, redirectionDesc);

    

    if (parseInt(redirectDuration)) {
        if (document.getElementById("MSOLayout_InDesignMode").value != '1') {
            setTimeout(function() { redirectTo(strIDValue, redirectionExpiration, redirectURL) }, redirectDuration * 1000); //Redirect after "Duration" Seconds
        }
    }
    else {
        if (document.getElementById("MSOLayout_InDesignMode").value != '1') {
            setTimeout(function() { redirectTo(strIDValue, redirectionExpiration, redirectURL) }, 5 * 1000); //Redirect after 5 Seconds
        }
    }
}

function redirectTo(idValue, expDate, url) {
    if (typeof expDate != 'undefined') {
        if (expDate != '') {

            var dateParts = expDate.split("/");

            var expirationDate = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
            if (todayDate < expirationDate) {
                if (idValue != 0) {
                    window.location = url;
                }
            }
        }
    }
}

    function DrawthePage(idValue, expDate, Msg)  //Use this function to assign the value to the DIV controls
    {
        if (typeof expDate != 'undefined') {
            if (expDate != '') {
                var dateParts = expDate.split("/");

                var expirationDate = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
                if (todayDate < expirationDate) {
                    if (idValue != 0) {
                        document.getElementById("redirectMessage").innerHTML = Msg;

                        document.getElementById("redirectIssueNote").innerHTML = "If you have any issues in redirection, please click <a href=" + redirectURL + ">here</a>.";
                    if (idValue==18) {
                        document.getElementById("redirectMessage").innerHTML = Msg;

                        document.getElementById("redirectIssueNote").innerHTML = "If you have any issues in redirection, please <a href=" + redirectURL + ">click here</a>.";
                                     }


                        if (parseInt(redirectDuration)) {
                            document.getElementById("redirectDurationNote").innerHTML = "You will be automatically redirected in " + redirectDuration + " seconds.";
                        }
                        else {
                            document.getElementById("redirectDurationNote").innerHTML = "You will be automatically redirected in 5 seconds.";
                        }
                    }
                    else {
                        var url = "http://honeywell.com/";
                        Msg = "The Page you requested does not exist. Please update your bookmark, <a href=" + url + ">Click here</a>" + " to go honeywell.com";
                        document.getElementById("redirectMessage").innerHTML = Msg;
                        document.getElementById("redirectDefaultNote").style.display = "none";
                    }
                }
                else {
                    var url = "http://honeywell.com/";
                    Msg = "The Page you requested does not exist. Please update your bookmark, <a href=" + url + ">Click here</a>" + " to go honeywell.com";
                    document.getElementById("redirectMessage").innerHTML = Msg;
                    document.getElementById("redirectDefaultNote").style.display = "none";
                }
            }
        }
    }




function getQuerystring(key, default_) 
{ 
 if (default_==null) 
 { 
    default_="0"; 
 } 
 var search = unescape(location.search); 
 if (search == "") 
 { 
    return default_; 
 } 
 search = search.substr(1); 
 var params = search.split("&"); 
 for (var i = 0; i < params.length; i++) 
 { 
    var pairs = params[i].split("=");
    if (pairs[0].toLowerCase() == key.toLowerCase()) 
    { 
        if(pairs[1]>0 && pairs[1]<=arrRedirection.length)
       {
        return pairs[1];        
        }
    } 
 } 
 return default_; 
}
