var host = window.location.hostname;
var ab_path;

if(host == 'jamidamerica.org') {
    ab_path = 'jamidamerica.abigail.supportmyweb3.com';
}

if(host == 'jasocal.supportmyweb3.com' ||
   host == 'jasocal.org' ||
   host == 'www.jasocal.org') {
    ab_path = 'jasocal.abigail.supportmyweb3.com';
}

if(host == 'baltimore.supportmyweb3.com' ||
   host == 'ja_maryland.local' ||
   host == 'jamaryland.org' ||
   host == 'www.jamaryland.org') {
    ab_path = 'maryland.abigail.supportmyweb3.com';
}

if(host == 'jacols.supportmyweb3.com' ||
   host == 'jacols.local' ||
   host == 'jacols.org' ||
   host == 'www.jacols.org') {
    ab_path = 'jacols.abigail.supportmyweb3.com';
}

if(host == 'janewjersey.supportmyweb3.com' ||
   host == 'janewjersey.local' ||
   host == 'janj.org' ||
   host == 'www.janewjersy.org') {
    ab_path = 'janewjersey.abigail.supportmyweb3.com';
}

if(host == 'jamichigan.org' ||
   host == 'www.jamichigan.org') {
    ab_path = 'jami.abigail.supportmyweb3.com';
}

if(host == 'jalasvegas.local' ||
   host == 'jalasvegas.org' ||
   host == 'www.jalasvegas.org') {
    ab_path = 'jalasvegas.abigail.supportmyweb3.com';
}

if(host == 'japortland.supportmyweb3.com' ||
   host == 'japortland.local' ||
   host == 'jaorswwa.org' ||
   host == 'www.jaorswwa.org') {
    ab_path = 'japortland.abigail.supportmyweb3.com';
}

if(host == 'jatest.supportmyweb3.com' ||
   host == 'jatest.supportmyweb3.com') {
    ab_path = 'jatest.abigail.supportmyweb3.com';
}

if(host == 'jatampabay.local' ||
   host == 'jatampabay.org' ||
   host == 'jatampabay.org')
    ab_path = 'tampa.abigail.supportmyweb3.com';
    
if(host == 'abigail.supportmyweb3.com')
    ab_path = 'abigail.supportmyweb3.com';
    
if(host == 'ab.local')
    ab_path = 'ab.local';

function widget_link(widget_id, id) {
    $("#widget-link-results-"+widget_id).load("http://"+host+"/ajax_proxy?csurl=http://"+ab_path+"/ab_interface/public_widget_link?id="+id+" .public-widget-link-results");
}

function event_link(id, title) {
    $("#event-details").load("http://"+host+"/ajax_proxy?csurl=http://"+ab_path+"/ab_interface/event_detail?id="+id+" .event-details");
    $(this).scrollTop(0);
    $("#event-banner-image").hide();
    if(ab_path != 'abigail.supportmyweb3.com')
        window.location.href = 'http://'+host+'/events#event='+title+'&id='+id;
}

function event_reg(id) {
    $("#event-banner-image").hide();
    
    if(ab_path != 'abigail.supportmyweb3.com') 
        window.location.href = window.location.href+'&event_reg='+id;
    
    var ab_logged = getCookie("AB_USER[logged]");
    
    if(ab_logged == "" || ab_logged == undefined) {
        var conf = confirm('Please login to register for events. If this is your first visit with us, you will have to create an account by clicking the sign up link at the bottom of the login popup. Once you create an account, you will have to revist this page to register for this event.');
        if(conf) {
	    $('#modal_login').jqmShow();
            void(0);
        }
    } else {
        $("#event-details").load("http://"+host+"/ajax_proxy?csurl=http://"+ab_path+"/ab_interface/register_for_event?id="+id+" .register-for-event", function() {
	    $('#secure-forms-url').attr('value', "https://"+host+"/secure_forms");
	});
        
    }
}

function donations() {
    $("#event-banner-image").hide();
    
    
        $("#donate").load("http://"+host+"/ajax_proxy?csurl=http://"+ab_path+"/ab_interface/donations #donate", function() {
	    $('#secure-forms-url').attr('value', "https://"+host+"/donations");
	});
}


// check first for a cookie, if none, prompt user to login or create an account
function register_for_event(id) {
    var ab_logged = getCookie("AB_USER[logged]");
    if(ab_logged == "" || ab_logged == undefined) {
        var conf = confirm('Please login to register for events. If this is your first visit with us, you will have to create an account by clicking the sign up link at the bottom of the login popup. Once you create an account, you will have to revist this page to register for this event.');
        if(conf) {
            $('#modal_login').jqmShow();
            void(0);
        }
    } else {
    	id = $('#id').val();
        $("#register-for-event").load("http://"+host+"/ajax_proxy?csurl=http://"+ab_path+"/ab_interface/register_for_event&id="+id+" .register-for-event");
    }
    
}

function ajax_login(sid) {
    $.get("http://"+host+"/ajax_proxy", {csurl: "http://"+ab_path+"/ab_interface/get_user_data?sid="+sid}, function(data) {
	setCookie("AB_USER[session_id]", data.session_id);
	setCookie("AB_USER[uname]", data.uname);
	setCookie("AB_USER[user_id]", data.user_id);
	setCookie("AB_USER[logged]", data.logged);
	$('#modal_login').jqmHide();
	window.location.reload(true);
	}, 'json');
}

function validate_register_for_event() {
    var tickets = $('#event-tickets').val();
    if(tickets == '' || tickets == undefined) {
	alert('Please enter a ticket quantity');
	$('#event-tickets').focus();
	return false;
    } else if(isNaN(tickets)) {
	alert('Please enter a numeric ticket quantity');
	$('#event-tickets').focus();
	return false;
    
    // added for maryland summer camp registration (remove after 8/5/2011)
    } else if(host == 'jamaryland.org' || host == 'www.jamaryland.org') { 
	if($('#child-name').val() == '' || $('#child-name').val() == undefined) {
	    alert('Please enter child\'s name');
	    $('#child-name').focus();
	    return false;
	} else if($('#child-age').val() == '' || $('#child-age').val() == undefined) {
	    alert('Please enter child\'s age');
	    $('#child-age').focus();
	    return false;
	}
	
    } else
	return true
}

function update_ticket_total() {
    var tickets = $("#event-tickets").val();
    var price = $("#event-ticket-price").val();
    if(isNaN(tickets)) {
	alert('Please enter a numeric ticket quantity');
	$('#event-tickets').focus();
	return false;
    } else {
	price = price.replace("$", "");
	var total = (tickets*price);
	$("#event-ticket-total").html("$"+total);
    }
    
}

function payment_validation() {
    $("#event-banner-image").hide();

    var ab_logged = getCookie("AB_USER[logged]");

    if (ab_logged == "" || ab_logged == undefined) {
        var conf = confirm('Please login to register for events. If this is your first visit with us, you will have to create an account by clicking the sign up link at the bottom of the login popup. Once you create an account, you will have to revist this page to register for this event.');
        if (conf) {
            $('#modal_login').jqmShow();
            void(0);
        }
    } else {

        if (
	    $('#pay-by-check').val() == undefined && $('#cc').val() == "" || 
	    $('#pay-by-check').val() == undefined && $('#exp').val() == "" ||
	    $('#pay-by-check').val() == '1' && $('#check_num').val() == "" || 
	    $('#pay-by-check').val() == '1' && $('#check_amt').val() == "" ||
	    $('#fname').val() == "" || 
	    $('#lname').val() == "" || 
	    $('#street').val() == "" ||
	    $('#city').val() == "" || 
	    $('#state').val() == "" || 
	    $('#zip').val() == "") {
	
            alert('All fields are required');

        } else {

            var datastring = $('#payment').serialize();
            $.post("https://" + host + "/ajax_proxy?csurl=http://" + ab_path + "/ab_interface/secure_forms", {
                payment_data: datastring
            }, function (data) {
		$('.payment').hide();
		$('.verification').html(data);
		$('.verification').show();
		
            });
	    
        }
    }
}

function donation() {
    $("#event-banner-image").hide();

        if (
	    $('#fname').val() == "" || 
	    $('#lname').val() == "" || 
	    $('#mail').val() == "" ||
	    $('#cc').val() == "" || 
	    $('#amt').val() == "" || 
	    $('#exp').val() == "" ||
	    $('#yy').val() == "" ||
	    $('#cvv').val() == "") {
	
            alert('All fields are required');

        } else {

            var datastring = $('#payment').serialize();
            $.post("https://" + host + "/ajax_proxy?csurl=http://" + ab_path + "/ab_interface/donations", {
                payment_data: datastring
            }, function (data) {
		$('.donation').hide();
		$('.donation-verification').html(data);
		$('.donation-verification').show();
		
            });
	    
        }
    
}

function showAddress(address, map) {
var geocoder = new GClientGeocoder();
geocoder.getLatLng( address,
  function(point) {
    if (!point) {
      alert(address + " not found");
    } else {
      map.setCenter(point, 15);
      var marker = new GMarker(point);
      map.addOverlay(marker);
    }
  });
}

function get_volunteer_data(class_id, volunteer_id) {
    $("#ab-modal-signup-content").load("http://"+host+"/ajax_proxy?csurl=http://"+ab_path+"/ab_interface/register_for_class&id="+class_id+"&volunteer_id="+volunteer_id+" .register-for-class");
}

function setCookie(name, value) {
    var today = new Date();
    today.setTime( today.getTime() );
    
    var expires_date = new Date( today.getTime() + 30000000 );
    
    document.cookie = name + "=" + encodeURIComponent( value ) +
    ";expires=" + expires_date.toGMTString() +
    ";path=/" +
    ";domain=" + window.location.host;
}

function getCookie(name) {
    if(document.cookie.length>0) {
        start=document.cookie.indexOf(name + "=");
        if(start!=-1) { 
            start=start + name.length+1; 
            end=document.cookie.indexOf(";",start);
            if(end==-1) {
                end=document.cookie.length;
            }
            return unescape(document.cookie.substring(start,end))
        } 
    }
    return "";

}

function deleteCookie(name) {
    value = null;
    var today = new Date();
    today.setTime( today.getTime() );
    
    var expires_date = new Date( today.getTime() - 30000000 );
    
    document.cookie = name + "=" + encodeURIComponent( value ) +
    ";expires=" + expires_date.toGMTString() +
    ";path=/" +
    ";domain=" + window.location.host;
}

function expandSelect(id) {
    if($.browser.msie) {
        $('#select-'+id).css('width', 'auto');
    }
}

function resetSelect(id) {
    if($.browser.msie) {
        $('#select-'+id).css('width', '45%');
    }
}

function get_url_hash_vars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
