

/*
 * Copyright 2011 DELVING BV
 *
 * Licensed under the EUPL, Version 1.1 or as soon they
 * will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 * you may not use this work except in compliance with the
 * Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://ec.europa.eu/idabc/eupl
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the Licence is
 * distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the Licence for the specific language governing
 * permissions and limitations under the Licence.
 */

function takeMeBack(){
    var futdate = new Date()		//Get the current time and date
    var expdate = futdate.getTime()  //Get the milliseconds since Jan 1, 1970
    expdate += 120000  //expires in 2 minutes (milliseconds)
    var location = document.location.href;
    $.cookie('takeMeBack', location, { expires: expdate,  path: portalName });
}

function setLang(lang) {
       takeMeBack();
       var langform = document.getElementById("frm-lang");
       var langval = langform.lang;
       langval.value = lang;
       langform.submit();
}

function toggleObject(oId) {
    var oObj = document.getElementById(oId);
    var cObj = (oObj.style.display == "none") ? "block" : "none";
    oObj.style.display = cObj;
}

function highLight(oId){
    // makes use of jQuery and jQueryUI javascript files
    if($(oId).length > 0){
       $(oId).effect("pulsate", { times:4 }, 500);
    }
    return false;
}

function writeMessage(target,msg){
    $(target).html(msg);
}

function ContactMe(prefix,suffix){
    var m =  Array(109,97,105,108,116,111,58);
    var s = '';
    for (var i = 0; i < m.length; i++){
        s += String.fromCharCode(m[i]);
    }
    window.location.replace(s + prefix + String.fromCharCode(8*8) + suffix);
    return false;
}

function isEmpty( inputStr ) {
    if ( null === inputStr || "" == inputStr ) {
        return true;
    }
    return false;
}

function checkFormSimpleSearch(oId){

    var sQuery = $("form#formSimpleSearch input#query");
    if (isEmpty(sQuery.val())){
        sQuery.css("background","#fffff0");
        showMessage("error","You must enter a search term");
                return false;
    } else {
        return true;
    }
}


function delvingPageCall(targetId,pageName,msgHead,msgBody,msgLink){

    if(!msgHead){msgHead="Error";}
    if(!msgBody){msgBody="Something went wrong";}

    $.ajax({
      url: pageName,
      async: false,
      cache: false,
      processData: false,
//      context: document.body.content,
      type: "GET",
        success: function(data) {
            if(data == "This page does not exist."){
//                $(targetId).html("<h2>"+msgHead+"<\/h2><p>"+msgBody+"</p>");
                $(targetId).html("<h2>Fout<\/h2>");
            }else{
                $(targetId).html(data);
            }
        },
        fail: function(data){
            alert('error');
        }
    });
}

function showMessage(messageClass, messageString){
    var top = $(document).scrollTop()+"px";
    $("#messages .message").html(messageString);
    $("#messages").addClass(messageClass).slideDown("slow").delay(5000).slideUp("slow");
    $("#messages").click(function(){
        $(this).stop(true, true).css("display","none");
    });
}

function showConfirm(messageClass, messageString, callback){
    var top = $(document).scrollTop()+"px";
    $("div#confirmation .message").html(messageString);
    $("div#confirmation").addClass(messageClass).slideDown("slow");
    $("div#confirmation button#cancel").click(function(){
        $("#confirmation").slideUp("slow");
        return false;
    });
    $("#confirmation button#ok").click(function(){
        $("div#confirmation").slideUp("slow");
         eval(callback);
    });
}

function showDefaultImage(obj) {
    obj.src = baseThemePath+'/images/defaultimage.jpg';
}



$(document).ready(function() {

    $("#formSimpleSearch").validate({
        errorLabelContainer: ".message",
        errorContainer: "",
        rules: {query: "required"},
        messages: {query: "" },
        invalidHandler: function(form, validator) {
          var errors = validator.numberOfInvalids();
          if (errors) {
            var message = "";
            try {
              message = msqRquiredSearchTerm; //defined in ftl macros
            }
            catch(err) {
              message = "U moet een zoekopdracht invullen";
            }
            showMessage("error", message);
            return false;
          }
        }
    });
    //onclick for login href to take user back to last visited page before logging in
    var pathname = window.location.pathname;
    if(!(pathname == portalName+"/register-request.html"
            || pathname==portalName+"/register-success.html"
            || pathname==portalName+"/forgot-password.html"
            || pathname==portalName+"/change-password-success.html"
            || pathname==portalName+"/login.html")){

       if($("a#login, a#logout")){
            $("a#login, a#logout").click(function(){
                takeMeBack();
            })
        }
    }
});



