// Functie bestand Catalogus module

function changeborder(color, id){
	a = document.getElementById(id);
	a.style.borderColor = color;
	return;
}

function getItem(id){
 location.href = '/cat/front/item_info.asp' + id;
}

function getimage(afbeelding){
	a = document.getElementById('img_big');
	a.src = afbeelding;
	
	if (a.width > 200) a.width = 200;
	if (a.Height > 200) a.Height = 200;
}

function numbersonly(){
 if (event.keyCode<48||event.keyCode>57)
  return false
}

function validateEmail(field, msg)
{
	if (field.value != '')
	{
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value))
		{
			return true;
		}
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function priceonly(){
 if ((event.keyCode<48&&event.keyCode!=44&&event.keyCode!=45)||event.keyCode>57)
  return false
}

function changeClass(id, klasse){
 document.getElementById(id).className = klasse;
}

function submitForm(formulier){
	document[formulier].submit();
}

/* Formulier check functies */

// Controleren van alleen getallen die ingevoerd mogen worden
function checkInteger(FormObject,melding){
	var checkOK = "0123456789";	
	var FieldFilled = true;
	for(var c=0; c <= FormObject.value.length -1; c++) {
		if (checkOK.indexOf(FormObject.value.charAt(c)) == -1) {
			FieldFilled = false;				
		}
	}
	if (FieldFilled == false){ 
		alert(melding);
		FormObject.select();
		FormObject.focus();
		return false;
	}
}


// Controleren van alleen prijzen die ingevoerd mogen worden
function checkPrice(FormObject,melding){
	var checkOK = "0123456789,";	
	var FieldFilled = true;
	for(var c=0; c <= FormObject.value.length -1; c++) {
		if (checkOK.indexOf(FormObject.value.charAt(c)) == -1) {
			FieldFilled = false;				
		}
	}
	if (FieldFilled == false){ 
		alert(melding);
		FormObject.select();
		FormObject.focus();
		return false;
	}
}

function setVerplicht(){
	td = document.getElementsByTagName('td');
	for (i = 0; i <= td.length-1; i++){
		if (td[i].id){
			if (td[i].id == 'form_verplicht'){
					td[i].className = 'form_verplicht_red';
			}
		}
	}
	
	return false;
}

function checkForm(id){
	a = document['itmFrm' + id].aantal;
	if (a.value == '' || parseInt(a.value) == 0){
	 alert('U dient een geldig aantal op te geven');
	 a.focus();
	 return;
	}
	
	
	// document['itmFrm' + id].action = '/cat/front/item_to_bag.asp';
	// document['itmFrm' + id].submit();
	// $('#itmFrm'+ id).attr("action", '/cat/front/item_to_bag.asp');
	$('itmFrm'+ id).writeAttribute('action', '/cat/front/item_to_bag.asp?'+Math.random()*1000);	
	doSubmit(id);
}
function doSubmit (id) {
	$('itmFrm'+ id).submit();
}
/* Einde Formulier check functies */


function bagDelete(txt){
	if (confirm(txt)){
	 location.href = '/cat/front/bag_item_delete.asp?id=all';
	}
	return;
}

function bagRecalc(){
 document.frmCheck.action = '/cat/front/bag_recalc.asp';
 document.frmCheck.submit();
 return;
}

function bagStep(page){
 location.href = page;
 return;
}

function bagNextStep(page){
 //document.frmCheck.action = page;
 if (document.frmCheck.onsubmit() == true){
	 document.frmCheck.action = page;
	 document.frmCheck.submit();
 }
}

function bagSend(){
	document.frmCheck.submit();
	return;
}

function capsError( capsEngaged ) {
	if( capsEngaged ) {
		document.getElementById('caps_error').style.visibility = 'visible';
	} else {
		document.getElementById('caps_error').style.visibility = 'hidden';
	}
}

function capsDetect( e ) {
	if( !e ) { e = window.event; } if( !e ) { MWJ_say_Caps( false ); return; }
	var theKey = e.which ? e.which : ( e.keyCode ? e.keyCode : ( e.charCode ? e.charCode : 0 ) );
	var theShift = e.shiftKey || ( e.modifiers && ( e.modifiers & 4 ) );
	MWJ_say_Caps( ( theKey > 64 && theKey < 91 && !theShift ) || ( theKey > 96 && theKey < 123 && theShift ) );
}

function MWJ_say_Caps( oC ) {
	if( typeof( capsError ) == 'string' ) { if( oC ) { alert( capsError ); } } else { capsError( oC ); }
}


	function getProductPrice(f_Form, f_Url, f_item_id, f_actie)
{
    var dForm = document.getElementById(f_Form);
    showAjaxLoadingDiv();
		
      var FormFields = '';
     	var FormElements = dForm.elements;
      
      for (i=0; i<FormElements.length; i++) {
     
         if (FormElements[i].tagName == "INPUT") {
     			 
            if (FormElements[i].type == "text") {
               FormFields += FormElements[i].name + "=" + escape(FormElements[i].value) + "&";
            }
            
            if (FormElements[i].type == "hidden") {
               FormFields += FormElements[i].name + "=" + escape(FormElements[i].value) + "&";
            }
            
            if (FormElements[i].type == "checkbox") {
               if (FormElements[i].checked) {
                  FormFields += FormElements[i].name + "=" + escape(FormElements[i].value) + "&";
               } else {
                  //FormFields += FormElements[i].name + "=&";
               }
            }
            if (FormElements[i].type == "radio") {
               if (FormElements[i].checked) {
                  FormFields += FormElements[i].name + "=" + escape(FormElements[i].value) + "&";
               }
            }
         }   
         if (FormElements[i].tagName == "SELECT") {
            var sel = FormElements[i];
            FormFields += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
         }
         
         if (FormElements[i].tagName == "TEXTAREA") {
            
            if (FormElements[i].id.substring(0,6) == "Editor") {
	            FormFields += FormElements[i].name + "=" + escape(tinyMCE.get(FormElements[i].id).getContent()) + "&";
  					} else {
  						FormFields += FormElements[i].name + "=" + escape(FormElements[i].value) + "&";
  					}          
            
         }
         
      }
      FormFields += 'a=1';
      		
		var httpRequest = createHttpRequest()
  	  
  	httpRequest.open('post', f_Url+'?Tijd='+getTimeString()); 
  	httpRequest.onreadystatechange = function() {handleResponse_getProductPrice(httpRequest,f_item_id,f_actie); };
    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    httpRequest.send(FormFields);
}


function handleResponse_getProductPrice(httpRequest,f_item_id,f_actie) { 
	if(httpRequest.readyState == 4) { 
		if (httpRequest.status == 200) {
			if (httpRequest.responseXML.getElementsByTagName('notfound')[0] != null){
				var notfound = httpRequest.responseXML.getElementsByTagName('notfound')[0].firstChild.nodeValue;
			}
			if (!notfound == '1') {
				if (httpRequest.responseXML.getElementsByTagName('price')[0] != null) {
					var price = httpRequest.responseXML.getElementsByTagName('price')[0].firstChild.nodeValue;
					if (document.getElementById('price'+f_item_id) != null) {
						document.getElementById('price'+f_item_id).innerHTML = '&euro;&nbsp;'+price;
					}				
					if (document.getElementById('VerzendingIcoon'+f_item_id) != null) {	
						if ( price.replace(",",".") >= 100 ) {					
							$('VerzendingIcoon'+f_item_id).show();
						}
						else {
							$('VerzendingIcoon'+f_item_id).hide();
						}
					}						
				}
				if (httpRequest.responseXML.getElementsByTagName('availability')[0] != null){
					var availability = httpRequest.responseXML.getElementsByTagName('availability')[0].firstChild.nodeValue;
					if (document.getElementById('availabilityDiv'+f_item_id) != null) {
						document.getElementById('availabilityDiv'+f_item_id).innerHTML = availability;
					}
				}
				if (f_actie == '1') {
					if (httpRequest.responseXML.getElementsByTagName('vanprice')[0] != null){
						var vanprice = httpRequest.responseXML.getElementsByTagName('vanprice')[0].firstChild.nodeValue;
						if (document.getElementById('vanprice'+f_item_id) != null) {
							document.getElementById('vanprice'+f_item_id).innerHTML = '&euro;&nbsp;'+vanprice;
						}
					}
				}
				if (httpRequest.responseXML.getElementsByTagName('new_item_id')[0] != null){
					var new_item_id = httpRequest.responseXML.getElementsByTagName('new_item_id')[0].firstChild.nodeValue;
					document.getElementById('item_id'+f_item_id).value	=	new_item_id;
				}
				//document.getElementById('submitDiv').innerHTML	=	"<input type='submit' name='Bestel' class='btn-bestellen' value=''>";
				//document.getElementById('Quantity').disabled = false;
			} 
			else {
				document.getElementById('submitDiv').innerHTML	=	"<span style='color:red;'>Huidige combinatie is niet mogelijk.</span>";
				document.getElementById('price').innerHTML = '';
				document.getElementById('Quantity').disabled = true;
			}	
		}
		else {
			alert('Fout opgetreden. Neem contact op met de webmaster.');
		}
	}
}


 
 
function showAjaxLoadingDiv()
{	
  if (document.getElementById('oldprice') != null) {
 		document.getElementById('oldprice').innerHTML='';
	}
	if (document.getElementById('price') != null) {
  	document.getElementById('price').innerHTML = '<img src=\'/cat/front/images/ajax-loader.gif\' />';
  }
}

function createHttpRequest()
{
    if (window.XMLHttpRequest)
    {
      var httpRequest = new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
      var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }    
    return httpRequest;
}

function getTimeString()
{
    var today=new Date();		//	We geven de datum/tijd mee zodat er altijd een 
    var h=today.getHours();		//	andere url is waardoor de pagina niet uit
    var m=today.getMinutes();	//	de cache zal worden gehaald.
    var s=today.getSeconds();
    return h+':'+m+':'+s;
}








	function getPaymentmethods(f_id,f_Url)
{
    showAjaxLoadingDiv();
		
		var FormFields = 'a=1&paymentmethods='+f_id;
      		
		var httpRequest = createHttpRequest()
  	  
  	httpRequest.open('post', f_Url+'?Tijd='+getTimeString()); 
  	httpRequest.onreadystatechange = function() {handleResponse_getPaymentmethods(httpRequest); };
    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    httpRequest.send(FormFields);
}


function handleResponse_getPaymentmethods(httpRequest,f_item_id,f_actie) { 
  if(httpRequest.readyState == 4) { 
		if (httpRequest.status == 200) {
				document.getElementById('PaymentOptions').innerHTML = httpRequest.responseText;    
		}	else {
        alert('Fout opgetreden. Neem contact op met de webmaster.')
      }
	}
}
