
function checkEmail(emailInputId) {
    var input = cssQuery('#' + emailInputId);
    var at = "@";
	var dot = ".";
	var lat = '';
	var lstr = '';
	var ldot = '';

    for (var i=0; i<input.length; i++) {
        str = input[i].value;
        lat = str.indexOf(at);
    	lstr = str.length;
    	ldot = str.indexOf(dot);

        // INVALID EMAIL
        if (str.indexOf(at)==-1){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        if (str.indexOf(at,(lat+1))!=-1){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        if (str.indexOf(dot,(lat+2))==-1){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        if (str.indexOf(" ")!=-1){
            input[i].className += ' invalid';
            input[i].focus()
            return "please enter a valid email address";
        }
        // VALID EMAIL
        input[i].className.replace(' invalid', '');
        return '';
    }
    return '';
}


function verifyInput(inputId, verifyId) {
    var error = '';
    if ( document.getElementById(inputId) || document.getElementById(verifyId) || document.getElementById('old_password')) {
        // IF ELEMENTS EXIST, CHECK EM

        // OLD PASSWORD
        if (document.getElementById('old_password') && document.getElementById('old_password').value == '') {
            error += 'please enter your old password'+"\n";
            document.getElementById('old_password').className += ' invalid';
        } else if (document.getElementById('old_password') && document.getElementById('old_password').value != '') {
            document.getElementById('old_password').className.replace(' invalid','');
        }

        // NEW PASSWORD
        if (document.getElementById(inputId) && document.getElementById(inputId).value == '') {
            document.getElementById(inputId).className += ' invalid';
            error += "please enter a new password!\n";
        } else {
            document.getElementById(inputId).className.replace(' invalid','');
        }

        // VERIFY NEW PASSWORD
        if (document.getElementById(verifyId) && document.getElementById(inputId) && document.getElementById(inputId).value != document.getElementById(verifyId).value || document.getElementById(verifyId).value == '') {
            document.getElementById(verifyId).className += ' invalid';
            error += "please verify your new password!\n";
        } else {
            document.getElementById(verifyId).className.replace(' invalid','');
        }

        // RETIRN ERROR
        if (error != '') {
            return error;
        }
    }
    return '';
}


function requiredInputsNotEmpty(formId) {
    var invalid = '';
    var inputs = cssQuery('#' + formId + ' .required');
    for (var i=0; i<inputs.length; i++) {
        if (inputs[i].value == '') {
            inputs[i].className += ' invalid';
            if (!z) {
                var z=1;
            }
            invalid = "please fill in required fields";
        } else {
            inputs[i].className = 'required';
            invalid = '';
        }
    }
    return invalid;
}

function checkLoginForm() {
    var forms = cssQuery('#loginForm');
    for (var i=0; i<forms.length; i++) {
        forms[i].onsubmit = function() {
            return checkEmail('email');
        }
    }
}
function checkSignupForm() {
    // ONSUBMIT
    var forms = cssQuery('#signupForm');
    for (var i=0; i<forms.length; i++) {
        forms[i].onsubmit = function() {
            var error = '';
            error += requiredInputsNotEmpty('signupForm') + "\n";
            error += checkEmail('signupEmail') + "\n";
            error += verifyInput('password', 'verifyPassword') + "\n";
            if (error && error != "\n\n\n") {
                alert(error);
                return false;
            }
        }
    }
}


function submitLink() {
    var form = cssQuery('form');
    for (var i=0; i<form.length; i++) {
        theForm = form[i];
        var children = form[i].getElementsByTagName('a');
        for (var z=0; z<children.length; z++) {
            children[z].onclick = function() {
                theForm.submit();
                return false;
            }
        }
    }
}


function init() {
    checkLoginForm();
    checkSignupForm();
    submitLink();
}

window.onload = init;