// JavaScript Document
function ConvertToFloat(sText) {
	var S = sText;
	
	S = S.replace(/,/, '.'); // for input
	//alert(S);
	x = parseFloat(S);
	if(isNaN(x)) { x=0 };
	return x;
}

function ConvertToText(sText) {
	var tmpNum = parseFloat(sText);

	// Return the right number of decimal places
	var S = new String(tmpNum.toFixed(2));
	
	S = S.replace(/\./, ','); // for output
	//alert(S);
	return S;
}

var good;
function checkEmailAddress(field) {

// Note: The next expression must be all on one line...
//       allow no spaces, linefeeds, or carriage returns!
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

if (goodEmail){
   good = true;
} else {
   alert('Een geldig e-mail adres invullen AUB.');
   field.focus();
   field.select();
   good = false;
   }
return good;
}

function sendOffAuthor(){
	textCheck = window.document.TheForm.name.value;
	if (textCheck.length <1) {
		alert('Het naam veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.address.value;    
	if (textCheck.length <1) {
		alert('Het adres veld invullen AUB.');
		return;
	}         
	textCheck = window.document.TheForm.pc.value;    
	if (textCheck.length <6) {
		alert('Dit is geen correcte postcode.');
		return;
	}         
	textCheck = window.document.TheForm.town.value;    
	if (textCheck.length <1) {
		alert('Het woonplaats veld invullen AUB.');
		return;
	}
	// Make up strings for mail message
	var To = "info@childrenasking.org";
	var Subject = "Aanvragen machtiging.";
	var CC = "";
	var BCC = "";
	var Body = 
		"Beste Children Asking,\n\n\tIk/Wij willen hierbij graag een machtigingskaart aanvragen. " +
		"\n\n" + "Naam=" + window.document.TheForm.name.value +
		"\n\n" + "Adres=" + window.document.TheForm.address.value +
		"\n\n" + "Postcode=" + window.document.TheForm.pc.value + "\t" + "Plaats=" + window.document.TheForm.town.value +
		"\n\nMet vriendelijke groet,\n" + window.document.TheForm.name.value

	// Put everything together
	// 19-03-2007 Due to problems with Outlook I removed the empty CC and BCC fields
	var doc = "mailto:" + To + 
		//"?cc=" + CC + 
		//"&bcc=" + BCC + 
		"?subject=" + escape(Subject) + 
		"&body=" + escape(Body); 
	
	// Launch e-mail window
	window.location = doc;
}

function getValue(varname)
{
  // First, we load the URL into a variable
  var url = window.location.href;
  // Next, split the url by the ?
  var qparts = url.split("?");
  // Check that there is a querystring, return "" if not
  if (qparts.length == 0) {
    return "";
  }
  // Then find the querystring, everything after the ?
  var query = qparts[1];
  // Split the query string into variables (separates by &s)
  var vars = query.split("&");
  // Initialize the value with "" as default
  var value = "";
  // Iterate through vars, checking each one for varname
  for (i=0;i<vars.length;i++) {
    // Split the variable by =, which splits name and value
    var parts = vars[i].split("=");
    // Check if the correct variable
    if (parts[0] == varname) {
      // Load value into variable
      value = parts[1];
      // End the loop
      break;
    }
  }
  // Convert escape code
  value = unescape(value);
  // Convert "+"s to " "s
  value.replace(/\+/g," ");
  // Return the value
  return value;
}

function sendOffRegInfo(pAdoption){
	if ((!window.document.TheForm.radiobegin[0].checked) && (!window.document.TheForm.radiobegin[1].checked) && (!window.document.TheForm.radiobegin[2].checked)) {
		alert('De gewenste aanhef aangeven AUB.');
		return;
	}
	var begin = "";
	for(cnt = 0;cnt < 3;cnt++) {
		if(window.document.TheForm.radiobegin[cnt].checked) {
			begin=window.document.TheForm.radiobegin[cnt].value;
		}
	}
	textCheck = window.document.TheForm.initial.value;
	if (textCheck.length <1) {
		alert('Het voorletter(s) veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.name.value;
	if (textCheck.length <1) {
		alert('Het achternaam veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.address.value;    
	if (textCheck.length <1) {
		alert('Het adres veld invullen AUB.');
		return;
	}         
	textCheck = window.document.TheForm.pc.value;    
	if (textCheck.length <6) {
		alert('Dit is geen correcte postcode.');
		return;
	}         
	textCheck = window.document.TheForm.town.value;    
	if (textCheck.length <1) {
		alert('Het woonplaats veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.telephone.value;    
	if (textCheck.length <1) {
		alert('Het telefoon nummer veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.mobile.value;    
	if (textCheck.length <1) {
		alert('Het mobiele telefoon nummer veld invullen AUB.');
		return;
	}
	if(!checkEmailAddress(window.document.TheForm.mail)) {
		return;
	}
	if ((!window.document.TheForm.radiopref[0].checked) && (!window.document.TheForm.radiopref[1].checked) && (!window.document.TheForm.radiopref[2].checked)) {
		alert('De gewenste voorkeur aangeven AUB.');
		return;
	}
	var pref = "";
	for(cnt = 0;cnt < 3;cnt++) {
		if(window.document.TheForm.radiopref[cnt].checked) {
			pref=window.document.TheForm.radiopref[cnt].value;
		}
	}
	// Make up strings for mail message
	var To = "info@childrenasking.org";
	var Subject = "Aanmelden adoptieprogramma " + pAdoption + ".";
	var CC = "";
	var BCC = "";
	var Body = 
		"Beste Children Asking,\n\n\tIk/Wij willen hierbij graag ons opgeven voor het adoptieprogramma " + pAdoption +". " +
		"\n\n" + "Naam=" + begin + " " + window.document.TheForm.initial.value + " " + window.document.TheForm.infix.value + " " + window.document.TheForm.name.value +
		"\n\n" + "Adres=" + window.document.TheForm.address.value +
		"\n\n" + "Postcode=" + window.document.TheForm.pc.value + "\t" + "Plaats=" + window.document.TheForm.town.value +
		"\n\n" + "Telefoon=" + window.document.TheForm.telephone.value +
		"\n\n" + "Mobiel=" + window.document.TheForm.mobile.value +
		"\n\n" + "E-mail=" + window.document.TheForm.mail.value +
		"\n\n" + "Kerk=" + window.document.TheForm.denomination.value +
		"\n\n" + "Voorkeur=" + pref +
		"\n\nMet vriendelijke groet,\n" + begin + " " + window.document.TheForm.initial.value + " " + window.document.TheForm.infix.value + " " + window.document.TheForm.name.value;

	// Put everything together
	// 19-03-2007 Due to problems with Outlook I removed the empty CC and BCC fields
	var doc = "mailto:" + To + 
		//"?cc=" + CC + 
		//"&bcc=" + BCC + 
		"?subject=" + escape(Subject) + 
		"&body=" + escape(Body); 
	
	// Launch e-mail window
	window.location = doc;
}

function sendOffTicketInfo(pAdoption){
	if ((!window.document.TheForm.radiobegin[0].checked) && (!window.document.TheForm.radiobegin[1].checked) && (!window.document.TheForm.radiobegin[2].checked)) {
		alert('De gewenste aanhef aangeven AUB.');
		return;
	}
	var begin = "";
	for(cnt = 0;cnt < 3;cnt++) {
		if(window.document.TheForm.radiobegin[cnt].checked) {
			begin=window.document.TheForm.radiobegin[cnt].value;
		}
	}
	textCheck = window.document.TheForm.initial.value;
	if (textCheck.length <1) {
		alert('Het voorletter(s) veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.name.value;
	if (textCheck.length <1) {
		alert('Het achternaam veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.address.value;    
	if (textCheck.length <1) {
		alert('Het adres veld invullen AUB.');
		return;
	}         
	textCheck = window.document.TheForm.pc.value;    
	if (textCheck.length <6) {
		alert('Dit is geen correcte postcode.');
		return;
	}         
	textCheck = window.document.TheForm.town.value;    
	if (textCheck.length <1) {
		alert('Het woonplaats veld invullen AUB.');
		return;
	}
	if(!checkEmailAddress(window.document.TheForm.mail)) {
		return;
	}
	textCheck = window.document.TheForm.NrTickets.value;    
	if (textCheck.length <1) {
		alert('Het gewenste aantal kaarten aangeven AUB.');
		return;
	}
	// Make up strings for mail message
	var To = "info@childrenasking.org";
	var Subject = "Bestellen kaarten" + pAdoption + ".";
	var CC = "";
	var BCC = "";
	var Body = 
		"Beste Children Asking,\n\n\tIk/Wij willen hierbij graag kaarten bestellen voor het " + pAdoption +". " +
		"\n\n" + "Naam=" + begin + " " + window.document.TheForm.initial.value + " " + window.document.TheForm.infix.value + " " + window.document.TheForm.name.value +
		"\n\n" + "Adres=" + window.document.TheForm.address.value +
		"\n\n" + "Postcode=" + window.document.TheForm.pc.value + "\t" + "Plaats=" + window.document.TheForm.town.value +
		"\n\n" + "Telefoon=" + window.document.TheForm.telephone.value +
		"\n\n" + "Mobiel=" + window.document.TheForm.mobile.value +
		"\n\n" + "E-mail=" + window.document.TheForm.mail.value +
		"\n\n" + "Gewenste aantal kaarten=" + window.document.TheForm.NrTickets.value +
		"\n\nMet vriendelijke groet,\n" + begin + " " + window.document.TheForm.initial.value + " " + window.document.TheForm.infix.value + " " + window.document.TheForm.name.value;

	// Put everything together
	// 19-03-2007 Due to problems with Outlook I removed the empty CC and BCC fields
	var doc = "mailto:" + To + 
		//"?cc=" + CC + 
		//"&bcc=" + BCC + 
		"?subject=" + escape(Subject) + 
		"&body=" + escape(Body); 
	
	// Launch e-mail window
	window.location = doc;
}

function sendOffShopInfo(pShopInfo){
	if ((!window.document.TheForm.radiobegin[0].checked) && (!window.document.TheForm.radiobegin[1].checked) && (!window.document.TheForm.radiobegin[2].checked)) {
		alert('De gewenste aanhef aangeven AUB.');
		return;
	}
	var begin = "";
	for(cnt = 0;cnt < 3;cnt++) {
		if(window.document.TheForm.radiobegin[cnt].checked) {
			begin=window.document.TheForm.radiobegin[cnt].value;
		}
	}
	textCheck = window.document.TheForm.initial.value;
	if (textCheck.length <1) {
		alert('Het voorletter(s) veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.name.value;
	if (textCheck.length <1) {
		alert('Het achternaam veld invullen AUB.');
		return;
	}
	textCheck = window.document.TheForm.address.value;    
	if (textCheck.length <1) {
		alert('Het adres veld invullen AUB.');
		return;
	}         
	textCheck = window.document.TheForm.pc.value;    
	if (textCheck.length <6) {
		alert('Dit is geen correcte postcode.');
		return;
	}         
	textCheck = window.document.TheForm.town.value;    
	if (textCheck.length <1) {
		alert('Het woonplaats veld invullen AUB.');
		return;
	}
	if(!checkEmailAddress(window.document.TheForm.mail)) {
		return;
	}
	// Check to see which order form we need to process
	if(pShopInfo == "Kerstkaartenactie") {
		AmountSerie1 = window.document.TheForm.NrSeriesOne.value;
		AmountSerie2 = window.document.TheForm.NrSeriesTwo.value;
		if((AmountSerie1.length<1)&&(AmountSerie2.length<1)) {
			alert('De gewenste aantallen invullen AUB.');
			return;
		}
	}
	if(pShopInfo == "Wenskaartenactie") {
		AmountCard1 = ConvertToFloat(window.document.TheForm.NrCardOne.value);
		AmountCard2 = ConvertToFloat(window.document.TheForm.NrCardTwo.value);
		AmountCard3 = ConvertToFloat(window.document.TheForm.NrCardThree.value);
		AmountCard4 = ConvertToFloat(window.document.TheForm.NrCardFour.value);
		AmountCard5 = ConvertToFloat(window.document.TheForm.NrCardFive.value);
		AmountCard6 = ConvertToFloat(window.document.TheForm.NrCardSix.value);
		AmountCard7 = ConvertToFloat(window.document.TheForm.NrCardSeven.value);
		AmountCard8 = ConvertToFloat(window.document.TheForm.NrCardEight.value);
		tmpTotal = AmountCard1 + AmountCard2 + AmountCard3 + AmountCard4 + AmountCard5 + AmountCard6 + AmountCard7 + AmountCard8;
		alert(tmpTotal);
		if(tmpTotal % 3) {
			alert('De gewenste totale hoeveelheid kaarten\n moet 3 of een veelvoud van 3 zijn AUB.');
			return;
		}
	}
	if(pShopInfo == "Bureaukalender 2008") {
		AmountSerie1 = window.document.TheForm.NrSeriesOne.value;
		if(AmountSerie1.length<1) {
			alert('De gewenste aantallen invullen AUB.');
			return;
		}
	}
	// Make up strings for mail message
	var To = "info@childrenasking.org";
	var Subject = "Winkel - " + pShopInfo + ".";
	var CC = "";
	var BCC = "";
	var Body = 
		"Beste Children Asking,\n\n\tIk/Wij willen hierbij graag meedoen met de " + pShopInfo +". " +
		"\n\n" + "Naam=" + begin + " " + window.document.TheForm.initial.value + " " + window.document.TheForm.infix.value + " " + window.document.TheForm.name.value +
		"\n\n" + "Adres=" + window.document.TheForm.address.value +
		"\n\n" + "Postcode=" + window.document.TheForm.pc.value + "\t" + "Plaats=" + window.document.TheForm.town.value +
		"\n\n" + "E-mail=" + window.document.TheForm.mail.value
		if(pShopInfo == "Kerstkaartenactie") {
			Body += "\n\n" + "Serie1=" + window.document.TheForm.NrSeriesOne.value + " Stuks" +
				"\n\n" + "Serie2=" + window.document.TheForm.NrSeriesTwo.value + " Stuks"
		}
		if(pShopInfo == "Wenskaartenactie") {
			if (window.document.TheForm.NrCardOne.value>0) { Body +=  "\n\n" + "Card1=" + window.document.TheForm.NrCardOne.value + " Stuks" };
			if (window.document.TheForm.NrCardTwo.value>0) { Body +=  "\n\n" + "Card2=" + window.document.TheForm.NrCardTwo.value + " Stuks" };
			if (window.document.TheForm.NrCardThree.value>0) { Body +=  "\n\n" + "Card3=" + window.document.TheForm.NrCardThree.value + " Stuks" };
			if (window.document.TheForm.NrCardFour.value>0) { Body +=  "\n\n" + "Card4=" + window.document.TheForm.NrCardFour.value + " Stuks" };
			if (window.document.TheForm.NrCardFive.value>0) { Body +=  "\n\n" + "Card5=" + window.document.TheForm.NrCardFive.value + " Stuks" };
			if (window.document.TheForm.NrCardSix.value>0) { Body +=  "\n\n" + "Card6=" + window.document.TheForm.NrCardSix.value + " Stuks" };
			if (window.document.TheForm.NrCardSeven.value>0) { Body +=  "\n\n" + "Card7=" + window.document.TheForm.NrCardSeven.value + " Stuks" };
			if (window.document.TheForm.NrCardEight.value>0) { Body +=  "\n\n" + "Card8=" + window.document.TheForm.NrCardEight.value + " Stuks" };
		}
		if(pShopInfo == "Bureaukalender 2008") {
			Body += "\n\n" + "Kalender=" + window.document.TheForm.NrSeriesOne.value + " Stuks" +
				"\n\n"
		}
		Body += "\n\nMet vriendelijke groet,\n" + begin + " " + window.document.TheForm.initial.value + " " + window.document.TheForm.infix.value + " " + window.document.TheForm.name.value;

	// Put everything together
	// 19-03-2007 Due to problems with Outlook I removed the empty CC and BCC fields
	var doc = "mailto:" + To + 
		//"?cc=" + CC + 
		//"&bcc=" + BCC + 
		"?subject=" + escape(Subject) + 
		"&body=" + escape(Body); 
	
	// Launch e-mail window
	window.location = doc;
}

function CheckDonationAmount(oForm){
	var tmp;
	
	/* Check Donation amount */
	tmp = ConvertToFloat(oForm.bedrag.value);
	tmp = tmp * 100;
	oForm.amount.value = tmp.toString();
	oForm.itemPrice1.value = tmp.toString();
	// Check if donation amount is more than Euro 5,00
	if(tmp>=500) {
		oForm.submit2.disabled = false;
	}
	else {
		oForm.submit2.disabled = true;
	}
	for (var i = 0;i < oForm.keuze.length; i++) {
		if (oForm.keuze[i].checked) {
			if (oForm.keuze[i].value!=tmp) {
				oForm.keuze[i].checked=false;
			}
		}
	}
}

function FillInAmount(oForm,value) {
	var tmp;
	
	// Fill in the right amount depending on the selected radio button
	if(value==1) {
		tmp = 5000;
	}
	if(value==2) {
		tmp = 2500;
	}
	if(value==3) {
		tmp = 1000;
	}
	oForm.bedrag.value = ConvertToText(tmp/100.0);
	CheckDonationAmount(oForm);
}
