//**************************************************************** 
// You are free to copy the "form_validate" script as long as you  
// keep this copyright notice: 
// Author: Zhouyu (xiaoyu_zhou@163.com)
// 2003/08/20
//**************************************************************** 
 
// Log of changes: 



// Definition of class form_validate 
// ***************************************************************** 
 
var img = null;
function form_validate() //constructor 
{
	//constant data 
	this.splitstr = ";";
	this.emailreg = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+([\.][a-zA-Z0-9-]+)+$/;
	this.alnumreg = /^[a-zA-Z0-9]{1,}$/;
	this.datereg = /^(\d+)\-(\d+)\-(\d+)\s/;
	this.doublereg = /^[0-9]{0,}\.{0,1}[0-9]{0,}$/; 
	this.message = new Array()
	{
		this.message[1]= " can not be empty.";	
		this.message[2] = " email string format is wrong. ";
		this.message[3] = " must be numeric.";
		this.message[4] = " date format is wrong";
		this.message[5] = " length must be ";
		this.message[6] = " length must little than ";
		this.message[7] = " length must large than ";
		this.message[8] = " can not be ahead of  ";
		this.message[9] = " string format is wrong. ";		
		this.message[10] = "Item ";	
		this.message[11] = " load image failed. ";	
		this.message[12] = " image width is wrong. ";
		this.message[13] = " image height is wrong. ";								
	}

	
	//dynamic data
	this.check = true;
	this.ifmsgbox = true;		//if need alter
	this.ifsetfocus = true;	// if need set focus	
	this.thisform = document.forms[0];
	
	this.emptyarray = "";	
	this.emailarray = "";
	this.numarray = "";
	this.limitnumarray = "";
	this.doublearray = "";
	this.datearray = "";
	this.datedifferarray = "";
	this.alnumarray = "";
	this.imagearray = "";
	
	//methods 
	this.get_split_string = get_split_string;
	this.focus_item = focus_item;
	this.msgbox = msgbox;
	this.check_email = check_email;	
	this.check_empty = check_empty;	
	this.check_num = check_num;
	this.check_limitnum = check_limitnum;
	this.check_date = check_date;
	this.check_datediffer = check_datediffer;
	this.check_alnum = check_alnum;
	this.check_double = check_double;
	this.check_image = check_image;
	this.get_split_array = get_split_array;
	this.set_img_error = set_img_error;
	this.check_all = check_all;

}
	
function check_email()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var thisform = this.thisform;
	var checkarray = this.emailarray;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		
		if (itemvalue)
		{			
			if(!this.emailreg.test(itemvalue))
			{
				this.msgbox(this.message[10] + itemname +this.message[2]);
				this.focus_item(itemid);
				return false;
			}
		
		}	
	}
	return true;	
}


function check_alnum()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var thisform = this.thisform;
	var checkarray = this.alnumarray;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		
		if (itemvalue)
		{
			//if (itemvalue.lastIndexOf(" ")!=(itemvalue.length-1)) itemvalue=itemvalue+" ";
			if(!this.alnumreg.test(itemvalue))
			{
				this.msgbox(this.message[10] + itemname +this.message[9]);
				this.focus_item(itemid);
				return false;
			}
		
		}	
	}
	return true;	
}

function check_empty()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var checkarray = this.emptyarray;
	var thisform = this.thisform;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		if (itemvalue=="")
		{
			this.msgbox(this.message[10] + itemname +this.message[1]);
			this.focus_item(itemid);
			return false;
		}	
	}
	return true;	
}

function check_num()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var checkarray = this.numarray;
	var thisform = this.thisform;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		if (isNaN(itemvalue))
		{
			this.msgbox(this.message[10] + itemname +this.message[3]);
			this.focus_item(itemid);
			return false;
		}	
	}
	return true;	
}

function check_double()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var checkarray = this.doublearray;
	var thisform = this.thisform;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		if (itemvalue)
		{
			//if (itemvalue.lastIndexOf(" ")!=(itemvalue.length-1)) itemvalue=itemvalue+" ";
			if(!this.doublereg.test(itemvalue))
			{
				this.msgbox(this.message[10] + itemname +this.message[3]);
				this.focus_item(itemid);
				return false;
			}
		
		}	
	}
	return true;	
}

//type = 0 : =
//type = 1 : <
//type = 2 : >
function check_limitnum()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var checkarray = this.limitnumarray;
	var thisform = this.thisform;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		limitnum = this.get_split_string(itemstr,2);
		type =  this.get_split_string(itemstr,3);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		if (!itemvalue)
		{
			this.msgbox(this.message[10] + itemname +this.message[1]);
			this.focus_item(itemid);
			return false;			
		}
		switch (type) {
			case "0" :
				if (itemvalue.length!=limitnum)
				{
					this.msgbox(this.message[10] + itemname +this.message[5] + limitnum);
					this.focus_item(itemid);
					return false;
				}
				break;				
			case "1" :
				if (itemvalue.length>limitnum)
				{
					this.msgbox(this.message[10] + itemname +this.message[6] + limitnum);
					this.focus_item(itemid);
					return false;
				}
				break;					
			case "2" :
				if (itemvalue.length<limitnum)
				{
					this.msgbox(this.message[10] + itemname +this.message[7] + limitnum);
					this.focus_item(itemid);
					return false;
				}
				break;					
			default :
				if (itemvalue.length!=limitnum)
				{
					this.msgbox(this.message[10] + itemname +this.message[5] + limitnum);
					this.focus_item(itemid);
					return false;
				}
				break;								
				
		} 
	
	}
	return true;	
}

function check_datediffer()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var thisform = this.thisform;
	var checkarray = this.datedifferarray;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		item_start = this.get_split_string(itemstr,0);
		item_end  = this.get_split_string(itemstr,1);
		item_start_id = this.get_split_array(item_start,0,":");
		item_start_name =  this.get_split_array(item_start,1,":");
		item_end_id =  this.get_split_array(item_end,0,":");
		item_end_name = this.get_split_array(item_end,1,":");
		if (!item_start_name)
		{
			item_start_name = item_start_id;	
		}
		if (!item_end_name)
		{
			item_end_name = item_end_id;	
		}
		
		item_start_value = check_item(item_start_id);		
		item_end_value = check_item(item_end_id);
		
		if (item_start_value && item_end_value)
		{
			
			start_year = this.get_split_array(item_start_value,0,"-");
			start_month = this.get_split_array(item_start_value,1,"-");
			start_day = this.get_split_array(item_start_value,2,"-");
			
			end_year = this.get_split_array(item_end_value,0,"-");
			end_month = this.get_split_array(item_end_value,1,"-");
			end_day = this.get_split_array(item_end_value,2,"-");
			
			start = new Date (start_year, start_month, start_day);
			end = new Date (end_year, end_month, end_day);
			utc_start = start.getTime();
			utc_end = end.getTime();
			
			if(utc_end < utc_start)
			{
				this.msgbox(this.message[10] + item_end_name +this.message[8] + item_start_name);
				this.focus_item(item_end_id);
				return false;
			}
		
		}	
	}
	return true;	
}

function check_date()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var thisform = this.thisform;
	var checkarray = this.datearray;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		
		if (itemvalue)
		{	
			if (itemvalue.lastIndexOf(" ")!=(itemvalue.length-1)) itemvalue=itemvalue+" ";		
			if(!this.datereg.test(itemvalue))
			{
				this.msgbox(this.message[10] + itemname +this.message[4]);
				this.focus_item(itemid);
				return false;
			}
		
		}	
	}
	return true;	
}

function check_image()
{
	var i = 0;
	var itemstr = "";
	var itemid = "";
	var itemname = "";
	var checkarray = this.imagearray;
	var thisform = this.thisform;
	if (checkarray =="")
	{
		return true;
	}
	var check_num = checkarray.length;
	for (i=0;i<check_num;i++)
	{
		itemstr = checkarray[i];
		itemid = this.get_split_string(itemstr,0);
		itemname = this.get_split_string(itemstr,1);
		limitwidth = this.get_split_string(itemstr,2);
		limitheight =  this.get_split_string(itemstr,3);
		if (!itemname)
		{
			itemname = itemid;	
		}
		itemvalue = check_item(itemid);
		if (itemvalue)
		{
			if(img)	img.removeNode(true);
			img=document.createElement("img");
			img.style.position="absolute";
			img.style.visibility="hidden";
			img.attachEvent("onerror",this.set_img_error);			
			document.body.insertAdjacentElement("beforeend",img);
			img.src=itemvalue;		
			if (img.error)	
			{
				this.msgbox(this.message[10] + itemname +this.message[11]);
				this.focus_item(itemid);
				return false;			
			}	
			if (img.offsetWidth>limitwidth)
			{
				this.msgbox(this.message[10] + itemname +this.message[12]);
				this.focus_item(itemid);
				return false;				
			}
			if (img.offsetHeight>limitheight)
			{
				this.msgbox(this.message[10] + itemname +this.message[13]);
				this.focus_item(itemid);
				return false;				
			}
		}			
	}
	return true;		
}


function check_all()
{
	if (this.emptyarray!="")
	{
		if (!this.check_empty())	return false;	
	}
	if (this.emailarray!="")
	{
		if (!this.check_email())	return false;	
	}	
	if (this.numarray!="")
	{
		if (!this.check_num())	return false;	
	}	
	if (this.datearray!="")
	{
		if (!this.check_date())	return false;	
	}	
	if (this.datedifferarray!="")
	{
		if (!this.check_datediffer())	return false;	
	}	
	if (this.alnumarray!="")
	{
		if (!this.check_alnum())	return false;	
	}
	if (this.limitnumarray!="")
	{
		if (!this.check_limitnum())	return false;	
	}	
	if (this.doublearray!="")
	{
		if (!this.check_double())	return false;	
	}				
	if (this.imagearray!="")
	{
		if (!this.check_image())	return false;	
	}				
	return true;		
}


function focus_item(itemname)
{
	var i = 0;
	var itemvalue = "";
	thisform = document.forms[0];	
	if (thisform.elements.length==0)
	{
		return false;
	}
	if (!this.ifsetfocus)
	{
		return false;	
	}
	for (i=0;i<thisform.elements.length;i++)
	{
		if (itemname ==thisform.elements[i].name)
		{
			thisform.elements[i].select();
			thisform.elements[i].focus();
			return true;
		}
	}			
	return false;
}

function msgbox(message)
{
	if (this.ifmsgbox)
	{
		alert (message);	
	}	
}

function get_split_string(str,num)
{
	if (!str || str=="") return "";
	strarray = str.split(this.splitstr);	
	return strarray[num];
}

function get_split_array(str,num,splitstr)
{
	if (!str || str=="") return "";
	if (!splitstr || splitstr=="") splitstr=":";
	strarray = str.split(splitstr);	
	return strarray[num];
}

function set_img_error()
{
	img.error="load image failed";	
}

// Definition of class form_validate 
// ***************************************************************** 
 





// common function
function check_item(itemname)
{
	var i = 0;
	var itemvalue = "";
	thisform = document.forms[0];	
	if (thisform.elements.length==0)
	{
		return false;
	}
	for (i=0;i<thisform.elements.length;i++)
	{
		if (itemname ==thisform.elements[i].name)
		{
			itemvalue = thisform.elements[i].value;
			return itemvalue;
		}
	}			
	return false;
}

function checkradio(id1)	{
	var obj1=document.all[id1];
	var id="";
	if (obj1)	{
		var c1=obj1.length;
		if (c1 > 0)	{
			for (var i=0; i < c1; i++)	{
				if (obj1.item(i).checked)	{
					id=obj1.item(i).value;
					break;
				}
			} 
		}	else	{
			if (obj1.checked)	{
				id=obj1.value;
			}
		}
	}
	return id;
}
