// copyright 2004 Mike Deeley



function version() {
alert('Neue Version')	
}
kill = 1

prices = [59,64]

items = [
'PAKKET_BUIK_BORSTEN',
'PAKKET_SHOUDER_HAND',
'NL_BABY_SET',
]

country_NL = 4.95


function trimAll( strValue ) {

/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}


function validateNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

function  validateNumeric( strValue ) {
	
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
	//var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
	//var objRegExp  =  /(^\d\d*$)|(^\d\d*\s?\d\d*$)|(^\(\d\d*\)\s?\d\d*\s?\d\d*$)/; 
	var objRegExp  =  /^(\+\d{1,3} ?)? ?(\(\d{1,5}\)|\d{1,5})? ?\d{3} ?\d{0,7}$/
	/*
	var stripped = strValue.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	 alert('The phone number contains illegal characters.')
	}
  */
 
  //check for numeric characters 
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  = /(^-?\d\d*$)/;
 
  //check for integer characters
  return objRegExp.test(strValue);
}

function validatePLZ( strValue ) {
	
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
/*
  switch(country_param) {
  	case 'DE':
  		var objRegExp  = /^\d{5}$/;
  		break
	default:
	  	var objRegExp  = /^\d{4}$/;
  		break
  }
  */
  var objRegExp  = /^\d{4}$/;
  //check for integer characters
  return objRegExp.test(strValue);
}


function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}




//--- checkbox_checker()

function checkbox_checker()
{

	el = document.getElementById('order_form')

	var item_order = new Array();
	item_amount = 0
	item_price = 0
	item_weight = 0
	item_quant = 0	
	
	for (counter = 0; counter < el["checkbox[]"].length; counter++)	{
	
		if(counter == 1) {
			part = 'PAKKET_BUIK_BORSTEN'
			part_x = 'PAKKET_SHOUDER_HAND'
			opp = 0
			opp_x = 1
			chk = 'check_0'
			chk_x = 'check_1'
		}
		if(counter == 0) {
			opp = 1
			opp_x = 0
			part = 'PAKKET_SHOUDER_HAND'
			part_x = 'PAKKET_BUIK_BORSTEN'
			chk = 'check_1'
			chk_x = 'check_0'
		}
		optio = part
		optio_x = part_x
		//alert(optio)

		if (el["checkbox[]"][counter].checked) {
			//alert('check_'+counter+': checked')
			item_quant = item_quant + (el[el["checkbox[]"][counter].value].value *1)
			item_order[item_amount] = el[el["checkbox[]"][counter].value].value+' x '+el[el["checkbox[]"][counter].value].id
			item_price = item_price + ( prices[counter] * el[el["checkbox[]"][counter].value].value )
			//item_weight = item_weight + ( weight[counter] * el[el["checkbox[]"][counter].value].value )
			item_amount++
			
			if(item_quant == 2) {
				
				document.getElementById(optio).disabled = 'true';
				//document.getElementById(optio).options[opp] = null;
				document.getElementById(chk).disabled = 'true';	
				if(el["checkbox[]"][opp].checked) {
					document.getElementById(chk).disabled = '';	
				}

			}
			else if(item_quant == 1) {
				//document.getElementById(optio).options[opp] = null;
				document.getElementById(optio).disabled = 'true'
				document.getElementById(chk).disabled = '';	
				document.getElementById(chk_x).disabled = '';	

			}
		}
		else {
			//alert('check_'+counter+': not checked')
			//alert('item_quant: '+item_quant)
			//alert('opp_x: '+opp_x)
			if(item_quant == 1) {
				document.getElementById(optio).disabled = '';
				document.getElementById(chk).disabled = ''
				document.getElementById(chk_x).disabled = '';	
			}
			if(item_quant == 0) {
				//alert('item_quant: '+item_quant)
				document.getElementById(optio).disabled = '';
				document.getElementById(chk).disabled = ''
				document.getElementById(optio_x).options[0].selected = 'true';
			}
		}
	
	}
	//alert('item_amount: '+item_amount+ ' | item_price: '+item_price+' | item_weight: '+item_weight)
	
	

	country_param = document.getElementById('country').value
	//alert(country_param)

	switch(country_param) {
		case 'NL':
			document.getElementById('postcode').setAttribute('maxLength', 4)
			document.getElementById('cntry_txt').value = 'NL-'
			break
		case 'BE':
			document.getElementById('postcode').setAttribute('maxLength', 4)
			document.getElementById('cntry_txt').value = 'B-'
			break
		default :
			document.getElementById('postcode').setAttribute('maxLength', 4)
			document.getElementById('cntry_txt').value = ''
			break
	}
	
	
	if(document.getElementById('postcode').value.length != document.getElementById('postcode').maxLength)
		document.getElementById('postcode').value = ''
	
		document.getElementById('ges_1').style.height = '100%'
		document.getElementById('ges_1').style.visibility = 'visible'
		document.getElementById('ges_1').style.position = 'relative'


	if(item_price != 0) {
		kill = 0
		switch(country_param) {
			case 'NL':
				document.getElementById('postcode').setAttribute('maxLength', 4)
				
				post_price = country_NL
				document.getElementById('cntry_txt').value = 'NL-'

				document.getElementById('ges_1').style.height = '100%'
				document.getElementById('ges_1').style.visibility = 'visible'
				document.getElementById('ges_1').style.position = 'relative'
				break;
			case 'BE':
				document.getElementById('postcode').setAttribute('maxLength', 4)
				
				post_price = country_NL
				document.getElementById('cntry_txt').value = 'B-'

				document.getElementById('ges_1').style.height = '100%'
				document.getElementById('ges_1').style.visibility = 'visible'
				document.getElementById('ges_1').style.position = 'relative'
				break;
			default:
				break;
		}
		//alert('post_price before: '+post_price)
	}
	else {
		post_price = 0
		document.getElementById('cntry_txt').value = ' '
	}
		
		
	if(String(post_price).indexOf(".") != -1) {
		fancy_post_price = String(post_price)
		//fancy_post_price = fancy_post_price.substring(0, fancy_post_price.indexOf(".")) + "," + fancy_post_price.substring(fancy_post_price.indexOf(".")+1, fancy_post_price.length) + '0'
		fancy_post_price = fancy_post_price.substring(0, fancy_post_price.indexOf(".")) + "," + fancy_post_price.substring(fancy_post_price.indexOf(".")+1, fancy_post_price.length) + ''
	}
	else
		fancy_post_price = post_price + ',00'

	tot_price = String(post_price + item_price)
	tot_nn_price = String( ( ( (post_price + item_price) * 10 ) + 36) /10 )

	if(tot_price.indexOf(".") != -1) {
			fancy_tot_price = tot_price.substring(0, tot_price.indexOf(".")) + "," + tot_price.substring(tot_price.indexOf(".")+1, tot_price.length) + ''	
	}
	else
		fancy_tot_price = tot_price + ',00'

	if(tot_nn_price.indexOf(".") != -1) {
			fancy_tot_nn_price = tot_nn_price.substring(0, tot_nn_price.indexOf(".")) + "," + tot_nn_price.substring(tot_nn_price.indexOf(".")+1, tot_nn_price.length) + ''	
	}
	else
		fancy_tot_nn_price = tot_price + ',00'

	
	fancy_item_price = item_price + ',00'


	//alert('fancy_post_price after: '+fancy_post_price)

	document.getElementById('description').value = item_order 
	document.getElementById('cart_id').value = item_order

	document.getElementById('amount').value = tot_price
	//document.getElementById('e_yes').innerHTML = ''

	document.getElementById('display_price').innerHTML = fancy_item_price
	document.getElementById('display_post_price').innerHTML = fancy_post_price
	
	document.getElementById('display_tot_price').innerHTML = fancy_tot_price

	
return (true);
}

//--- checkbox_checker()



//---

function do_pre_submit() {

	msg = ''
	warn = ''
	toggle_field()
	for (names in u_input) {
		//alert(names)
		//alert('msg: '+msg)
		//alert('names: '+names+' | u_input['+names+'].value: '+ u_input[names].value)
		if(!msg)
			switch(names) {
				case 'naam':
				case 'adres':
				case 'woonplaats':
					if(validateNotEmpty(u_input[names].value)) {
						//msg += names +' is OK\n'
						break
					}
					else {
						msg += 'Het veld ' + names.toUpperCase() +' is niet geldig!\n'
						break
					}
				case 'postcode':
					//alert(country_param)
					if(validatePLZ(u_input[names].value))
						break
					else
						msg += 'Het veld ' + names.toUpperCase() +' is niet geldig!\n'
						break
				case 'telefoon':
					//if(validateNotEmpty(u_input[names].value)) {
					if(validateNumeric(u_input[names].value)) {
						//msg += names +' is OK\n'
						break
					}
					else {
						msg += 'Het veld ' + names.toUpperCase() +' is niet geldig!\n'
						/*
						msg += 'Bitte geben Sie Ihre Nummer in der Form \n'
						msg += '(Vorwahl) Telefon Nr. ein. \n'
						msg += 'Beispiel: (030) 456789 \n'
						msg += 'Danke!'
						*/
						break
					}
				case 'email':
					
					//if(document.getElementById('kk').checked) {
					if(1) {
						if(isEmail(u_input[names].value)) {
							//msg += names +' is OK\n'
							break
						}
						else {
								msg += 'Het veld ' + names.toUpperCase() +' is niet geldig!\n'
							break
						}
					}
					else if(u_input[names].value != '') {
						if(isEmail(u_input[names].value)) {
							//msg += names +' is OK\n'
							break
						}
						else {
							warn += 'Het veld ' + names.toUpperCase() +' is niet geldig!\n'
							break
						}
					}
					else {
						u_input[names].value = ''
						break
					}
				default :
					break
			
			}

		if(msg) {
			//alert('msg1: '+msg)
			//alert(names)
			//return
			break
		}

	
	}

	if(msg) {
		alert(msg)
		toggle_field(names)
		return
		
	}
	else if(warn)
		alert('AANDACHT: '+warn)
	else {
		//alert('Alle Eingaben korrekt...')
		document.getElementById('address').value = '<br>\n\n-----------------\n<br>' + document.getElementById('name').value + '\n<br>' + u_input['adres'].value + '\n<br>' + document.getElementById('cntry_txt').value + u_input['postcode'].value + ' ' + u_input['woonplaats'].value + '<br>\n-----------------\n<br>' 
		submitit()
	}
		


} // end of function


function submitit() {

if(kill) {
	//alert('Sie haben gar nix ausgewählt!!!\n\nKilcken Sie oben rechts of ein Kästchen')
	alert('Gelieve te kiezen een product!')
	document.getElementById('check_0').focus()
	return	
}

document.getElementById('order_form').action = 'https://select.worldpay.com/wcc/purchase'	
//alert('submit')
document.getElementById('order_form').submit()
	
}

	
function toggle_field(curr_field) {
	for (names in u_input) {
		u_input[names].style.margin = '0 0 0 0'
		u_input[names].style.border = '0px solid red'
	}
	if(curr_field) {
		u_input[curr_field].style.margin = '-1 -1 0 0'
		u_input[curr_field].style.border = '1px solid red'
		u_input[curr_field].focus()
	}

}


//---


