 //-----------------------------------------Messages------------------------------------------------------------------------
  str_ErrMess_ErrorFormat="Error format: '%s'";
  str_ErrMess_FaultFiledLen="%s should be shorter then %s symbols";
  str_ErrMess_FieldIsRequired="%s is required";
  str_ErrMess_FieldIsIdentifier="%s is not a identifier";
  str_ErrMess_FieldIsNumerical="%s is not a number";
  str_ErrMess_FieldIsInteger="%s is not integer";
  str_ErrMess_FieldIsDate="%s should be MM/DD/YY[YY]";
  str_ErrMess_FieldIsTime="%s should be HH:MM[:SS]";
  str_ErrMess_FieldIsEMail="%s is not a valid e-mail address";
  str_ErrMess_FieldIsFile="%s filename is not latin-coded";
  str_ErrMess_ErrorFileFormat="%s error file format '%s'";  
  str_ErrMess_FieldIsImageWithSize="%s dimensions should be %sx%s or less";
  str_ErrMess_FieldIsImageWithWidthSize="%s width must be less than %s";
  str_ErrMess_FieldIsImageWithHeightSize="%s height must be less than  %s";
  str_ErrMess_FieldIsImageWithSizeEq="%s is not an image (%sx%s)";
  str_ErrMess_FieldIsImageWithWidthSizeEq="%s must be %s pixels wide";
  str_ErrMess_FieldIsImageWithHeightSizEqe="%s must be %s pixels tall";  
  str_ErrMess_ErrorFileName="Filename '%s' is not latin coded";
  str_ErrMess_FieldIsPasswordConfirm="%s confirmation failed";
  str_ErrMess_FieldIsPassword="%s should be 6-10 alphanumerical characters.";
  str_ErrMess_FieldIsZip="%s invalid zip code";
  str_ErrMess_FieldIsPhone="%s invalid phone. It should be (###)###-####";
  str_ErrMess_FieldIsCardNum="%s invalid card number";
 //-----------------------------------------/Messages------------------------------------------------------------------------

 function  FormatStr(str,params) {
  fl=0;
  result="";
  currParam=0;
  for (i=0; i<str.length;i++) 
    switch (str.charAt(i)) {
     case "%": 
      if (fl == 0)
 	   fl=1;
	  else {
	   result=result+"%";
	   fl=0;
	  } 
	  break;
	 case "s": 
	  if (fl==1) 
	   if (params[currParam]!=undefined) {
	    result=result+params[currParam];
		currParam++;
		fl=0;
	   }	
	   else return "";
	  else result=result+str.charAt(i);
	  break;
	 default:
	  if (fl==1)
	   result=result+"%"+str.charAt(i);
	  else result=result+str.charAt(i);
    }
   return result;	
 }
  
 function Trim(s) {
  while (s.substring(0,1)==" ")
   s=s.substring(1,s.length);
  
  while (s.substring(s.length-1,s.length)==" ")
   s=s.substring(0,s.length-1);
  
  return s;
 }
 

  var ReserveFormats= Array("doc","rtf","xls","pdf","gif","jpg","txt","cvs","zip","rar","tar"); //!!!!!!!!!!!! изменить!!!!!!!!!
  var FileIconPath="img/file_icons/";   //!!!!!!!!!!!! изменить!!!!!!!!!
  
  function ExtractFileName(FullFileName) {
   s="";
   for(i=0; i<FullFileName.length;i++) {
	if((FullFileName.charAt(i)=="/")||(FullFileName.charAt(i)=="\\"))
     s="";
    else 
	 s=s+FullFileName.charAt(i);
   }
   return (s);
  }

  function ExtractFileExt(FullFileName) {
   s="";
   for(i=0; i<FullFileName.length;i++) {
	if(FullFileName.charAt(i)==".")
     s="";
    else 
	 s=s+FullFileName.charAt(i);
   }
   return (s.toLowerCase(s));
  }
  
  function GetFormatIcon(FileName) {
   ext=ExtractFileExt(FileName);
   for(i=0; i < ReserveFormats.length; i++)
    if (ext == ReserveFormats[i]) 
	 return(FileIconPath+ReserveFormats[i]+".gif");
   return(FileIconPath+"df.gif");	 
  }
  
  function ShowFullImage(value) {
    w= window.open("","","cannelmode,resizable, status,width=900,height=450,scrollbars");
    ell=w.document.createElement("img");
    ell.src=value;
    w.document.body.appendChild(ell);	  
    w.focus; 
  }
  
  function changeImage(id,value,maxW,maxH,alt,id_link,value2,id_enlarge_div){
   fl_IsEnlarge = false;
   if (value != "")
    if (CheckFileName_WithAlert(value)) {  
     curr_image=document.getElementById(id);
     ext=ExtractFileExt(value);
     if ((ext == 'jpeg')||(ext == 'jpg')||(ext == 'gif')/*||(ext == 'png')||(ext == 'bmp')*/) {	  
      if ((maxW!=null)||(maxH!=null)) {	 
       load_image = new Image();
       load_image.src = value;	  
       if ((load_image.width==0)||(load_image.Height==0)) {
	    fl_IsEnlarge = true;
        load_image = new Image(maxW,maxH);
        load_image.src = value;
       };
      
       wKoef=load_image.width/maxW;
       hKoef=load_image.height/maxH;
	  
       if (wKoef>hKoef) 
        Koef=wKoef;
       else Koef=hKoef;        
	    	  
       if (Koef>1) {
		fl_IsEnlarge = true;
        load_image.width=load_image.width/Koef;
        load_image.height=load_image.height/Koef;
       }       
       curr_image.width = load_image.width;
       curr_image.height = load_image.height;	  	  
      } 
      curr_image.src= value;	  
	  if (alt != null)
	   if (alt != "") 
	    curr_image.title = alt;
	   else curr_image.title = ExtractFileName(value);
	  
	  linkEll=document.getElementById(id_link);
	  edivEll = document.getElementById(id_enlarge_div);
	  if (linkEll!=null) {	   
	   linkEll.href="";	   		 
	   if (fl_IsEnlarge) {
  	    if(edivEll!=null)
		 edivEll.style.display="block";	   	   
	    linkEll.style.cursor="pointer";		
	    if ((value2!=null)&&(value2!="")) 
	     linkEll.onclick=function(){ShowFullImage(value2); return false;};
	    else linkEll.onclick=function(){ShowFullImage(value); return false;};
	   } else {
		  if(edivEll!=null)
		   edivEll.style.display="none";	   
	      linkEll.style.cursor="default"; 
		  linkEll.onclick=function(){return false;};
	     }	
	  } 
	  
      return(true);
     } else { 
	    alert(FormatStr(str_ErrMess_ErrorFormat,[ext]));
        return(false);
      }	
    } else return(false);
   else return(true);
  }  
	  
  function changeFile(id_link,href,id_image,alt) {
   if (href != "")
    if (CheckFileName_WithAlert(href)) {
     curr_img = document.getElementById(id_image);
     curr_img.src = GetFormatIcon(href);
     curr_img.title = ExtractFileName(alt);
     curr_link = document.getElementById(id_link);
     curr_link.href = href;  
     return (true);
    } else return (false); 
   else return (true);	  	
  } 

// Function for checking form values
 
 var ident_filter  = /^(\w+)$/;
 var num_filter  = /^([0-9]+)(\.[0-9]+)?$/;
 var int_num_filter  = /^([0-9]+)$/;
 var date_filter  = /^([0-9]{2})\/([0-9]{2})\/([0-9]{2}|[0-9]{4})$/;
 var time_filter  = /^([0-9]{2}:[0-9]{2}(:[0-9]{2})?)$/;
 var email_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
 var file_filter = /^(([\s\w_\.\-\(\)\/\\\:#%])+)$/;
 var password_filter = /[\w_]{6,10}/;
 var zip_filter = /^([0-9]{5})$/; 
 var phone_filter = /^(\(?)([0-9]{3})(\)?)(\s*)([0-9]{3})(-?)(\s*)([0-9]{4})$/; 
 var card_number_filter = /^([0-9]+)(-[0-9]+)*$/; 
 
 function CheckStrField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
	return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
 
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));

  return("");	
 }  

 function CheckIdentField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
	return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
 
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
	
  if (value != "") 
   if (!ident_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsIdentifier,[field_name])); 
	
  return("");	
 }  
  
 function CheckNumField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));  
	
  if (value != "") 
   if (!num_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsNumerical,[field_name]));
  return("");	 
 }  

 function CheckIntegerField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
  if (value != "") 
   if (!int_num_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsInteger,[field_name]));
  return("");	 
 }  
 
 function CheckDateField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
 
  if (value != "") 
   if (!date_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsDate,[field_name]));
   
  return("");	 
 }  

 function CheckTimeField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
  
  if (value != "")  
   if (!time_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsTime,[field_name])); 
   
  return("");	 
 }  
 
 function CheckEMailField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));

  if (value != "")  
   if (!email_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsEMail,[field_name])); 
   
  return("");	 
 }  
 
 function CheckURLField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
 
  return("");	 
 }  
 
   function CheckZipField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
/*  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));*/
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
  
  if (value != "") 
   if (!zip_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsZip,[field_name]));
   
  return("");	 
 }  
 
 function CheckPhoneField (field_name, value, required) {  
  field_name= field_name.toUpperCase(field_name);
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
  
  if (value != "") 
   if (!phone_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsPhone,[field_name]));
   
  return("");	 
 }  
 
 function CheckCardNumField (field_name, value, maxLen, required) {  
  field_name= field_name.toUpperCase(field_name);
  
  if (maxLen != "")
   if (maxLen < value.length) 
    return (FormatStr(str_ErrMess_FaultFiledLen,[field_name,maxLen]));
   	
  if (required)
   if (Trim(value) == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
  
  if (value != "") 
   if (!card_number_filter.test(value))
    return (FormatStr(str_ErrMess_FieldIsCardNum,[field_name]));
   
  return("");	 
 }  
 
 function AddHttpToURLField (value) { 
  if (value!="")    
   if (value.length>6) {
     s=value.substring(0,7);
     s=s.toUpperCase("");	
     if(s!="HTTP://")
      value = "http://" + value;
   } else	value = "http://" + value;
  return value;
 }		

 
 function CheckFileField (field_name, value, required, file_formats) { 
  field_name= field_name.toUpperCase(field_name);
  short_value = ExtractFileName(value);
  if (required)
   if (short_value == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));

  if (short_value != "")
   if (!file_filter.test(short_value))
    return (FormatStr(str_ErrMess_FieldIsFile,[field_name]));
 
  if ((file_formats!=null)&&(short_value != "")) {
   ext = ExtractFileExt(short_value);
   fl = true;
   for (i=0; i < file_formats.length; i++)
    if (file_formats[i].toLowerCase(file_formats[i]) == ext) {
	 fl = false;
	 break;
	}
   if (fl) 
   	return (FormatStr(str_ErrMess_ErrorFileFormat,[field_name,ext]));    
  }  
  return("");	 
 }  

 function CheckImageField (field_name, value, required, width, height, equal) {      
  field_name= field_name.toUpperCase(field_name);
  if (required)
   if (ExtractFileName(value) == "spacer.gif") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
 
  if (!file_filter.test(ExtractFileName(value)))
   return (FormatStr(str_ErrMess_FieldIsFile,[field_name]));

  is_err=false;
  res_mess="";
  if (ExtractFileName(value) != "spacer.gif") {
   load_image = new Image();
   load_image.src = value;    
   if ((width!=null)&&(width!="")) {
    if (equal) {
     res_mess=FormatStr(str_ErrMess_FieldIsImageWithWidthSizeEq,[field_name,width]);
	 if (load_image.width != width) 
	  is_err=true;	 	  	  
	} else {
       res_mess=FormatStr(str_ErrMess_FieldIsImageWithWidthSize,[field_name,width]);
	   if (load_image.width > width) 
	    is_err=true;	 	  	  	
	  }
   }   
   if ((height!=null)&&(height!="")) {
    if (equal) {
     if (res_mess=="")
	  res_mess=FormatStr(str_ErrMess_FieldIsImageWithHeightSizeEq,[field_name,height]);
	 else res_mess=FormatStr(str_ErrMess_FieldIsImageWithSizeEq,[field_name,width,height]);
     if (load_image.height != height)
	  is_err=true;	 
	} else {
       if (res_mess=="")
	    res_mess=FormatStr(str_ErrMess_FieldIsImageWithHeightSize,[field_name,height]);
	   else res_mess=FormatStr(str_ErrMess_FieldIsImageWithSize,[field_name,width,height]);	
       if (load_image.height > height)
	    is_err=true;	   
	  }	  
   }   
  }   
  if (is_err) 
   return (res_mess);
  else	return "";  
 }
 
 function CheckFileName_WithAlert (value) { 
  if (!file_filter.test(value)) {   
   alert(FormatStr(str_ErrMess_ErrorFileName,[value]));
   return (false);
  }
  return(true); 
 } 
 
 function CheckPasswordField (field_name, value1, value2, required) {  
  field_name= field_name.toUpperCase(field_name);
  
  if (value1 != value2)
   return (FormatStr(str_ErrMess_FieldIsPasswordConfirm,[field_name]));
  
  if (required)
   if (value1 == "") 
    return (FormatStr(str_ErrMess_FieldIsRequired,[field_name]));
 
  if (value1 != "")   
   if (!password_filter.test(value1)) 
    return (FormatStr(str_ErrMess_FieldIsPassword,[field_name]));
	
  return("");	 
 }  
 

  function FCKeditor_OnSubmit(name) {
   document.getElementsByName(name)[0].value = FCKeditorAPI.GetInstance(name).GetXHTML(true);    
  } 
