var ROOT="http://www.ffshoppen.nl/";
function elId(id) {
  return document.getElementById(id);  
}

function selectedValue(id) {
  return elId(id).options[elId(id).selectedIndex].value;
}

function alphanumeric(text,separator) {
  if(!separator) separator="_";
  text=text.replace(/[^a-zA-Z0-9_]+/g,"_");
  text=text.replace(/_[a-zA-Z0-9_]_/g,"_");
  text=text.replace(/^_+/g,"");
  text=text.replace(/_+$/g,"");
  text=text.replace(/_/g,separator);
  return text;
}

function getKeyCode(e) {
  var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
  return code
}

function submitMZ() {
  var search=elId("mzzoek").value;
  var cat=selectedValue("mzcat");
  var prijs=selectedValue("mzprijs");
  var location=ROOT;
  if(cat!="-")
    location+=alphanumeric(cat);
  location+="?zoek="+alphanumeric(search);
  if(prijs!="-")
    location+="&prijs="+alphanumeric(prijs);
  window.location=location;
}

function submitMZOnEnter(e) {	
  var code=getKeyCode(e);
  if(code==13)
    submitMZ(); 
}
function submitSB() {
  var search=elId("zoekbalk").value;
  var url=""+window.location;
  var location=url.split("?");
  location=location[0]+"?zoek="+alphanumeric(search);
  var prijs=url.split("?prijs=");
  if(prijs.length<2)
    prijs=url.split("&prijs=");
  if(prijs.length==2)
    location+="&prijs="+parseInt(prijs[1]);
  window.location=location;  
}
function submitSBOnEnter(e) {	
  var code=getKeyCode(e);
  if(code==13)
    submitSB(); 
}


function pwdBlur(id,id2) {
  if(elId(id).value=="")
  {
    elId(id).style.display="none";
    elId(id2).style.display="inline";
  }    
}
function pwdFocus(id,id2) {
  elId(id).style.display="inline";
  elId(id2).style.display="none";
  elId(id).focus();
}

function userBlur(id) {
  if(elId(id).value=="")
  {
    elId(id).value="Gebruikersnaam";
    elId(id).className="grayinput";
  }    
}
function userFocus(id) {
  elId(id).className="blackinput";
  if(elId(id).value=="Gebruikersnaam")
  {
    elId(id).value="";
  }
}

function toggleAfleverAdres(checked) {
  if(checked)
    elId("anderafadres").style.display="block";
  else
    elId("anderafadres").style.display="none";
}
function submitTo(id,to,check) {
  if(check)
  {
    var retval=check();
    if(retval)
    {
      elId(id).action=to;
      elId(id).submit();
    } 
  }   
  else
  {
    elId(id).action=to;
    elId(id).submit();
  }
}
function checkIncasso(value) {
  if(value=="incasso")
    elId("incassogegevens").style.display="block";
  else
    elId("incassogegevens").style.display="none";
}
var checkBetaalwijze = function () {
  if(elId('betaalwijze').value=="")
  {
    alert("Zou u nog een betaalwijze willen selecteren?");
    return false;
  }
  else
    return true;
}
var checkAkkoord = function () {
  if(elId('akkoord').checked)
    return true;
  else
  {
    alert("Wilt u de algemene voorwaarden nog voor akkoord tekenen?");
    return false;
  }
}


function sendAjaxRequest(url, postdata, eventhandler) {
  if (window.XMLHttpRequest) {
    var xhr = new XMLHttpRequest();
  } else  if (window.ActiveXObject) {
    var xhr = new ActiveXObject("Microsoft.XMLHTTP")
  }
  xhr.onreadystatechange = function() {
     if(xhr.readyState == 4)
        if(xhr.status == 200)
           eventhandler(xhr)
        else {
           alert('mislukt: ' + xhr.status + ' ' + xhr.statusText )
           document.body.innerHTML = xhr.responseText
        }
  }
  xhr.open("POST", url, true );
  xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xhr.send(postdata);
}

function getSubcats(cat,subcat) {
  sendAjaxRequest(ROOT+'ajax/subcats.php', 'cat='+cat+'&subcat='+subcat, replaceSubcats);
}

function replaceSubcats(ajaxRequest) {
  elId("subcatdiv").innerHTML=ajaxRequest.responseText;
}

