/*
  give_regularly_score_form.js
  Generates online donation form for Score Ethiopia

  Copyright (c) 2009 Link Ethiopia

  $HeadURL: https://admin.linkethiopia.org:2080/svn/website-pub/releases/rel_1.0.28/javascript/give_regularly_score_form.js $
  $Id: give_regularly_score_form.js 3 2009-11-22 22:06:16Z sroberts $

   Date       Name         Comments
  ----------+------------+---------------------------------------------
   19/11/09   sroberts     Re-baselined codeset

 */

var currentStage = 1;

function back() {
    switch(currentStage) {
        case 2:
            document.getElementById("donate_stage_2").className = "hidden";
            
            document.getElementById("donate_stage_1_sub").style.color = "#000000";
            document.getElementById("donate_stage_1_single").style.color = "#000000";
            
            document.getElementById("donate_stage_1_sub").style.backgroundColor = "#F0C659";
            document.getElementById("donate_stage_1_single").style.backgroundColor = "#DD9547"; 
            
            document.donate_form.single_amt.disabled = false;
            document.donate_form.sub_amt.disabled = false;
	    document.donate_form.freq.disabled = false;
	    document.donate_form.next_sub.disabled = false;
	    document.donate_form.next_single.disabled = false;
	    document.donate_form.sub_currency.disabled = false;
	    document.donate_form.single_currency.disabled = false;
	    currentStage = 1;
        break;
        default:
        	return false;
    }
}

function format_amount(elem) {
	if(! isNaN(elem.value)) {
		elem.value = (Math.floor(elem.value * 100)) / 100.0;
	}
}

function read_params() {
    var searchString = window.location.search;
    if(searchString.length > 0) {
    	searchItems = (searchString.substring(1, searchString.length) + "&").split("&");
    	for(i = 0; i < searchItems.length; i ++) {
   		  	searchKeyVal = searchItems[i].split("=");
   		  	if(searchKeyVal[0] == "monthly") {
   		  		document.donate_form.sub_amt.value = searchKeyVal[1];
   		  		to_next(1);
   		  	}
   		  	if(searchKeyVal[0] == "amount") {
   		  		document.donate_form.single_amt.value = searchKeyVal[1];
   		  		to_next(2);
   		  	}
   		}
    }
}

function reset_error() {
	document.getElementById("donate_error").innerHTML = "";
	document.getElementById("donate_error").style.border = "none";
	document.getElementById("donate_error").style.backgroundColor = "transparent";
	document.getElementById("donate_error").style.padding = "0px";
}

function donate_error(msg) {
	document.getElementById("donate_error").innerHTML = "<b>" + msg + "</b>";
	document.getElementById("donate_error").style.border = "solid #C00000 3px";
	document.getElementById("donate_error").style.backgroundColor = "#FFDBDB";
	document.getElementById("donate_error").style.padding = "10px";
    /* window.location="#donate"; */
}

function to_next(mode) {
	reset_error();
	switch(currentStage) {
	    case 1:
	    	var donateAmount = 0;
			var donateFreq = "One-off";
	
			if(mode == 1) {
				donateAmount = document.donate_form.sub_amt.value;
				donateCurrency = document.donate_form.sub_currency.value;
				donateFreq   = document.donate_form.freq.value;
			}
			if(mode == 2) {
				donateAmount = document.donate_form.single_amt.value;
				donateCurrency = document.donate_form.single_currency.value;
				donateFreq   = "One-off";
			}
	    
			if((donateAmount < 1) || (isNaN(donateAmount))) {
				donate_error("Donation must be of at least 1.");
				return false;
			}
				    
	    	if(mode == 1) {
	    		document.donate_form.cmd.value = "_xclick-subscriptions";
	    		document.donate_form.amount.value = donateAmount;
	    		document.donate_form.currency_code.value = donateCurrency;
	    		if(donateFreq == "Monthly") {
	    			document.donate_form.frequency.value = 1;
	    		} else if(donateFreq == "Quarterly") {
	    			document.donate_form.frequency.value = 3;
	    		} else if(donateFreq == "Yearly") {
	    			document.donate_form.frequency.value = 12;
	    		}
	    		
	    		document.donate_form.sub_amt.disabled = true;
	    		document.donate_form.sub_currency.disabled = true;
	    		document.donate_form.single_currency.disabled = true;
	    		document.donate_form.freq.disabled = true;
	    		document.donate_form.single_amt.disabled = true;
	    		document.getElementById("donate_stage_1_single").style.color = "#C0C0C0";
	    		document.getElementById("donate_stage_1_single").style.backgroundColor = "#EEEEEE";
	    	}
	    	if(mode == 2) {
	    		document.donate_form.cmd.value = "_xclick";
	    		document.donate_form.amount.value = donateAmount;
	    		document.donate_form.currency_code.value = donateCurrency;
	    		
	    		document.donate_form.single_amt.disabled = true;
	    		document.donate_form.sub_currency.disabled = true;
	    		document.donate_form.single_currency.disabled = true;
	    		document.donate_form.sub_amt.disabled = true;
	    		document.donate_form.freq.disabled = true;
	    		/* document.donate_form.single_amt.readonly = true; */
	    		document.getElementById("donate_stage_1_sub").style.color = "#C0C0C0";
	    		document.getElementById("donate_stage_1_sub").style.backgroundColor = "#EEEEEE";
	    	}
	    	
	    	document.donate_form.item_name.value = donateFreq + " donation.";
	    	
			document.getElementById("donate_stage_2_hdr").innerHTML = "Thank you, you have chosen to make a <b>" + donateFreq + "</b> donation of <b>" + donateAmount + " " + donateCurrency + "</b> (<a href=\"javascript:back();\">to change your donation click here</a>)";
	    	document.donate_form.next_single.disabled = true;
	    	document.donate_form.next_sub.disabled = true;
	    	document.getElementById("donate_stage_2").className = "visible;";
	    	
	    	
	    	currentStage = 2;
	    	document.donate_form.first_name.focus();
	    	
	    	return false;
	    	break;
	    case 2:
	    	if(document.donate_form.first_name.value.length == 0) {
	    		donate_error("Please enter your first name.");
    			return false;
    		}
    		if(document.donate_form.last_name.value.length == 0) {
    			donate_error("Please enter your last name.");
    			return false;
    		}
    		if(document.donate_form.address1.value.length == 0) {
    			donate_error("Please enter the first line of your address.");
    			return false;
    		}
    		if(document.donate_form.address2.value.length == 0) {
    			donate_error("Please enter the second line of your address.");
    			return false;
    		}
    		/*
    		if(document.donate_form.city.value.length == 0) {
    			donate_error("Please enter your city.");
    			return false;
    		}
    		*/
    		if(document.donate_form.state.value.length == 0) {
    			donate_error("Please enter your county/state.");
    			return false;
    		}
    		if(document.donate_form.zip.value.length == 0) {
    			donate_error("Please enter your postal / zip code.");
    			return false;
    		}
    		if(document.donate_form.country.value.length == 0) {
    			donate_error("Please enter your country.");
    			return false;
    		}
    		if(document.donate_form.mailing_list.value == "yes") {
    			if(document.donate_form.email.value.length == 0) {
    				donate_error("Please enter your e-mail address.");
    				return false;
    			}
    		}
    		
    		return true;
	    	
	    default:
	    	return false;
	}
	return false;
}

document.write('<div id="donate">');
document.write('<form action="/donate_score.php" method="post" name="donate_form" onSubmit="return to_next()">');
document.write('<table width="100%"><tr valign="top">');
document.write('<td width="48%"><div id="donate_stage_1_sub" class="visible donate_stage_1_sub">');
document.write('<h3>Make a regular donation</h3>');
document.write('<p><b>Donation amount:</b> ');
document.write('<input name="sub_amt" value="0.00" size="5" maxlength="8" onChange="format_amount(this);" /> <br />');

document.write('<select name="sub_currency">');
document.write('<option value="GBP" selected>Pounds Sterling (&pound;)</option>');
document.write('<option value="USD">U.S. Dollars ($)</option>');
document.write('<option value="EUR">Euros (&euro;)</option>');
document.write('<option value="AUD">Australian Dollars (A$)</option>');
document.write('<option value="CAD">Canadian Dollars (C$)</option>');
document.write('<option value="NZD">New Zealand Dollar ($)</option>');
document.write('<option value="JPY">Yen (&yen;)</option>');
document.write('<option value="CHF">Swiss Franc</option>');
document.write('<option value="HKD">Hong Kong Dollar ($)</option>');
document.write('<option value="SGD">Singapore Dollar ($)</option>');
document.write('<option value="SEK">Swedish Krona</option>');
document.write('<option value="DKK">Danish Krone</option>');
document.write('<option value="PLN">Polish Zloty</option>');
document.write('<option value="NOK">Norwegian Krone</option>');
document.write('<option value="HUF">Hungarian Forint</option>');
document.write('<option value="CZK">Czech Koruna</option>');
document.write('<option value="ILS">Israeli Shekel</option>');
document.write('<option value="MXN">Mexican Peso</option>');
document.write('</select><br />');

document.write('<b>Donation frequency:</b> ');
document.write('<select name="freq">');
document.write('<option value="Monthly">Monthly</option>');
document.write('<option value="Quaterly">Quarterly (3 months)</option>');
document.write('<option value="Yearly">Yearly (12 months)</option>');
document.write('</select><br /><input name="next_sub" type="button" onClick="to_next(1);" value="Next &gt;" /></p>');
document.write('</div></td>');

document.write('<td width="4%" valign="middle"><b>or</b></td>');

document.write('<td width="48%"><div id="donate_stage_1_single" class="visible donate_stage_1_single">');
document.write('<h3>Make a one-off donation</h3>');
document.write('<p><b>Donation amount:</b><br />');
document.write('<input name="single_amt" value="0.00" size="5" maxlength="8" onChange="format_amount(this);" /> <br />');
document.write('<select name="single_currency">');
document.write('<option value="GBP" selected>Pounds Sterling (&pound;)</option>');
document.write('<option value="USD">U.S. Dollars ($)</option>');
document.write('<option value="EUR">Euros (&euro;)</option>');
document.write('<option value="AUD">Australian Dollars (A$)</option>');
document.write('<option value="CAD">Canadian Dollars (C$)</option>');
document.write('<option value="NZD">New Zealand Dollar ($)</option>');
document.write('<option value="JPY">Yen (&yen;)</option>');
document.write('<option value="CHF">Swiss Franc</option>');
document.write('<option value="HKD">Hong Kong Dollar ($)</option>');
document.write('<option value="SGD">Singapore Dollar ($)</option>');
document.write('<option value="SEK">Swedish Krona</option>');
document.write('<option value="DKK">Danish Krone</option>');
document.write('<option value="PLN">Polish Zloty</option>');
document.write('<option value="NOK">Norwegian Krone</option>');
document.write('<option value="HUF">Hungarian Forint</option>');
document.write('<option value="CZK">Czech Koruna</option>');
document.write('<option value="ILS">Israeli Shekel</option>');
document.write('<option value="MXN">Mexican Peso</option>');
document.write('</select><br />');
document.write('<input name="next_single" type="button" onClick="to_next(2);" value="Next &gt;" /><br />&nbsp;<br />&nbsp;</p>');
document.write('</div></td>');
document.write('</tr></table>');

document.write('<div id="donate_stage_2" class="hidden">');
document.write('<h3>Step 2 - Enter your details</h3>');
document.write('<p id="donate_stage_2_hdr"></p>');
document.write('<table border="0"><tr>');
document.write('<td class="donate_form"><b>First Name</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="first_name" size="15" maxlength="32" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>Last Name</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="last_name" size="15" maxlength="64" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>House No &amp; Street Name</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="address1" size="25" maxlength="100" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>Town</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="address2" size="25" maxlength="100" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>City</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="city" size="25" maxlength="100" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>County / State</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="state" size="15" maxlength="32" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>Postal / Zip Code</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="zip" size="10"  maxlength="32" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>Country</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="country" size="20" maxlength="32" value="United Kingdom" /></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>Gift Aid?</b></td>');
document.write('<td class="donate_form"><input type="checkbox" name="giftaid" value="yes" /></td>');
document.write('<td class="donate_form"><p>If you are a UK taxpayer, please Gift Aid your donation. It enables us to reclaim <emp>28 pence for every pound</emp> you donate, at no extra cost to you.</p></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"></td>');
document.write('<td class="donate_form" colspan="2"><span class="small"><i>I understand that I must pay an amount of UK income tax or capital gains tax equal to the tax reclaimed on my donations (currently 28p for every &pound;1 given.).</i></span></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>E-Mail</b></td>');
document.write('<td class="donate_form" colspan="2"><input name="email" size="30" maxlength="100"/></td>');
document.write('</tr><tr>');
document.write('<td class="donate_form"><b>Mailing List?</b></td>');
document.write('<td class="donate_form"><input type="checkbox" name="mailinglist" value="yes" /></td>');
document.write('<td class="donate_form"><p>Join our mailing list to keep up to date with how we are spending your generous donations. Receive monthly newsletters and occasional other e-mails about our work. (<i>Your details are never passed to other companies.</i>)</p></td>');
document.write('</tr></table>');

document.write('<p>Your donation will now be made quickly and securely via our payment partner, Paypal, in the next step. This keeps our administrative costs to a minimum, ensuring that more of your gift goes to help those in need. You do <b>not</b> have to create a Paypal account first, simply choose the option to pay by debit/credit card. Thank you!</p>');
document.write('<input type="submit" value="Proceed &gt;" />');
document.write('<input type="hidden" name="cmd" value="" />');
document.write('<input type="hidden" name="amount" value="" />');
document.write('<input type="hidden" name="currency_code" value="" />');
document.write('<input type="hidden" name="frequency" value="0" />');
document.write('<input type="hidden" name="item_name" value="" />');
document.write('</div>');

document.write('<p><span id="donate_error" style="color: #C00000;"></span></p>');

document.write('</form>');
document.write('<br />');
document.write('</div>');


read_params();

