﻿$(document).ready(function() {
   init();
});





function init(){

    // Set focus
    $("#UI_UserName").focus();


    DoBrowserCheck();
    
  // check for cookie with username
   AutoFillForm();


    // Set_Cookie (name,               value,                          expires, path, domain, secure) {

   var domain = '';


    $('#UI_UserName').blur(function() {
    Set_Cookie('Icodis_Username', $('#UI_UserName').val().toString(), 3650, '/', domain, '')
    });
    
    $('#UI_UserName').keypress(function() {
    Set_Cookie('Icodis_Username', $('#UI_UserName').val().toString(), 3650, '/', domain, '')
    });
    
    
    $('#UI_Password').keypress(function() {
    Set_Cookie('Icodis_Password', $('#UI_Password').val().toString(), 3650, '/', domain, '')
    });
    
    $('#UI_Password').blur(function() {
    Set_Cookie('Icodis_Password', $('#UI_Password').val().toString(), 3650, '/', domain, '')
    });


// Bind clicks to events

    //Clicking on submit button
    // First reset this button
    $('#UI_LoginButton_UI_Link').attr("href", "#");
    $('#UI_LoginButton_UI_Link').click(function() {
        Pre_Login();
    });

    $('.FocusButton_Link').click(function() {
    //document.form1.submit();
        __doPostBack('UI_LicenseOverlay$UI_Agree_Link', '')
    });
    




    
    //Clicking enter key
$('#UI_UserName, #UI_Password').keypress(function(event) {
    if (event.keyCode == '13') {
        event.preventDefault();
        Pre_Login();
    }
});

Init_Buttons();
    

$('#UI_ForgottenPassword').click(function() {
Show_ForgottenPassword();
});

$('#UI_BackToNormalView').click(function() {
Show_Normal();
});





}



function Init_Buttons() {

    $(".FocusButton_Link ").bind("mouseenter", function() {
        $(this).find(".FocusButton").addClass("FocusButton_H");
    }).bind("FocusButton_Link", function() {
    });


}


function Pre_Login() {
    //UI_UserName
    //UI_Password
    //UI_LoginButton_UI_Link

    // Validating form...{Simple}
    // first disable the panel

    if (Validate_Form()) {
        //Validated with succes, do the postback

        //document.form1.submit();
        __doPostBack('UI_LoginButton$UI_Link', '')
      
    } 
   
   
}

function GetCaption(CapCode) {
    var capDesc = $("#UI_ClientCaptioning table tr td:contains('" + CapCode + "')").parent().find("td:eq(1)").text();

    return capDesc;
}


function Validate_Form() {
    var r = true;
    var ResponsTexts = [];

    var UserName = $("#UI_UserName").val().toString();
    var Password = $("#UI_Password").val().toString();

// validate username
   if (UserName.length == 0 || UserName == "") {
       r = false;
       ResponsTexts.push(GetCaption("@Login_Validation_IncorrectUserName"));

   }

// validate password
   if (Password.length == 0 || Password == "") {
       r = false;
       ResponsTexts.push(GetCaption("@Login_Validation_IncorrectPassword"));
       
   }


// iterate trhough array of responselines 
   $("#UI_Respons").text("");
   if (ResponsTexts.length > 0) {
       for (i = 0; i < ResponsTexts.length; i++) {
           // append UI_Respons with this line.
           var newlineText = ResponsTexts[i] + "<br/>"
           $("#UI_Respons").append(newlineText);
           $("#UI_Respons_Panel").slideDown();
       }

   } else {
   $("#UI_Respons_Panel").slideUp();
   }
   
   
   
   
    return r;
}


function Show_ForgottenPassword() {
    $("#UI_View_Normal").slideUp();
    $("#UI_View_ForgottenPassword").slideDown();


}

function Show_Normal() {
  
    $("#UI_View_ForgottenPassword").slideUp();
    $("#UI_View_Normal").slideDown();

}



function AutoFillForm() {


    if (Get_Cookie('Icodis_Username')) {
        var username = Get_Cookie('Icodis_Username');
        $("#UI_UserName").val(username);
        $("#UI_UserPassword").focus();

    } else {
    $("#UI_UserName").focus();
    }



    if (Get_Cookie('Icodis_Password')) {
        var password = Get_Cookie('Icodis_Password');
        $("#UI_Password").val(password);
    } 
    
    
    


}





//Set_Cookie( 'mycookie', 'visited 9 times', 30, '/', '', '' )
function Set_Cookie(name, value, expires, path, domain, secure) {
   
    
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

// this deletes the cookie when called
//Here all you need to do is put in: Delete_Cookie('cookie name', '/', '') 
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	



function DoBrowserCheck(){


var BrowserIsCapable = false;

// Internet epxlorer check Minimal 7
if (BrowserDetect.browser == "Explorer") {
    // should be minimal 7
    if (BrowserDetect.version > 6 && BrowserDetect.version  < 10) {
        BrowserIsCapable = true;
    }

}



// Firefox minimal 3.5
if (BrowserDetect.browser == "Firefox") {
    // should be minimal 7
    if (BrowserDetect.version > 3.3) {
        BrowserIsCapable = true;
    }

}

// Chrome minimal 6
if (BrowserDetect.browser == "Chrome") {
    // should be minimal 7
    if (BrowserDetect.version > 5) {
        BrowserIsCapable = true;
    }

}



if (BrowserIsCapable == false) {
// show browser choose window
    $("#UI_BrowserNotGoodSection").slideDown();
    $("#UI_BrowserVersion").text("(" + BrowserDetect.browser + " versie: " + BrowserDetect.version + ")");
}



}


var BrowserDetect = {
    init: function() {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();
