/*
***************************************************************************
***************************************************************************
************************* CLIENT SIDE VALIDATIONS *************************
*********************************** BY ************************************
*************************** LUAY H. ABU SHARIHA ***************************
********************** Integrated Business Solutions **********************
***************************************************************************
***************************************************************************
*/


//This function is used to count the number
//of spaces in the field and compare this number
//to field length, and check for zero length strings
function check_str(str){
	var spaces=0; //space counter
	//check if zero length string
	if(str.length==0){
		return false;
	}else{
		//check each char if its a space increase counter
		for(var i=0;i<str.length;i++){
			if(str.charAt(i)==" ")
				spaces++;
		}
		if(str.length==spaces)//true if all char's are spaces
			return false;
		else
			return true;
	}
}


//This function is used to count the number
//of spaces in the field and compare this number
//to field length, and check for numeric fields only
function check_num(str){
	var spaces=0; //space counter
	//check if zero length string
	if(str.length==0||isNaN(str)){
		return false;
	}else{
		//check each char if its a space increase counter
		for(var i=0;i<str.length;i++){
			if(str.charAt(i)==" ")
				spaces++;
		}
		if(str.length==spaces)//true if all char's are spaces
			return false;
		else
			return true;
	}
}

//This fucntion is used to check dates in the format
//of dd/mm/yyyy
function check_date_dmy(str){
	if(!check_str(str)){
		return false;
	}
	if(str.length>10 || str.length<8){
		return false;
	}else{
		var day_str=str.substring(0,str.indexOf("/"));
		var month_str=str.substring(str.indexOf("/")+1,str.lastIndexOf("/"));
		var year_str=str.substring(str.lastIndexOf("/")+1,str.length);
		
		if(isNaN(day_str)||isNaN(month_str)||isNaN(year_str)){
			return false;
		}
		
		if((day_str>31||day_str<1)||(month_str>12||month_str<1)){
			return false;
		}else{
			if(month_str==4||month_str==6||month_str==9||month_str==11){
				if(day_str>30){
					return false;
				}
			}else if(month_str==2){
				if (((year_str % 4 == 0) && !(year_str % 100 == 0))|| (year_str % 400 == 0)){
					if(day_str>29){
						return false;
					}
				}else{
					if(day_str>28){
						return false;
					}
				}
			}
		}
	}
	return true;
}

//This fucntion is used to check dates in the format
//of mm/dd/yyyy
function check_date_mdy(str){
	new_str=str.substring(str.indexOf("/")+1,str.lastIndexOf("/"))+"/"+str.substring(0,str.indexOf("/"))+"/"+str.substring(str.lastIndexOf("/")+1,str.length);
	if(!check_date_dmy(new_str)){
		return false;
	}else{
		return true;
	}
}
 
//This function is used to check if the email is in correct format
function check_email(str){
	if(!check_str(str)){
		return false;
	}
	if((str.indexOf('@',0)==-1)||(str.indexOf('.',0)==-1)){
		return false;
	}else{
		return true;
	}
}

//This function is used to check if from date is less or more than to date
function check_date_from_to(date_from,date_to){
	var day_str_from=date_from.substring(0,date_from.indexOf("/"));
	var month_str_from=date_from.substring(date_from.indexOf("/")+1,date_from.lastIndexOf("/"));
	var year_str_from=date_from.substring(date_from.lastIndexOf("/")+1,date_from.length);
	
	var day_str_to=date_to.substring(0,date_to.indexOf("/"));
	var month_str_to=date_to.substring(date_to.indexOf("/")+1,date_to.lastIndexOf("/"));
	var year_str_to=date_to.substring(date_to.lastIndexOf("/")+1,date_to.length);

	day_str_from=parseInt(day_str_from);
	month_str_from=parseInt(month_str_from);
	year_str_from=parseInt(year_str_from);

	day_str_to=parseInt(day_str_to);
	month_str_to=parseInt(month_str_to);
	year_str_to=parseInt(year_str_to);

	if(year_str_from < year_str_to){
		return true;
	}
	if(year_str_from > year_str_to){
		return false;
	}
	if(month_str_from < month_str_to){
		return true;
	}
	if(month_str_from > month_str_to){
		return false;
	}
	if(day_str_from > day_str_to){
		return false;
	}
	return true;
}

/*
***************************************************************************
***************************************************************************
***************************************************************************
*/