﻿function setCookie(id, collapse, root) {
    var http_request = false;
    if (window.XMLHttpRequest) // Mozilla, Safari....
    {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
            http_request.overrideMimeType('text/xml');
    }
    else		// IE
    {
        if (window.ActiveXObject) {
            try
				{ http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
            catch (e)
				{ http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
        }
    }

    http_request.open('POST', root + '/service/controlservice.asmx/SetCookie', true);
    try {
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    catch (e) { }

    http_request.onreadystatechange = function() {
        try {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                }
                else { }
            }
        }
        catch (e)
		{  }
    }
    http_request.send('id=' + id + '&collapsed=' + collapse);
}

// Copied from http://www.scripts.com/viewscript/validate-email-address/17603/
function ValidateString(string, return_invalid_chars) {
    valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    invalid_chars = '';

    if (string == null || string == '')
        return (true);

    //For every character on the string.   
    for (index = 0; index < string.length; index++) {
        char = string.substr(index, 1);

        //Is it a valid character?
        if (valid_chars.indexOf(char) == -1) {
            //If not, is it already on the list of invalid characters?
            if (invalid_chars.indexOf(char) == -1) {
                //If it's not, add it.
                if (invalid_chars == '')
                    invalid_chars += char;
                else
                    invalid_chars += ', ' + char;
            }
        }
    }

    //If the string does not contain invalid characters, the function will return true.
    //If it does, it will either return false or a list of the invalid characters used
    //in the string, depending on the value of the second parameter.
    if (return_invalid_chars == true && invalid_chars != '') {
        last_comma = invalid_chars.lastIndexOf(',');

        if (last_comma != -1)
            invalid_chars = invalid_chars.substr(0, $last_comma) +
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);

        return (invalid_chars);
    }
    else
        return (invalid_chars == '');
}


function ValidateEmailAddress(email_address) {
    //Assumes that valid email addresses consist of user_name@domain.tld
    at = email_address.indexOf('@');
    dot = email_address.lastIndexOf('.');

    if (at == -1 ||
            dot == -1 ||
            dot <= at + 1 ||
            dot == 0 ||
            dot == email_address.length - 1)
        return (false);

    user_name = email_address.substr(0, at);
    domain_name = email_address.substr(at + 1, email_address.length);

    if (ValidateString(user_name) === false ||
            ValidateString(domain_name) === false)
        return (false);

    return (true);
}

function trim(value) {
    value = value.replace(/^\s+/, '');
    value = value.replace(/\s+$/, '');
    return value;
}

//Checkt het formaat van de aangeleverde input.
//Let op: bij meerdere adressen dienen deze gescheiden te zijn door een ; OF een ,
function CheckEmailInputValues(source, eventArgs) {
    var separator = null;
    if (eventArgs.Value.indexOf(';') > -1) {
        separator = ';';
    }
    if (eventArgs.Value.indexOf(',') > -1) {
        separator = ",";
    }
    
    if (separator != null) {
        var addresses = eventArgs.Value;
        var index = 0;
        var nextIndex = 0;
        var stillValid = true;
        
        while (index <= (addresses.lastIndexOf(separator) + 1) && stillValid) {
            nextIndex = addresses.indexOf(separator, index + 1);
            if (nextIndex == -1) {
                nextIndex = addresses.length;
            }
            var address = trim(addresses.substring(index, nextIndex));
            stillValid = ValidateEmailAddress(address);
            index = nextIndex + 1;
        }
        eventArgs.IsValid = stillValid;
    }
    else {
        eventArgs.IsValid = ValidateEmailAddress(trim(eventArgs.Value));
    }

}

var newwnd;

function windowopen(id, url, height, width, scrollbars, sizeable, force) {
    var newWindow = true;
    var features = 'width=' + width + ',height=' + height + ',status=0,scrollbars=' + scrollbars + ',sizeable=' + sizeable;

    if (newwnd) {
        try {
            if (newwnd.document) newWindow = false;
        }
        catch (e) {
        }
    }

    if (force) {
        newWindow = force;
        newwnd = null;
    }

    if (newWindow)
        newwnd = window.open(url, id, features);
    else {
        newwnd.document.location.href = url;
        newwnd.resizeTo(width, height);
    }

    newwnd.focus();
}

// ****************** //
// MySettings scripts //
//       BEGIN        //
// ****************** // 
function showClearViewedLink() {
    document.getElementById('clearviewedcollection').style.display = 'inline';
}

function hideClearViewedLink() {
    document.getElementById('clearviewedcollection').style.display = 'none';
}

function showClearKofferLink() {
    document.getElementById('clearkoffer').style.display = 'inline';
}

function hideClearKofferLink() {
    document.getElementById('clearkoffer').style.display = 'none';
}
// ****************** //
// MySettings scripts //
//       EINDE        //
// ****************** // 
function GetRadWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}

function CloseWindow() {
    var oWindow = GetRadWindow();
    oWindow.Close();
}


// ********************** //
//  Master Page scripts   //
//         EINDE          //
// ********************** //

function NormalizeString(input) {
    var text = input;
    
    text = text.replace('é', 'e');
    text = text.replace('ë', 'e');
    text = text.replace('ê', 'e');
    text = text.replace('è', 'e');

    text = text.replace('á', 'a');
    text = text.replace('ä', 'a');
    text = text.replace('â', 'a');
    text = text.replace('à', 'a');

    text = text.replace('ó', 'o');
    text = text.replace('ö', 'o');
    text = text.replace('ô', 'o');
    text = text.replace('ò', 'o');

    text = text.replace('í', 'i');
    text = text.replace('ï', 'i');
    text = text.replace('î', 'i');
    text = text.replace('ì', 'i');

    text = text.replace('ú', 'u');
    text = text.replace('ü', 'u');
    text = text.replace('û', 'u');
    text = text.replace('ù', 'u');

    text = text.replace('(', '');
    text = text.replace(')', '');
    text = text.replace(':', '');

    return text;
}

function toggle(id, root) {
    var list = document.getElementById(id);
    var obj;
    var collapse = false;

    for (var i = 0; i < list.getElementsByTagName('dd').length; i++) {
        obj = list.getElementsByTagName('dd')[i];
        if (obj) {
            if (obj.style.display != 'none') {
                obj.style.display = 'none';
                collapse = true;
            }
            else
                obj.style.display = '';
        }
    }

    if (collapse)
        list.getElementsByTagName('dt')[0].className = 'collapsed';
    else
        list.getElementsByTagName('dt')[0].className = '';

    setCookie(id, collapse, root);
}

// **************
// Zoek bij Enter
// **************
/*
function getEnterAction(txtSearch, obj) {
    alert(txtSearch + "is voige enter ");
    if (event.which || event.keyCode) {
        if ((event.which == 13) || (event.keyCode == 13)) {
            var text = obj.value;
            if (text.length > 0) {
                if (event.which || event.keyCode) {
                    if ((event.which == 13) || (event.keyCode == 13)) {
                        var a = txtSearch.replace(/_/gi,  "$");
                        var b = a + '$ctl01';
                        __doPostBack(b, '');
                        return false;
                    }
                }
                else
                { return true; };
            }    
        } 
    }
}
*/
function SubmitSearch() {
    var text = NormalizeString(document.getElementById(txtSearch).value);
    window.status = 'Er wordt voor u gezocht op ' + text + '...'
    // Deze service moeten we straks wel weer aanzetten.
    // dit is om te controleren waarop mensen hebben gezocht.
    //registerSearchWord(text);
    window.location = appRoot + '/zoeken.aspx?zoek=' + text;
    return false;
}

var Nav = 'NS';
if (navigator.appName == 'Microsoft Internet Explorer') Nav = 'IE';
document.onkeydown = keyDown;
if (Nav == 'NS') document.captureEvents(Event.KEYDOWN | Event.KEYUP);

function keyDown(DnEvents) {
    var k = (Nav == 'NS') ? DnEvents.which : window.event.keyCode;
    if (k == 13) {
        if (document.getElementById(txtSearch)) {
            var tb = document.getElementById(txtSearch).value;
            if ((tb != '') && (tb.length > 0)) {
                SubmitSearch();
                window.event.keyCode = 9;
            }
        }      
    }
}

function registerSearchWord(word) {
    var http_request = false;
    if (window.XMLHttpRequest) // Mozilla, Safari....
    {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
            http_request.overrideMimeType('text/xml');
    }
    else		// IE
    {
        if (window.ActiveXObject) {
            try
				{ http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
            catch (e)
				{ http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
        }
    }

    //http_request.open('POST', appRoot + '/service/controlservice.asmx/RegisterSearchWord', true);
    try {
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    catch (e) { }

    http_request.onreadystatechange = function() {
        try {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                }
                else { }
            }
        }
        catch (e)
		{ }
    }
    //http_request.send('word=' + word);
}

function printpage(root, reiscode) {
    windowopen('printvakantie', root + '/popups/printpage.aspx?productcode=' + reiscode, 900, 850, 1, 0, true);
    
}
function resetBoeking(day, vertrek, terug) {
    //var a = document.getElementByID("<%=ddVertrekdatum.ClientID%>").innerText;
    alert( day + " - "+ vertrek +" - "+ terug);
}

//function showExceptionText() {
//    document.getElementById('exceptionMessage').style.height = 'auto';
//    document.getElementById('exceptionText').style.display = 'block';
//}    
