
//
// Core functions
//
//  check for valid numeric strings

function checkEmail(str) {

    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    var msg = "";

    if (str.indexOf(at)==-1){
    msg = "- Email should contain @ \n";
    return msg;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    msg = "- Email should contain @  \n";
    return msg;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        msg = "- Email should contain . \n";
        return msg;
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        msg = "- Email should not contain . next to the @ \n";
        return msg;
    }

    if (str.substring(str.length-1,str.length)== dot || str.substring(str.length-1,str.length)== at ){
        msg = "- Email should not end on "+dot+" or " +at +" \n";
        return msg;
    }

    if (str.indexOf(" ")!=-1){
        msg = "- Email should not contain blank spaces \n";
        return msg;
    }

    return msg;
}

function checkPassword () {

    var msg = "";
    var form = "form2";
	var password = document.forms[form].password.value;
	var old_password = document.forms[form].old_password.value;

    // alert ("password : " +password + "  \n" + "old_password : " +old_password + "  \n")

	if (password == null || password == "undefined" ) {
		msg = "- Password not given \n";
        return  msg;
	}

    if ( password.length  < 5 || password.length > 30 ) {
		msg = "- Password should be between 5 and 30 charecters long \n";
        return msg;
	}

    if (  password != old_password ) {
		msg = "- Password and confirm password should match \n";
        return msg;
	}

	return msg;

}



function checkAccountId () {

    var msg = "";
    var form = "form2";
	var value = trim(document.forms[form].accountId.value);
	
	value = trimLeadingChar(value,"0"); // cant just do parseInt as the code maybe alpha-numeric

    if (value != null && value.length > 0 ) {
        if ( value != parseInt(value) || parseInt(value) < 1 ) {
	    	msg = "- Invalid account number \n";
            return msg;
        }
	}

	document.forms[form].accountId.value=value;
	
	// alert(value);

	return msg;

}



function checkTermAndCond () {

	// alert(document.form2.terms.checked);

	if (!document.form2.terms.checked ) {
		alert ("To create an account you must agree with our terms and conditions \n");
		return false;
	}

	return true;
}



function submitEmailForm () {
	var msg = "";
    var form = "sendPassword";
	var str = document.forms[form].email.value;

	msg = checkEmail(str);

     if (msg.length > 0) {
	    alert ("Please correct the following \n" +msg);
     } else {
        document.forms[form].submit();
    }
}


function submitForm () {
	var msg = "";
    var form = "form2";                                     

    var username = document.forms[form].username.value;

    msg = checkEmail(username);
	msg += checkPassword();
    msg += checkAccountId();
    	 
    if (msg.length > 0) {
	    alert ("Please correct the following \n" +msg);
    } else {
        if (checkTermAndCond()) {
            document.forms[form].submit();
        }
    }
}


