function elm(id)
{
  return document.getElementById(id);
}



function switchPreviewColor(url)
{
   document.getElementById('colorPreviewImg').src = url;
}

function switchPreviewSize(width, height, actualwidth, actualheight, color, code)
{
   document.getElementById('sizePreviewDiv').style.width = width+"px";
   document.getElementById('sizePreviewDiv').style.height = height+"px";
   document.getElementById('sizePreviewDiv').style.backgroundColor = color;
   
   
   
   var html = "<strong>inbouwmaat</strong> "+actualwidth+" x "+actualheight+" <br /><strong>maatcodering</strong> "+code+" <br /><strong>netto</strong> glasoppervlakte <br /><strong>isolatiewaarde</strong> <br />";
   
   document.getElementById('sizePreviewText').innerHTML = html;
}

function loader(productsAdded, numProducts, file)
{
  var percent = numProducts / 100;
  var percent = Math.round(productsAdded / percent);
  document.getElementById('loaderBar_'+file).style.width = percent+'%';
  document.getElementById('loaderText_'+file).innerHTML = percent+'%';
}

function updateMessage(file, errorString, warningString, numImported, errorCount, warningCount)
{
   var numbers = errorCount+" fouten / "+warningCount+" waarschuwingen";

   if((errorString)||(warningString))
   {

     document.getElementById('message_'+file).innerHTML = "<span style='color: red;'>"+numImported+" producten succesvol geimporteerd. <a onclick='showErrors(this.id);' id='"+file+"'>( "+numbers+" )</a><div id='errorBox_"+file+"' class='importErrorBox'><div><a onclick=\"showErrors('"+file+"');\">Sluiten</div>"+errorString+" "+warningString+"</div></span>";
          
   }
   else
   {
     document.getElementById('message_'+file).innerHTML = "<span style='color: green;'>"+numImported+" producten succesvol geimporteerd</span>";
     
   }
   
}

var chosenExactCode = '';

function selectVenster(id, exactCode)
{ 
  chosenExactCode = exactCode;
}

function showErrors(filename)
{
  if(document.getElementById('errorBox_'+filename).style.visibility == 'visible')
    document.getElementById('errorBox_'+filename).style.visibility = 'hidden';
  else
    document.getElementById('errorBox_'+filename).style.visibility = 'visible';
}

function showOrHideById(id)
{
   var elm = document.getElementById(id);
   if(elm.style.visibility == 'visible')
      elm.style.visibility = 'hidden';
   else
      elm.style.visibility = 'visible';
}

function pointVenster(id,color)
{
   var row = document.getElementById('row_'+id);
   if(!color)
   {
     row.style.color = 'black';
     row.style.fontSize = '9px';
   }  
   else
   {
     row.style.fontSize = '11px';
     row.style.color = color;
   }
}

function hideVenster(id)
{
   var color = document.getElementById('row_'+id+'_color').value;
   document.getElementById('row_'+id).style.backgroundColor = color;
}


function validateAndSubmit_old(newClient)
{
  var fields = document.getElementsByClassName('obligatorily');
  
  if(newClient)
  var passFields = document.getElementsByClassName('obligatorilyPass');
  var noErrors = true;
  cleanUpErrorMessages();
  
  
  
  for(i=0;i<fields.length;i++)
  {
   
   
    var value = fields[i].value;
    var errorBox = fields[i].name+"Error";
    
    if((value=='')||(!value))
    {
       noErrors = false;
       fields[i].style.border = "1px solid #ff5d5d";
       fields[i].style.backgroundColor = "#ffdbdb";
       
       if(fields[i].id=='tfStreetNumber')
       {
          var errorBox = "tfStreetError";
       }
       
    
         document.getElementById(errorBox).innerHTML = "<span style='color: red;'>Geen of onjuiste invoer!</span>";
       
    }
    else
    {
    
    if(newClient)
    {
    
    
    
    ///////// check email error field ///////
    if(fields[i].name=='tfEmail')
       {
         name.substr(0,name.indexOf('Error'));
         emailErrorBoxContent = document.getElementById(fields[i].name+"Error").innerHTML;
         if(emailErrorBoxContent.search(/beschikbaar/)==-1) 
           noErrors = false;
       }
    }

    }
  }
  if(newClient)
  {
  
  var email1 = document.getElementById('tfEmail').value;
    var email2 = document.getElementById('tfEmailAgain').value;
  
  if(email1!=email2)
  {
    noErrors = false;
    document.getElementById("tfEmailAgainError").innerHTML = "<span style='color: red;'>E-mailadressen zijn niet gelijk</span>";
  }

  
  if((passFields[0].value!=passFields[1].value)||(passFields[0].value==''))
  {
    
    document.getElementById("pfPasswordError").innerHTML = "<span style='color: red;'>Geen of onjuiste invoer!</span>";
    noErrors = false;
    passFields[0].style.border = "1px solid #ff5d5d";
    passFields[0].style.backgroundColor = "#ffdbdb";
    passFields[1].style.border = "1px solid #ff5d5d";
    passFields[1].style.backgroundColor = "#ffdbdb";
    if(!passFields[0].value=='')
      document.getElementById('pfPasswordError').innerHTML = "<div style='position: relative; top: 10px; color: red; font-size: 10px;padding-left: 20px;'><img src='/img/shop/passerror.jpg' style='position: absolute; top: -2px; left: 2px;'/>wachtwoorden zijn niet gelijk</div>";
  }
  }
  
  if(newClient)
  {
    var box = document.getElementById('AVbox');
    if(!box.checked)
    {
      noErrors = false;
      document.getElementById('AVboxError').innerHTML = "<div style=' color: red; font-size: 10px; padding-left: 10px;'><img src='/img/shop/AVerror.jpg' style='position: relative; top: -4px; margin-right: 5px;'/>Lees en accepteer onze algemene voorwaarden</div>";
    
    }
 }
  
  
  if(noErrors)
    document.getElementById('checkedForm').submit();
 
  
}

function cleanUpErrorMessages()
{
var boxes = document.getElementsByClassName('error_message');

		// loop through all checkboxes
		for (var i=0; i<boxes.length; i++){
        
        if(boxes[i].id!='tfEmailError')
		boxes[i].innerHTML = '';
				
		var name = boxes[i].id;
        var fieldName = name.substr(0,name.indexOf('Error')); 
     
        if(fieldName!='AVbox')
		document.getElementById(fieldName).style.border='1px solid #CCC';
		document.getElementById(fieldName).style.backgroundColor='white';  
		}
}

function checkIfExists(address)
{
   var file = "../inc/checkEmail.php?address="+address;
   var action = "checkEmail";
   //alert(file);
   dbRequest(action, file, 0, 0);
}

function checkIfExistsAdmin(address, original)
{
   if(address!=original)
   {
     var file = "../inc/checkEmail.php?address="+address;
     var action = "checkEmail";
     //alert(file);
     dbRequest(action, file, 0, 0);
   }
   else
   {
     writeCheckMessage('');
   }
}

function generatePass(limit, inclNumbers)
{
    var vowels = 'aeiou'.split('');
    var constonants = 'bcdfghjklmnpqrstvwxyz'.split('');
    var word = '', i, num;

    if (!limit) limit = 8;

    for (i = 0; i < (inclNumbers ? limit - 3 : limit); i++) {
        if (i % 2 == 0) { // even = vowels
            word += vowels[Math.floor(Math.random() * 4)]; 
        } else {
            word += constonants[Math.floor(Math.random() * 20)];
        } 
    }

    if (inclNumbers) {
        num = Math.floor(Math.random() * 99) + '';
        if (num.length == 1) num = '00' + num;
        else if (num.length == 2) num = '0' + num;
        word += num;
    }

    document.getElementById('pfPassword').value = word.substr(0, limit);
    document.getElementById('pfPasswordAgain').value = word.substr(0, limit);
}

function calculateTotal()  
{
  var pieceEuros = document.getElementById('newPieceEuros').value;
  var pieceCents = document.getElementById('newPieceCents').value;
  var num = document.getElementById('newNum').value;
  
  var hups = pieceEuros + '' + pieceCents;
  hups = parseInt(hups);
  num = parseInt(num);

  hups = hups * num; 
  hups += '';
  
  var totalEuros = hups.substr(0,hups.length-2);
  var totalCents = hups.substr(hups.length-2, 2);
  
  document.getElementById('newTotalEuros').value = totalEuros;
  document.getElementById('newTotalCents').value = totalCents;
  
  
}
