function CheckAll(Element,Name)
{
    thisCheckBoxes = Element.parentNode.parentNode.parentNode.getElementsByTagName('input');
    for (i = 1; i < thisCheckBoxes.length; i++){
        if (thisCheckBoxes[i].name.substring(0, Name.length) == Name){
            thisCheckBoxes[i].checked = Element.checked;
        }
    }
}

function ow(uri, width, height){
    window.open(uri,'popup','width='+width+',height='+height+',status=yes,menubar=no,resizable=yes,scrollbars=1')
    return false;
}

function check_step_one() {
    f = document.forms['bigForm'];
    //alert(f);
    f.done.disabled = ((f.iname.value == "") || (f.fname.value == ""))     
}

function check_step_one2() {
    f = document.forms['bigForm'];
    //alert(f);
    f.done.disabled = ((f.login.value == "") || (f.iname.value == "") || (f.fname.value == ""))     
}

function copyL(login) {
    if(document.forms["bigForm"].elements["login"]) {
        document.forms["bigForm"].elements["login"].value = login;
        var signal = document.getElementById('signal');
        if(signal) {
            signal.style.display = 'none';
        }    
    }    
check_step_one2();
return false;
}

var passComplete = false;
function comparePasswords(first, repeate){
    if(repeate.value != first.value) {
        if(document.getElementById) {
            document.getElementById("passStatus").innerHTML = "пароли не совпадают, попробуйте еще раз";
            document.getElementById("passStatus").style.color = "red";
			document.getElementById("passStatus").style.display = "block";
            repeate.style.color = "red";
        }
        passComplete = false;
    } else {
        if(document.getElementById) {
            document.getElementById("passStatus").innerHTML = "введено верно";
            document.getElementById("passStatus").style.color = "green";
			document.getElementById("passStatus").style.display = "block";
            repeate.style.color = "green";
        }
        passComplete = true;
    }
}

function check_pass_change(f) {
    //alert(f);
    if(f.done) {
        f.done.disabled = ((f.passwd.value == "") || !passComplete );
    }
    if(f.code) {
        var checkCodeRegex = /^\d{6}$/;
        if(!checkCodeRegex.test(f.code.value)){
            f.done.disabled = true;
        }
    }
}

FirstIntent = true;

function checkPass(f, ffirst, checkQ) {
    var checkPassRegex = /[^(\040a-zA-Z0-9_\-\!\@\#\$\%\^\&\*\(\)\+\=\{\}\[\]\;\:\.\>\<\,\\\/\`\~\|)]/;
    var badchars = false;
    
    if (f.value != "" && checkPassRegex.test(f.value)){
        badchars = true;
        f.style.color = "red";
    } else{
        f.style.color = "";
    }
    if (ffirst.value != "" && checkPassRegex.test(ffirst.value)){
        badchars = true;
        ffirst.style.color = "red";
    } else{
        ffirst.style.color = "";
    }
    if (!badchars){
        if (f.value != "") {
            if (!FirstIntent || (f.value.length >= ffirst.value.length )) {
                FirstIntent = false;
                comparePasswords(ffirst,f);
            }
        }
    }
    if (checkQ){
        if (badchars){
            displayPswdStatus(checkQ, 'badchars', ffirst.value.length);
        } else{
            if (ourRequest){
                if (ffirst.value.length == 0){
                    displayPswdStatus(checkQ, 'empty', ffirst.value.length);
                } else if (ffirst.form.login && (ffirst.value == ffirst.form.login.value)){
                    displayPswdStatus(checkQ, 'coincide', ffirst.value.length);
                } else if (ffirst.value.length < 4){
                    displayPswdStatus(checkQ, 'short', ffirst.value.length);
                } else{
                    var testURL = '/testpassword?ncrnd=' + (1000 + Math.floor(Math.random() * 8999));
                    var Qresponse = fetchURL(testURL, 'passwd=' + ffirst.value);
                    displayPswdStatus(checkQ, Qresponse, ffirst.value.length);
                }
            } else{
                displayPswdStatus(checkQ, 'empty');
            }
        }
    }

    check_pass_change(f.form);
}

function displayPswdStatus(infoId, mode, len){
    var infodiv = document.getElementById(infoId);
    var sStatusBar = document.getElementById(infoId + '_status');
    if (mode == 'empty'){
        infodiv.innerHTML = '&nbsp;';
        infodiv.style.color = '';
        sStatusBar.className = '';
        sStatusBar.style.width = 0;
    } else if (mode == 'short'){
        infodiv.innerHTML = "короткий (" + len + accus125(len, " символ)", " символа)", " символов)");
        infodiv.style.color = 'red';
        sStatusBar.className = 'red_status';
        sStatusBar.style.width = '25%';
    } else if (mode == 'coincide'){
        infodiv.innerHTML = "совпадает с логином (" + len + accus125(len, " символ)", " символа)", " символов)");
        infodiv.style.color = 'red';
        sStatusBar.className = 'red_status';
        sStatusBar.style.width = '99%';
    } else if (mode == 'weakpasswd'){
        infodiv.innerHTML = "простой (" + len + accus125(len, " символ)", " символа)", " символов)");
        infodiv.style.color = '#E9922B';
        sStatusBar.className = 'yellow_status';
        sStatusBar.style.width = '50%';
    } else if (mode == 'badpasswd'){
        infodiv.innerHTML = "недопустимый (" + len + accus125(len, " символ)", " символа)", " символов)");
        infodiv.style.color = 'red';
        sStatusBar.className = 'red_status';
        sStatusBar.style.width = '99%';
    } else if (mode == 'badchars'){
        infodiv.innerHTML = "Недопустимые символы в пароле. Возможно, у вас включена русская раскладка клавиатуры.";
        infodiv.style.color = 'red';
        sStatusBar.className = 'red_status';
        sStatusBar.style.width = '99%';
    } else if (mode == 'ok'){
        infodiv.innerHTML = "надежный (" + len + accus125(len, " символ)", " символа)", " символов)");
        infodiv.style.color = 'green';
        sStatusBar.className = 'green_status';
        sStatusBar.style.width = '99%';
    }
}

function accus125 (num, word1, word2, word5){
    if ((num >= 10) && (num <= 20)){
        return word5;
    } else if ((num % 10) == 1){
        return word1;
    } else if (((num % 10) == 2) || ((num % 10) == 3) || ((num % 10) == 4)){
        return word2;
    } else {
        return word5;
    }
}

function checkSinglePass(input, contId){
    var checkPassRegex = /[^(\040a-zA-Z0-9_\-\!\@\#\$\%\^\&\*\(\)\+\=\{\}\[\]\;\:\.\>\<\,\\\/\`\~\|)]/;
    var cont = document.getElementById(contId);
    
    if (input.value != "" && checkPassRegex.test(input.value)){
        input.style.color = "red";
        if (cont != null){
            cont.innerHTML = "Недопустимые символы в пароле. Возможно, у вас включена русская раскладка клавиатуры.";
            cont.style.color = "red";
        }
    } else{
        input.style.color = "";
        if (cont != null){
            cont.innerHTML = "&nbsp;";
            cont.style.color = "";
        }
    }
}

function checkRequieredField (field) {
    if(field.value == '') {
        field.focus();
        alert("Заполните, пожалуйста, поле \"" + field.title + "\"");
        return false
    } else {
        return true;
    }
}

function checkPasswd(f,passwd) {
    login = f.login ? (f.login) : "";
    if(validatePasswd(passwd.value, login)) {
        return true;
    } else {
        passwd.focus();
        return false;
    }
    return false;
}

function validatePasswd(passwd, login) {
    var validPasswd = /[^(\040a-zA-Z0-9_\-\!\@\#\$\%\^\&\*\(\)\+\=\{\}\[\]\;\:\.\>\<\,\\\/\`\~\|)]/
    if(validPasswd.exec(passwd)) {
        alert("Вы используете в пароле недопустимые символы: " + passwd);
        return false;
    }    
    if(passwd.length < 4) {
        alert('Слишком короткий пароль');
        return false
    }
    if(passwd == login)  {
        alert('Вы используете в качестве пароля логин!');
        return false;
    }
    return true;
}

var ourRequest;

function initTestPassword(){
    var maxTimeout = 500;
    
    ourRequest = initRequest();
    if (!ourRequest){
        drawCheckButton();
    }
    var myDate0 = new Date();
    var $testresponse = fetchURL('/testpassword?ncrnd=' + (1000 + Math.floor(Math.random() * 8999)), 'passwd=XXXXXXXX');
    if ($testresponse != 'weakpasswd'){ // Can't get expected response from server. Disable ajacs
        ourRequest = null;
        drawCheckButton();
        return;
    }
    var myDate1 = new Date();
    var myMins0 = myDate0.getMinutes();
    var myMins1 = myDate1.getMinutes();
    if (myMins1 < myMins0){
        myMins1 += 60;
    }
    var Miliseconds0 = myDate0.getMilliseconds() + 1000 * (myDate0.getSeconds() + 60 * myMins0);
    var Miliseconds1 = myDate1.getMilliseconds() + 1000 * (myDate1.getSeconds() + 60 * myMins1);
    var myRuntime = Miliseconds1 - Miliseconds0;
    
    if (myRuntime > maxTimeout){ // Very slow connection
        ourRequest = null;
        drawCheckButton();
    }
}

function initRequest(){
    var request=null;
    if(!request){
        try {
            request=new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){}
    }
    if(!request){
        try {
            request=new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e){}
    }
    if(!request){
        try {
            request=new XMLHttpRequest();
        } catch (e){}
    }
    if(!request){
        return null;
    } else{
        return request;
    }
}

function fetchURL(URL, data){
    ourRequest.open('POST', URL, false);
    ourRequest.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    ourRequest.send(data);
    return ourRequest.responseText;
}

function drawCheckButton(){
    var ButtonDiv = document.getElementById('testButton');
    if (ButtonDiv != null){
        ButtonDiv.style.display='';
    }
}

function noajacsTestPassword(inp){
    var myForm = document.getElementById('testpassword');
    var password = inp.value;
    
    if (password == ''){
        displayPswdStatus('quality', 'empty', password.length);
    } else if (myForm.login && (password == myForm.login.value)){
        displayPswdStatus(checkQ, 'coincide', ffirst.value.length);
    } else if (password.length < 4){
        displayPswdStatus('quality', 'short', password.length);
    } else{
        myForm.passwd.value = password;
        myForm.submit();
    }
}

function r(a, w)
{
    img = new Image();
    img.src = 'http://clck.yandex.ru/click/dtype=' + w + '/*' + a.href;
}

function s(a, w)
{
    img = new Image();
    img.src = 'http://clck.yandex.ru/click/dtype=' + w + '/*' + a.form.action;
    window.setTimeout(function() { a.form.submit() }, 100)
    return false;
}
