﻿SUI.util.CapRefreshImage = function(valImageId) {
	var objImage = document.getElementById(valImageId);
	document.getElementById('ics_id').value = '';
	document.getElementById(valImageId+'-holder').innerHTML = '';
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
	//objImage.setAttribute('t',now.getTime());
}
YAHOO.util.Event.on('imgCaptcha','click',function(e){
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	var region = YAHOO.util.Dom.getRegion(this);
	var input = document.getElementById('ics_id');
	var times = document.getElementById(this.id+'-holder').getElementsByTagName('span').length;
	if(times < 4){
		//alert((posx - region.left) +','+ (posy - region.top));
		input.value = (times != 0 ? input.value+'|' : '')+(posx - region.left) +'/'+ (posy - region.top);
		//this.setAttribute('one','true');
		var num = document.createElement('span');
		num.innerHTML = times + 1;
		num.style.cssText = 'display: inline-block; margin: 3px; font-size: 18px; font-weight: bold; font-family: arial;';
		//num.id = this.id + '-image-one';
		//num.style.cssText = 'background: url('+ ( this.getAttribute('t') ? this.src.split('?')[0] + '?x=' + this.getAttribute('t') : this.src ) +') no-repeat -'+(posx - region.left - 15)+'px -'+(posy - region.top - 15)+'px; display: inline-block; width: 30px; height: 30px; margin: 2px; border: 1px solid #aaa;';
		//this.parentNode.appendChild(num);
		/*
		num.style.cssText = 'display: inline-block; width: 30px; height: 30px; margin: 2px; border: 1px solid #aaa; overflow: hidden;';
		var inum = this.cloneNode(true);
		inum.id = 'r-'+(Math.floor(Math.random()*1000000));
		num.appendChild(inum);
		
		num.scrollLeft = posx - region.left - 15;
		num.scrollTop = posy - region.top - 15;
		*/
		document.getElementById(this.id+'-holder').appendChild(num);
	}
	times = document.getElementById(this.id+'-holder').getElementsByTagName('span').length;
	if(times == 4){
		var id = this.id;
		var success = function(o){
			var img = document.getElementById(id);
			if(o.responseText == '12121212'){
				
				img.parentNode.style.borderColor = '#00bb00';
				img.style.cssText = 'opacity: 0.1; filter: alpha(opacity=10);';
				img.parentNode.getElementsByTagName('div')[0].style.display = 'block';
				document.getElementById(id+'-refresh').style.display = 'none';
				document.getElementById(id+'-holder').style.display = 'none';
			}
			else{
				img.parentNode.style.borderColor = 'Red';
				alert('We could not verify the pictures were clicked in the correct order. Please try again.');
				SUI.util.CapRefreshImage(id);
				img.parentNode.style.borderColor = '#aaa';
			}
			//alert(o.responseText);
		};
		var failure = function(o){
			alert('Error Code: '+ o.statusText);
			
		};
		var request = YAHOO.util.Connect.asyncRequest('GET','/main/systems/icaptcha_check.asp?check='+input.value,{success:success,failure:failure});
		
	}

	
});