function getElementsById(sId)
{
    var outArray = new Array();
    if(typeof(sId)!='string' || !sId) {
	return outArray;
    };
    
    if(document.evaluate) {
	var xpathString = "//*[@id='" + sId.toString() + "']"
	    var xpathResult = document.evaluate(xpathString, document, null, 0, null);
	while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
	outArray.pop();
    }
    else if(document.all) {
	
	for(var i=0,j=document.all[sId].length;i<j;i+=1){
	    outArray[i] =  document.all[sId][i];}
	
    } else if(document.getElementsByTagName) {
	
	var aEl = document.getElementsByTagName( '*' );
	for(var i=0,j=aEl.length;i<j;i+=1) {
	    
	    if(aEl[i].id == sId ) {
		outArray.push(aEl[i]);
	    };
	};
	
    };
    
    return outArray;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
            num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}

function calculate_total() {
    var total = 0;
    var val = 0;
    var id = 1;
    while (document.getElementById('gift'+id) != undefined) {
	val = document.getElementById('gift'+id).value.replace(/[^.0-9]/g,'');
	if (val) {
	    document.getElementById('gift'+id).value = val;
	    total = total + parseFloat(val);
	}
	id ++;
    }

    document.getElementById('total').innerHTML = '<b>$ '+formatCurrency(total)+'</b>';
}

function checkcc() {
    var visatemplate = 'XXXX-XXXX-XXXX-XXXX';
    var amextemplate = 'XXXX-XXXXXX-XXXXX';
    var template;
    
    card = document.getElementById('card');
    number = document.getElementById('ccnum');
    
    entry = number.value;
    
    // Find out what card from the number
    ch = entry.substring(0,1);
    if (ch == '4') {
	card.value = 'Visa';
	template = visatemplate;
    } else if (ch == '5') {
	card.value = 'Mastercard';
	template = visatemplate;
    } else if (ch == '6') {
	card.value = 'Discover';
	template = visatemplate;
    } else if (ch == '3') {
	card.value = 'American Express';
	template = amextemplate;
    } else {
	number.value = '';
	return;
    }
    number.value = format_template(entry, template);
    setSelectionRange(number, number.value.length, number.value.length);    
}

function checkccexp() {
    var template = 'XX/XX';
    exp = document.getElementById('ccexp');
    exp.value = format_template(exp.value, template);
}

var giving_http = createRequestObject();

function giving_login() {
    var login_email = document.getElementById('login_email').value;
    var password = document.getElementById('login_password').value;
    var data="login_email="+login_email+"&password="+password;
    giving_http.abort();
    giving_http.open('post', 'giving_login.php', true);
    giving_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    giving_http.onreadystatechange = giving_response;
    giving_http.send(data);
}

function giving_logout() {
    giving_http.abort();
    giving_http.open('get', 'giving_logout.php', true);
    giving_http.onreadystatechange = giving_response;
    giving_http.send(null);    
}

function giving_response() {
    if(giving_http.readyState == 4) {
	document.getElementById('giving_account_info').innerHTML = giving_http.responseText;
	display_giving_info();
    }    
}

function display_giving_info() {
    if (document.getElementById('logged_in').value == '1') {
	document.getElementById('giving_info').style.display = 'block';
    } else {
	document.getElementById('giving_info').style.display = 'none';
    }
}

function giving_password_checkbox() {
    var checkbox = document.getElementById('password_show').checked;
    if (checkbox) {
	document.getElementById('password_info').style.display = 'block';
    } else {
	document.getElementById('password_info').style.display = 'none';
	document.getElementById('password').value = '';
	document.getElementById('password_confirm').value = '';
    }
}

function giving_login_form(field) {
    document.getElementById(field).onkeydown = giving_login_keyup;
}
function giving_login_keyup(e) {
    if (!e) {
	e = window.event;
    }
    if (e.keyCode == 13) {
	giving_login();
	return false;
    }
}