﻿var xmlHttp; 
var msgControlId;
var valType; 

var nospaces = "Kan ikke inneholde mellomrom";
var passwordmissing = "Må ha minst 6 tegn";
var passwordnotmatching = "Passordene er ulike";
var usernamemissing = "Må være mellom 4 og 16 tegn";
var aliasmissing = "Må være mellom 4 og 16 tegn";
var usernameinvalidchars = "Må bare innhold små og store bokstaver (a-z) og tall (ikke æ, ø, å, mellomrom etc.)";
var emailwrongformat = "Ugyldig e-postadresse";
var usernameinuse = "Ikke tilgjengelig";
var aliasinuse = "Ikke tilgjengelig";
var phonenumberinuse = "Mobilnummeret er i bruk";
var emailinuse = "E-postadressen er i bruk";
var usernameinusetooltip = "";
var phonenumberinusetooltip = "";
var emailinusetooltip = "";
var oktooltip = "OK";
var disallowedword = "Kan ikke brukes";

function ValidateInput(validationType, controlId, inputValue, compareValue)   
{   
    msgControlId = controlId;
    valType = validationType;
    inputValue = trim(inputValue, null);
    compareValue = trim(compareValue, null);

    if (inputValue.length == 0)   
    {   
        document.getElementById(controlId).innerHTML = '';   
        
        return;   
    }   
    
    if (validationType == "username")
    {
        if (inputValue.length < 4 || inputValue.length > 16)
        {
            RenderErrorMessage(controlId, usernamemissing, null, null);
        }
        else if (!ValidateAsciiString(inputValue))
        {
            RenderErrorMessage(controlId, usernameinvalidchars, null, null);
        }
        else
        {
            CheckAvailability(validationType, inputValue);
        }
    }
    else if (validationType == "alias")
    {
        if (inputValue.length < 4 || inputValue.length > 16)
        {
            RenderErrorMessage(controlId, aliasmissing, null, null);
        }
        else
        {
            CheckAvailability(validationType, inputValue);
        }
    }
    else if (validationType == "password")
    {
        var password = inputValue.split(' ').join('');
        
        if (password.length != inputValue.length)
        {
            RenderErrorMessage(controlId, nospaces, null, null);
               
            return;
        }

        if (inputValue.length < 6)
        {
            RenderErrorMessage(controlId, passwordmissing, null, null);
        }
        else
        {
            RenderErrorMessage(controlId, null, null, null);
        }
        
        return;
    }
    else if (validationType == "confirmpassword")
    {
        if (inputValue != compareValue)
        {
            RenderErrorMessage(controlId, passwordnotmatching, null, null);
        }
        else
        {
            if (inputValue.length < 6)
            {
                RenderErrorMessage(controlId, passwordmissing, null, null);
            }
            else
            {
                RenderErrorMessage(controlId, null, null, null);
            }
        }
        
        return;
    }
    else if (validationType == "phone")
    {
        var phoneNumber = inputValue.split(' ').join('');
    
        if (phoneNumber.length != 8)
        {
            RenderErrorMessage(controlId, '', null, null);
        }
        else
        {
            CheckAvailability(validationType, inputValue);
        }
    }
    else if (validationType == "email")
    {
        if (!ValidateEmail(inputValue))
        {
            RenderErrorMessage(controlId, emailwrongformat, null, null);
        }
        else
        {
            CheckAvailability(validationType, inputValue);
        }
    }
}   

function StateChanged()   
{   
    if ((xmlHttp.readyState == 4) && (xmlHttp.status == 200))   
    {   
        var message = '';
 
        var responseText = trim(xmlHttp.responseText);
 
        if (responseText == 'false')
        {
            if (valType == 'username')
            {
                RenderErrorMessage(msgControlId, usernameinuse, null, null);
            }
            else if (valType == 'alias')
            {
                RenderErrorMessage(msgControlId, aliasinuse, null, null);
            }
            else if (valType == 'phone')
            {
                RenderErrorMessage(msgControlId, phonenumberinuse, null, null);
            }
            else if (valType == 'email')
            {
                RenderErrorMessage(msgControlId, emailinuse, null, null);
            }
        }
        else
        {
            RenderErrorMessage(msgControlId, null, null, null);
        }
    }   
}

function RenderErrorMessage(controlId, message, helpText, helpLink)
{
    if (message != null)
    {
        if (helpText != null && helpLink != null)
        {
            document.getElementById(controlId).innerHTML = "<img style=\"vertical-align: middle;\" src=\"/Templates/Images/Icons/Invalid.png\" height=\"24\" width=\"24\" alt=\"\" /> <span class=\"Error\">" + message + "</span> <a href=\"" + helpLink + "\" title=\"" + helpText  + "\" target=\"_blank\"><img style=\"vertical-align: middle;\" src=\"/Templates/Images/Help.gif\" height=\"20\" width=\"20\" alt=\"" + helpText + "\" /></a>";
        }
        else
        {
            document.getElementById(controlId).innerHTML = "<img style=\"vertical-align: middle;\" src=\"/Templates/Images/Icons/Invalid.png\" height=\"24\" width=\"24\" alt=\"\" /> <span class=\"Error\">" + message + "</span>";       
        }
    }
    else
    {
        document.getElementById(controlId).innerHTML = "<img style=\"vertical-align: middle;\" src=\"/Templates/Images/Icons/Valid.png\" height=\"24\" width=\"24\" alt=\"" + oktooltip + "\" />";    
    }
}

function CheckAvailability(inputParameter, inputValue)
{
    try  
    {   
        if (window.XMLHttpRequest)
        { 
            xmlHttp = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject)
        {  
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");               
        }
        
        if (!xmlHttp || xmlHttp == null)   
        {   
            return;   
        }  
        
        var url="/Templates/Pages/CheckAvailability.aspx?" + inputParameter + "=" + inputValue;   
        xmlHttp.onreadystatechange = StateChanged;   
        xmlHttp.open("GET", url, true);   
        xmlHttp.send(null);   
    }   
    catch(e)   
    {   

    }   
}

function ClearValidator(controlId)  
{
  document.getElementById(controlId).innerHTML = "";
}


function trim(str, chars)
{
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ValidateEmail(inputValue)
{
    var regEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (inputValue.match(regEx))
    {
        return true;
    }

    return false;
}

function ValidateInteger(inputValue)
{
    var regEx = /^-?\d+$/;

    if (inputValue.match(regEx))
    {
        return true;
    }

    return false;
}

function ValidateAsciiString(inputValue)
{
    var regEx = /^([a-zA-Z0-9_]){4,16}$/;

    if (inputValue.match(regEx))
    {
        return true;
    }

    return false;
}