var $j = jQuery;
$j(document).ready(function () {
	targetBlank();
});
function targetBlank() {
	// fix for target=”_blank”
	$j("a[@rel~='external']").click(function(){
		window.open($j(this).attr("href"));
		return false;
	});
}

function addTableRow(table_for, idx){
	var tb= document.getElementById(eval("table_for + '_table'"));
	var tr= document.createElement('TR');
	tr.id= eval("'additional_' + table_for + '_' + idx");
	tb.appendChild(tr);
}

function uncheckOthers(formId, checkbox_element){
	theForm = document.getElementById(formId);
	for(i=0; i < theForm.elements.length; i++){
		if (theForm.elements[i].name == checkbox_element.name){
			if (theForm.elements[i].value != checkbox_element.value)
				theForm.elements[i].checked = false;
		}
	}
}

function populateDonationBillingForm(formId){
	if ($('same_billing_address').checked){
		$('billing_address1').value = $('donor_address1').value;
		$('billing_address2').value = $('donor_address2').value;
		$('billing_city').value = $('donor_city').value;
		$('billing_state').value = $('donor_state').value;	
		$('billing_postal_code').value = $('donor_postal_code').value;
	}
	else{
		$('billing_address1').value = '';
		$('billing_address2').value = '';
		$('billing_city').value = '';
		$('billing_state').value = '';
		$('billing_postal_code').value = '';
	}
}

function populateDonationContactForm(formId){
	if ($('same_contact_address').checked){
		$('donor_address1').value = $('billing_address1').value;
		$('donor_address2').value = $('billing_address2').value;
		$('donor_city').value = $('billing_city').value;
		$('donor_state').value = $('billing_state').value;	
		$('donor_postal_code').value = $('billing_postal_code').value;
	}
	else{
		$('billing_address1').value = '';
		$('billing_address2').value = '';
		$('billing_city').value = '';
		$('billing_state').value = '';
		$('billing_postal_code').value = '';
	}
}

function populateProgramBillingForm(){
	if ($('same_billing_address').checked){
		$('credit_card_first_name').value = $('user_firstname').value;
		$('credit_card_last_name').value = $('user_lastname').value
		$('billing_address1').value = $('contact_address1').value;
		$('billing_address2').value = $('contact_address2').value;
		$('billing_city').value = $('contact_city').value;
		$('billing_state').value = $('contact_state').value;	
		$('billing_postal_code').value = $('contact_postal_code').value;
	}
	else{
		$('credit_card_first_name').value = '';
		$('credit_card_last_name').value = '';
		$('billing_address1').value = '';
		$('billing_address2').value = '';
		$('billing_city').value = '';
		$('billing_state').value = '';
		$('billing_postal_code').value = '';
	}
}

function populateProgramContactForm(){
	if ($('same_billing_address').checked){
		$('user_firstname').value = $('credit_card_first_name').value;
		$('user_lastname').value = $('credit_card_last_name').value;
		$('contact_address1').value = $('billing_address1').value;
		$('contact_address2').value = $('billing_address2').value;
		$('contact_city').value = $('billing_city').value;
		$('contact_state').value = $('billing_state').value;	
		$('contact_postal_code').value = $('billing_postal_code').value;
		if ($('contact_country').value != "") {
			contactElement = $('contact_country');
			billingElement = $('billing_country');			
			for (optionCounter = 0; optionCounter < contactElement.length; optionCounter++) { 
				if (contactElement.options[optionCounter].value == billingElement.value)
					contactElement.options[optionCounter].selected = true;
			}
		}
	}
	else{
		$('user_firstname').value = '';
		$('user_lastname').value = '';
		$('contact_address1').value = '';
		$('contact_address2').value = '';
		$('contact_city').value = '';
		$('contact_state').value = '';
		$('contact_postal_code').value = '';
		contactElement = $('contact_country');
		for (optionCounter = 0; optionCounter < contactElement.length; optionCounter++) { 
			if (contactElement.options[optionCounter].value == 'United States')
				contactElement.options[optionCounter].selected = true;
		}
	}
}

function copyBilling() {
	if (document.getElementById("form_copy_billing").checked == true) {
		document.getElementById("donor_firstname").value = document.getElementById("donation_firstname").value;
		document.getElementById("donor_lastname").value = document.getElementById("donation_lastname").value;
		document.getElementById("donor_company").value = document.getElementById("donation_company").value;
		document.getElementById("donor_address1").value = document.getElementById("donation_address1").value;
		document.getElementById("donor_address2").value = document.getElementById("donation_address2").value;
		document.getElementById("donor_city").value = document.getElementById("donation_city").value;
		document.getElementById("donor_state").value = document.getElementById("donation_state").value;
		document.getElementById("donor_postal_code").value = document.getElementById("donation_postal_code").value;
		
		if (document.getElementById("donor_country").value != "") {
			theCompanyElement = document.getElementById("donor_country");
			theBillingCompanyElement = document.getElementById("form_country");
			
			for (optionCounter = 0; optionCounter < theCompanyElement.length; optionCounter++) { 
				if (theCompanyElement.options[optionCounter].value == theBillingCompanyElement.value)
					theCompanyElement.options[optionCounter].selected = true;
			}
		}
	}
}

function copyPersonal() {
	if (document.getElementById("donation_copy_personal").checked == true) {
		document.getElementById("donation_firstname").value = document.getElementById("donor_firstname").value;
		document.getElementById("donation_lastname").value = document.getElementById("donor_lastname").value;
		document.getElementById("donation_company").value = document.getElementById("donor_company").value;
		document.getElementById("donation_address1").value = document.getElementById("donor_address1").value;
		document.getElementById("donation_address2").value = document.getElementById("donor_address2").value;
		document.getElementById("donation_city").value = document.getElementById("donor_city").value;
		document.getElementById("donation_state").value = document.getElementById("donor_state").value;
		document.getElementById("donation_postal_code").value = document.getElementById("donor_postal_code").value;
		
		if (document.getElementById("donor_country").value != "") {
			theCompanyElement = document.getElementById("form_country");
			theBillingCompanyElement = document.getElementById("donor_country");
			
			for (optionCounter = 0; optionCounter < theCompanyElement.length; optionCounter++) { 
				if (theCompanyElement.options[optionCounter].value == theBillingCompanyElement.value)
					theCompanyElement.options[optionCounter].selected = true;
			}
		}
	}
	else{
		document.getElementById("donation_firstname").value = '';
		document.getElementById("donation_lastname").value = '';
		document.getElementById("donation_company").value = '';
		document.getElementById("donation_address1").value = '';
		document.getElementById("donation_address2").value = '';
		document.getElementById("donation_city").value = '';
		document.getElementById("donation_state").value = '';
		document.getElementById("donation_postal_code").value = '';
		contactElement = $('donor_country');
		for (optionCounter = 0; optionCounter < contactElement.length; optionCounter++) { 
			if (contactElement.options[optionCounter].value == 'United States')
				contactElement.options[optionCounter].selected = true;
		}
	}
}

function checkAll(field, value) {
	for (i = 0; i < field.length; i++)
		field[i].checked = value;
}