RegExp.escape=function(b){var a=new RegExp("[.*+?|()\\[\\]{}\\\\\\/]","g");return b.replace(a,"\\$&")};if(typeof JSON=="undefined"){JSON={}}JSON.stringify=JSON.stringify||function(e){var d=typeof(e);if(d!="object"||e===null){if(d=="string"){e='"'+e+'"'}return String(e)}else{var f,b,c=[],a=(e&&e.constructor==Array);for(f in e){b=e[f];d=typeof(b);if(d=="string"){b='"'+b+'"'}else{if(d=="object"&&b!==null){b=JSON.stringify(b)}}c.push((a?"":'"'+f+'":')+String(b))}return(a?"[":"{")+String(c)+(a?"]":"}")}};JSON.parse=JSON.parse||function(str){if(str===""){str='""'}eval("var p="+str+";");return p};xdRequest_callback_array=new Array();
function xdRequest(f){var a="http://xdrequest.googlecode.com/svn/tags/0.1.1-alpha/xdRequest.xml";var d="https://query.yahooapis.com/v1/public/yql?format=json&debug=true&q=";var g="USE '"+a+"' AS remote;";this.post_body;this.post_hidden_fields=true;var m=this;var h={headers:[]};var k=new Array();var b=false;var e;this.setURL=function(n){if(n.match(/^https?:\/\/([\w]*\.)*[\w]{2,}$/)||n.match(/^https?:\/\/([\w]*\.)*[\w]{2,}\/.*$/)){b=n}else{throw"Invalid URL specified."}return m};this.getURL=function(){return b};this.header=function(n,o){h.headers.push(new Array(n,o));return m};this.headers=function(){return h.headers};this.get=function(q,o){if(!q){throw"get method requires at least a callback function"}if(!o){if(!typeof q=="function"){throw"If get is called with a single parameter, it must be a callback function."}callback=q}else{if(!typeof o=="function"){throw"Second parameter in get method must be a callback function."}try{this.setURL(q)}catch(r){throw"Malformed URL was specified in get method."}callback=o}if(!typeof callback=="function"){throw"Callback needs to be a function."}h.method="GET";var v=this.cookies();if(v){var w="";for(var n in v){if(n!="index"){if(v[n].match(b,this.secure())){w+=v[n].name+"="+v[n].value+"; "}}}w=w.replace(/; $/,"");this.header("Cookie",w)}var s=g+"SELECT * FROM remote WHERE url='"+b+"' AND parameters='"+JSON.stringify(h)+"'";var u=d+encodeURIComponent(s).replace(/%255C'/g,"%5C'")+"&callback="+encodeURIComponent(l(callback,b));var t=document.createElement("script");t.id=b;t.src=u;document.body.appendChild(t);h={headers:[]};return m};this.post=function(q,o){if(!q){throw"get method requires at least a callback function"}if(!o){if(!typeof q=="function"){throw"If get is called with a single parameter, it must be a callback function."}callback=q}else{if(!typeof o=="function"){throw"Second parameter in get method must be a callback function."}try{this.setURL(q)}catch(r){throw"Malformed URL was specified in get method."}callback=o}if(!typeof callback=="function"){throw"Callback needs to be a function."}if(!m.post_body){throw"Postbody required for post method"}h.method="POST";h.postbody=m.post_body;if(m.post_hidden_fields){var x=m.getHiddenFields();var v;if(x){for(v in x){h.postbody+="&"+x[v].name+"="+x[v].value}}}var w=this.cookies();if(w){var y="";for(var n in w){if(n!="index"){if(w[n].match(b,this.secure())){y+=w[n].name+"="+w[n].value+"; "}}}y=y.replace(/; $/,"");this.header("Cookie",y)}var s=g+"SELECT * FROM remote WHERE url='"+b+"' AND parameters='"+JSON.stringify(h)+"'";var u=d+encodeURIComponent(s).replace(/%255C'/g,"%5C'")+"&callback="+encodeURIComponent(l(callback,b));
var t=document.createElement("script");t.id=b;t.src=u;document.body.appendChild(t);h={headers:[]};return m};
function l(o,n){xdRequest_callback_array.push(function(r){if(r.query.results)
{if(r.query.results.error){o(r.query.results.error)}else
{if(r.query.results.result.html.match(/org.mozilla.javascript.Undefined/))
{var s={error:"Invalid URL was specified."};o(s)}
else{if(r.query.results.result.response_headers["set-cookie"]){j(r.query.results.result.response_headers["set-cookie"])}if(r.query.results.result.status.match(/^3[\d]{2}/)){for(var v in r.query.results.result.response_headers){if(v!="index"){if(v=="location"){var u=r.query.results.result.response_headers[v];break}}}m.get(u,o)}else{if(r.query.results.result!=200){m.html=r.query.results.result.html;o(r.query.results.result)}else{m.html=r.query.results.result.html;o(r.query.results.result)}}}}}else{if(r.error){if(r.error.description.match(/mismatched character/)){s={error:"Invalid parameters transmitted.  Please ensure that single quotes are properly escaped."}}else{s={error:r.error.description}}o(s)}else{var s={error:"Unknown error."};o(s)}}var q=document.getElementById(n);
try{q.parentNode.removeChild(q)}catch(t){}});return"xdRequest_callback_array["+(xdRequest_callback_array.length-1).toString()+"]"}this.addCookie=function(n){var q;var o;q=new xdRequest_cookie(n);if(!q.domain){q.domain=this.domain()}if(o=i(q.name,q.value,true)){k[o].expiration=q.expiration}else{k.push(q)}return m};function j(n){if(typeof n=="string"){n=[n]}for(var q in n){if(q!="index"){m.addCookie(n[q])}}for(var o in k){if(o!="index"){if(k[o].isExpired()){c(o)}}}}this.cookies=function(){var n=new Array();for(cookie in k){if(k[cookie].match(b,this.secure())){n.push(k[cookie])}}if(n.length>0){return n}else{return false}};this.secure=function(){if(b.match(/^https/)){return true}else{return false}};this.domain=function(){if(!b){return false}
if(b.match(/^https?:\/\/([^\/]*).*/)){return b.match(/^https?:\/\/([^\/]*).*/)[1]}return false};function i(q,r,n){for(var o in k){if(o!="index"){if(q==k[o].name&&r==k[o].value){if(n){return o}else{return k[o]}}}}return false}function c(n){if(typeof n=="number"){return k.splice(n,1)}else{if(typeof n=="xdRequest_cookie"){return k.splice(i(n.name,n.value,true),1)}else{return false}}}this.getHiddenFields=function(){if(!this.html){return false}var o;if(o=this.html.match(/<\s*input[^>]*>/ig)){var r;var q;if(r=o.join("").match(/<\s*input(?:(?:(?!type\s*=\s*['"]hidden['"])[^>])*)type\s*=\s*['"]hidden['"][^>]*>/ig)){for(var n in r){if(n!="index"){q={};if(r[n].match(/.*name\s*=\s*['"]([^'"]*)['"].*/)){q.name=r[n].match(/.*name\s*=\s*['"]([^'"]*)['"].*/)[1]}if(r[n].match(/.*value\s*=\s*['"]([^'"]*)['"].*/)){q.value=r[n].match(/.*value\s*=\s*['"]([^'"]*)['"].*/)[1]}r[n]=q}}return r}else{return false}}else{return false}};this.getInputs=function(){if(!this.html){return false}var o;var q;if(o=this.html.match(/<\s*input[^>]*>/ig)){for(var n in o){if(n!="index"){q={};if(o[n].match(/.*name\s*=\s*['"]([^'"]*)['"].*/)){q.name=o[n].match(/.*name\s*=\s*['"]([^'"]*)['"].*/)[1]}if(o[n].match(/.*value\s*=\s*['"]([^'"]*)['"].*/)){q.value=o[n].match(/.*value\s*=\s*['"]([^'"]*)['"].*/)[1]}o[n]=q}}return o}else{return false}};this.getInput=function(q){var n=this.getInputs();if(n){for(var o in n){if(o!="index"){if(n[o].name==q){return n[o].value}}}}return false};if(f){this.setURL(f)}}function xdRequest_cookie(f){this.name;this.value;this.domain;this.path;this.expiration;this.secure=false;this.isExpired=function(){if(this.expiration=="session"){return false}if(Date.parse(Date())>this.expiration){return true}else{return false}};this.match=function(h,g){if(this.isExpired()){return false}var j=h.match(/^https?:\/\/([^\/]*).*/);if(!j){throw"Invalid URL specified on cookie match."}j=j[1];if(!j.match(RegExp.escape(this.domain))){return false}if(!h.match(/^https?:\/\/[^\/]*\/.*/)){var i="/"}else{var i=h.match(/^https?:\/\/[^\/]*(\/.*)/)[1]}if(!i.match(RegExp.escape("^"+this.path))){return false}if(h.match(/^http:/)&&this.secure){return false}return true};if(f){var c;var a;var b=this;this.name=f.split(/;\s*/)[0].match(/^([^=]*)=/)[1];this.value=f.split(/;\s*/)[0].match(/^[^=]*=(.*)$/)[1];var d=f.split(/;\s*/);for(var e in d){if(e!="index"){if(d[e].match(/^([^=]*)=/)){c=d[e].match(/^([^=]*)=/)[1];a=d[e].match(/^[^=]*=(.*)$/)[1];switch(c){case"domain":case"Domain":this.domain=a.replace(/"|'/g,"");break;case"path":case"Path":this.path=a.replace(/"|'/g,"");break;case"expires":case"Expires":this.expiration=Date.parse(a.replace(/"|'/g,""));break;default:break}}}else{switch(d[e]){case"secure":this.secure=true;break;default:break}}}if(!this.expiration){this.expiration="session"}return b}};
