function LogOff(){
	if (confirm("Weet u zeker dat u wilt uitloggen?\nEventuele wijzigingen die nog niet zijn opgeslagen gaan verloren.")) {			
		window.location="logoff.php";		
	}
}

function ShowHide(id){
    if(document.getElementById(id).style.display == 'none'){
        document.getElementById(id).style.display = '';
    }else{
        document.getElementById(id).style.display = 'none';
    }
}

function MailFriend(item_id) {
	url="tellfriend_algemeen.php?item_id="+item_id;
	window.open(url ,"windowname","height=440,width=540");
}

/*
function emailvalidation(entered,alertbox) {
	with (entered) {
		apos=value.indexOf("@"); 
		dotpos=value.lastIndexOf(".");	
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			if (alertbox) {alert('Het ingevoerde email adres is ongeldig.');}
			return false;			
		} else {
			return true; 
		}
	} 	
}
*/
function emailvalidation(entered,alertbox) {
	emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([-a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	with (entered) {
		if( !emailpat.test(value) ) {
			if (alertbox) {alert('Het ingevoerde email adres is ongeldig.');}
			return false;			
		} else {
			return true; 
		}
	}
}

function NieuwsbriefVolgende() {
	//hier nog checks!!
	if (document.nieuwsbriefformulier.email.value=="") {
		alert("Je hebt nog geen emailadres opgegeven!");
	} else {							
		if (emailvalidation(document.nieuwsbriefformulier.email,1)) {
			if (document.nieuwsbriefformulier.naam.value=="") {
				alert("Je hebt nog geen (nick)naam opgegeven!");
			} else {
				document.nieuwsbriefformulier.submit();
			}									
		}
	}
}


function CheckImageFile (form_image) {
	with (form_image) {
		bestandsnaam=new String(value);
		pos=bestandsnaam.lastIndexOf(".")+1;
		extensie = bestandsnaam.substr(pos,3);
		extensie = extensie.toUpperCase();
		if (extensie==""){ 
			return true;
		}
		if (extensie!="JPG"){
			alert("Alleen JPG bestanden zijn toegestaan.");
			value="";		
			return false;
		}  else {
			return true;
		}
	}	
}

function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


function disableRightClick(e){
  var message = "Right click disabled";  
  if(!document.rightClickDisabled){
    if(document.layers){
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all)){
    if (e.which==2||e.which==3){
      //alert(message);
      return false;
    }
  } else {
    //alert(message);
    return false;
  }
}
//disableRightClick();



// FINANCIELE FUNCTIES


function checkMoneyInput(field,e){
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e){
        keycode = e.which;
    } else{
        return true;
    }

    if ((keycode >= 58 && keycode <= 126) || (keycode >= 32 && keycode <= 43) || keycode == 45 || keycode == 47){
        return false;
    } else if (keycode == 44){
		//comma
        if(field.value.indexOf(',')!=-1){
            //already have a comma
            return false;
        }
    }
    return true;
}

function fix_price_format(field){

    var price = ''+field.value;
    //find indices of 1st dot & 1st comma
    var commaLoc = price.indexOf(",");
    var dotLoc = price.indexOf(".");

    if (price == "0,00")
    {
        return;
    }

    if (commaLoc == -1 && dotLoc == -1)
    {//if no comma and no dot
        price = price + ",00";
    }
    else if (commaLoc == -1 && dotLoc != -1)
    {//no comma, at least one dot
        var lastDot = price.lastIndexOf(".");
        if ( lastDot < price.length-3)
        {//no significant dot
            while (price.indexOf(".") != -1)
            {//fix for IE...
                price = price.replace(".","","g");
            }
            price = price + ",00";
        }
        else
        {//at least one significant dot
            if (lastDot == dotLoc)
            {// only one dot
                price = price.replace(".",",","g");
                if (lastDot == price.length-1)//dot at end
                {
                    price = price + "00";
                }
                else if (lastDot == price.length-2)
                {
                    price = price + "0";
                }
            }
            else
            {// multiple dots, one significant
                if (lastDot == price.length-1)
                {// last dot is at end of the string, no chars after dot
                  while (price.indexOf(".") != -1)
                    {//fix for IE...
                        price = price.replace(".","","g");
                    }
                    price = price + ",00";
                }
                else
                {//last dot not at end
                    var price2 = price.substring(0,lastDot)+",";
                    while (price2.indexOf(".") != -1)// remove dots in first part
                    {//fix for IE...
                        price2 = price2.replace(".","","g");
                    }
                    if (price.substring(lastDot+1).length == 1 )
                    {// one char after the dot
                        price = price2 + price.substring(lastDot+1) + "0";
                    }
                    else
                    {//2 chars after dot, keep them
                        price = price2 + price.substring(lastDot+1);
                    }
                //remove remaining insignificant dots
                while (price.indexOf(".") != -1)
                {//fix for IE...
                    price = price.replace(".","","g");
                }
                }
            }
        }

    }
    // have comma, maybe have dot: comma more important, remove all dots
        while (price.indexOf(".") != -1)
        {//fix for IE...
            price = price.replace(".","","g");
        }
        commaLoc = price.indexOf(","); //refind first comma location if dots are removed

        if(commaLoc == 0)
        {
            price = "0" + price;
            commaLoc = price.indexOf(",");
        }

        var lastComma = price.lastIndexOf(",");
        var price2 = price.substring(0,commaLoc);

        while (price2.indexOf("0") == 0 && price2.length > 1)//leading zeros should be removed, unless followed by a comma
        {
           price2 = price2.substring(1);
        }

        price2 = price2 + ",";//put comma back
        var price3 = "";

        if (lastComma != commaLoc)
        {//this should not be possible due to input filtering of checkMoneyInput(field,e) but code it to be sure
            price3 = price.substring(commaLoc+1);
            while (price3.indexOf(",") != -1)//remove remaining commas
            {//fix for IE..
                price3 = price3.replace(",","","g");
            }
        }
        else
        {//only one comma
            price3 = price.substring(commaLoc+1); //get string after the comma
        }
        if (price3.length > 1) // do the length of the string after comma
        {
            price = price2 + price3.substring(0,2);
        }
        else if (price3.length == 1)
        {
            price = price2 + price3 + "0";
        }
        else if (price3.length == 0)
        {
            price = price2 + "00";
        }
    //insert dots every 3 numbers, put max on 99.999.999,00 (without the ,00)
    if(parseInt(price) >= 99999999)
    {
        price = "99.999.999,00";
    }
    else{
        commaLoc = price.indexOf(",");
        euros = price.substring(0,commaLoc);
        cents = price.substring(commaLoc);

        if (euros.length > 3){
            if (euros.length >5){
                //cents not interesting no more
                cents = "";
            }
            if(euros.length > 6)
            {//insert 1st dot for million
                euros = euros.substring(0,euros.length-6) + "." + euros.substring(euros.length-6);
            }

            //insert dot for thousands
            euros = euros.substring(0,euros.length-3) + "." + euros.substring(euros.length-3);
        }
     price = euros + cents;

    }
    field.value = price;
}
