
/*







*/


/************

   captcha.cc - form update & text validator
  
   Copyright 2008 by captcha.cc, All Rights Reserved.
  
   Credits:
  
   WARNING: Using this code is NOT a substitute for validating the POST on the server
            This is merely here to eliminate page-reload on failed attempts.... that's all!
  
   Rather than rewriting, and falling out of sync, feel free to make "parameterization" requests
   we're quick to add them.

   Params:

   KEY: EuWMvfxtKYplRsdLHuCCCTLEBlykJqFb
   AUDIO: 
   JS-AUTO: T
  
*************/

var cap_theForm;
var cap_prevSub;
var cap_rCount = 0;
var cap_errs = 0;
var cap_headLoc;



document.write('<input type=hidden id=cap_h name=cap_h id=cap_h value=""><img border=1 id=cap_i src="http://www.captcha.cc/img.cgi?cap_k=EuWMvfxtKYplRsdLHuCCCTLEBlykJqFb" align=middle><img align=middle style="margin-left: 4px;" id=cap_r onload="return cap_showReload(event);" src="http://www.captcha.cc/blank.png"><br><input id=cap_t name=cap_t type=text value="" style="margin-top:4px;">');

function cap_showReload(evt)
{
  var b = document.getElementById('cap_r');
  var h = document.getElementById('cap_h');
  if (!h) {
	h=document.forms[0].cap_h;
	b=document.forms[0].cap_r;
  }
  if (!h) {
	alert("Can't get element cap_h");
	return true;
  } else {
    if (!cap_theForm) {
	cap_theForm = h.form;
    }
  }
  
  if (b) {
  b.onload='';
  b.onclick=cap_reload;
    b.src = 'http://www.captcha.cc/reload.png';
    cap_prevSub = cap_theForm.onsubmit;
	 cap_theForm.onsubmit=cap_valid; 
  }
  return true;
}



function cap_gup(name)
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function cap_reload(evt) {
	var event =(typeof window.event!=='undefined')? window.event : evt;
	getOffsets(event);

	++cap_rCount;
	if (cap_rCount > 3) {
		alert("Too many reloads!");
		return false;
	}
        var t = document.getElementById('cap_i');
        if (t) {
                t.src='http://www.captcha.cc/img.cgi?cap_k=EuWMvfxtKYplRsdLHuCCCTLEBlykJqFb&nocache=' + Math.random();
		cap_errs = 0;
        }



	return false;
}

function cap_return(ok, msg) {
	if (ok != '') {
        	var t = document.getElementById('cap_h');
		if (!t) {
			if (cap_theForm) {
	    			t = cap_theForm.elements['cap_h'];
			}
		}
		if (!t) {
			alert('cannot find cap_h');
		}
		if (t) {
			t.value = ok;
		}
		// continue with form submisson
		// note: you still should validate the hash on the server !
		if (cap_theForm) {
			if (cap_prevSub) {
				cap_theform.onsubmit=cap_prevSub;
			}
			cap_theForm.submit();
		} else {
			alert("Captcha OK, but can't submit form.")
		}
	} else {
		if (msg) {
			alert(msg);
		} else if (cap_errs > 1) {
			alert("Too many errors, click reload to see a new image");
		} else {
			alert("Please enter the text seen in the image");
		}
		++cap_errs;
	}
	cap_headLoc.removeChild(cap_scriptObj);  
}

function cap_valid(evt) {
	var event =(typeof window.event!=='undefined')? window.event : evt;
	var target = typeof event.target =='undefined' ? event.srcElement : event.target;
	if (!cap_theForm) {
		cap_theForm = target.form;
		
	}
        var t = document.getElementById('cap_t');
	if (!t && cap_theForm) {
	    t = cap_theForm.elements['cap_t'];
	}
	if (t && !cap_theForm) {
	    cap_theForm = t.form;
	}
        if (t) {
		var v = t.value;
                if (v.length < 4) {
                        alert("Please enter the text seen in the image");
                } else {
			cap_callToServer(t.value);
                }
        }
        return false;
}

function getOffsets (evt) {
  if (typeof evt.offsetX == 'undefined') {
  var target = evt.target;
  if (typeof target.offsetLeft == 'undefined') {
    target = target.parentNode;
  }
  var pageCoords = getPageCoords(target);
  var eventCoords = { 
    x: window.pageXOffset + evt.clientX,
    y: window.pageYOffset + evt.clientY
  };
  evt.offsetX = eventCoords.x - pageCoords.x;
  evt.offsetY = eventCoords.y - pageCoords.y;
  }
}

function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}

function cap_callToServer(tex) {
    cap_scriptObj = document.createElement("script");
    cap_scriptObj.setAttribute("type", "text/javascript");
    cap_scriptObj.setAttribute("charset", "utf8");
    cap_scriptObj.setAttribute("src", 'http://www.captcha.cc/val.cgi?cap_k=EuWMvfxtKYplRsdLHuCCCTLEBlykJqFb&cap_t=' + escape(tex) + '&cap_j=' + Math.random());
    cap_scriptObj.setAttribute("id", "cap_s");
    cap_headLoc = document.getElementsByTagName("head").item(0);
    cap_headLoc.appendChild(cap_scriptObj);
}
