/*
 * 730-2009
 * Copyright(c) 2006, Andrea Bruglia, Davide Stefanini.
 *
 * This code is licensed under BSD license. Use it as you wish,
 * but keep this copyright intact.
 */
/**
 * @author Davide Stefanini, Andrea Bruglia
 */
Ext.ns('Ext.ux');
Ext.ns('Ext.ux.form');

Ext.ux.form.VTypes = {};

Ext.ux.form.VTypes["hostnameVal1"] = /^[a-zA-Z][-.a-zA-Z0-9]{0,254}$/;
Ext.ux.form.VTypes["hostnameVal2"] = /^[a-zA-Z]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}([.][a-zA-Z]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9]){0,1}){0,}$/;
Ext.ux.form.VTypes["ipVal"] = /^([1-9][0-9]{0,1}|1[013-9][0-9]|12[0-689]|2[01][0-9]|22[0-3])([.]([1-9]{0,1}[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){2}[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-4])$/;
Ext.ux.form.VTypes["netmaskVal"] = /(^(128|192|224|24[08]|25[245])\.0\.0\.0$)|(^255\.(0|128|192|224|24[08]|25[245])\.0\.0$)|(^255\.255\.(0|128|192|224|24[08]|25[245])\.0$)|(^255\.255\.255\.(0|128|192|224|24[08]|252)$)/;
Ext.ux.form.VTypes["portVal"] = /^(0|[1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
Ext.ux.form.VTypes["multicastVal"] = /^((22[5-9]|23[0-9])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3})|(224[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])){2})|(224[.]0[.]([1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])([.](0|[1-9][0-9]{0,1}|1[0-9]{2}|2[0-4][0-9]|25[0-5])))$/;
Ext.ux.form.VTypes["usernameVal"] = /^[a-zA-Z][-_.a-zA-Z0-9]{0,30}$/;
Ext.ux.form.VTypes["passwordVal1"] = /^.{6,31}$/;
Ext.ux.form.VTypes["passwordVal2"] = /[^a-zA-Z].*[^a-zA-Z]/;
Ext.ux.form.VTypes["hostname"]=function(v){
 if(!Ext.ux.form.VTypes["hostnameVal1"].test(v)){
  Ext.ux.form.VTypes["hostnameText"]="Must begin with a letter and not exceed 255 characters"
  return false;
 }
 Ext.ux.form.VTypes["hostnameText"]="L[.L][.L][.L][...] where L begins with a letter, ends with a letter or number, and does not exceed 63 characters";
 return Ext.ux.form.VTypes["hostnameVal2"].test(v);
}
Ext.ux.form.VTypes["hostnameText"]="Invalid Hostname"
Ext.ux.form.VTypes["hostnameMask"]=/[-.a-zA-Z0-9]/;
Ext.ux.form.VTypes["ip"]=function(v){
 return Ext.ux.form.VTypes["ipVal"].test(v);
}
Ext.ux.form.VTypes["ipText"]="1.0.0.1 - 223.255.255.254 excluding 127.x.x.x"
Ext.ux.form.VTypes["ipMask"]=/[.0-9]/;
Ext.ux.form.VTypes["netmask"]=function(v){
 return Ext.ux.form.VTypes["netmaskVal"].test(v);
}
Ext.ux.form.VTypes["netmaskText"]="128.0.0.0 - 255.255.255.252"
Ext.ux.form.VTypes["netmaskMask"]=/[.0-9]/;
Ext.ux.form.VTypes["port"]=function(v){
 return Ext.ux.form.VTypes["portVal"].test(v);
}
Ext.ux.form.VTypes["portText"]="0 - 65535"
Ext.ux.form.VTypes["portMask"]=/[0-9]/;
Ext.ux.form.VTypes["multicast"]=function(v){
 return Ext.ux.form.VTypes["multicastVal"].test(v);
}
Ext.ux.form.VTypes["multicastText"]="224.0.1.0 - 239.255.255.255"
Ext.ux.form.VTypes["multicastMask"]=/[.0-9]/;
Ext.ux.form.VTypes["username"]=function(v){
 return Ext.ux.form.VTypes["usernameVal"].test(v);
}
Ext.ux.form.VTypes["usernameText"]="Username must begin with a letter and cannot exceed 255 characters"
Ext.ux.form.VTypes["usernameMask"]=/[-_.a-zA-Z0-9]/;
Ext.ux.form.VTypes["password"]=function(v){
 if(!Ext.ux.form.VTypes["passwordVal1"].test(v)){
  Ext.ux.form.VTypes["passwordText"]="Password length must be 6 to 31 characters long";
  return false;
 }
 Ext.ux.form.VTypes["passwordText"]="Password must include atleast 2 numbers or symbols";
 return Ext.ux.form.VTypes["passwordVal2"].test(v);
}
Ext.ux.form.VTypes["passwordText"]="Invalid Password"
Ext.ux.form.VTypes["passwordMask"]=/./;
Ext.ux.form.VTypes["time"] = /^([1-9]|1[0-9]):([0-5][0-9])(\s[a|p]m)$/i;
Ext.ux.form.VTypes["timeMask"] = /[\d\s:amp]/i;
Ext.ux.form.VTypes["timeText"] = 'Not a valid time.  Must be in the format "12:34 PM".';
Ext.apply(Ext.ux.form.VTypes, {
		'numeric': function(){
			
				/*****************************************************************
				DESCRIPTION: Validates that a string contains only valid numbers.
				PARAMETERS:
				   strValue - String to be tested for validity
				RETURNS:
				   True if valid, otherwise false.
				******************************************************************/
				  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
				  return function(strValue){
					  //check for numeric characters
					  return objRegExp.test(strValue);
				  }
		}(),
		'numericText': 'Only numbers are allowed'
	});
Ext.ux.form.VTypes["directory"]=function(v){
 return /^(([a-zA-Z]:){0,1}(\\|\/){1})(([-_.a-zA-Z0-9\\\/ ]+)(\\|\/){1})+$/.test(v);
} 
Ext.ux.form.VTypes["directoryText"]="This must be a valid directory location."
Ext.ux.form.VTypes["directoryMask"]=/[-_.a-zA-Z0-9\\\/: ]/;
Ext.apply(Ext.ux.form.VTypes, {
	'ssn': function(){
        var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
        return function(v){
            return re.test(v);
        }
    }(),
    'ssnText' : 'SSN format: xxx-xx-xxxx'
})
Ext.apply(Ext.ux.form.VTypes, {
	'imagefile': function(){
        return function(v){
            v = v.replace(/^\s|\s$/g, ""); //trims string
           if (v.match(/([^\/\\]+)\.(bmp|gif|png|jpg|jpeg)$/i) )
               return true;
           else
               return false;
        }
    }(),
    'imagefileText' : 'Must be a valid image file: GIF, JPG, BMP, PNG'
})
Ext.ux.form.VTypes["cfiscMask"] = /^[A-Za-z]{6}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{3}[A -Za-z]{1}$/;
Ext.ux.form.VTypes["cfiscText"] = "Codice fiscale non corretto";
Ext.ux.form.VTypes["alphanumMask"] = /[a-z0-9]/i;
Ext.ux.form.VTypes["numericMask"] = /[0-9]/;
Ext.ux.form.VTypes["nameMask"] = /[a-z \']/i;
Ext.ux.form.VTypes["sessoMask"] = /[MF]/i;
Ext.ux.form.VTypes["capMask"] = /^\d{5}$/;
Ext.ux.form.VTypes["nCivico"] = /[0-9A-Z \/]/i;

Ext.form.VTypes["dollar"] = /^[\$]?[\d]*(.[\d]{2})?$/;
Ext.form.VTypes["dollarMask"] = /[\d\$.]/;
Ext.form.VTypes["dollarText"] = 'Not a valid dollar amount.  Must be in the format "$123.45" ($ symbol and cents optional).';
