//Captcha functionality
//	Usage:
//		place DIV Section in code where captch should appear.
//		place hidden field in form to hold hidden key
//		Create Captch object with parameters (div_id, hidden_key)
//		where div_id refers to above div section
//		and hidden_key refers to hidden form field
var wxtools = {};

wxtools.CaptchaInstances = {};

wxtools.CaptchaImageResponse = function(id, url, value) {
   wxtools.CaptchaInstances[id].imageResponse(url, value);
};


wxtools.Captcha = function(div_id,hidden_key) {
	var id = Math.random();	
      wxtools.CaptchaInstances[id] = this;

      this.imageResponse = function(url, value){ 
	   var div = document.getElementById(div_id);
	   var new_image = document.createElement('img');
	   new_image.width=250;

	   new_image.src = url; 

	   div.appendChild(new_image);	
         document.getElementById(hidden_key).value = value;
      };

      this.getCaptcha = function(){
         var head = document.getElementsByTagName('head')[0];
         var script = document.getElementById('script_' + wxtools.CaptchaInstances[id].id);
         if (script) {
            head.removeChild(script);
            delete script;
         }

         script = document.createElement('script');
         script.id = ['script_',wxtools.CaptchaInstances[id].id].join('');
         script.type = 'text/javascript';
         script.src = ['http://fcgi.weather.com/fcgi-bin/captcha/captcha.cgi?callback=wxtools.CaptchaImageResponse','&instance=',id].join('');
         head.appendChild(script);
	}
	this.getCaptcha();
};

wxtools.Captcha = function(div_id,hidden_key,imagesize) {
	var id = Math.random();	
      wxtools.CaptchaInstances[id] = this;

      this.imageResponse = function(url, value){ 
	   var div = document.getElementById(div_id);
	   var new_image = document.createElement('img');
	  // new_image.width=250;
		new_image.width=imagesize;
	   new_image.src = url; 

	   div.appendChild(new_image);	
         document.getElementById(hidden_key).value = value;
      };

      this.getCaptcha = function(){
         var head = document.getElementsByTagName('head')[0];
         var script = document.getElementById('script_' + wxtools.CaptchaInstances[id].id);
         if (script) {
            head.removeChild(script);
            delete script;
         }

         script = document.createElement('script');
         script.id = ['script_',wxtools.CaptchaInstances[id].id].join('');
         script.type = 'text/javascript';
         script.src = ['http://fcgi.weather.com/fcgi-bin/captcha/captcha.cgi?callback=wxtools.CaptchaImageResponse','&instance=',id].join('');
         head.appendChild(script);
	}
	this.getCaptcha();
};