// when making changes to this file: change filename and update reference in Shared.Master to prevent caching 

Validation.addAllThese([
	['validate-number', 'Please enter a valid number in this field.', function(v) {
	            v = v.replace(",","."); // MD: accepts commas as well.
				return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
			}],
	['validate-positive-number', 'Please enter a positive number in this field.', function(v) {
	            v = v.replace(",",".");
	            return Validation.get('IsEmpty').test(v) || (!isNaN(v) && v > 0);	            
            }],
    ['validate-number-max9999', 'Please enter a positive number in this field.', function(v) {
                v = v.replace(",",".");
                return Validation.get('IsEmpty').test(v) || (!isNaN(v) && v > 0 && v < 10000);	            
            }],
	['validate-date-no', 'Please use this date format: dd.mm.yyyy. For example 03.17.2006 for the 17th of March, 2006.', function(v) {
				if(Validation.get('IsEmpty').test(v)) return true;
				var regex = /^(\d{2})\.(\d{2})\.(\d{4})$/;
				if(!regex.test(v)) return false;
				var d = new Date(v.replace(regex, '$2/$1/$3'));
				return ( parseInt(RegExp.$2, 10) == (1+d.getMonth()) ) && 
							(parseInt(RegExp.$1, 10) == d.getDate()) && 
							(parseInt(RegExp.$3, 10) == d.getFullYear() );
			}],
	['validate-after-date', 'Please check that the from date is before the end date.',
                { isAfter: '' } ],
    ['validate-isin', 'Please enter a valid isin.', function(v) {
                try {
                    return Validation.get('IsEmpty').test(v) || isISINValid(v.replace(' ', '').replace('.',''));
	            }catch(e)
	            {
	                alert(e);
	                return false;
	            }	            
    }]                 
]);

    Validator.methods = {
        isAfter: function(v, elm, opt) {
            var fromrange = $(elm).ancestors()[1].select('.fromrange')[0].value; // torange
            var torange = $(elm).ancestors()[1].select('.torange')[0].value; // torange
            var regex = /^(\d{2})\.(\d{2})\.(\d{4})$/;
            // Create Date Object from Start Date            
            if (!regex.test(fromrange)) return false;
            var start = new Date(fromrange.replace(regex, '$2/$1/$3'));

            // Create Date Object from End Date          
            if (!regex.test(torange)) return false;
            var end = new Date(torange.replace(regex, '$2/$1/$3'));

            return end >= start;
        }
    }

function isISINValid(ISIN) {
    // Format check
	if (!/^[A-Z]{2}[A-Z0-9]{9}[0-9]{1}$/.test(ISIN))
		return false;
	
	// ISO 10383
	// The 2 first characters can be checked 
	var countryCode = new Array(
	"AE","AL","AM","AR","AT","AU","AZ","BA","BB","BD","BE","BG","BH","BM","BO","BR","BS","BW","BY",
	"CA","CH","CI","CL","CN","CO","CR","CV","CY","CZ","DE","DK","DO","DZ","EC","EE","EG","ES",
	"FI","FJ","FR","GB","GE","GG","GH","GR","GT","HK","HN","HR","HU",
	"ID","IE","IL","IN","IQ","IR","IS","IT","JM","JO","JP","KE","KG","KN","KR","KW","KY","KZ",
	"LB","LK","LT","LU","LV","MA","MD","ME","MG","MK","MN","MT","MU","MW","MX","MY","MZ",
	"NA","NG","NI","NL","NO","NP","NZ","OM","PA","PE","PG","PH","PK","PL","PS","PT","PY","QA",
	"RO","RS","RU","SA","SD","SE","SG","SI","SK","SV","SZ","TH","TN","TR","TT","TW","TZ",
	"UA","UG","US","UY","UZ","VE","VN","WS","XS", "ZA","ZM","ZW","ZZ"
	);	
	var isinCountryCode = ISIN.charAt(0) + ISIN.charAt(1);
	var bCountryCode=false;
	for(var i=0; i<countryCode.length; i++){
        if(countryCode[i] == isinCountryCode){
            bCountryCode=true;
           	break;
        }
    }
    
    if(!bCountryCode)
  	    return false;
  	
	// ISO 6166
	// Step 1: Alphabetic characters are assigned the numeric values A=10 ... Z=35
	var isinNumber = '';
	for(var i=0; i <11; i++) {
  	if(ISIN.charCodeAt(i)<59) {
  		isinNumber += ISIN.charCodeAt(i)-48; // '0'=0 ... '9'=9
  	    } else {
  		isinNumber += ISIN.charCodeAt(i)-55; // 'A'=10 ... 'Z'=35
  	    }
    }
    // Step 2: Double the value of alternate digits beginning with the first right-hand digit (low order), including zeros
	// Step 3: Add the individual digits composing the products obtained in step 2 and each of the unaffected digits in the original number
	//         collect odd and even and set the group for the first right-hand digit
	var oddChar = "";
	var evenChar = "";
	var group = 0;
	for(var i=0; i<isinNumber.length ; i++) {
		if(i%2) {
			evenChar += isinNumber.charAt(i);
			group = 1;
		} else {
			oddChar += isinNumber.charAt(i);
			group = 2;
		}
	}
	// double the value of the first right-hand digit		
	var total = 0;
	if(group==2) {
		// multiply oddChar by 2
		var oddChar2 = "";
		for(var i=0; i<oddChar.length ; i++) {
			oddChar2 += ((oddChar.charCodeAt(i)-48)*2);
		}
		// and add each char
		for(var i=0; i<oddChar2.length ; i++) {
			total += (oddChar2.charCodeAt(i)-48);
		}
		for(var i=0; i<evenChar.length ; i++) {
			total += (evenChar.charCodeAt(i)-48);
		}
	} else {
		// multiply evenChar by 2
		var evenChar2 = "";
		for(var i=0; i<evenChar.length ; i++) {
			evenChar2 += ((evenChar.charCodeAt(i)-48)*2);
		}
		for(var i=0; i<evenChar2.length ; i++) {
			total += (evenChar2.charCodeAt(i)-48);
		}
		for(var i=0; i<oddChar.length ; i++) {
			total += (oddChar.charCodeAt(i)-48);
		}
	}
	
		
	/* Step 4: Subtract the total obtained in step 3 from the next higher number ending in 0 [this is the equivalent of calculating
	           the "tens complements" of the low order digit (unit digit) of the total]. If the total obtained in step 3 is a number
	           ending in zero (30, 40, etc.), the check digit is 0.
	*/
	var checksum = (10 - (total%10))%10;
	// make the check
	if((ISIN.charCodeAt(11)-48)!=checksum) {
		return false;
	}
	return true;
}