main.js

var err_img = '';
var temp = '';

function login_validate()
{
var msg = "";
if($('#signin_username').val()=="")
{
msg = msg+err_img+'Username is required.
';
}
if($('#signin_password').val()=="")
{
msg = msg+err_img+'Password is required.
';
}
else if($('#signin_password').val().length<6)
{
msg = msg+err_img+'Password needs to be more than 6 characters.
';
}

if(msg!="")
{
$('#error').html(msg);
return false;
}else{
return true;
}

}

function register_validate()
{
var msg = "";
if($('#register_username').val()=="")
{
msg = msg+err_img+'Username is required.
';
}
if($('#register_password').val()=="")
{
msg = msg+err_img+'Password is required.
';
}
else if($('#register_password').val().length<6)
{
msg = msg+err_img+'Password needs to be more than 6 characters.
';
}
else if($('#register_password_conf').val()=="")
{
msg = msg+err_img+'Password confirmation required.
';
}
else if($('#register_password_conf').val()!=$('#register_password').val())
{
msg = msg+err_img+'Password confirmation missmatched.
';
}
if($('#register_fname').val()=="")
{
msg = msg+err_img+'First name is required.
';
}
if($('#register_lname').val()=="")
{
msg = msg+err_img+'Last name is required.
';
}
if($('#register_email').val()=="")
{
msg = msg+err_img+'Email is required.
';
}
else if(!isValidEmailAddress($('#register_email').val()))
{
msg = msg+err_img+'Email provided is invalid.
';
}
if($('#register_address').val()=="")
{
msg = msg+err_img+'Address is required.
';
}

if(msg!="")
{
$('#error').html(msg);
return false;
}else{
return true;
}
}


function edit_profile_validate()
{
var msg = "";
if($('#edit_fname').val()=="")
{
msg = msg+err_img+'First name is required.
';
}
if($('#edit_lname').val()=="")
{
msg = msg+err_img+'Last name is required.
';
}
if($('#edit_email').val()=="")
{
msg = msg+err_img+'Email is required.
';
}
else if(!isValidEmailAddress($('#edit_email').val()))
{
msg = msg+err_img+'Email provided is invalid.
';
}
if($('#edit_address').val()=="")
{
msg = msg+err_img+'Address is required.
';
}

if($('#edit_password:checked').val()=="on")
{
if($('#edit_old_password').val()=="")
{
msg = msg+err_img+'Current password is required.
';
}
if($('#edit_new_password').val()=="")
{
msg = msg+err_img+'New password is required.
';
}
else if($('#edit_new_password').val().length<6)
{
msg = msg+err_img+'New password needs to be more than 6 characters.
';
}
else if($('#edit_new_password_conf').val()=="")
{
msg = msg+err_img+'Password confirmation required.
';
}
else if($('#edit_new_password_conf').val()!=$('#edit_new_password').val())
{
msg = msg+err_img+'Password confirmation missmatched.
';
}
else if($('#edit_new_password').val()==$('#edit_old_password').val())
{
msg = msg+err_img+'Current and new passwords are identical.
';
}
}

if(msg!="")
{
$('#error').html(msg);
return false;
}else{
return true;
}

}

function delete_confirm(type)
{
var conf = confirm("Do you really want to delete this "+type+"?");
if(conf){
return true;
}else{
return false;
}
}

function validate_book()
{
var msg = "";
if($('#book_title').val()=="")
{
msg = msg+err_img+'Please enter the book title.
';
}
if($('#book_content').val()=="")
{
msg = msg+err_img+'Please enter the book contents.
';
}

if(msg!="")
{
$('#error').html(msg);
return false;
}else{
return true;
}
}


function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

errors.php



if($_GET['msg'])
{
$msg = new Message();
echo $msg->getMessage($_GET['msg']);
}
?>

Tools class

class Tools
{
public function escape($str) {
$str = get_magic_quotes_gpc()?stripslashes($str):$str;
$str = mysql_real_escape_string($str);
return $str;
}

public function redirect($str,$msg_no=NULL,$var=NULL) {
$str =$str.'.php';

if($msg_no):$msg = '?msg='.self::escape($msg_no);
else:$msg='';
endif;

if($var):$var=self::escape($var);
else:$var='';
endif;

header('location:'.$str.$msg.$var);
return true;
}

public function setSession($variable,$value)
{
$_SESSION[$variable] = $value;
}

public function unsetSession($variable)
{
session_unregister($variable);
}

public function authenticate($array)
{
$session = $_SESSION['user'];
$auth = false;
if(isset($session))
{
foreach($array as $arr)
{
if($arr==$_SESSION['user']['group'])
{
$auth = true;
}
}

if(!$auth)
{
self::redirect('notauthorized');
exit;
}
}else{
self::redirect('notauthorized');
exit;
}
}
}

?>