
bbcjs.regex={not_empty:/^.+$/,numeric:/^\d+$/,postcode:/^[a-z]{1,2}\d{1,2}[a-z]? ?\d{1}[a-z]{2}$/i,postcode_partial:/^[a-z]{1,2}\d{1,2}[a-z]?( ?\d{1}[a-z]{2})?$/i,email:/^[\w\d\._\-\!\#\$\%\&\'\*\+\/\=\?\^`\{\}\|\~]+@[\w\d-\.]+\.\w{2,4}(\.\w{2})?$/,string:/^[a-zA-Z\-\'_ \?]+$/,age:/^1?\d{1,2}$/,date:/^(\d{1,2}[\-|\/]\d{1,2}[\-|\/]\d{4})$/,currency:/^[\$\£]?\d+(\.\d{2})?$/};new function(){var $s=bbcjs.lib.string={uriEscape:function(s){return escape(s).replace(/\+/g,"%2b");},obj2string:function(o,pd,vd){var _opts=typeof(pd)=='object'?pd:{pd:pd||'&',vd:vd||'='};var opts=bbcjs.extend({pd:'&',vd:'=',repeatKeys:false},_opts);var pd=opts.pd,vd=opts.vd;var str="",i,t="",cnt=0,j;if(typeof(o)=="object"){for(i in o){cnt++;if(typeof(o[i])=="object"&&((o[i].constructor==Object)||(o[i].constructor==Array))){if(opts.repeatKeys){for(j=0;j<o[i].length;j++)t+=i+vd+o[i][j]+pd;}
else t+=i+vd+$s.uriEscape($s.obj2string(o[i],pd,vd))+pd;}
else if(typeof(o[i])!="object"&&(typeof(o[i])!="function")){t+=i+vd+$s.uriEscape(o[i])+pd;}}
str=t.substring(0,t.length-1);}
else bbcjs.trace("obj2string expected typeof object, not "+typeof(o),1);if(!cnt)str="_{}";if(pd!=";")str=str.replace(/;/g,"%3B");return str;},string2obj:function(str,pd,vd){if(pd==undefined)pd="&";if(vd==undefined)vd="=";var pairs=str.split(pd);var i,j,o={},pair=[],t;for(i=0;i<pairs.length;i++){pair=pairs[i].split(vd);if(i===0&&(parseInt(pair[0])===0))o=[];if(pair.length==2){t=unescape(pair[1]);if(t.indexOf(vd)!=-1){o[pair[0]]=$s.string2obj(t,pd,vd);}
else{if(!isNaN(parseInt(pair[0])))pair[0]=parseInt(pair[0]);if(unescape(pair[1])!="_{}")o[pair[0]]=unescape(pair[1].replace(/%3B/g,";"));else o[pair[0]]={};}}
else bbcjs.trace("WARNING: string2obj:: '"+pairs[i]+"' is not a proper name-val pair (using '"+pd+"' and '"+vd+"')",5);}
return o;},countWords:function(str){var matches=new Array();matches=str.match(/\w+/g);if(!matches)return 0;else return matches.length;},WordLimit:function(limit){this.limit=limit;this.type='WordLimit';}};$s.WordLimit.prototype.validate=function(str){return($s.countWords(str)<=this.limit);};}();function sprintf(){if(!arguments||arguments.length<1||!RegExp){return 0;}
var str=arguments[0];var re=/([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;var a=re.exec(str);var numSubstitutions=0;var numMatches=0;while(re.exec(str)){var leftpart=a[1],pPad=a[2],pJustify=a[3],pMinLength=a[4];var pPrecision=a[5],pType=a[6],rightPart=a[7];numMatches++;if(pType=='%'){subst='%';}
else{numSubstitutions++;if(numSubstitutions>=arguments.length){bbcjs.trace('sprintf : Not enough function arguments ('+(arguments.length-1)+', excluding the string)\n'+'for the number of substitution parameters in string ('+
numSubstitutions+' so far).',1);}
var param=arguments[numSubstitutions];var pad='';if(pPad&&pPad.substr(0,1)=="'")pad=leftpart.substr(1,1);else if(pPad)pad=pPad;var justifyRight=true;if(pJustify&&pJustify==="-")justifyRight=false;var minLength=-1;if(pMinLength)minLength=parseInt(pMinLength);var precision=-1;if(pPrecision&&pType=='f')
precision=parseInt(pPrecision.substring(1));var subst=param;switch(pType){case'b':subst=parseInt(param).toString(2);break;case'c':subst=String.fromCharCode(parseInt(param));break;case'd':subst=parseInt(param)?parseInt(param):0;break;case'u':subst=Math.abs(param);break;case'f':subst=(precision>-1)?Math.round(parseFloat(param)*Math.pow(10,precision))/Math.pow(10,precision):parseFloat(param);break;case'o':subst=parseInt(param).toString(8);break;case's':subst=param;break;case'x':subst=(''+parseInt(param).toString(16)).toLowerCase();break;case'X':subst=(''+parseInt(param).toString(16)).toUpperCase();break;}
var padLeft=minLength-subst.toString().length;var padding="";if(padLeft>0){var arrTmp=new Array(padLeft+1);padding=arrTmp.join(pad?pad:" ");}}
str=leftpart+padding+subst+rightPart;}
return str;}
var printf=sprintf;bbcjs.trace('<b style="color:Green">jst_forms.js was included.</b>',2);new function(){var forEach=bbcjs.forEach;var forms=bbcjs.forms={required:{},req_err:{},valErrorMsg:'Please enter all required information correctly.',alertOnValidationError:true,validate:function(form,userhandler){bbcjs.trace('<b>bbcjs.forms.validate('+form.name+')</b>',2);var error,message;var messages=forms.req_err;var validators=forms.required;var handler=userhandler?userhandler:forms.handleError;for(var name in validators){if(!forms.validateElement(form,name,validators[name])){message=(typeof(messages[name])!="undefined")?messages[name]:forms.valErrorMsg;error=new forms.Error(form,name,validators[name],message);break;}}
return handler(error);},validateElement:function(form,name,validator){bbcjs.trace("Checking element: <b>"+name+"</b>",3);var ok=true,val=forms.getValue(form[name]);if((typeof(validator)=="object")&&(typeof(validator.type)!="undefined")){bbcjs.trace("&nbsp;&nbsp;Checking <b>"+validator.type+"</b> element",3);ok=forms.validateComplex(form,name,validator);}
else{if(val)val=val.toString();val=val.replace(/[\r|\n]/g,"");ok=!!val.match(validator);}
if(!ok){bbcjs.trace("&nbsp;&nbsp;Element does not validate! Value: "+val,3);bbcjs.trace("&nbsp;&nbsp;&nbsp;&nbsp;Requires: "+validator,4);}
return ok;},validateComplex:function(form,name,validator){bbcjs.trace("bbcjs.forms.validateComplex() called...",3);var ok=true;if(validator.validate){ok=validator.validate(forms.getValue(form[name]));}
bbcjs.trace("validateComplex is returning <b>'"+ok+"'</b>",4);return ok;},handleError:function(e){if(e){if(forms.alertOnValidationError)alert(e.message);if(forms.getElementType(e.element)!="hidden"){var node=e.element[0]?e.element[0]:e.element;if(typeof(node.focus)!="undefined")node.focus();if(bbcjs.ui)new bbcjs.ui.Fader(node,true).fade();}}
bbcjs.trace('<b>bbcjs.forms.handleError</b> is returning <font color="'+(e?'red':'green')+'"><b>'+!e+'</b></font>',2);return!e;},getElementType:function(fe){var typestring="";if(typeof(fe.type)!="undefined")typestring=fe.type;else if(typeof(fe.length)!="undefined")typestring=fe[0].type;return typestring;},getValue:function(fe){var typestring=forms.getElementType(fe);var t="";if((typeof(fe)!="object")&&(typeof(fe)!="function")){bbcjs.trace("WARNING - getValue expects type: form element, not '"+typeof(fe)+"'. Your element name may be wrong.",2);return false;}
bbcjs.trace("<b>getValue()</b> is checking item of type <b>"+typestring+"</b>",4);switch(typestring){case"radio":t=forms.getRadioValue(fe);break;case"checkbox":t=forms.getCheckboxValues(fe);break;case"select-one":t=forms.getSelectValue(fe);break;case"select-multiple":t=forms.getSelectValues(fe);break;default:t=fe.value;break;}
bbcjs.trace("getValue is returning <b>'"+t+"'</b>",4);return t;},getRadioValue:function(fe){var val='';forEach(fe,function(o){if(o.checked)val=o.value;});bbcjs.trace("getRadioValue is returning <b>"+val+"</b>",5);return val;},getCheckboxValues:function(fe){var retval=[];if(typeof(fe.length)!='undefined'&&fe.length>0){forEach(fe,function(o){if(o.checked)retval.push(o.value);});}
else if(fe.checked)retval=fe.value;else retval='';bbcjs.trace("getCheckboxValues is returning <b>"+retval+"</b>",5);return retval;},getSelectValue:function(sb){var retval=(sb.selectedIndex>-1)?sb.options[sb.selectedIndex].value:'';bbcjs.trace("getSelectValues is returning <b>"+retval+"</b>",5);return retval;},getSelectValues:function(sb){var retval=[];forEach(sb.options,function(o){if(o.selected&&(o.value!==''))
retval.push(o.value);});bbcjs.trace("getSelectValues is returning <b>"+retval+"</b>",5);return retval;},setValue:function(fe,val){var typestring=forms.getElementType(fe),t=0;bbcjs.trace("<b>setValue()</b>, setting value of type <b>"+typestring+"</b>",4);switch(typestring){case"radio":t=forms.setRadioValue(fe,val);break;case"checkbox":t=forms.setCheckboxValues(fe,val);break;case"select-one":t=forms.setSelectValue(fe,val);break;case"select-multiple":t=forms.setSelectValues(fe,val);break;default:t=1;fe.value=val;break;}
bbcjs.trace("setValue is returning <b>"+t+"</b>",4);return t;},setRadioValue:function(fe,val){var retval=0,name='unknown';forEach(fe,function(o){name=o.name;o.checked=false;if(o.value==val){o.checked=true;retval=1;}});if(retval)bbcjs.trace("setRadioValue set value of <b>"+val+"</b> for element <b>"+name+"</b>",5);else bbcjs.trace("WARNING setRadioValue could not find element <b>"+val+"</b> in group <b>"+name+"</b>",2);return retval;},setCheckboxValues:function(fe,val){var retval=0,name='unknown';fe=(fe.length!=undefined)?fe:[fe];val=(typeof(val)=='object')?val:[val];forEach(fe,function(f){f.checked=false;});forEach(val,function(v){forEach(fe,function(f){name=f.name;if(f.value==v){f.checked=true;retval++;}});});if(retval)bbcjs.trace("setCheckboxValues set <b>"+retval+"</b> item(s) for element <b>"+name+"</b>",5);else bbcjs.trace("WARNING setCheckboxValues could not find any items matching <b>"+val+"</b> in group <b>"+name+"</b>",2);return retval;},setSelectValue:function(fe,val){var retval=0,name=fe.name;fe.selectedIndex=0;forEach(fe,function(f,i){if(f.value==val&&retval<1){retval=1;fe.selectedIndex=i;}});if(retval)bbcjs.trace("setSelectValue set value <b>"+val+"</b> for element <b>"+name+"</b>",5);else bbcjs.trace("WARNING setSelectValue could not find item matching <b>"+val+"</b> in element <b>"+name+"</b>",2);return retval;},setSelectValues:function(fe,val){var retval=0,name=fe.name;fe=fe.options||fe;val=(typeof(val)=='object')?val:[val];forEach(fe,function(f){f.selected=false;});forEach(val,function(v){forEach(fe,function(f){if(f.value==v){f.selected=true;retval++;}})})
if(retval)bbcjs.trace("setSelectValues set <b>"+retval+"</b> items for element <b>"+name+"</b>",5);else bbcjs.trace("WARNING setSelectValues could not find items matching <b>"+val+"</b> in element <b>"+name+"</b>",2);return retval;},form2object:function(f){bbcjs.trace("form2object called on form named: '"+f.name+"'",3);var o={},t=name=type='';forEach(f.elements,function(e){type=forms.getElementType(e),name='';if(type&&type!='submit'&&type!='reset'){t=forms.getValue(f[e.name]);if(e.name!=undefined)name=e.name;else if(e.length!=undefined)name=e[0].name;if(name)o[name]=t;}});bbcjs.trace("form2object returned: "+bbcjs.obj2list(o),5);return o;},object2form:function(f,o){var retval=0;bbcjs.trace("object2form called on form names: '"+f.name+"'",3);bbcjs.trace("object2form was passed: "+bbcjs.obj2list(o),5);for(var i in o){if(f[i])retval+=forms.setValue(f[i],o[i]);}
bbcjs.trace("object2form made "+retval+" change(s) to the form",4);return retval;},form2cookie:function(f,name,exp,pth,dmn){var retval=false,t={},c={},i=0;if(bbcjs.cookies){bbcjs.trace("form2cookie:: '"+f.name+"' -&gt; '"+name+"'",3);t=forms.form2object(f);if(bbcjs.cookies.cookieData[name]){c=bbcjs.cookies.cookieData[name];for(i in t)c[i]=t[i];t=c;}
retval=bbcjs.cookies.setCookie(name,t,exp,pth,dmn);}
else bbcjs.trace("form2cookie:: Could not find cookie module! Please include jst_cookies.",1);return retval;},cookie2form:function(f,cn){var retval=0;if(bbcjs.cookies){if(bbcjs.cookies.cookieData[cn]){bbcjs.trace("cookie2form:: '"+cn+"' -&gt; '"+f.name+"'",3);retval+=forms.object2form(f,bbcjs.cookies.cookieData[cn]);}
else bbcjs.trace("WARNING: cookie2form() could not find a cookie named '"+cn+"' to apply to form.",2);}
else bbcjs.trace("cookie2form:: Could not find cookie module! Please include jst_cookies.",1);return retval;},form2string:function(f,pd,vd){var obj=forms.form2object(f);var str=bbcjs.lib.string.obj2string(obj,pd,vd);str=str.replace(/_%7B%7D/g,"");return str;},string2form:function(f,str,pd,vd){var obj=bbcjs.lib.string.string2obj(str,pd,vd);return forms.object2form(f,obj);}};forms.Error=function(form,name,validator,message){this.form=form;this.name=name;this.element=form[name];this.validator=validator;this.message=message;};}();