﻿function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function checkCookie() {

    username = getCookie('username');
    if (username != null && username != "") {
        // alert('Welcome again ' + username + '!');
        return true;
    }
    else {
        redirect("index.html");
    }
}

// Note: Like all Javascript password scripts, this is hopelessly insecure as the user can see 
//the valid usernames/passwords and the redirect url simply with View Source.  
// And the user can obtain another three tries simply by refreshing the page.  
//So do not use for anything serious!

var count = 2;
function validate() {
    var un = document.myform.username.value;
    var pw = document.myform.pword.value;
    var valid = false;

    var unArray = ["Bimal", "Lehar", "Lutfi"];  // as many as you like - no comma after final entry
    var pwArray = ["Bimal", "Lehar", "Lutfi"];  // the corresponding passwords;

    for (var i = 0; i < unArray.length; i++) {
        if ((un == unArray[i]) && (pw == pwArray[i])) {
            valid = true;
            break;
        }
    }

    if (valid) {
        setCookie('username', document.myform.username.value, 1)
        redirect("Index.html");
        return true;
    }
    
    var t = " tries";
    if (count == 1) { t = " try" }

    if (count >= 1) {
        alert("Invalid username and/or password.  You have " + count + t + " left.");
        document.myform.username.value = "";
        document.myform.pword.value = "";
        setTimeout("document.myform.username.focus()", 25);
        setTimeout("document.myform.username.select()", 25);
        count--;
    }

    else {
        alert("Still incorrect! You have no more tries left!");
        document.myform.username.value = "No more tries allowed!";
        document.myform.pword.value = "";
        document.myform.username.disabled = true;
        document.myform.pword.disabled = true;
        return false;
    }

}
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());

}

function CheckReferer(sRedirectPath) {
    var sPath = document.referrer;
    var sCurrentPath = "http://localhost:27958/YLEFINALWS/";
    /*var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);*/
    var vServicesPath = sCurrentPath + "services/";
    var vAboutUsPath = sCurrentPath + "aboutus/";
    var vOthersPath = sCurrentPath + "others/";
    var RefArray = [vAboutUsPath + "about_us.html", vAboutUsPath + "leadership.html",
                    vAboutUsPath + "management.html", vAboutUsPath + "mission.html", vAboutUsPath + "philosophy.html", vAboutUsPath + "Sitemap.html", 
                    vServicesPath + "asis.html", vServicesPath + "bp.html", vServicesPath + "capability_matrix.html", vServicesPath + "delivery_approach.html", vServicesPath + "dims.html",
                    vServicesPath + "engagement_models.html", vServicesPath + "services.html", vServicesPath + "set.html", vServicesPath + "uis.html",
                    sCurrentPath + "index.html", vOthersPath + "contactus.aspx", vOthersPath + "locations.html", sCurrentPath + "Index.html",
                    vOthersPath + "yles_advantage.html", vOthersPath + "yles_copyrights.html", vOthersPath + "yles_disclaimer.html", vOthersPath + "yles_privacypolicy.html", vOthersPath + "yles_terms_and_coditions.html",
                    sCurrentPath + "VisitorLogin.aspx" ];  // as many as you like - no comma after final entry
     var bool = false;
    for (var i = 0; i < RefArray.length; i++) {
        // need to use a double equals sign "==" to test for equality
        //alert((sCurrentPath + RefArray[i]));
        if ((RefArray[i].toUpperCase()) == sPath.toUpperCase())
         {
            bool = true;
            break;
           // return true;    // following code is unnecessary
        }
    }
    if (i == RefArray.length && bool == false) { redirect(sRedirectPath); }
}
 function redirect(url) { 
                var referLink = document.createElement('a'); 
                referLink.href = url; 
                document.body.appendChild(referLink); 
                referLink.click(); 
       
}
function GetFrom(sRedirectPath) {
    //alert(document.referrer);
    if (document.referrer == '') {//location.href = "SignIn.html";
        redirect(sRedirectPath);
    }
    else {
        /*if (checkCookie()) { CheckReferer();}*/
        CheckReferer(sRedirectPath);
    }
}


