﻿function commonLoad()
{
if($('postcode') && $('postcode').value=='Postcode' && us_pc!='') {$('postcode').value=us_pc;};
if($('mf_scroll')){$('mf_scroll').scrollTop = 17*mfSelIndex;};
if($('ht_scroll')){$('ht_scroll').scrollTop = 17*htSelIndex;};
//if($('postcode')){makeInputFilterPostalCode($('postcode'));};
//if($('imei')){makeInputFilterImei($('imei'));};
//if($('ad_bid_amount')){makeInputFilter($('ad_bid_amount'));};
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function addUnLoadEvent(func) {
  var oldunonload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      if (oldonunload) {
        oldonunload();
      }
      func();
    }
  }
}

addLoadEvent(commonLoad);

function focusInput(obj, sValue)
{ if(obj.value==sValue)
  { obj.value = ''; }
  obj.className = 'input_focus';
}
function blurInput(obj, sValue)
{ if(obj.value=='' || obj.value==sValue)
  { obj.value = sValue;
    obj.className = 'input_blur'; }
}
function changeTerm(form)
{   if(trim(form.term.value)=='' || trim(form.term.value)=='Trefwoord')
    { return false; }
    go(form.url.value+"term="+form.term.value);
}
function changeProx(form)
{   var regex=/^[1-9]\d{3}(\s?[a-zA-Z]{2})?$/;
    form.postcode.focus(); // gets rid of 'Postcode'
    if (form.postcode.value == ""){
        alert("Voer eerst uw postcode in.");
        form.distance.selectedIndex=0;
        return false;
    } else if (!regex.test(form.postcode.value)) {
        alert("De ingevoerde postcode is onjuist");
        form.distance.selectedIndex=0;
        return false;
    } else if(form.distance.selectedIndex==0) {
        alert("Selecteer een afstand.");
        return false;
    }
  
  //submit postcode and distance
  go(form.url.value+((form.url.value.indexOf('?')==-1)?'?':'&')+'pc='+form.postcode.value+'&ds='+form.distance.value);
}

function go(url){ window.location.href = url; }

function showMyAnymobile()
{
    if(us_signedin==1)
    { go('/mijn/anymobile.html');}
    else { showSignInBox(); };
    return false;
 }

function bgc(obj, color){//change bg color
if(obj){obj.style.backgroundColor=color;};return true;
}

function showSignInBox() {
var s='<form method="post" action="/account/inloggen.html" onsubmit="return checkSignIn(this);">';
s+='<table style="width:160px;height:180px;margin:10px;" cellpadding="0" cellspacing="0">';
s+='<tr><td align="center" style="font-size:14px;font-weight:bold;">MIJN ANYMOBILE</td><td align="right"><img src="/img/btn/btn_close_yellow.gif" alt="Sluiten" title="Sluiten" onclick="hideSignInBox();" style="cursor:pointer;" /></td></tr>';
s+='<tr><td style="font-size:11px;" colspan="2">E-MAILADRES</td></tr>';
s+='<tr><td colspan="2"><input type="text" name="email" id="signin_email" class="text" style="width:156px;" /></td></tr>';
s+='<tr><td style="font-size:11px;" colspan="2">WACHTWOORD</td></tr>';
s+='<tr><td colspan="2"><input class="text" name="password" style="width: 156px;" type="password" /></td></tr>';
s+='<tr><td colspan="2"><input type="checkbox" name="remember" id="remember_me" value="1" class="chk" /><label for="remember_me">Onthoud mij op deze PC</label></td></tr>';
s+='<tr><td align="right" colspan="2"><input type="submit" value="INLOGGEN" style="height:18px;color:#fff;background:#282944;border:none;font-weight:bold;font-size:11px;" /></td></tr>';
s+='<tr><td colspan="2"><a href="/account/wachtwoord.html">Wachtwoord vergeten</a></td></tr>';
s+='<tr><td colspan="2"><a href="/account/nieuw.html">Nieuw account maken</a></td></tr>';
s+='</table></form>';
$('signin_box').innerHTML=s; 
$('signin_box').style.visibility='visible'; 
$('signin_email').focus(); return false; }
function hideSignInBox()
{ $('signin_box').style.visibility='hidden'; }

function showMoreMf()
{$('more_mf').style.display='block';}

function showMoreShops()
{$('more_sh').style.display='block';}

function $(id)
{
    var obj=document.all? document.all[id] : document.getElementById? document.getElementById(id) : "";
    return obj;
}
function trim (val) {
     return val.replace(/^\s*|\s*$/,"");
}
/* unobtrusive style input filter on numeric, comma and point */
function makeInputFilter(element) {
    if(element){
        element.onkeydown = function (e) {
            e = (e) ? e : event;
            var keyCode = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
            //alert(keyCode);
            return ((keyCode >= 48 && keyCode <= 57 && !(e.shiftKey || e.altKey)) || (keyCode >= 96 && keyCode <= 105) || keyCode == 188 || keyCode == 110 || keyCode == 190 || keyCode == 46 || (keyCode <= 40  && keyCode != 32));
        }
    }
}
function makeInputFilterPostalCode(element) {
    element.onkeydown = function (e) {
        e = (e) ? e : event;
        var keyCode = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
        if(element.value.length<4) return ((keyCode >= 48 && keyCode <= 57 && !(e.shiftKey || e.altKey)) || (keyCode >= 96 && keyCode <= 105) || keyCode == 46 || (keyCode <= 40  && keyCode != 32));
        if(element.value.length<6) return ((keyCode >= 65 && keyCode <= 90) || (keyCode <= 40  && keyCode != 32));
        if(element.value.length>5) return (keyCode <= 40  && keyCode != 32);
    }
}
function makeInputFilterImei(element) {
    element.onkeydown = function (e) {
        e = (e) ? e : event;
        var keyCode = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
        if(element.value.length<15) return ((keyCode >= 48 && keyCode <= 57 && !(e.shiftKey || e.altKey)) || (keyCode >= 96 && keyCode <= 105) || keyCode == 46 || (keyCode <= 40  && keyCode != 32));
        if(element.value.length>14) return (keyCode <= 40  && keyCode != 32);
    }
}
function Reformat(tempAmountFormat) {
    if (tempAmountFormat!='') {
        return(tempAmountFormat.replace('.', ''));
    }
    return '';
}

function Recalculate(tempAmount, minAmountInCents) {
    if (!isNaN((tempAmount+'').replace(',', '')) && tempAmount!='') {
        tempAmount=(tempAmount+'').replace('.', ',');
        commaPosition = tempAmount.lastIndexOf(",");
        if (commaPosition == -1) {
            amountFull = tempAmount;
            amountCents = '';
        }
        else {
            amountFull = tempAmount.substring(0, commaPosition);
            amountCents = tempAmount.substring(commaPosition+1, commaPosition+3);
            amountCents = amountCents+'00';
            if (parseInt(amountCents) == 0) {amountCents = '';}
        }
        amountFull = amountFull.replace(',', '');
        amountFull = amountFull-0;
        //amountFull = currencyFormat(amountFull);
        if (amountCents > 0) {
            tempAmountNew = amountFull+','+amountCents.substring(0,2);
        }
        else {
            tempAmountNew = amountFull+',00';
        }
        //min value
        if(null!=minAmountInCents && parseInt(tempAmountNew.replace(',', ''), 10)<minAmountInCents)
        {
            tempAmount = minAmountInCents+'';
            if(tempAmount.length==1)
            { tempAmountNew = '0,0'+tempAmount; }
            else if(tempAmount.length==2)
            {tempAmountNew = '0,'+tempAmount;}
            else {tempAmountNew = tempAmount.substring(0, tempAmount.length-2)+','+tempAmount.substring(tempAmount.length-2, tempAmount.length);}
            alert('Het minimum bedrag is: € ' + tempAmountNew);
        }
        return tempAmountNew;
    }

    return '0,00';
}

function currencyFormat(number) {
    number = '' + number;
    if (number.length > 3) {
        var mod = number.length%3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0)) {
                output+= number.substring(mod+3*i,mod+3*i+3);
            } else {
                //output+= '' + number.substring(mod+3*i,mod+3*i+3);
                output+= '.' + number.substring(mod+3*i,mod+3*i+3);
            }
        }
        return (output);
    }

    return number;
}
function checkSignIn(form)
{
if(trim(form.email.value)=='' || !checkEmail(form.email.value))
{//alert("Vul uw e-mail adres in."); 
form.email.focus();return false;};
if(trim(form.password.value)=='')
{//alert("Vul uw wachtwoord in.");
form.password.focus();return false;}
if($('terms') && $('terms').checked==false)
{alert("U dient de algemene voorwaarden te accepteren.");return false;}
return true;
}

//function isValidEmail(email){
//if (email.indexOf(' ') != -1) {
//return false;
//}
//return checkEmail(email);
//}
function checkPostalCode(postalcode) {
   var regex=/^[1-9]\d{3}\s?[a-zA-Z]{2}$/;
   return regex.test(postalcode);
};
function checkEmail(email) {
    var regex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return regex.test(email);
};
function checkImei(imei){
    var regex = /^\d{15}$/;
    return regex.test(imei);
};
function checkPhoneNumber(phonenumber){
    var regex = /^[0]\d{9,11}$/;
    return regex.test(phonenumber);
};


function changePostcode(input)
{
    input.value = input.value.replace(' ', '');
    if(input.value!='')
    {
        if(!checkPostalCode(input.value))
        {
            alert("U heeft een onjuiste postcode ingevoerd!");
            input.focus();
        }
        else
        {
            loadPostcodeXML(input.value);
        }
    }
}
var xmlhttp;
function loadPostcodeXML(postcode) {
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = xmlhttpChangePostcode;
    xmlhttp.open("GET", "/services/postcode/"+postcode+".html", true);
    xmlhttp.send(null);
    xmlhttp.onreadystatechange = xmlhttpChangePostcode;
  }
  else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp) {
      xmlhttp.onreadystatechange = xmlhttpChangePostcode;
      xmlhttp.open("GET", "/services/postcode/"+postcode+".html", true);
      xmlhttp.send();
    }
  }
}

function xmlhttpChangePostcode()
{
    if (xmlhttp.readyState==4)
    {
        if (xmlhttp.status==200)
        {
           var postcode_array=xmlhttp.responseText.split(";");
           if(postcode_array[0]=='')
           {alert('De postcode is onjuist.');}
           $('postcode_city').innerHTML = postcode_array[0];
          //$('postcode_province').innerHTML = postcode_array[1];
        }
    }
}
function openNewWindow(url,name,w,h,scroll,pos,res)
{
    if(pos==null || pos=="center"){
        leftPosition=(screen.width)?(screen.width-w)/2:100;
        topPosition=(screen.height)?(screen.height-h)/2:100;
    }else if(pos=="top"){
        leftPosition=0;topPosition=20;
    }else{
        leftPosition=0;topPosition=20;
    }
    if(res==null){
        resizable="no";
    }else{
        resizable=res;
    }
    settings='width='+w+',height='+h+',top='+topPosition+',left='+leftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable='+resizable;
    win=window.open(url,name,settings);
    if (window.focus) {win.focus()}
}
//date constants
var today = new Date();
var curQuarter = Math.ceil(( today.getMonth() + 1) / 3 );
var curYear = today.getFullYear();
//color contstants
var cGray = "#e9e9ef";
var cWhite = "#ffffff";
var cWhite1 = "#f6f6f8";


