function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function ddShowHideList() {
  var boxText = document.getElementById('dd_text');
  var list = document.getElementById('dd_list');

  if (list) {
    if (list.style.display == '' || list.style.display == 'none') {
      list.style.display = 'block';

      document.getElementById(readCookie('lng')).className = 'dd_list_item_over';

    } else {
      list.style.display = 'none';
    }
  }
}

function ddSet(elm, hide) {
  var text = document.getElementById('text');
  var list = document.getElementById('dd_list');

  if (text) {
    if (elm) {
      createCookie('lng', elm.id, 7);

      text.innerHTML = elm.innerHTML;

      if (hide) {
        ddShowHideList();
      }
    }
  }
}

function itemOver(elm) {
  if (elm) {
    elm.className = 'dd_list_item_over';
  }
}

function itemOut(elm) {
  if (elm) {
    if (elm.id != readCookie('lng')) {
      elm.className = 'dd_list_item';
    }
  }
}

function buttonOver(elm) {
  if (elm) {
    var children = elm.getElementsByTagName('div');
    children[0].className = 'left_over';
    children[1].className = 'middle_over';
    children[2].className = 'right_over';
  }
}

function buttonOut(elm) {
  if (elm) {
    var children = elm.getElementsByTagName('div');
    children[0].className = 'left';
    children[1].className = 'middle';
    children[2].className = 'right';
  }
}


function changeStylesheet(type) {
  var link = document.getElementById("font");

  if (link) {
    link.href = '/etc/sites/hilverdadeboer.nl/css/'+type+'.css';
    createCookie('stylesheet', type, 7);
  }
}

function loadStylesheet() {
  if (readCookie('stylesheet')) {
    changeStylesheet(readCookie('stylesheet'));
  }
}

/* Load stylesheet */
window.onload = loadStylesheet;

function login(username_msg, password_msg) {
  var username = document.getElementById('Username');
  var password = document.getElementById('Password');
  var error = '';

  if (username && username.value.length == 0) {
    error += '- '+ username_msg +'\n';
  }

  if (username && username.value.length == 0) {
    error += '- '+ password_msg +'\n';
  }

  if (error.length == 0) {
    if (document.getElementById('form_login')) {
      document.getElementById('form_login').submit();
    }

    return true;
  } else {
    alert(error);

    return false;
  }
}

function login_enter(e) {
	var key = e.keyCode || e.which;

	if (key == 13) {
		login('No username was entered.', 'No password was entered.');
	}
}

function showSlideShow(cpa_id) {
  if (cpa_id) {
        var options = {
            caption: 'Slideshow',
            center_win: true,
            height: 500,
            width: 500,
            show_close_img: true,
            show_loading: true,
            fullscreen: false
        }

        var win = new GB_Window(options);

        return win.show('/scripts/slideshow/'+cpa_id+'/');
  }
}

function selectProduct() {
  if (window.location.href.indexOf('product2') != -1) {
    var cat = document.getElementById('category');
    var color = document.getElementById('color');
    var date = document.getElementById('date');

    string  = '?cat='+ ((cat.selectedIndex > 0)?cat.options[cat.selectedIndex].value:'null');
    string += '&color='+ ((color.selectedIndex > 0)?color.options[color.selectedIndex].value:'null');
    string += '&date='+ ((date.selectedIndex > 0)?date.options[date.selectedIndex].value:'null');
    
    window.location.href = '/product2/'+string;
  } else {
    var string = new Array();

    var cat = document.getElementById('category');
    var color = document.getElementById('color');
    var week = document.getElementById('week');

    string[0] = cat.selectedIndex > 0 ? cat.options[cat.selectedIndex].value : 'null';
    string[1] = color.selectedIndex > 0 ? color.options[color.selectedIndex].value : 'null';
    string[2] = week.selectedIndex > 0 ? week.options[week.selectedIndex].value : 'null';

    string = string.join('/', string);
    
    window.location.href = '/product/'+string;
  }
}