function SwitchLanguage(lang) {
    var pathToRedirect;
    var fullPath = location.href;
    var path = ExtractPathOnly(fullPath);
    var webPage = ExtractPage(fullPath);

    //alert(fullPath);
    //alert(path);
    //alert(webPage);

    if (lang == 'en') {
        pathToRedirect = 'en' + webPage;
    }
    else if (lang == 'de') {
        pathToRedirect = path + webPage
    }
    window.location = pathToRedirect;

}

function ExtractPage(strUrl) {
    urlLength = strUrl.length;
    index = strUrl.lastIndexOf('/');
    return strUrl.substring(index, urlLength);
}

function ExtractPathOnly(strUrl) {
    index = strUrl.lastIndexOf('/');
    return strUrl.substring(0, index);
}

function IsValidEmail(emailText) 
{
    var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    return pattern.test(emailText)
}

function IsValidName(text) {
    var pattern = /^[a-zA-Z.\s]+$/;
    return pattern.test(text)
}

function SubmitForm() {
    var errorBgColor = '#ffa1a1';
    var normalColor = 'white';
    
    firstNameElm = document.getElementById('first_name');
    lastNameElm = document.getElementById('last_name');
    email = document.getElementById('from_email');
    purpose = document.getElementById('purpose');
    comment = document.getElementById('comment');

    var invalidCount = 0;

    if (firstNameElm.value.length == 0 || !IsValidName(firstNameElm.value)) {
        firstNameElm.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        firstNameElm.style.background = normalColor;
    }

    if (lastNameElm.value.length == 0 || !IsValidName(lastNameElm.value)) {
        lastNameElm.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        lastNameElm.style.background = normalColor;
    }

    if (email.value.length == 0 || !IsValidEmail(email.value)) {
        email.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        email.style.background = normalColor;
    }

    if (comment.value.length == 0) {
        comment.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        comment.style.background = normalColor;
    }
    
    if (purpose.selectedIndex == 0) {
        purpose.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        purpose.style.background = normalColor;
    }

    if (invalidCount == 0) {
        //alert('TO DO: send mail.');
        //document.getElementById('contactForm').submit();
        document.getElementById('contactForm').submit();    
    }
    

}

function SubmitSurvey() {
    var errorBgColor = '#ffa1a1';
    var normalColor = 'white';

    
    email = document.getElementById('email');


    var invalidCount = 0;


    if (email.value.length == 0 || !IsValidEmail(email.value)) {
        email.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        email.style.background = normalColor;
    }

    if (invalidCount == 0) {
        //alert('TO DO: send mail.');
        //document.getElementById('contactForm').submit();
        document.getElementById('surveyForm').submit();
    }


}

function SendMailToFriend()
{
    var errorBgColor = '#ffa1a1';
    var normalColor = 'white';
    
    sender_email = document.getElementById('sender_email');
    receiver_email = document.getElementById('receiver_email');
    comment = document.getElementById('comment');
    captcha = document.getElementById('captcha');

    var invalidCount = 0;

    if (sender_email.value.length == 0 || !IsValidEmail(sender_email.value)) {
        sender_email.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        sender_email.style.background = normalColor;
    }


    if (receiver_email.value.length == 0 || !IsValidEmail(receiver_email.value)) {
        receiver_email.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        receiver_email.style.background = normalColor;
    }

    if (comment.value.length == 0) {
        comment.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        comment.style.background = normalColor;
    }
    
    if (captcha.value.length == 0) {
        captcha.style.background = errorBgColor;
        invalidCount++;
    }
    else {
        captcha.style.background = normalColor;
    }
    
      

    if (invalidCount == 0) {
        document.getElementById('sendMailToFriend').submit();    
    }
}
