/*







*/


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

   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: SWuKoWFndLwEkwnCtRsEgLIbDBvOxsDq
   AUDIO: T
   JS-AUTO: 
  
*************/

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

var cap_AudObj;



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("Kan element cap_h niet vinden");
    return true;
  } else {
    if (!cap_theForm) {
    cap_theForm = h.form;
    }
  }
  
  if (b) {
  b.onload='';
  b.onclick=cap_reload;
    b.src = 'http://www.captcha.cc/relaudio.png';
     
  }
  return true;
}

function cap_audio()
{
    var url = 'http://www.captcha.cc/mp3.cgi?cap_k=SWuKoWFndLwEkwnCtRsEgLIbDBvOxsDq&nocache=' + Math.random();
        if (navigator.userAgent.indexOf('MSIE') != -1) {
                if (cap_AudObj) {
                        cap_AudObj.setAttribute("src", url);
                } else {
                        cap_AudObj = document.createElement("bgsound");
                        cap_AudObj.setAttribute("src", url);
                        document.body.appendChild(cap_AudObj);
                }
        } else {
                if (cap_AudObj) {
                        document.body.removeChild(cap_AudObj);
            cap_AudObj = '';
                }
                if (!cap_AudObj ) {
                        cap_AudObj = document.createElement("embed");
                        cap_AudObj.setAttribute("src", url);
                        cap_AudObj.setAttribute("hidden", true);
                        cap_AudObj.setAttribute("autostart", true);
                        document.body.appendChild(cap_AudObj);
                }
        }
}

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);
if (event.offsetY >= 27) {
    ++cap_rCount;
    if (cap_rCount > 3) {
        alert("Code te vaak geladen. Graag pagina opniew laden.");
        return false;
    }
        var t = document.getElementById('cap_i');
        if (t) {
                t.src='http://www.captcha.cc/img.cgi?cap_k=SWuKoWFndLwEkwnCtRsEgLIbDBvOxsDq&nocache=' + Math.random();
        cap_errs = 0;
        }

}
     if (event.offsetY <= 22) {
        cap_audio();
     }

    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('Kan cap_h niet vinden.');
        }
        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("Te veel errors, click op reload om een nieuwe text te zien.");
        } else {
            alert("Voer a.u.b. de correcte code in.");
        }
        ++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("Voer a.u.b. de correcte code in.");
                } 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=SWuKoWFndLwEkwnCtRsEgLIbDBvOxsDq&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);
}
