/*
*Author     : Matthew Allan Newman, Move Social Networking Platform
*Description: Move SNP Version 1.0
*
*Copyright (c) 2008-2009
*Matthew Allan Newman & Move Social Networking Platform,
*All rights reserved.
*Refer to license included with the distribution.
*/


function formValidator(){
    // Make quick references to our fields
    var firstname = document.getElementById('fname');
    var lastname = document.getElementById('lname');
    var email = document.getElementById('email');
    var gender = document.getElementById('gender');
    var month = document.getElementById('birthday_month');
    var day = document.getElementById('birthday_day');
    var year = document.getElementById('birthday_year');
    
	
    // Check each input in the order that it appears in the form!
    if(isAlphabet(firstname, "Please enter only letters for your name")){
        if(isAlphabet(lastname, "Please enter only letters for your name")){
            if(emailValidator(email, "Please enter a valid email address")){
                if(madeSelection(gender, "Please select your gender")){
                    if(madeSelection(month, "Please select your birth month")){
                        if(madeSelection(day, "Please select your birth day")){
                            if(madeSelection(year, "Please select your birth year")){
                                return true;
                            }
                        }   
                    }
                }
            }
        }
    }
	
    return false;
	
}

function isEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return true;
    }
    return false;
}

function isNumeric(elem, helperMsg){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function isAlphabet(elem, helperMsg){
    var alphaExp = /^[a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function isAlphanumeric(elem, helperMsg){
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

function lengthRestriction(elem, min, max){
    var uInput = elem.value;
    if(uInput.length >= min && uInput.length <= max){
        return true;
    }else{
        alert("Please enter between " +min+ " and " +max+ " characters");
        elem.focus();
        return false;
    }
}

function madeSelection(elem, helperMsg){
    if(elem.value == "-1"){
        alert(helperMsg);
        elem.focus();
        return false;
    }else{
        return true;
    }
}

function emailValidator(elem, helperMsg){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.match(emailExp)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}
