/* FORM FIELDS SUMBIT CHECKER
© 2006 Analytic Soft, www.analytic.com.ua
--------------------------------------------------------------------------------------
Description

STEP1. First form fileds should be registered.
registerFieldChecking([field_name], [check_type], [message], [param1], [param2])
!!!One field CAN be registered twice or more with different value of [check_type]

STEP2. When user submits form we call testFormBeforeSubmit()
--------------------------------------------------------------------------------------
FUNCTION registerFieldChecking([field_name], [check_type], [message], [param1], [param2])
PARAMETERS:
1. [field_name]: input, textarea, checkbox, radiobutton tag attribute NAME
2. [check_type]:
'NOT_EMPTY'  - checks field for not empty.
'MIN_LENGTH' - checks field for minimal length. [param1] is the minimal allowed length.
'MAX_LENGTH' - checks field for minimal length. [param1] is the maximal allowed length.
'EMAIL'      - checks field to be email.
'REGEXP'     - checks field with regexp.
3. [message] - message for alert.
4. [param2]  - reserved.

EXAMPLES:
      registerFieldChecking('NameTxt', 'NOT_EMPTY','??? ??? ?? ?????? ?????','','');
	  registerFieldChecking('NameTxt', 'MIN_LENGTH','??? ???? ?? ???????? ???????? ??? ???????? ??????','3','');

*/
     var checkedFormFields= new Array();
	 function registerFieldChecking(field_name, check_type, message, param1, param2)
	 { 
	  if (checkedFormFields[field_name]==null) {checkedFormFields[field_name] = new Array();}
	  checkedFormFields[field_name][check_type] = new Array(); 
	  checkedFormFields[field_name][check_type]["field_name"]	=field_name;
	  checkedFormFields[field_name][check_type]["message"]		=message;
	  checkedFormFields[field_name][check_type]["param1"]		=param1;
	  checkedFormFields[field_name][check_type]["param2"]		=param2;
	  checkedFormFields[field_name][check_type]["object"]		=document.getElementsByName(field_name)[0];
	  checkedFormFields[field_name][check_type]["param2"]		=param2;
	  
	 }
/*
--------------------------------------------------------------------------------------
FUNCTION testFormBeforeSubmit()
Checks all registered fields

*/ 
	 function testFormBeforeSubmit()
	 { 
	  var field; 
	  for (field in checkedFormFields)
	  {
	    if (!checkField(field)) return false;
	  }
	  return true;
	 }
/*
--------------------------------------------------------------------------------------
FUNCTION checkField([field_name])
Checks one field for field with tag attribute NAME=[field_name].
Fuction sets focus to tested field

*/ 

	 function checkField(field_name)
	 {  
	  var check_type;
	  for (check_type in checkedFormFields[field_name])
	  {
	    if (checkedFormFields[field_name][check_type]["object"]) checkedFormFields[field_name][check_type]["object"].focus();
		switch (check_type)
		{
		 case "NOT_EMPTY":
			if (checkedFormFields[field_name][check_type]["object"].value=='') 
			{
			 alert(checkedFormFields[field_name][check_type]["message"]);
			 return false;
			}
		  break;

		 case "MIN_LENGTH":
			if (checkedFormFields[field_name][check_type]["object"].value.length<checkedFormFields[field_name][check_type]["param1"]) 
			{
			 alert(checkedFormFields[field_name][check_type]["message"]);
			 return false;
			}
		  break;
		 case "MAX_LENGTH":
			if (checkedFormFields[field_name][check_type]["object"].value.length>checkedFormFields[field_name][check_type]["param1"]) 
			{
			 alert(checkedFormFields[field_name][check_type]["message"]);
			 return false;
			}
		  break;
		 case "EMAIL":
			var email_exp=new RegExp("^[a-z0-9_-]{1,20}@(([a-z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$"); 
			if (!checkedFormFields[field_name][check_type]["object"].value.match(email_exp)) 
			{
			 alert(checkedFormFields[field_name][check_type]["message"]);
			 return false;
			}
		  break;
		 case "REGEXP":
			var email_exp=new RegExp(checkedFormFields[field_name][check_type]["param1"]); 
			if (!checkedFormFields[field_name][check_type]["object"].value.match(email_exp)) 
			{
			 alert(checkedFormFields[field_name][check_type]["message"]);
			 return false;
			}
		  break;
		 case "GROUP_NOT_EMPTY":
			var field_in_group, field_name_in_group; 
			for(field_name_in_group in checkedFormFields[field_name][check_type]["param1"])
			{ 
				field_in_group = document.getElementsByName(checkedFormFields[field_name][check_type]["param1"][field_name_in_group])[0];

				if (!field_in_group) break;
				if ((field_in_group.type.toLowerCase() == 'checkbox') && field_in_group.checked) return true; //Checked
				if ((field_in_group.type.toLowerCase() != 'checkbox') && (field_in_group.value != '')) return true; //Filled
			}
			 alert(checkedFormFields[field_name][check_type]["message"]);
			 return false;

		  break;
		}
       }
		return true;
	 }
	
function getDims(winWidth, winHeight) {
	var dims = new Object();
	if (winWidth) {
		dims.width = winWidth;
		dims.scrollbars = false;
		if (screen.width < dims.width + 70) {
			dims.width = screen.width - 70;
			dims.scrollbars = true;
		}
	}

	if (winHeight) {
		dims.height = winHeight;
		if (screen.height < dims.height + 100) {
			dims.height = screen.height - 100;
			dims.scrollbars = true;
		}
	}
	dims.heightStr = (winHeight)? ',height=' + dims.height : '';
	dims.scrollbarsStr = (dims.scrollbars)? ',scrollbars=yes' : ',scrollbars=no';
	dims.width=(dims.scrollbars)? dims.width+17 : dims.width;
	dims.widthStr = (winWidth)? ',width=' + dims.width : '';
	dims.posX = Math.round((screen.width - dims.width) / 2);
	dims.posY = Math.round((screen.height - winHeight) / 2);
	dims.posCode = (document.all)? ',left=' + dims.posX + ',top=' + dims.posY : ',screenX=' + dims.posX + ',screenY=' + dims.posY;
	return dims;
}
function popupImg(imgSrc, winName, imgWidth, imgHeight, winTitle) {

	winWidth = (imgWidth)? imgWidth +0 : null;
	winHeight = (imgHeight)? imgHeight + 0 : null;
	var dims = getDims(winWidth, winHeight);
	var popupWin = window.open('', winName, 'menubar=no,toolbar=no,resizable=yes,status=yes' + dims.scrollbarsStr + dims.widthStr + dims.heightStr + dims.posCode);
	if (popupWin) {
		popupWin.document.open();
		popupWin.document.write('<html><head><title>' + winTitle + '</title></head><body bgcolor="white" style="margin: 0px 0px; padding: 0px;">')
		popupWin.document.write('<img src="' + imgSrc + '" width="' + imgWidth + '" height="' + imgHeight + '" />')
		popupWin.document.write('</body></html>')
		popupWin.document.close();
	}
	return false;
}
