////////////////////////////SAMER////////////////
/*
- This plugin returns an object has two attributes : first the error message and the second the result for submit(true or false).
- To validate the form : class (validForm) must added.
- To make an input field is required : class (si_required) must added.
- To make an input field is checked as email : class (si_email) must added.
- To make an input field is checked as number : class (si_num) must added.
- To make an input field is match another field [confirm it] : must add to the class ( : the id of field that confirm with).
- Add class (submit) to the submit field.
 */

(function($){
$.fn.FValidate = function(){
            O = new Object();
		   obj = $(this);
		    checkForm();
		   return O;

//------------------ check email
function isValidEmail(email){
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return reg.exec(email);
} 
//------------------------- check number
function isValidNumber(number){
	var reg = /^[0-9]+(\.[0-9]{1,3})?$/;
	return reg.exec(number);
}
//-----------------------check Date
function isValidDate(day, month, year){
	var ret = true;
	var days = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if(!day || !month || !year) return false;
	if(day < 1 || day > 31) return false;
	if(month < 1 || month > 12) return false;
	if(day > days[month-1] ) return false;
	if(month == 2 && day == 29 && year % 4 != 0) return false;
	return true;
}

//----------------- check form
function checkForm(){
var res=true;
pass='';
var msg='';

$(":text.si_required",obj).each(function(){
var val = $(this).val();
val = $.trim(val);
if(val=='')
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";
}
else
{
if(($(this).attr('class').indexOf('si_email'))!=-1)
{
if(!isValidEmail(val))
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";
}
}
 if(($(this).attr('class').indexOf('si_num'))!=-1)
{
if(!isValidNumber(val))
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";
}
}
if(($(this).attr('class').indexOf(':'))!=-1)
{
var id = $(this).attr('class').substr($(this).attr('class').indexOf(':')+1);	
var pass = $("#"+id).val();
pass =$.trim(pass); 
if(pass!=val)
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";
}
}
}
})///////text

$(":password.si_required",obj).each(function(){

var val;
var val = $(this).val();
val = $.trim(val);
if(val=='')
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";
}
else
if(($(this).attr('class').indexOf(':'))!=-1)
{
var id = $(this).attr('class').substr($(this).attr('class').indexOf(':')+1);	
var pass = $("#"+id).val();
pass =$.trim(pass); 
if(pass!=val)
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";	
}
}
})///////password

$("select.si_required").each(function(){
var val;
var val = $(this).val();
if(val=='')
{
res=false;
msg+=$(this).attr('errorMsg')+"<br/>";
}	
})///////select

$("#date_div").each(function(){
{
var d =$("#day").val();
var y =$("#year").val();
var m =$("#month").val();

res1 =  isValidDate(d, m, y);
if(res1==false)
{
res=res1;
msg+=$(this).attr('errorMsg')+"<br/>";
}
}})

	O.msg = msg;
	O.res = res;		
}	
}
})(jQuery);



