/* default js scripts */

window.onload = init;

function init() {
	
	var arr = document.getElementsByTagName('a');
	for(i = 0; i < arr.length; i++)
		arr[i].onfocus = new Function("this.blur()");
	
	var arr = document.getElementsByTagName('input');
	for(i = 0; i < arr.length; i++)
		if(arr[i].className == 'button')
			arr[i].onfocus = new Function("this.blur()");
}

function redirect( strURL ) {
	window.location = strURL;
}

function selectOptionByValue(strValue, obj) {
	var i = 0
	while ( i < obj.options.length) {
		if(obj.options[i].value == strValue) {
			obj.selectedIndex = i;
			return;
		}
		i++;
	}
	obj.selectedIndex = 0;
}

function removeOptionByValue(strValue, obj) {
	var i = 0
	while ( i < obj.options.length) {
		if(obj.options[i].value == strValue) {
			obj.options[i] = null;
			return;
		}
		i++;
	}
}

function getkey(e) {
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
		return null;
}

function validChars(e, goods) {
	var key, keychar;
	key = getkey(e);
	if (key == null) 
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	if (goods.indexOf(keychar) != -1)
		return true;

	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
	
	return false;
}

function isDate(intDay, intMonth, intYear) {
	var daysInMonth = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if( intDay == ''  &&  intMonth == ''  &&  intYear == '')
		return false;
	
	if( isNaN(intDay) || isNaN(intMonth) || isNaN(intYear))
		return false;

	if( intMonth < 1 || intMonth > 12 )
		return false;
	
	if( intYear < 1753 || intYear > 2100)
		return( false);
	
	if( intMonth == 2 && intDay == 29)
	   if(!(intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0))) 
	   	return false;
	
	if( intDay < 1  ||  intDay > daysInMonth[intMonth - 1])
		return false;
	
	return true;
}

function isEmail(strEmail) {
	var AtSym    = strEmail.indexOf('@')
	var Period   = strEmail.lastIndexOf('.')
	var Space    = strEmail.indexOf(' ')
	var Length   = strEmail.length - 1
	
	if( AtSym < 1 || Period <= AtSym + 1 || Period == Length || Space != -1 )
	 return false;

	return true;
}

function isURL(strUrl) {

  if (strUrl.indexOf(" ") != -1)
    return false;

  if (strUrl.indexOf(".") == -1)
    return false;
  else if (strUrl.indexOf(".") == 0)
    return false;
  else if (strUrl.charAt(strUrl.length - 1) == ".")
    return false;

  if (strUrl.indexOf("/") != -1) {
    strUrl = strUrl.substring(0, strUrl.indexOf("/"));
    if (strUrl.charAt(strUrl.length - 1) == ".")
      return false;
  }

  if (strUrl.indexOf(":") != -1) {
    if (strUrl.indexOf(":") == (strUrl.length - 1))
      return false;
    else if (strUrl.charAt(strUrl.indexOf(":") + 1) == ".")
      return false;
    strUrl = strUrl.substring(0, strUrl.indexOf(":"));
    if (strUrl.charAt(strUrl.length - 1) == ".")
      return false;
  }

  return true;

}

function isIp(strIp) {
	ipArray = strIp.split('.');
	if(ipArray.length != 4) {
		alert("Je moet een correct IP-adres opgeven (xxx.xxx.xxx.xxx)");
		return false;
	}
	for (var i = 0; i < ipArray.length; i++)
		if (ipArray[i] < 0 || ipArray[i] > 255 || ipArray[i] == '') {
			alert("Je moet een correct IP-adres opgeven (xxx.xxx.xxx.xxx en xxx < 256)");
			return false;
		}
	return true;
}

function checkZip( strZip, strFormat){
    var good, pcPos, pcChar, fmtPos, fmtChar, strNew;

    /* Lege strFormat string? dan is alles goed */
    if( strFormat == null  ||  strFormat == '') return strZip;

    strNew = '';
    fmtPos = 0;
    pcPos  = 0;
    good   = true;
    while( fmtPos < strFormat.length  &&  pcPos <= strZip.length  &&  good) {
        fmtChar = strFormat.charAt( fmtPos);
        pcChar  = strZip.charAt( pcPos);
        copy = true;

        if( fmtChar == '9') {
            good = (pcChar >= '0' &&  pcChar <= '9');
        }
        else if( fmtChar == 'a' ||  fmtChar == 'A') {
            good = ((pcChar >= '0'  &&  pcChar <= '9')  ||  (pcChar >= 'a' &&  pcChar <= 'z')  ||  (pcChar >= 'A'  &&  pcChar <= 'Z'));
        }
        else if( fmtChar == '*') {          /* We hebben een ster, dus we zijn klaar, kopieer de rest van de strZip */
            strNew  += strZip.substring( pcPos, strZip.length);
            fmtPos += 1;
            pcPos   = strZip.length;
            copy = false;
        }
        else if( fmtChar != pcChar) {       /* We hebben niet het gewenst format character, dus voeg het toe aan de nieuwe */
            strNew  += fmtChar;
            fmtPos += 1;
            copy = false;
        }

        if( good  &&  copy) {
            strNew += pcChar;
            pcPos += 1;
            fmtPos += 1;
        }
    }

    good = (pcPos == strZip.length  &&  fmtPos == strFormat.length);
    return (good ? strNew : null);
} 

/* function default checkFrm */
function checkForm( arr ) {
	
	var frm = $('frm');
	
	for( i=0; i < arr.length; i++ ) {
		
		/* split key->value */
		tmp = arr[i].split("->");
		
		/* key: email */
		if( tmp[0] == 'id_email' && frm[tmp[0]].value != '' && !isEmail(frm[tmp[0]].value) ) {
			alert("Je moet een geldig e-mailadres invoeren bij `"+ tmp[1] + "`");	
			frm[tmp[0]].focus();
			return false;
		}
		
		/* key: default */
		if( frm[tmp[0]].value == '' ) {
			alert("Je moet het veld `"+ tmp[1] + "` invullen.");	
			frm[tmp[0]].focus();
			return false;
		}
		
	}
	
	return true;
	
}

function checkPersonalia() {
	
	if ( $F('id_password') != $F('id_confirm') ) {
		alert("De wachtwoorden komen niet overeen.");	
		$('id_password').value = "";
		$('id_confirm').value = "";
		$('id_password').focus();
		return false;
	}
	
	return checkForm(Array('id_initials->Voorletters', 'id_first->Voornaam', 'id_last->Achternaam', 'id_street->Straat', 'id_nr->Huisnummer', 'id_zip->Postcode', 'id_city->Woonplaats', 'id_country->Land', 'id_email->E-mailadres', 'id_password->Wachtwoord', 'id_confirm->Nogmaals'))	
}

function checkProfile() {
	
	if ( $F('id_password') != $F('id_confirm') ) {
		alert("De wachtwoorden komen niet overeen.");	
		$('id_password').value = "";
		$('id_confirm').value = "";
		$('id_password').focus();
		return false;
	}
	
	return checkForm(Array('id_initials->Voorletters', 'id_first->Voornaam', 'id_last->Achternaam', 'id_street->Straat', 'id_nr->Huisnummer', 'id_zip->Postcode', 'id_city->Woonplaats', 'id_country->Land', 'id_email->E-mailadres'))	
}


function checkConfirm() {
	
	if ( $F('id_confirm') == undefined ) {
		alert("Je moet akkoord gaan met de algemene voorwaarden om een bestelling te kunnen plaatsen.");	
		$('id_confirm').focus();
		return false;
	}
	
	return true;
	
}

function checkRecover() {
	
	if ( $F('id_confirm') == undefined ) {
		alert("Je moet bevestigen dat je je wachtwoord wilt herstellen om door te gaan.");
		$('id_confirm').focus();
		return false;
	}
	
	return true;
}


function checkSMS() {
	return checkForm(Array('id_code->Jouw code van 8 tekens'));
}

function setInitials( objField ) {
	objField.value = objField.value.replace(/(\.)|(\s)/g, "").replace(/([a-zA-Z])/g, "$1.").toUpperCase();
}

/* custom scripts */
