﻿/**
 * When a textbox has lost focus and is empty, set
 * a clarifiing text given in the text parameter.
 */
function TextBoxOnBlur(textbox, text)
{
    if(textbox.value == "")
    {
        textbox.style.color = "#cccccc";
        textbox.value = text;
    }    
}

/**
 * Clear the textbox if the text is equal to the text variable.
 */
function TextBoxOnFocus(textbox, text)
{
    if(textbox.value == text)
    {
        textbox.style.color = "";
        textbox.value = "";
    }
}

function ShowHidePanel(panelid) {
    var panel = document.getElementById(panelid);

    if (panel.style.visibility != 'visible') 
    {
        panel.style.visibility = 'visible';
    }
}

function SetContentPanel() {
    var contentpanel = document.getElementById('ctl00_pnlNestedContent');
    //alert(contentpanel.clientHeight);
    var footerpanel = document.getElementById('ctl00_pnlBottom');
    var treeview = document.getElementById('ctl00_pnlMenuLeftContent');
    var containerpanel = document.getElementById('ctl00_pnlMainBackground');

    if ((treeview.clientHeight + 200) > contentpanel.clientHeight) {
        footerpanel.style.top = (treeview.clientHeight + 760) + "px";
        containerpanel.style.height = (treeview.clientHeight + 650) + "px";
    }
    else {
        if (contentpanel.clientHeight < 475) {
            footerpanel.style.top = (contentpanel.clientHeight + 490) + "px";
            containerpanel.style.height = (contentpanel.clientHeight + 380) + "px";
        }
        else {
            footerpanel.style.top = (contentpanel.clientHeight + 410) + "px";
            containerpanel.style.height = (contentpanel.clientHeight + 300) + "px";
        }
    }
}

/**
 * Check the email adres if correct, do not interfere, else display red border.
 */
function CheckEmailAddress(textboxid, targetid)
{
    var textbox = document.getElementById(textboxid);
    if(!isValidEmailAddress(textbox.value))
    {
        textbox.style.border = "1px solid red";

        var target = document.getElementById(targetid);
        var info = '<div class="pnlNewsletterPopUp" id="pnlNewsletterPopUp"><div class="pnlPopUpHeader">' + 
        '<strong>Onjuist email adres</strong><div class="pnlCloseButton"><img src="images/btnClose.jpg" alt="Close" Style="cursor:pointer;" onclick="' + 
        'document.getElementById(\'pnlNewsletterPopUp\').style.visibility=\'hidden\';"></div></div>' + 
        'U dient een correct email adres op te geven.</div>';
        target.innerHTML = info;
        return false;
    }
    else
    {
        var target = document.getElementById(targetid);
        var info = '<div class="pnlNewsletterPopUp" id="pnlNewsletterPopUp"><div class="pnlPopUpHeader">' + 
        '<strong>Bedankt voor uw inschrijvig</strong><div class="pnlCloseButton"><img src="images/btnClose.jpg" alt="Close" Style="cursor:pointer;" onclick="' + 
        'document.getElementById(\'pnlNewsletterPopUp\').style.visibility=\'hidden\';"></div></div>' + 
        'Bedankt voor het inschrijven op onze nieuwsbrief.</div>';
        textbox.style.border = "1px solid #b8b9ba";
        target.innerHTML = info;
        return true;
    }
}

/**
 * Check the email adres if correct, do not interfere, else display red border.
 */
function CheckSearchInput(textboxid, targetid)
{
    var textbox = document.getElementById(textboxid);
    if ((textbox.value == 'Uw zoekterm...') || (textbox.value.length < 3))
    {
        //textbox.style.border = "1px solid red";
        textbox.style.color = "Red";
        
        var target = document.getElementById(targetid);
        /*var info = '<div class="pnlSearchPopUp" id="pnlSearchPopUp"><div class="pnlPopUpHeader">' + 
        '<strong>Onjuiste zoekopdracht</strong><div class="pnlCloseButton"><img src="images/btnClose.jpg" alt="Close" Style="cursor:pointer;" onclick="' + 
        'document.getElementById(\'pnlSearchPopUp\').style.visibility=\'hidden\';"></div></div>' + 
        'U dient minimaal drie tekens op te geven om een zoekactie te plaatsen.</div>';
        target.innerHTML = info;        */
        return false;
    }
    else
    {
        textbox.style.color = "#cccccc";
        //textbox.style.color = "#00543d";
        return true;
    }
}

/**
 * Check if the email adres is valid, if valid, return true, otherwise false.
 */
function isValidEmailAddress(address)
{
    var regex = new RegExp(/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i);

    return regex.test(address);
}

/**
 * Highlights the given menu item.
 */
function HighlightMenuItem(control, panelid, imageurl, color)
{
    var panel = document.getElementById(panelid);
    panel.style.backgroundColor = color;
    control.src = "../images/" + imageurl;
}

/**
 * Lowers the given menu item.
 */
function LowerMenuItem(control, panelid, imageurl, color)
{
    var panel = document.getElementById(panelid);
    control.src = "../images/" + imageurl;
    panel.style.backgroundColor = color;            
}

/**
 * Preload a given image.
 */
function PreloadImage(imageurl)
{
    if(document.images)
    {
        var image = new Image(100, 200);
        image.src = "../images/" + imageurl;
    }
}

/**
 * Changes the color of the row where the mouse is hovering above.
 */
function HighlightRow(row, color)
{
    row.style.backgroundColor = color;
}

/**
 * Changes the color of the row back to the original color.
 */
function LowerRow(row)
{
    row.style.backgroundColor = '';
}

/**
 * Shows the stock indicator panel.
 */
function ShowStockIndicatorPanel(panelid)
{
    var panel = document.getElementById(panelid);
    
    panel.style.visibility = 'visible';    
}

/**
 * Hides the stock indicator panel.
 */
function HideStockIndicatorPanel(panelid)
{
    var panel = document.getElementById(panelid);
    
    panel.style.visibility = 'hidden';    
}

/**
 * Checks the strength of the given password 
 * and returns the result to the user.
 */
function CheckPasswordStrength(textboxid, labelid)
{
    var textbox = document.getElementById(textboxid);
    var label = document.getElementById(labelid);
    var password = textbox.value;
    var points = 0;    
    
    // Check the length of the password
    if(password.length < 6) // Add 1 point
    {
        points += 1;
    }
    else
    {
        points += 3;
    }
    
    // Check letters and capitalized letters
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
    {
        points += 2;
    }
    
    // Check if the password contains any numbers with the letters
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))
    {
        points += 3;
    }    
    
    // Numbers, letters and special chars
    if(password.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
    {
        points += 5;
    }
    
    if(points < 4) // Very weak password
    {
        label.innerHTML = "Matig";
        label.style.color = 'red';
        textbox.style.borderColor = 'red';
    }
    else if((points > 3) && (points < 10))
    {
        label.innerHTML = 'Sterk';
        label.style.color = 'green';
        textbox.style.borderColor = 'green';
    }
    else
    {
        label.innerHTML = 'Uitstekend';
        label.style.color = '#0033cc';
        textbox.style.borderColor = '#0033cc';
    }
}

function ShowThumbnail(imageid, imageurl) 
{
    var image = document.getElementById(imageid);
    image.src = imageurl;
}

function ShowInformation(image, information, cellid)
{
    image.style.cursor = 'pointer'
    var cell = document.getElementById(cellid);
    var info = '<div class="pnlInformationPopUp">' + information + '</div>';
    cell.innerHTML = info;
}

function HideInformation(cellid)
{
    var cell = document.getElementById(cellid);
    var info = '<div class="pnlInformationPopUp" style="visibility: hidden;"></div>';
    cell.innerHTML = info;
}

function sendIdealRequest(targetUrl, fields)
{
    var form;
    var input;
        
    form = document.createElement('form');
    form.name = 'frmIdeal';
    form.action = targetUrl;
    form.method = 'post';
    
    /* Add all constant input fields with their associated values */
    for (var key in fields)
    {
        input = document.createElement('input');
        input.type = 'hidden';
        input.name = key;
        input.value = fields[key];
        form.appendChild(input);
    }
    
    document.body.appendChild(form);
    form.submit();
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src='" + img.src + "', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }
}

/**
* Check the guestbookmessage form, do not interfere, else display red border.
*/
function CheckMailForm(nameid, emailid, messageid) {
    var nametextbox = document.getElementById(nameid);
    var emailtextbox = document.getElementById(emailid);
    var messagetextbox = document.getElementById(messageid);

    /// Check the name input
    if (nametextbox.value.length < 3) {
        nametextbox.style.backgroundColor = "#CF8484";
        nametextbox.style.color = "White";
        return false;
    }
    else {
        nametextbox.style.backgroundColor = "";
        nametextbox.style.color = "";
    }

    /// Check the email input
    if (!isValidEmailAddress(emailtextbox.value)) {
        emailtextbox.style.backgroundColor = "#CF8484";
        emailtextbox.style.color = "White";
        return false;
    }
    else {
        emailtextbox.style.backgroundColor = "";
        emailtextbox.style.color = "";
    }

    /// Check the message input
    if (messagetextbox.value.length < 3) {
        messagetextbox.style.backgroundColor = "#CF8484";
        messagetextbox.style.color = "White";
        return false;
    }
    else {
        messagetextbox.style.backgroundColor = "";
        messagetextbox.style.color = "";
    }

    return true;
}
