;(function($){$.fn.jxrc=function(input){var params={};var defaults={url:false,destination:false,debug:false,context:false,displayErrors:false,data:{},depth:1,label:'Please select...',loading:'Loading...',optionValueKey:'V',optionTextKey:'T'};$.extend(params,defaults,input);return this.each(function(){var $self=$(this);var $dest=null;if(params.destination)
{if(params.destination instanceof $)
{$dest=params.destination;}
else
{if(params.debug)
{alert('jQuery Selector: ('+params.destination+')');}
if(!params.context)
{params.context=$('*');}
$dest=$(params.destination,params.context);}}
else
{$dest=$(null);}
if(params.debug)
{alert('destination name: '+$dest.get(0).name);}
$dest.unbind('change');if($dest.is('select'))
{$dest.empty();if($dest.is(":visible"))
{$dest.attr('disabled','disabled');var o=document.createElement("OPTION");o.value='';o.text=params.loading;$dest.get(0).options[0]=o;}}
var args={};args[this.name]=$self.val();args['field']=this.name;args['depth']=params.depth;$.ajax({url:params.url,data:$.extend(params.data,args),type:'get',dataType:'json',success:function(reply){if($dest.is('select'))
{var j=[];if('undefined'!=typeof reply.options)
{j=reply.options;}
if(j.length>0)
{var optionIndex=0;var selectLabel=reply.label?reply.label:params.label;if(selectLabel)
{var o=document.createElement("OPTION");o.value='';o.text=selectLabel;$dest.get(0).options[optionIndex]=o;optionIndex++;}
for(i=0;i<j.length;i++)
{var o=document.createElement("OPTION");if('string'==typeof j[i])
{o.value=j[i];o.text=j[i];$dest.get(0).options[optionIndex]=o;optionIndex++;}
else if(typeof j[i]=='object')
{if(j[i][params.optionValueKey]&&j[i][params.optionTextKey])
{o.value=j[i][params.optionValueKey];o.text=j[i][params.optionTextKey];$dest.get(0).options[optionIndex]=o;optionIndex++;}
else if(params.debug)
{alert('Option value missing key: "'+params.optionValueKey+'" and/or "'+params.optionTextKey+'"');}}}
$dest.show();}
else
{$dest.hide();}
setTimeout(function(){$dest.find('option:first').attr('selected','selected');$dest.removeAttr('disabled');},0);}
if('undefined'!=typeof reply.hide)
{var toHide=[];if('string'==typeof reply.hide)
{toHide.push(reply.hide);}
else
{toHide=$.makeArray(reply.hide);}
for(i=0;i<toHide.length;i++)
{var $reveal=$(toHide[i]);if($reveal.get(0))
{$reveal.hide();}}}
if('undefined'!=typeof reply.show)
{var toShow=[];if('string'==typeof reply.show)
{toShow.push(reply.show);}
else
{toShow=$.makeArray(reply.show);}
for(i=0;i<toShow.length;i++)
{var $reveal=$(toShow[i]);if($reveal.get(0))
{$reveal.show();}}}
if('undefined'!=typeof reply.copy)
{var copyList=[];if('undefined'!=typeof reply.copy.from&&'undefined'!=typeof reply.copy.to)
{copyList.push(reply.copy);}
else if('object'==typeof reply.copy&&reply.copy.constructor==Array)
{for(i=0;i<reply.copy.length;i++)
{copyList.push(reply.copy[i]);}}
for(i=0;i<copyList.length;i++)
{var copyInfo=copyList[i];if('undefined'!=typeof copyInfo.from&&'undefined'!=typeof copyInfo.to)
{var copyFrom=[];var copyTo=[];var copyVal='';var allowedFrom=['html','text','val'];var $from=null;var $to=null;if('string'==typeof copyInfo.from)
{copyVal=copyInfo.from;}
else
{copyFrom=$.makeArray(copyInfo.from);$from=$(copyFrom[0]);if(copyFrom.length>1&&-1!=$.inArray(copyFrom[1],allowedFrom))
{var callback=copyFrom[1];copyVal=$from[callback]();}
else
{copyVal=$from.val();}}
if('string'==typeof copyInfo.to)
{copyTo.push(copyInfo.to);}
else
{copyTo=$.makeArray(copyInfo.to);}
$to=$(copyTo[0]);if($to&&$to.get(0))
{var allowedTo=['html','text','val','append','prepend','replaceWith'];if(copyTo.length>1&&-1!=$.inArray(copyTo[1],allowedTo))
{var callback=copyTo[1];$to[callback](copyVal);}
else
{$to.val(copyVal);}}}}}
if($dest.get(0)&&'undefined'!=typeof reply.onchange)
{if('undefined'!=typeof reply.onchange.target)
{var $target=$(reply.onchange.target);if($target&&$target.get(0)&&$dest.get(0).name!=$target.get(0).name)
{$dest.bind('change',function(){var opts={};opts['url']=params.url;if('string'==typeof reply.onchange.url)
{opts['url']=reply.onchange.url;}
opts['destination']=reply.onchange.target;opts['data']={};if('undefined'!=typeof reply.data)
{$.extend(opts['data'],reply.data);}
opts['depth']=params.depth+1;opts['debug']=params.debug;if('undefined'!=typeof reply.onchange.label)
{opts['label']=reply.onchange.label;}
if(params.context)
{opts['context']=params.context;}
else
{if(params.debug)
{alert('context is empty for target: '+reply.onchange.target);}}
$dest.jxrc(opts);});}}
var toHide=[];var toShow=[];if('undefined'!=typeof reply.onchange.hide)
{if('string'==typeof reply.onchange.hide)
{toHide.push(reply.onchange.hide);}
else
{toHide=$.makeArray(reply.onchange.hide);}}
if('undefined'!=typeof reply.onchange.show)
{if('string'==typeof reply.onchange.show)
{toShow.push(reply.onchange.show);}
else
{toShow=$.makeArray(reply.onchange.show);}}
if(toShow.length>0||toHide.length>0)
{$dest.bind('change',function(){for(i=0;i<toHide.length;i++)
{var $reveal=$(toHide[i]);if($reveal.get(0))
{$reveal.hide();}}
for(i=0;i<toShow.length;i++)
{var $reveal=$(toShow[i]);if($reveal.get(0))
{$reveal.show();}}});}
if('undefined'!=typeof reply.onchange.copy)
{var copyList=[];if('undefined'!=typeof reply.onchange.copy.from&&'undefined'!=typeof reply.onchange.copy.to)
{copyList.push(reply.onchange.copy);}
else if('object'==typeof reply.onchange.copy&&reply.onchange.copy.constructor==Array)
{for(i=0;i<reply.onchange.copy.length;i++)
{copyList.push(reply.onchange.copy[i]);}}
for(i=0;i<copyList.length;i++)
{var copyInfo=copyList[i];if('undefined'!=typeof copyInfo.from&&'undefined'!=typeof copyInfo.to)
{var copyFrom=[];var copyTo=[];var copyVal='';var allowedFrom=['html','text','val'];var $from=null;var $to=null;if('string'==typeof copyInfo.from)
{copyVal=copyInfo.from;}
else
{copyFrom=$.makeArray(copyInfo.from);$from=$(copyFrom[0]);if(copyFrom.length>1&&-1!=$.inArray(copyFrom[1],allowedFrom))
{var callback=copyFrom[1];copyVal=$from[callback]();}
else
{copyVal=$from.val();}}
if('string'==typeof copyInfo.to)
{copyTo.push(copyInfo.to);}
else
{copyTo=$.makeArray(copyInfo.to);}
$to=$(copyTo[0]);if($to&&$to.get(0))
{$dest.bind('change',function(){var allowedTo=['html','text','val','append','prepend','replaceWith'];if(copyTo.length>1&&-1!=$.inArray(copyTo[1],allowedTo))
{var callback=copyTo[1];$to[callback](copyVal);}
else
{$to.val(copyVal);}});}}}}}
if('undefined'!=typeof reply.trigger)
{if('undefined'!=typeof reply.trigger.target&&'undefined'!=typeof reply.trigger.event)
{var $target=$(reply.trigger.target);if($target&&$target.get(0))
{var evnts=[];if('string'==typeof reply.trigger.event)
{evnts.push(reply.trigger.event);}
else
{evnts=$.makeArray(reply.trigger.event);}
for(i=0;i<evnts.length;i++)
{$target.trigger(evnts[i]);}}}}},error:function(xhr,desc,er){if(params.displayErrors)
{alert("error: "+er+"\ntext: "+desc+"\nresponse: "+xhr.responseText);}}});return false;});};})(jQuery);
