var settings;

$(document).ready(function(){
	$('.additionalPromo').click(function(){
		SelectPromoItem();
	});
	$("#rdCheck").click(function(){
    if($("#rdCheck").attr("checked")){
        $("input[name$='VariantID']").val("63968");
        $("#rdRecurrency").val("2");
    }else{
        $("input[name$='VariantID']").val("20247");        
        $("#rdRecurrency").val("0");
    }
});
});

function HandleSourceCode(){
	$("input[name=SourceCode]").remove();
	var fh = $('#donateForm');
	var sourcecode = GetParam("sourcecode");
	if(sourcecode == ''){
		sourcecode = settings.defaultSourceCode;
	}
	if(sourcecode == null){
		sourcecode = '';
	}
	fh.append('<input type="hidden" name="SourceCode" Value="' + sourcecode + '"/>');
}
function buildForm(formHolderID, formSettings){
	settings = formSettings;
	var fh = $("#" + formHolderID);
	fh.html("");
	var seloffer = GetParam("seloffer");
	if(seloffer == null || seloffer == ''){
		seloffer = settings.defaultOffer;
	}
	if(seloffer == null){
		seloffer = '';
	}
	$.each(formSettings.levels, function(i, item){
		fh.append(BuildRadio(item, seloffer));
	});
	if($("input[name=r1]:checked").length == 0){
		if(seloffer != ''){
			$("input[name=r1]:last").attr("checked", true);
		}
		else{
			$("input[name=r1]:eq(1)").attr("checked", true);
		}
	}
	UpdateDonation();
	HandleSourceCode();
}

function BuildRadio(value, seloffer){
	if(seloffer == null){
		seloffer = '';
	}
	var radio = '<label class="input-amounts"><input type="radio" name="r1" value="' + value + '" ';
	if(value == "customPrice"){
		if($("input[name=r1]:checked").length > 0){
			seloffer = '';
		}
		radio = radio + ' onClick="$(\'#customPrice\').focus();" />' + '$<input name="customPrice" id="customPrice" type="text" onBlur="UpdateDonation()" value="' + seloffer + '"	 /> Other';
	}
	else{
		if(value == seloffer){
			radio = radio + ' checked ';
		}
		radio = radio + ' onClick="UpdateDonation()" />' + '$' + value;
	}
	return radio + '</label>';
}

function UpdateDonation(){
	var donation = GetDonation();
	var sel = $("#opttwo");
	sel.find('option').remove();//clear options
	$.each(settings.promos, function(i, item){
		if(donation >= item.min_donation){
			sel.append('<option value="' + item.sku + '">' + item.name + '</option>');
		}
	});
	RefreshSelects();
	SelectPromoItem();
}

function GetDonation(){
	var priceString = $("input[name=r1]:checked").val();
	if(priceString == "customPrice"){
		priceString = $("#customPrice").val();
	}
	if(priceString < 1 || priceString/priceString != 1){
	   priceString = 1;
	}
	$('#Price').val(priceString);
	return priceString;
}

function SelectPromoItem(){
	$("input[name=PromoItem]").remove();
	var fh = $('#donateForm');
	$.each($('#opttwo').val().split(' '), function(i, token){
		fh.append('<input type="hidden" name="PromoItem" value="' + token + '" />');
	});
	$.each($('.additionalPromo:checked'), function(i, item){
		fh.append('<input type="hidden" name="PromoItem" value="' + item.value + '" />');
	});
}

function RefreshSelects(){
		var selectBoxes = $("select");
		for (var i = 0; i < selectBoxes.length; i++) {
	    	    customSelectBoxChanged(selectBoxes[i]);
		}
	}
function GetParam(param) {
    var regex = '[?&]' + param + '=([^&#]*)';
    var results = (new RegExp(regex)).exec(window.location.href);
    if(results) return results[1];
    return '';
}

