﻿/**
 * 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 = "#666666";
        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 = "";
    }    
}

/**
 * Check the guestbookmessage form, do not interfere, else display red border.
 */
function CheckComplaint(nameid, streetid, numberid, zipcodeid, cityid, phoneid, emailid, messageid)
{
    var nametextbox = document.getElementById(nameid);
    var streettextbox = document.getElementById(streetid);
    var numbertextbox = document.getElementById(numberid);
    var zipcodetextbox = document.getElementById(zipcodeid);
    var citytextbox = document.getElementById(cityid);
    var phonetextbox = document.getElementById(phoneid);
    var emailtextbox = document.getElementById(emailid);
    var messagetextbox = document.getElementById(messageid);
    
    /// Check the name input
    if(nametextbox.value.length < 3)
    {
        nametextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        nametextbox.style.border = "";
    }
    
    /// Check the street input
    if(streettextbox.value.length < 3)
    {
        streettextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        streettextbox.style.border = "";
    }
    
    /// Check the address number input
    if(numbertextbox.value.length < 1)
    {
        numbertextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        numbertextbox.style.border = "";
    }

    /// Check the zipcode input
    if(zipcodetextbox.value.length < 4)
    {
        zipcodetextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        zipcodetextbox.style.border = "";
    }
    
    /// Check the city input
    if(citytextbox.value.length < 2)
    {
        citytextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        citytextbox.style.border = "";
    }
    
    /// Check the phone input
    if(phonetextbox.value.length < 10)
    {
        phonetextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        phonetextbox.style.border = "";
    }
    
    /// Check the email input
    if(!isValidEmailAddress(emailtextbox.value))
    {
        emailtextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        emailtextbox.style.border = "";
    }
    
    /// Check the message input
    if(messagetextbox.value.length < 3)
    {
        messagetextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        messagetextbox.style.border = "";
    }
    
    return true;
}

/**
 * Check the guestbookmessage form, do not interfere, else display red border.
 */
function CheckGuestbookMessage(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.border = "1px solid red";
        return false;
    }
    else
    {
        nametextbox.style.border = "";
    }
    
    /// Check the email input
    if(!isValidEmailAddress(emailtextbox.value))
    {
        emailtextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        emailtextbox.style.border = "";
    }
    
    /// Check the message input
    if(messagetextbox.value.length < 3)
    {
        messagetextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        messagetextbox.style.border = "";
    }
    
    return true;
}

/**
 * Check the email adres if correct, do not interfere, else display red border.
 */
function CheckEmailAddress(textboxid)
{
    var textbox = document.getElementById(textboxid);
    if(!isValidEmailAddress(textbox.value))
    {
        textbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        return true;
    }
}

/**
 * Check the email adres if correct, do not interfere, else display red border.
 */
function CheckSearchInput(textboxid)
{
    var textbox = document.getElementById(textboxid);
    if((textbox.value == 'Zoekopdracht') || (textbox.value.length < 3))
    {
        textbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        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;
    row.style.cursor = 'pointer';
}

/**
 * Changes the color of the row back to the original color.
 */
function LowerRow(row)
{
    row.style.backgroundColor = '';
}

/**
 * Shows the menu panel.
 */
function ShowMenu(panelid)
{
    var panel = document.getElementById(panelid);
    
    panel.style.height = '';
    panel.style.width = '150px'
    panel.style.cursor = 'pointer';
}

/**
 * Hides the menu panel.
 */
function HideMenu(panelid)
{
    var panel = document.getElementById(panelid);
    
    panel.style.height = '17px';    
    panel.style.width = '';
}

/**
 * Checks the strength of the given password 
 * and returns the result to the user.
 */
function CheckPasswordStrength(textboxid, panelid)
{
    var textbox = document.getElementById(textboxid);
    var panel = document.getElementById(panelid);
    var password = textbox.value;
    var points = 0;    
    alert(panel.innerHtml);
    //alert(document.getElementById("ctl00_cpPageContent_lblePassword").innerHtml);
    
    // 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
    {
        textbox.style.borderColor = 'red;';
        panel.innerText = 'Matig wachtwoord';
    }
    else if((points > 3) && (points < 8))
    {
        textbox.style.borderColor = 'orange;';
        panel.innerText = 'Redelijk wachtwoord';
    }
    else if((points > 9) && (points < 10))
    {
        textbox.style.borderColor = 'yellow;';
        panel.innerText = 'Goed wachtwoord';
    }
    else
    {
        textbox.style.borderColor = 'green;';
        panel.innerText = 'Uitstekend wachtwoord';
    }
}

function popUp(URL) 
{
    day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=400,left = 192,top = 144');");
}

/**
 * Check the guestbookmessage form, do not interfere, else display red border.
 */
function CheckMessage(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.border = "1px solid red";
        return false;
    }
    else
    {
        nametextbox.style.border = "";
    }
    
    /// Check the email input
    if(!isValidEmailAddress(emailtextbox.value))
    {
        emailtextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        emailtextbox.style.border = "";
    }
    
    /// Check the message input
    if(messagetextbox.value.length < 3)
    {
        messagetextbox.style.border = "1px solid red";
        return false;
    }
    else
    {
        messagetextbox.style.border = "";
    }
    
    return true;
}

