﻿//function CheckIfSingle() {
//    var test = $.getJSON("medlem/CountSearchResult", {
//        value: document.getElementById('searchValue').value
//    },
//            function(data) {
//                alert(data.foo);
//                
//            })
//           
//}
function redirectToLogin() {
    $.getJSON("/Account/LogOff", null, move("/Default.aspx"));

    alert("Du har varit inaktiv för länge och har loggats ut");

}
function ShowHide(id) {
    var div = document.getElementById(id);
    if (div.style.display == "none")
        div.style.display = "block";
    else
        div.style.display = "none";
}
function submitform() {
    document.myform.submit();


}
function submitThisForm(form) {
    form.submit();

}
function open(caller) {
    if (caller == 'villkor') {
        window.open("/Site/Cookies", "Villkor")
    }
    else if (caller == 'faq') {

    }
}

function move(url) {
    //window.location = document.URL.substring(0, document.URL.indexOf("Medlemmar", 0)) + url;
    window.location = url;
}
function checkIfEmail() {
    var epost = document.getElementsByName("Epost");
    var choice = document.getElementsByName("editChoice");
    if (epost.length > 0) {
        if (epost[0].value == "" && choice[0].value == 2) {
            epost[0].style.backgroundColor = "#ffa4a4";
            document.getElementById("error." + epost[0].id).style.display = "inline";
            document.getElementById('submitButton').disabled = true;
            return false;
        }
    }

    var im = document.getElementsByName("antalIckeMedlem");
    var m = document.getElementsByName("antalMedlem");
    var r = im[0].value + m[0].value;
    var error = document.getElementById("error");
    if (r < 1) {
        error.style.display = "block";
        im[0].style.backgroundColor = "#ffa4a4";
        m[0].style.backgroundColor = "#ffa4a4";
        return false

    }
    document.getElementById('submitButton').disabled = false;
    return true;

}

function Change() {
    var control = document.getElementById("red");
    if (control.style.color != "Orange")
        control.style.color = "Orange";
}
function ChangeBorder(id) {
    var control = document.getElementsByName(id);
    for (var i = 0; i < control.length; i++) {
        if (control[i].type == "text" || control[i].tagName == "SELECT") {
            if (control[i].value != "") {
                if (control[i].style.borderBottomColor != "Lime")
                    control[i].style.borderBottomColor = "Lime";
            }
            else {
                if (control[i].style.borderBottomColor == "Red")
                    control[i].style.borderBottomColor = "Red";
            }

        }
    }

    //    if (ifStringIsEmpty(control[0].value))
    //        control[0].style.borderBottomColor = "#CCC";
}
function CheckIfChanged() {

    var form = document.getElementById('formMain');

    //    for (var i = 0; i < 100; i++) {

    //        if (control.all[i].nodeName == "FORM") {
    //            var form = control.all[i];
    //            break;
    //        }
    //    }

    if (form != null) {
        for (i = 0; i < form.length; i++) {
            //alert(form[i].style.borderBottomColor)
            if (form[i].style.borderBottomColor == "Lime" || form[i].style.borderBottomColor == "Red" || form[i].style.borderBottomColor == "#ff0000" || form[i].style.borderBottomColor == "#00ff00" || form[i].style.borderBottomColor == "lime" || form[i].style.borderBottomColor == "red") {
                var answer = confirm("Vill du lämna sidan utan att spara ändringarna?");
                if (answer) {

                    return true;
                }
                else {
                    return false;
                }
            }
        }
        return true;
    }

}
function checkFormMedlem(form, medlem, ickeMedlem) {

    if (document.getElementById(medlem).value == "0" && document.getElementById(ickeMedlem).value == "0") {
        document.getElementById(medlem).style.backgroundColor = "#ffa4a4";
        document.getElementById(ickeMedlem).style.backgroundColor = "#ffa4a4";
        return false;
    }
    return true;
}
function CheckDates(SlutDatumIn, StartDatumIn, BokningCloseIn, BokningOpenIn, form) {
    var SlutDatum = document.getElementsByName(SlutDatumIn);

    SlutDatum = SlutDatum[0];

    SlutDatum.style.backgroundColor = "#adffa0"; ;
    CheckLenght('SlutDatum', SlutDatum.value, 'datum');

    var StartDatum = document.getElementsByName(StartDatumIn);
    StartDatum = StartDatum[0];
    StartDatum.style.backgroundColor = "#adffa0"; ;
    CheckLenght('StartDatum', StartDatum.value, 'datum');

    var BokningClose = document.getElementsByName(BokningCloseIn);
    BokningClose = BokningClose[0];
    BokningClose.style.backgroundColor = "#adffa0"; ;
    CheckLenght('BokningClose', BokningClose.value, 'datum');

    var BokningOpen = document.getElementsByName(BokningOpenIn);
    BokningOpen = BokningOpen[0];
    BokningOpen.style.backgroundColor = "#adffa0"; ;
    CheckLenght('BokningOpen', BokningOpen.value, 'datum');







    var i = 0;
    if (SlutDatum.value < StartDatum.value) {
        SlutDatum.style.backgroundColor = "#ffa4a4"; ;
        i++;
    }


    if (BokningClose.value > SlutDatum.value) {
        BokningClose.style.backgroundColor = "#ffa4a4"; ;
        i++;
    }
    if (BokningOpen.value > StartDatum.value) {
        BokningOpen.style.backgroundColor = "#ffa4a4"; ;
        i++;
    }
    if (BokningClose.value < BokningOpen.value) {
        BokningClose.style.backgroundColor = "#ffa4a4"; ;
        i++;
    }

    return checkForm(form);




}
function CheckPostNumber(evt, name) {


    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 47 && charCode < 58 || charCode == 9 || charCode == 8 || (charCode > 36 && charCode < 41)) {
        return true;
    }

    return false;
}
function CheckPhoneNumber(evt, name) {

    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode == 45 || charCode == 43 || (charCode > 47 && charCode < 58) || charCode == 9 || charCode == 8 || (charCode > 36 && charCode < 41)) {
        return true;
    }

    return false;
}
function CheckClock(evt, name) {


    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 47 && charCode < 59 || charCode == 9 || charCode == 8 || (charCode > 36 && charCode < 41)) {
        return true;
    }

    return false;
}
function CheckDate(evt, name) {


    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode == 45 || (charCode > 47 && charCode < 58) || charCode == 9 || (charCode > 36 && charCode < 41)) {
        return true;
    }

    return false;
}
function CheckNumber(evt, name) {


    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 47 && charCode < 58 || charCode == 9 || charCode == 8 || (charCode > 36 && charCode < 41)) {
        return true;
    }

    return false;
}
function checkDecimal(evt, name) {


    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode == 44 || charCode > 47 && charCode < 58 || charCode == 9 || charCode == 8 || (charCode > 36 && charCode < 41)) {
        return true;
    }

    return false;
}
function CheckLenght(name, text, typ) {

    var control = document.getElementsByName(name);
    var val = new String(control[0].value);

    //    if (typ == "postNr") {
    //        if (val.length < 5 || val.length > 6)
    //            control[0].style.borderBottomColor = "Red";
    //        if (val.length == 6) {
    //            if (val.indexOf(" ") != 3)
    //                control[0].style.borderBottomColor = "Red";
    //        }

    //    }
    if (typ == "tid") {

        if (val.length < 4 || val.length > 5) {
            control[0].style.borderBottomColor = "Red";

        }

        if (val.length == 5) {
            if (val.indexOf(":") != 2)
                control[0].style.borderBottomColor = "Red";
        }

    }
    if (typ == "personNr") {
        if (val.length < 4 || val.length > 12)
            control[0].style.borderBottomColor = "Red";


        if (val.length == 4) {
            var regExp = /^[12]{1}[09]{1}[0-9]{1}[0-9]{1}$/;
            if (!control[0].value.match(regExp))
                control[0].style.borderBottomColor = "Red";
        }
        //        else if (val.length == 6) {
        //            var regExp = /^[0-9]{1}[0-9]{1}[01]{1}[0-9]{1}[012]{1}[0-9]{1}$/;
        //            if (!control[0].value.match(regExp))
        //                control[0].style.borderBottomColor = "Red";
        //        }
        else if (val.length == 8) {
            var regExp = /^[12]{1}[09]{1}[0-9]{1}[0-9]{1}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}$/;
            if (!control[0].value.match(regExp))
                control[0].style.borderBottomColor = "Red";
        }
        else {
            control[0].style.borderBottomColor = "Red";
        }

    }
    if (typ == "datum") {


        if (val.length < 10 || val.length > 10) {
            control[0].style.borderBottomColor = "Red";


        }

        if (val.length == 10) {
            if (val.indexOf("-") != 4)
                control[0].style.borderBottomColor = "Red";
            if (val.lastIndexOf("-") != 7)
                control[0].style.borderBottomColor = "Red";
        }


    }
    //if (ifStringIsEmpty(control[0].value))
    //    control[0].style.borderBottomColor = "#CCC";

}

function checkLetters(evt, name) {

    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode

    if (charCode == 32
    || charCode == 8
    || charCode == 9
    || charCode == 39
    || charCode == 180
    || charCode == 229
    || charCode == 228
    || charCode == 246
    || charCode == 197
    || charCode == 196
    || charCode == 214
    || charCode == 45
    || (charCode > 36 && charCode < 41)
    || (charCode > 96 && charCode < 132)
    || (charCode > 64 && charCode < 91)) {
        return true;
    } else {

        return false;
    }
}
function checkEmail(name) {
    var control = document.getElementsByName(name)
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if (!control[0].value.match(emailExp))
        control[0].style.borderBottomColor = "Red";


    if (ifStringIsEmpty(control[0].value))
        control[0].style.borderBottomColor = "#CCC";

}
function checkUrl(name) {
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w\.]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    var control = document.getElementsByName(name)
    if (!control[0].value.match(RegExp))
        control[0].style.borderBottomColor = "Red";


    if (ifStringIsEmpty(control[0].value))
        control[0].style.borderBottomColor = "#CCC";

}
function checkForm(form) {

    if (form != null) {
        for (var i = 0; i < form.length; i++) {
            //alert(form[i].style.borderBottomColor);
            if ((form[i].style.borderBottomColor == "Red" || form[i].style.borderBottomColor == "#ff0000" || form[i].style.borderBottomColor == "red")) {
                alert("Du kan inte spara ett formulär med röda fält?");
                return false;
            }
        }
    }


}
function ifStringIsEmpty(s) {
    var q = new String(s);
    if (q.length == 0)
        return true;
    else
        return false;

}


//function ValidateInfo(name) {


//    if (name == "p")
//        alert("Denna kontroll valideras mot ett korrekt inskrivet postnummer. \n Godkända värden är XXXXX och XXX XX.\n Enbart siffror kan skrivas in.\Max sex tecken")
//    if (name == "P")
//        alert("Denna kontroll valideras mot ett korrekt inskrivet personnummer. \n Godkända värden är XXXXXX-XXXX och XXXXXXXXXX.\n Enbart siffror och bindestreck kan skrivas in.")
//    if (name == "t")
//        alert("Denna kontroll valideras mot ett korrekt inskrivet personnummer. \n Godkända värden är XXX-XXXXXXX och XXXXXXXXXX.\n Enbart siffror och bindestreck  kan skrivas in.")
//    if (name == "c")
//        alert("Denna kontroll valideras mot ett korrekt inskrivet klockslag. \n Godkända värden är XX:XX\n Enbart siffror och kolon kan skrivas in.\n Antal tecken fem.")


//}
function CheckAgainst(name, value, CheckName, type) {

    var controlToCheck = document.getElementsByName(CheckName);
    var control = document.getElementsByName(name);
    if (type != "") {
        if (value > controlToCheck[0].value) {
            control[0].style.borderBottomColor = "red"
        }
    }
    else {
        if (value < controlToCheck[0].value) {
            control[0].style.borderBottomColor = "red"
        }

    }

}
function CheckIfValid(id, validateType, form) {
    switch (validateType) {
        case 1: //sträng
            {
                var regExp = /^.+$/;
                break;
            }
        case 2: // heltal
            {
                var regExp = /^[0-9]+$/;
                break;
            }

        case 3: // decimal
            {
                var regExp = /^[0-9\,\.]+$/;
                break;
            }
        case 4: // datum
            {
                var regExp = /^[12]{1}[09]{1}[0-9]{2}[-]{1}[01]{1}[0-9]{1}[-]{1}[0123]{1}[0-9]{1}$/;
                break;
            }
        case 5: // email
            {
                var regExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
                break;
            }
        case 6: // personnummer
            {
                if (document.getElementById(id).value != null) {
                    var str = new String(document.getElementById(id).value);
                    str = str.replace("-", "");
                    str = str.replace("-", "");
                    str = str.replace("-", "");
                    str = str.replace("-", "");
                    document.getElementById(id).value = str;
                }
                var regExp = /^[12]{1}[09]{1}[0-9]{1}[0-9]{1}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}([0-9]{4})*$/;
                break;
            }
        case 12: // URL
            {
                var regExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w\.]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
                break;
            }
        case 13: // PostNr tillåter 123456789 och mellanslag, 6 tecken om ett mellanslag annars 5 tecken
            {
                var regExp = /^.+$/;
                if (document.getElementById(id).value != null) {
                    var str = new String(document.getElementById(id).value);
                    str = str.replace(" ", "");
                    str = str.replace(" ", "");
                    str = str.replace(" ", "");
                    document.getElementById(id).value = str;
                }
                break;
            }
        case 9: //Telefonnummer tillåter "() - 0123456789"
            {
                var regExp = /^[0-9-()]+$/;
                break;
            }
        case 10: // Ingen
            {
                var regExp = "";
                break;
            }
        case 11: // Tid
            {
                var regExp = /^[012]{1}[0-9]{1}[:]{1}[0-9]{2}$/;
                break;
            }

    }
    //    var button = $(".submitButton").id = "submitButton";
    //    if (button == null)
    //button = ;
    if (document.getElementById(id).rel != null) {
        if (!document.getElementById(id).value.match(regExp)) {
            //document.getElementById("ok." + id).style.display = "none";
            document.getElementById("error." + id).style.display = "inline";
            document.getElementById(id).style.backgroundColor = "#ffa4a4";
            document.getElementById(id).rel = "required";
        }
        else {
            document.getElementById("error." + id).style.display = "none";
            document.getElementById(id).style.backgroundColor = "#adffa0";
            //document.getElementById("ok." + id).style.display = "inline";
            document.getElementById(id).rel = "ok";
        }
        $('.buttonSubmit')[0].disabled=true;
        //button.disabled = true;
    }
    else {

        if (document.getElementById(id).value != "") {
            if (!document.getElementById(id).value.match(regExp)) {
                //document.getElementById("ok." + id).style.display = "none";
                if (document.getElementById("error." + id) != null)
                    document.getElementById("error." + id).style.display = "inline";
                document.getElementById(id).style.backgroundColor = "#ffa4a4";

            }
            else {
                if (document.getElementById("error." + id) != null)
                    document.getElementById("error." + id).style.display = "none";
                document.getElementById(id).style.backgroundColor = "#adffa0";

            }


        }
        else {
            if (document.getElementById("error." + id) != null)
                document.getElementById("error." + id).style.display = "none";
            document.getElementById(id).style.backgroundColor = "#ffffff";

        }

    }

    var count = 0;
    for (i = 0; i < form.length; i++) {


        if ((form[i].rel != null && form[i].rel == "required" && form[i].value == "") || form[i].style.backgroundColor == "#ffa4a4") {
            count++;
        }
    }
    if (count == 0) {
        if ($(".buttonSubmit") != null)
            $('.buttonSubmit')[0].disabled = false;
    }
    else {
        if ($(".buttonSubmit") != null)
            $('.buttonSubmit')[0].disabled = true;
    }


}
function clear() {
    document.getElementById('mess').innerHtml = "";
}
function GetAktuell(link, id) {
    //link.style = "black";
    $(".image img").each(function() {

        this.style.display = 'none';


    });

    document.getElementById('a.' + id).style.display = "inline";


}
function SetAktuell(link, id) {
    if (document.getElementById('aktuellflik') != null) {
        var enums = "&";

        $(".enums").each(function() {

            if (this.checked)
                enums += this.value + "=true&"

        })
        link.href = "/Medlem/setidsmall/" + id + "?aktuell=" + document.getElementById('aktuellflik').innerHTML + enums;
    }
    else {

        link.href = "/Medlem/setidsmall/" + id + "?aktuell=medlem";
    }
}
function SetAsAktuell(link, arg) {

    document.getElementById('aktuellflik').innerHTML = arg;
}
//Epost javascript
//Slut epost javascript



//1 = sträng
//2 = Heltel
//3 = Decimal
//4 = datum
//5 = epost
//6 = personnummer
//12 = url
//13 = PostNr


/*    
Charcodes
a-z = 97-122
A-Z = 65-90
mellanslag = 32
å = 229
ä = 228
ö = 246
Å = 197
Ä = 196
Ö = 214
' = 39
´ = 180
; = 59
: = 58
. = 46
, = 44
! = 33
" = 34
# = 35
¤ = 164
% = 37
& = 38
/ = 47
( = 40
) = 41
= = 61
? = 63
` = 96
{ = 123
[ = 91
] = 93
} = 125
\ = 92
@ = 64
£ = 163
$ = 36
€ = 8364
+ = 43
- = 45
tabb = 9
enter = 13
*/