NS4 = (document.layers) ? true : false;

// Check if the enter key is hit in a form. The enter key has
// code 13. It's not possible to just submit the form on a enter
// key because the system need to set a option variable. Otherwise
// this code could be called om the form tas on a onKeyPress event like
// the following line:
// onKeyPress="checkEnter(event)"
function checkEnter(event) {
	var code = 0;
	
	if (NS4) {
		code = event.which;
	} else {
		code = event.keyCode;
	}
	
	return (code == 13);
		
}
	
// Submit on enter function. Used to submit a form when the
// enter-key is pressed. It takes the event from a onkeyPress
// action. The option and task are passed to the hidden input
// fields.
function submit_on_enter(event,option,task) {
	if (checkEnter(event)) {
		document.getElementById("option").value = option;
		document.getElementById("task").value = task;
		document.getElementById("mainForm").submit();
	}
}		


function validateCallMe() {
  	
  	error = 0;
  	
  	// Tjekker er der er skevet noget indhold
	if((document.mainForm.callMe_name.value=='') && (error==0)) {		
		alert('Navn mangler');
		document.mainForm.callMe_name.focus();
		error = 1;		
	}
	if((document.mainForm.callMe_phone.value=='') && (error==0)) {		
		alert('Telefonnummer mangler');
		document.mainForm.callMe_phone.focus();
		error = 1;		
	}
	
	if((document.mainForm.callMe_phone.value.length != 8) && (error==0)) {		
		alert('Fejl i telefonnummer');
		document.mainForm.callMe_phone.focus();
		error = 1;		
	}
					
	if(error == 0){
		
		document.mainForm.task.value='callMe';
		document.mainForm.submit();
	
	}

}

function validateOrder() {
  	
  	error = 0;
  	
  	// Tjekker er der er skevet noget indhold
	if((document.mainForm.billing_name.value=='') && (error==0)) {		
		alert('Navn mangler');
		document.mainForm.billing_name.focus();
		error = 1;		
	}
	if((document.mainForm.billing_address.value=='') && (error==0)) {		
		alert('Adresse mangler');
		document.mainForm.billing_address.focus();
		error = 1;		
	}
	if((document.mainForm.billing_zip.value=='') && (error==0)) {		
		alert('Postnummer mangler');
		document.mainForm.billing_zip.focus();
		error = 1;		
	}
	if((document.mainForm.billing_city.value=='') && (error==0)) {		
		alert('By mangler');
		document.mainForm.billing_city.focus();
		error = 1;		
	}
	
	if((document.mainForm.billing_phone.value=='') && (error==0)) {		
		alert('Telefonnummer mangler');
		document.mainForm.billing_phone.focus();
		error = 1;		
	}
	if((document.mainForm.billing_phone.value.length != 8) && (error==0)) {		
		alert('Fejl i telefonnummer');
		document.mainForm.billing_phone.focus();
		error = 1;		
	}
	
	if((document.mainForm.billing_email.value=='') && (error==0)) {		
		alert('E-mail mangler');
		document.mainForm.billing_email.focus();
		error = 1;		
	}
	
	if(document.mainForm.billing_company.value!='' && document.mainForm.billing_cvr.value=='' && error==0) {		
		alert('CVR mangler');
		document.mainForm.billing_cvr.focus();
		error = 1;		
	}
	
	if (document.getElementById('shippingCheckBox').checked == true) {
	
		if((document.mainForm.shipping_name.value=='') && (error==0)) {		
			alert('Navn mangler');
			document.mainForm.shipping_name.focus();
			error = 1;		
		}
		if((document.mainForm.shipping_address.value=='') && (error==0)) {		
			alert('Adresse mangler');
			document.mainForm.shipping_address.focus();
			error = 1;		
		}
		if((document.mainForm.shipping_zip.value=='') && (error==0)) {		
			alert('Postnummer mangler');
			document.mainForm.shipping_zip.focus();
			error = 1;		
		}
		if((document.mainForm.shipping_city.value=='') && (error==0)) {		
			alert('By mangler');
			document.mainForm.shipping_city.focus();
			error = 1;		
		}
		
		if((document.mainForm.shipping_phone.value=='') && (error==0)) {		
			alert('Telefonnummer mangler');
			document.mainForm.shipping_phone.focus();
			error = 1;		
		}
		if((document.mainForm.shipping_phone.value.length != 8) && (error==0)) {		
			alert('Fejl i telefonnummer');
			document.mainForm.shipping_phone.focus();
			error = 1;		
		}
		
		if((document.mainForm.shipping_email.value=='') && (error==0)) {		
			alert('E-mail mangler');
			document.mainForm.shipping_email.focus();
			error = 1;		
		}
		
		if(document.mainForm.shipping_company.value != '' && document.mainForm.shipping_cvr.value == '' && error==0) {		
			alert('CVR mangler');
			document.mainForm.shipping_cvr.focus();
			error = 1;		
		}	
	
	}
					
	if(error == 0){
		
		if (document.getElementById('paymentMethod1').checked == true){
			document.mainForm.paymentMethod.value='creditcard';
			document.mainForm.option.value='com_paymentCreditcard';
		}
		else {
			document.mainForm.paymentMethod.value='banktransfer';
			document.mainForm.option.value='com_confirmation';
		}
		
		
		if (document.mainForm.paymentMethod.value == "creditcard"){
			//document.mainForm.action = "https://secure.csite.com/relay.php";
			//document.mainForm.action = "https://secure.onlinepayment.dk/relay.aspx";
			document.mainForm.action = "https://securerelay.onlinepayment.dk/relay.aspx";
			document.mainForm.submit();
		}
		else {
			document.mainForm.method = "GET";
			document.mainForm.action = "order_redirect.php";
			document.mainForm.submit();			
		}
		
	}

}

function toogleShippingFields(checkbox) {
	
	
	if (checkbox.checked == true){
		document.getElementById('shipping_name').disabled = false;
		document.getElementById('shipping_company').disabled = false;
		document.getElementById('shipping_cvr').disabled = false;
		document.getElementById('shipping_address').disabled = false;
		document.getElementById('shipping_zip').disabled = false;
		document.getElementById('shipping_city').disabled = false;		
		document.getElementById('shipping_phone').disabled = false;		
		document.getElementById('shipping_email').disabled = false;
		document.getElementById('shipping_name').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_company').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_cvr').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_address').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_zip').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_city').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_phone').style.backgroundColor = 'FFFFFF';
		document.getElementById('shipping_email').style.backgroundColor = 'FFFFFF';
	}
	else {
		document.getElementById('shipping_name').disabled = true;
		document.getElementById('shipping_company').disabled = true;
		document.getElementById('shipping_cvr').disabled = true;
		document.getElementById('shipping_address').disabled = true;
		document.getElementById('shipping_zip').disabled = true;
		document.getElementById('shipping_city').disabled = true;
		document.getElementById('shipping_phone').disabled = true;
		document.getElementById('shipping_email').disabled = true;
		document.getElementById('shipping_name').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_company').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_cvr').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_address').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_zip').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_city').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_phone').style.backgroundColor = 'CCCCCC';
		document.getElementById('shipping_email').style.backgroundColor = 'CCCCCC';
	}
	
}


function newWindow(mypage,myname,w,h,features) {
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += features;
	win = window.open(mypage,myname,settings);
	win.window.focus();
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validateCreditCard() {
  	
  	error = 0;
  	
  	// Tjekker er der er skevet noget indhold
	if((document.mainForm.cardnumber.value=='') && (error==0)) {		
		alert('Kreditkortnummer mangler');
		document.mainForm.cardnumber.focus();
		error = 1;		
	}
	
	if((document.mainForm.cardnumber.value.length < 12 || document.mainForm.cardnumber.value.length > 16) && (error==0)) {		
		alert('Fejl i kreditkortnummer');
		document.mainForm.cardnumber.focus();
		error = 1;		
	}
	
	if((document.mainForm.cvc.value=='') && (error==0)) {		
		alert('Kontrolcifre mangler');
		document.mainForm.cvc.focus();
		error = 1;		
	}
	
	if((document.mainForm.cvc.value.length != 3) && (error==0)) {		
		alert('Fejl i kontrolcifre');
		document.mainForm.cvc.focus();
		error = 1;		
	}
					
	if(error == 0){
		
		document.mainForm.action='https://secure.onlinepayment.dk/auth.aspx';
		document.mainForm.submit();
		
		
	}
	
}

function numbersonly(myfield, e, dec) {
	var key;
	var keychar;
	
	if (window.event)
	  key = window.event.keyCode;
	else if (e)
	  key = e.which;
	else
	  return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	  return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	  return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	  {
	  myfield.form.elements[dec].focus();
	  return false;
	  }
	else
	  return false;
}
