//form check
function isNotEmpty(elem) {
   var str = elem.value;
   if(str == null || str.length == 0)
      return false;
   else
      return true;
}

String.prototype.rot13 = function(){ //v1.0
	return this.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};


function validate(form) {
   var attrVal, attrReg, attrEq, attrFail, strTemp;

   for (var i = 0; i < form.length; i++) {
      attrVal = form[i].getAttribute("validate");

      switch (attrVal) {
         case 'required' :
            if (!isNotEmpty(form[i])) {
               attrFail = form[i].getAttribute("failure");

               if (attrFail)
                  alert(attrFail);
               else
                  alert('Niet alle verplichte velden zijn ingevuld.');
               form[i].focus();
               return false;
            }
            break;

         case 'regex' :
            attrReg = form[i].getAttribute("regex");
            if (attrReg != null && attrReg.length != 0) {
               var regex = new RegExp(attrReg);
               strTemp = form[i].value;
               if (!strTemp.match(regex)) {
                  attrFail = form[i].getAttribute("failure");

                  if (attrFail)
                     alert(attrFail);
                  else
                     alert('Invalid data format at field "' + form[i].name + '".');
                  form[i].focus();
                  return false;
               }
            }
            break;

         case 'equals' :
            attrEq = form[i].getAttribute("equals");
            var objEq = document.getElementById(attrEq);
            if (objEq) {
               if (form[i].value != objEq.value) {
                  attrFail = form[i].getAttribute("failure");

                  if (attrFail)
                     alert(attrFail);
                  else
                     alert('Form fields do not match');
                  form[i].focus();
                  return false;
               }
            }
         break;
      }

   }
   return true;
}

function FormFields() {
   var htmlElements = Array('select', 'input', 'textarea');
   var htmlTempArray;
   for (var i = 0; i < htmlElements.length; i++) {
      htmlTempArray = document.getElementsByTagName(htmlElements[i]);

      for (var j = 0; j < htmlTempArray.length; j++) {
         if(htmlTempArray[j].type!='checkbox') {
            htmlTempArray[j].onfocus = htmlTempArray[j].onfocus = function() {
               this.style.background = '#BBBBBB';
            }
            htmlTempArray[j].onblur = htmlTempArray[j].onblur = function() {
               this.style.background = '#FFFFFF';
            }
         }
      }
   }
}

function ExpandCollapse(name, img1, img2) {
	if (document.getElementById(name).style.display == "")
		document.getElementById(name).style.display = "none";
	else
		document.getElementById(name).style.display = "";

	if (document.getElementById(img1).style.display == "") {
		document.getElementById(img1).style.display = "none";
		document.getElementById(img2).style.display = "";
	}
	else {
		document.getElementById(img2).style.display = "none";
		document.getElementById(img1).style.display = "";
  }
}

function openBlock(pID, pNum) {
	for(var i=1; i<=pNum; i++) {
		var myDiv = document.getElementById('block_'+i);
		var myDiv2 = document.getElementById('block_'+i+'_image');
		if(i==pID) {
			myDiv.style.display='block';
			myDiv2.innerHTML = '<img src=\"/admin/images/arrow_black_down.gif\">';
		} else {
			myDiv.style.display='none';
			myDiv2.innerHTML = '<img src=\"/admin/images/arrow_dark.gif\">';
		}
	}
}

//function to open a window
var win=null;
function openWindow(mypage,myname,w,h) {
	myleft=(screen.width)?(screen.width-w)/2:100;
	mytop=(screen.height)?(screen.height-h)/2:100;
	settings="width="+w+",height="+h+",top="+mytop+",left="+myleft+",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	win=window.open(mypage, myname, settings);
	win.focus();
}

var colWin = null;
function colorPicker(myname,w,h,nr,color) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,resizable=no'
	colWin = window.open('include/colorpicker.php?color='+color+'&nr='+nr,myname,settings)
	if(colWin.window.focus) {
		colWin.window.focus();
	}
}
