function Validator(){this.validators=[];this.errors=[];this.isValid=true;this.add=function(value,testType,errorMessage,options){this.validators.push(new Array(value,testType,errorMessage,options));}
this.isValid=function(){for(var i=0;i<this.validators.length;i++){var validValue=true;var value=this.validators[i][0];var testType=this.validators[i][1];var errorMessage=this.validators[i][2];var options=this.validators[i][3];switch(testType){case"required":if(value==''){validValue=false;}
break;case"email":if(value.indexOf('@',1)==-1||value.indexOf('.',3)==-1){validValue=false;}
break;case"numeric":if(isNaN(value)){validValue=false;}
break;case"phone":phone=value.replace(/[^\d]+/,'')
if(phone.length!=7||phone.length!=10){validValue=false;}
break;case"alpha_num":if(value.match(/[^\da-z]+/i)!=null){validValue=false;}
break;case"cc_number":var cc_number=value.replace(/[^\d]/g,'');if(cc_number.length<13||cc_number.length>19){validValue=false;}
var sum=0;var weight=2;for(i=cc_number.length-2;i>=0;i--){var digit=weight*cc_number[i];sum+=Math.floor(digit/10)+digit%10;weight=weight%2+1;}
if((10-sum%10)%10!=cc_number[cc_number.length-1]){validValue=false;}
break;case"zip_code":if(isNaN(value)||value.length!=5){validValue=false;}
break;case"posnum":if(isNaN(value)||value<=0){validValue=false;}
break;case"match":if(value!=options.compare){validValue=false;}
break;case"length":if(options.min>0&&value.length<options.min){validValue=false;}
if(options.max>0&&value.length>options.max){validValue=false;}
break;case"datetime":aElements=value.split(' ');if(aElements.length!=2){validValue=false;}else{if(!this.isDate(aElements[0])||!this.isTime(aElements[1])){validValue=false;}}
break;case"date":value=value.replace(/[^\d]+/g,'/');if(!this.isDate(value)){validValue=false;}
break;case"dateDiff":value=value.replace(/[^\d]+/g,'/');var compare=options.compare.replace(/[^\d]+/g,'/');if(this.dateDiff(value,compare)<0){validValue=false;}
break;case'range':if(options.min>0&&value<options.min){validValue=false;}
if(options.max>0&&value>options.max){validValue=false;}
break;case'in_array':validValue=false;for(var i=0;i<options.array.length;i++){if(options.array[i]==value){validValue=true;}}
break;case'uri':if(options.protocol){if(value.match(/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i)==null){validValue=false;}}else{if(value.match(/^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i)==null){validValue=false;}}
break;}
if(!validValue){this.isValid=false;this.errors.push(errorMessage);if(options&&options.breakChain)
break;}}
this.displayErrors();return this.isValid;}
this.getDaysPerMonth=function(iMonth,iYear){var iFebruary=(new Date(iYear,1,29).getDate()==29)?29:28;var aDays=Array(31,iFebruary,31,30,31,30,31,31,30,31,30,31);return aDays[iMonth-1];}
this.isDate=function(sDate){aDate=sDate.split("/");if(aDate.length!=3){return false;}else{for(var i=0;i<aDate.length;i++){if(isNaN(aDate[i])){return false;}}
if((aDate[0].length!=2&&aDate[0].length!=1)||(aDate[1].length!=2&&aDate[1].length!=1)||(aDate[2].length!=4&&aDate[2].length!=2)){return false;}
iMonth=parseFloat(aDate[0]);iDay=parseFloat(aDate[1]);iYear=parseInt(aDate[2]);if(iMonth<1||iMonth>12){return false;}
if(iDay<1||iDay>this.getDaysPerMonth(iMonth,iYear)){return false;}}
return true;}
this.dateDiff=function(date1,date2){aDate1=date1.split("/");aDate2=date2.split("/");date1=parseInt(aDate1[2]+aDate1[0]+aDate1[1]);date2=parseInt(aDate2[2]+aDate2[0]+aDate2[1]);return date2-date1;}
this.isTime=function(sTime){aTime=sTime.split(":");if(aTime.length!=2){return false;}else{for(var i=0;i<aTime.length;i++){if(isNaN(aTime[i])){return false;}}
if(aTime[0].length!=2||aTime[1].length!=2){return false;}
iHour=parseInt(aTime[0]);iMinute=parseInt(aTime[1]);if(iHour<0||iHour>23){return false;}
if(iMinute<0||iMinute>59){return false;}}
return true;}
this.displayErrors=function(){if(this.errors.length){var output='<div><a class="btnX" href="javascript:void(0)" onclick="Effect.Fade(this.parentNode)" title="Hide this message"><img src="/img/btnX.png" alt="x" /></a>'+'<ul class="error"><li>'+this.errors.join('</li><li>')+'</li></ul></div>';$('notificationArea').innerHTML=output;$('notificationArea').getElementsByTagName('ul')[0].style.backgroundColor='transparent';new Effect.Highlight('notificationArea',{startcolor:'#fab7a9',endcolor:'#fff1ef',restorecolor:'#fff1ef'});}else{$('notificationArea').innerHTML='';}}}
function validateSignUpForm(form){oValidator=new Validator();oValidator.add(form.email.value,"email","Please enter a valid email address.");oValidator.add(form.postal_code.value,"zip_code","Please enter a valid zip code.");oValidator.add(form.password.value,"length","Please enter a password using 6 or more characters.",{min:6,max:16});oValidator.add(form.password_confirm.value,"match","Please re-type your password.",{compare:form.password.value});oValidator.add(form.terms.checked,"required",'You need to agree to the <a href="/terms-of-service" onclick="this.target=\'_blank\'">terms of service</a> before using Budgetoo.');return oValidator.isValid();}
function validateLoginForm(form){oValidator=new Validator();oValidator.add(form.email.value,"email","Please enter your email address.");oValidator.add(form.password.value,"length","Please enter your password.",{min:6,max:16});return oValidator.isValid();}
function validateForgotPasswordForm(form){oValidator=new Validator();oValidator.add(form.email.value,"email","Please enter your email address.");return oValidator.isValid();}
function validateAddPaymentForm(form){oValidator=new Validator();oValidator.add(form.expense.value,"range","Please select an expense for this payment.",{min:0});oValidator.add(form.amount.value,"posnum","Please enter an amount for this payment.");oValidator.add(form.post_time.value,"date","Please enter a valid date for this payment.");return oValidator.isValid();}
function validateAddExpenseForm(form){oValidator=new Validator();oValidator.add(form.category.value,"posnum","Please select a category for this expense.");oValidator.add(form.description.value,"required","Please enter a description for this expense.");oValidator.add(form.amount.value,"posnum","Please enter an amount for this expense.");return oValidator.isValid();}
function validateAddIncomeForm(form){oValidator=new Validator();oValidator.add(form.description.value,"required","Please enter a description for this expense.");oValidator.add(form.amount.value,"posnum","Please enter an amount for this expense.");oValidator.add(form.frequency.value,"posnum","Please select a frequency for this income.");return oValidator.isValid();}
function validateAddExtraIncomeForm(form){oValidator=new Validator();oValidator.add(form.description.value,"required","Please enter a description for this extra income.");oValidator.add(form.amount.value,"posnum","Please enter an amount for this extra income.");oValidator.add(form.post_time.value,"date","Please enter a valid date for this extra income.");return oValidator.isValid();}
function validateMyAccountForm1(form){oValidator=new Validator();oValidator.add(form.email.value,"email","Please enter your email address.");oValidator.add(form.postal_code.value,"zip_code","Please enter a valid zip code.");return oValidator.isValid();}
function validateMyAccountForm2(form){oValidator=new Validator();oValidator.add(form.password.value,"length","Please enter a password using 6 or more characters.",{min:6,max:16});oValidator.add(form.password_confirm.value,"match","Please re-type your password.",{compare:form.password.value});return oValidator.isValid();}
function validateReportsForm(form){oValidator=new Validator();oValidator.add((form.startYear.value+form.startMonth.value/100),"range","Please make sure the end date is not before the start date.",{max:(form.endYear.value+form.endMonth.value/100)});return oValidator.isValid();}
function validateTellFriendForm(form){oValidator=new Validator();oValidator.add(form.name.value,"required","Please enter your name.");oValidator.add(form.friend.value,"required","Please enter your friend's name.");oValidator.add(form.email.value,"email","Please enter a valid email address.");return oValidator.isValid();}
function validateContactForm(form){oValidator=new Validator();oValidator.add(form.name.value,"required","Please enter your name.");oValidator.add(form.email.value,"email","Please enter a valid email address.");oValidator.add(form.subject.value,"posnum","Please select a subject.");oValidator.add(form.details.value,"required","Please enter some details.");return oValidator.isValid();}
function validateShareStoryForm(form){oValidator=new Validator();oValidator.add(form.name.value,"required","Please enter your name.");oValidator.add(form.email.value,"email","Please enter a valid email address.");oValidator.add(form.details.value,"required","Please enter your story.");return oValidator.isValid();}