var constraints = { "required" : function(value, params){ if(typeof(value) == 'string'){ if(value.match(/^$/) == null){ return true; } else{ return false; } }else{ return false; } }, "match" : function(value, params){ var regex = new RegExp(params.shift()); if(typeof(value) == 'string'){ if(regex.test(value)){ return true; } else{ return false; } }else{ return false; } }, "not_match" : function(value, params){ var regex = new RegExp(params.shift()); if(typeof(value) == 'string'){ if(!regex.test(value)){ return true; } else{ return false; } }else{ return false; } }, "date" : function(value, params){ var format = params.shift(); if(value == ''){ return true; } if(value.length == 10){ if(value.match(/^(19\d{2}|20[0-2]\d)\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2]\d|3[0-1])$/)){ return true; } else return false; } else if(value.length == 7){ if(value.match(/^(19\d{2}|20[0-2]\d)\-(0[1-9]|1[0-2])$/)){ return true; } else return false; } else if(value.length == 4){ if(value.match(/^(19\d{2}|20[0-2]\d)$/)){ return true; } else return false; } else{ return false; } }, "notfuture" : function(value, params){ var format = params.shift(); if(value == ''){ return true; } if(value.length == 7){ value = value + "-01"; } else if(value.length == 4){ value = value + "-01-01"; } var today = new Date(); var date = value.split('-'); date = new Date(date[0], parseInt(date[1]-1), date[2]); if(date < today){ return true; } else{ return false; } }, "future" : function(value, params){ var format = params.shift(); if(value == ''){ return true; } if(value.length == 7){ value = value + "-01"; } else if(value.length == 4){ value = value + "-01-01"; } var today = new Date(); var date = value.split('-'); date = new Date(date[0], parseInt(date[1]-1), date[2]); if(date > today){ return true; } else{ return false; } }, "older" : function(value, params){ var format = params.shift(); if(value == ''){ return true; } if(value.length == 7){ value = value + "-01"; } else if(value.length == 4){ value = value + "-01-01"; } var year_difference = parseInt(params.shift(), 10); var compare_date = new Date(); compare_date.setFullYear(compare_date.getFullYear() - year_difference); var date = value.split('-'); date = new Date(date[0], parseInt(date[1]-1), date[2]); if(date < compare_date){ return true; } else{ return false; } }, "younger" : function(value, params){ var format = params.shift(); if(value == ''){ return true; } if(value.length == 7){ value = value + "-01"; } else if(value.length == 4){ value = value + "-01-01"; } var year_difference = parseInt(params.shift(), 10); var compare_date = new Date(); compare_date.setFullYear(compare_date.getFullYear() - year_difference); var date = value.split('-'); date = new Date(date[0], parseInt(date[1]-1), date[2]); if(date > compare_date){ return true; } else{ return false; } }, "not_same_person" : function(value, params){ var person1 = params.shift(); var person2 = params.shift(); if ($('#'+person1+"FirstName_id").val() == $('#'+person2+"FirstName_id").val() && $('#'+person1+"LastName_id").val() == $('#'+person2+"LastName_id").val() && $('#'+person1+"BirthDate_id").val() == $('#'+person2+"BirthDate_id").val()) { return false; } else { return true; } }, "luhn" : function(value, params){ var SIN = value.split(''); var sum = 0; var index = 0; for( var i = SIN.length - 1; i >= 0; i--){ var num = parseInt(SIN[i],10); if(index%2){ if((num = num*2) >= 10){ num = 1 + (num%10) }; } sum += num; index++; } if( sum%10 == 0)return true; else return false; }, "SIN" : function(value, params){ if(!value.match(/^([1-79]\d{8})?$/) || !constraints.luhn(value, [""]) ){ return false; }else { return true; } }, "postal_code" : function(value, params){ var province_field = params.shift(); var province = $("#"+province_field+"_id").value(); if(!province){ return true; } var postal_code = value.split(''); if(postal_code[0].match(/[DFIOQUWZ]/i)) return false; if(postal_code[2].match(/[DFIOQU]/i)) return false; if(postal_code[4].match(/[DFIOQU]/i)) return false; if(province == 'NL' && postal_code[0] != 'A') return false; if(province == 'NS' && postal_code[0] != 'B') return false; if(province == 'PE' && postal_code[0] != 'C') return false; if(province == 'NB' && postal_code[0] != 'E') return false; if(province == 'QC' && !postal_code[0].match(/[GHJ]/i)) return false; if(province == 'ON' && !postal_code[0].match(/[KLMNP]/i)) return false; if(province == 'MB' && postal_code[0] != 'R') return false; if(province == 'SK' && postal_code[0] != 'S') return false; if(province == 'AB' && postal_code[0] != 'T') return false; if(province == 'BC' && postal_code[0] != 'V') return false; if(province.match(/(NT|NU)/) && postal_code[0] != 'X') return false; if(province == 'YT' && postal_code[0] != 'Y') return false; return true; }, "client_card" : function(value, params){ if (value == "") return true; //this field is optional; if(constraints.match(value,["^\\d{16}$"])){ return constraints.luhn(value, ""); }else if(!constraints.match(value,["^\\d{12}$"])){return false;} var value = "4519" + value; return constraints.luhn(value, ""); }, "membership" : function(value, params) { if(value == '') return true; var pid = params.shift(); var pid_value = $('input[name="'+pid+'"]').val(); if(pid_value == "westjet" || pid_value == "westjet_world") { return constraints.match(value,["^\\d{9}$"]); } else if (pid_value == "cp_platinum") { return constraints.match(value,["^\\d{10}$"]); } else if (pid_value == "ba_platinum") { return constraints.match(value,["^\\d{8}$"]); } else{ return true; } }, "birthday_rules" : function(value, params) { var province_field = params.shift(); var province = $("#"+province_field+"_id").value(); var min_age = 18; if (province.match("^BC$|^NB$|^NS$|^NL$|^NT$|^NU$|^YT$")) { min_age = 19; } return constraints.older(value,["ymd",min_age]); }, "birthday_rules_postal_code" : function(value, params) { var postal_code_field = params.shift(); var postal_code = $("#"+postal_code_field+"_id").value(); var min_age = 18; if (postal_code.match("^[ABEVXYabevxy]")) { min_age = 19; } return constraints.older(value,["ymd",min_age]); }, "identification" : function(value, params){ var field_type = params.shift(); var dependencies = { // "":{ "num":false, "country":false, "prov":false, "state":false, "expiry":false }, //ATM/CLient Card-CPA Member bering client name and signature "22":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //Cdn Birth Certificate Province/Territory "41":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //Cdn Citizenship Certificate "42":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Cdn Ctzn/Immigration # 1000 (Pre Jun 28/02) "43":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Cdn Ctzm/Immigration #1442 "44":{ "num":true, "country":true, "prov":false, "state":false, "expiry":true }, //Cdn Ctzm/Immigration #5292 "45":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Cdn Drivers License Province/Territory "2":{ "num":true, "country":true, "prov":true, "state":false, "expiry":true }, //Cdn Indian Status Certificate "15":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Cdn Naturalization Certificate "16":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Cdn Old-Age Security Card "17":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Cdn Passport "18":{ "num":true, "country":true, "prov":false, "state":false, "expiry":true }, //Cdn Permanent Resident Card "19":{ "num":true, "country":true, "prov":false, "state":false, "expiry":true }, //Cdn SIN-Social Insurance Number "1":{ "num":true, "country":true, "prov":false, "state":false, "expiry":false }, //Client Card CNIB (Photo/Signed) "23":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //Dept. of Community Gov. and Trans. Of the Territory of Nunavut "50":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //Diplomatic ID Issued by Foreign Affairs "56":{ "num":true, "country":true, "prov":false, "state":false, "expiry":true }, //Foreign Passport "29":{ "num":true, "country":true, "prov":false, "state":false, "expiry":true }, //Health Ins Card/Prov/Terr (Where allowed) "47":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-Alberta Registries (Photo/signed) "48":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-Employee of Known Company (Photo) "26":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-Insur Corp BC Card (Photo/signed) "49":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-New Brunswick Svc (Photo/signed) "10":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-Nfld/Labr-Svcs/Land (Photo/signed) "11":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-NS Svcs/Mun Relat'ns (Photo/signed) "12":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-NWT-Dept Trans'n (Photo/signed) "13":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-PEI Tran/Pbc Works (Photo/signed) "14":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //ID Card-Sask Gov't Insurance (Photo/signed) "20":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //Provincially Issued ID Card with Photo (in lieu of Driver's License) "57":{ "num":true, "country":true, "prov":true, "state":false, "expiry":true }, //Senior Citizens Privilege Card "30":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //Student Card-Cdn Univ/College (Photo) "31":{ "num":true, "country":true, "prov":true, "state":false, "expiry":false }, //US Driver's License "54":{ "num":true, "country":true, "prov":false, "state":true, "expiry":true }, //US Birth Certificate "55":{ "num":true, "country":true, "prov":false, "state":true, "expiry":false } }; return dependencies[value][field_type]; }, "or_match_on_fields" : function(value, params){ var regex = new RegExp(params.shift()); for( var i = 0; i < params.length; i++){ var value = $(":input[name="+params[i]+"]").val(); if(regex.test(value)){ return true; } } return false; }, "not_same_value" : function(value, params){ var field_name = params.shift(); var field_value = $("#"+field_name+"_id").val(); if(value == field_value){ return false; } else{ return true; } }, "same_value" : function(value, params){ var field_name = params.shift(); var field_value = $("#"+field_name+"_id").val(); return (value == field_value); }, "options" : function(value, params){ if (value == "") return true; var result = false; var options_key = params.shift(); var options_json = options[options_key][$('#lang_id').val()]; for (var i in options_json) { var obj = options_json[i]; for (var j in obj) { if (j == value){ result = true; } } } return result; }, "initial" : function(value, params){ return (value.match(/^([A-Za-z]{0,1})?$/))? true : false; }, "email" : function(value, params){ return (value.match(/^([\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3})?$/))? true : false; }, "name" : function(value, params){ return (value.match(/^([A-Za-z\-\'\.\s]{0,25})?$/))? true : false; }, "streetname" : function(value, params){ return (value.match(/^([A-Za-z\-\'\.\s]{0,25})?$/))? true : false; }, "city" : function(value, params){ return (value.match(/^([A-Za-z\-\'\.\s]{0,25})?$/))? true : false; }, "postalcode" : function(value, params){ return (value.match(/^([A-Za-z][\d][A-Za-z][\d][A-Za-z][\d])?$/))? true : false; }, "phone" : function(value, params){ return (value.match(/^([2-9]\d{2}\-\d{3}\-?\d{4})?$/))? true : false; } };