// JavaScript Document
function popUp(url,width,height){

    attrib = 'width=' + width + ',height='+ height+ ',top=10,left=10,scrollbars=yes';
    popwindow = window.open(url,'PopUp',attrib);
}

function actionOnload(e) {

    boxReset("zoek");

    if (typeof $ != 'undefined'){
        if($(".poll").length) {
            initPoll();
        }
        $('.normalizeSsn:input').each(function () {
            addNormalizeSsnListeners(this);
        });

        $('.focusInput:input').first().focus();

        $('.dropdownMenuBtn').click(function (){
            $('.dropdownMenu').find('ul').toggleClass('noHeight');
        });
    }
}

function ttpKeepAlive(obj) {
  document.getElementById(obj.id).style.visibility = "visible";
}

function ttpHide(obj) {
  document.getElementById(obj.id).style.visibility = "hidden";
}

var inpBox = new Object();

function boxReset() {
	var myElem;
	for(var i=0;i<arguments.length;i++) {
		myElem = document.getElementById(arguments[i]);

		if (myElem) {
			inpBox[myElem.id] = myElem.value;

			if (document.addEventListener) {
			  // REGISTERING THE EVENTS FOR DOM COMPLIANT BROWSERS
				myElem.addEventListener("focus",boxGotFocus,false);
				myElem.addEventListener("blur",boxLostFocus,false);
			} else {
			  // REGISTERING THE EVENTS FOR IE
				myElem.attachEvent("onblur",boxLostFocus);
				myElem.attachEvent("onfocus",boxGotFocus);
			}

		}
	}
}

function boxGotFocus(e) {
  myBox = (!window.event) ? e.target:window.event.srcElement;

  if (myBox.value == inpBox[myBox.id]) {
    myBox.value = "";
  }
}

function boxLostFocus(e) {
  myBox = (!window.event) ? e.target:window.event.srcElement;

  if (myBox.value == "") {
    myBox.value = inpBox[myBox.id];
  }
}

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

function initPoll(){
    $(".poll-submit").click(function(event){event.preventDefault();$(this).parents("form").submit();});
}

function normalizeSsn(ssnInput) {
    //Jquery is needed
    if (typeof $ !== 'undefined') {
        var ssn = $(ssnInput).val(),
            newSsn = "",
            match,
            i = 1;

        //match against (ÅÅ)ÅÅMMDD(-)nnnn
        match = ssn.match(/^(\d{2})?(\d{6})(-)?(\d{4})$/);

        // trim any white space
        ssn = ssn.replace(/\s/g, "");

        if (match == null) {
            // we can't fix this thing
            return;
        }

        // look for missing century
        if (match[1] == null || match[1] == "") {
            match[1] = "19";
        }

        // remove optional dash
        match[3] = "";

        // re-build SSN from it's parts
        // this simple splice()+join() could have been more elegant, hadn't it been for IE
        for (i = 1; i < match.length; i += 1) {
            newSsn += match[i];
        }

        $(ssnInput).val(newSsn);
    }
}

function addNormalizeSsnListeners(ssnInputElem) {
   if (typeof $ !== 'undefined') {
       var ssnInput =  $(ssnInputElem);
        ssnInput.each(function() {
            normalizeSsn(this)
        });
        ssnInput.change(function() {
            normalizeSsn(this)
        });
        ssnInput.blur(function() {
            $(this).trigger("change");
        });
   }
}


