var TABS = {};

function popup_show() {
  document.getElementById("infoPopup").style.display = "";
}

function popup_hide() {
  document.getElementById("infoPopup").style.display = "none";
}

function popup_showTab(elem)
{
  id = elem.id;
  group = id.split('_')[0];
  tab_id = id + "_tab";
  
  prev = TABS[group + "_tab"];
  foo = TABS[group + "_foo"];
  curr = document.getElementById(tab_id);
  
  if (prev) prev.style.display = "none";
  if (foo) foo.className = foo.className.substring(0,foo.className.length-9);
  if (curr) curr.style.display = "block";
  if (elem) elem.className += " selected";
  
  TABS[group + "_tab"] = curr;
  TABS[group + "_foo"] = elem;

  if (typeof(urchinTracker) == 'function') urchinTracker(search_results['trackers'][horizon_last_ad_id] + "/" + tab_id);
}

function popup_setDataAndShow(elem, id)
{

  if(!this.done) {
    this.done = [];
    search_results['periods'] = [];
  }
  
  horizon_last_ad_id = id;

 
//  if(!this.done[id]) {
//    this.done[id] = true;
//    doRequest(
//        "/_pages/_mforce_details.php",
//        function(){},
//        { "id": id });
//    urchinTracker(search_results['trackers'][id]);

    
//    doRequest(
//        "/_pages/__details_periods.php",
//        function(x) { search_results['periods'][id] = x.response; 
//          document.getElementById('popup_date_tab').innerHTML = x.response;},
//          { "id": id, "period": s_periods });
//  }
  
  // set data voodoo
  document.getElementById('popup_photos_tab').innerHTML = search_results['fotos'][id];
  //document.getElementById('popup_date_tab').innerHTML = search_results['vertrekdata'][id];
  document.getElementById('popup_info_tab').innerHTML = search_results['info'][id];
  document.getElementById('popup_area_tab').innerHTML = landinfo[search_results['landcodes'][id]];
  if (typeof search_results['periods'][id] != 'undefined')
    document.getElementById('popup_date_tab').innerHTML = search_results['periods'][id];
  // call show
  popup_show();
  popup_showTab(elem);
}

function preview_popup(elem)
{
    popup_show();
    popup_showTab(elem);
}

function popup_setPhoto(elem) {
    document.getElementById("popup_img_main").src = elem.src
}

function favoritesPopUp(visibility, id)
{
    var elem = document.getElementById(id);

	if(elem != null)
	{	
	    elem.style.visibility = visibility;
    	elem.style.left = getWidth() / 2 - 315;
	
    	if (visibility != "visible") {
        	return false;
    	}
	
		favoritesUpdate();
	}
 	
	
    return false;
}

function favoritesUpdate() {
	doRequest(
        "/include/favorites.inc.php",
        function(x) { 
          document.getElementById('favorites_content').innerHTML = x.response;},
          { "ids": getCookie("favorites") });
          
    var count = getCookie("favorites").split(",").length;
    
    var elem = document.getElementById("favorites_content");
    
    switch(count) {
    case 0:
        elem.style.height = 100;
        break; 
    case 1:
        elem.style.height = 220;
        break;
    default:
        elem.style.height = 420;
        break;
    }
}



function mailerPopUp(visibility, id)
{
    elem = document.getElementById(id);
    elem.style.visibility = visibility;
    elem.style.left = getWidth() / 2 -250;
    
    return false;
}

function stuurmailerPopUp(visibility, id)
{
    var email = document.getElementById("stuurmail").value;
    if ((email == null) || (email == "")) {window.alert('Vul SVP een correct e-mailadres in.'); return; }
    if(!email.match(/^[A-Za-z0-9\.\_]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
    { window.alert('Vul SVP een correct e-mailadres in'); return false }
    else { document.getElementById("stuuremail").value = email;
    elem = document.getElementById(id);
    elem.style.visibility = visibility;
    elem.style.left = getWidth() / 2 -250;

    return false; }
}

function checkStuur(obj){
	var email = document.getElementById("stuuremail").value;
	var names = document.getElementById("clientNames").value;
	var surn = document.getElementById("clientSur").value;
	var checkobj = 0;
        if(obj.checked) checkobj++;
            else checkobj--;
        if (checkobj<=0) document.getElementById("button_mailer").disabled = true;
            else document.getElementById("button_mailer").disabled = false;
	if ((email == null) || (email == "")) {document.getElementById("button_mailer").disabled = true; obj.checked = false; window.alert('No email!!!'); return false }
	if(!email.match(/^[A-Za-z0-9\.\_]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
	{ document.getElementById("button_mailer").disabled = true; window.alert('Vul SVP een correct e-mailadres in'); obj.checked = false; return false }
//	if (mailcheckform() != true) { document.getElementById("button_mailer").disabled = true; obj.checked = false; return false }
mailcheckform();
	if ((names == null) || (names == "")) {document.getElementById("button_mailer").disabled = true; obj.checked = false; window.alert('No Name!!!'); return false }
	if ((surn == null) || (surn == "")) {document.getElementById("button_mailer").disabled = true; obj.checked = false; window.alert('No Surname!!!'); return false }  
} 


function stuurmailerSend(visibility, id) {

    var email = document.getElementById("stuuremail").value;
    var names = document.getElementById("clientNames").value;
    var surn = document.getElementById("clientSur").value;
    if ((email == null) || (email == "")) {window.alert('No email!!!'); return false }
    if(!email.match(/^[A-Za-z0-9\.\_]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
    { window.alert('Vul SVP een correct e-mailadres in'); return false }
    if ((names == null) || (names == "")) {window.alert('No Name!!!'); return false }
    if ((surn == null) || (surn == "")) {window.alert('No Surname!!!'); return false }
    
    elem = document.getElementById(id);
    elem.style.visibility = visibility;
    elem.style.left = getWidth() / 2 -250;
//   cBoxPopUpMail('visible','confirmBoxMail');
//  if (elem.style.visibility == "hidden")
  return true;
//else { return false; }

}

function mailcheker (request) {
//window.alert(request.result);
if (request.result == 2) { window.alert('U heeft zich al opgegeven voor deze nieuwsbrief.'); return false }
return true;
}

function mailchekerform (request) {
//window.alert(request.result);
if (request.result == 2) { document.getElementById("button_mailer").disabled = true; this.checked = false; window.alert('U heeft zich al opgegeven voor deze nieuwsbrief.'); return false }
return true;
}

function mailcheckform()
{
var stmail = document.getElementById("stuuremail").value;
if ((stmail == null) || (stmail == "")) { return false }
if (!stmail.match(/^[A-Za-z0-9\.\_]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
        { return false }
doRequest("/util/email_saver.php", function(x) { mailchekerform(x); }, {
"mailer_email_own"      : stmail
});

}

function mailcheck()
{
var stmail = document.getElementById("stuurmail").value;
if ((stmail == null) || (stmail == "")) { return false }
if (!stmail.match(/^[A-Za-z0-9\.\_]{1,32}\@[A-Za-z0-9\.\_]{1,32}\.[a-zA-Z]{2,6}$/))
        { return false }
doRequest("/util/email_saver.php", function(x) { mailcheker(x); }, {
"mailer_email_own"	: stmail
});

}


function stuurCheck()
{
var visSet = document.getElementById("stuur_checkbox").checked ? "false" : "true";
document.getElementById("button_mailer").disabled = visSet;
}


function cBoxPopUp(visibility, id)
{
    elem = document.getElementById(id);
    elem.style.visibility = visibility;
    return false;
}
function cBoxPopUpMail(visibility, id)
{
    elem = document.getElementById(id);
    elem.style.visibility = visibility;
    return true;
}

function getWidth() {

var viewportwidth;
var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
{
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth
}
return viewportwidth;
}
