/* General JS for Camfed site */

// Validate DotMailer subscribe form
function  dm_validate(dmForm) {
  var ok = true;
  var errorMsg = "";
  
  var emailAddress = dmForm.Email.value;
  if (emailAddress == "" || emailAddress.indexOf("@") == -1) {
    errorMsg = "Please enter your email address";
    ok = false;
  }
  if (dmForm.FirstName.value == "") {
    errorMsg = errorMsg + "\r\n" + "Please enter your first name";
    ok = false;
  }
  if (dmForm.Surname.value == "") {
    errorMsg = errorMsg + "\r\n" + "Please enter your surname";
    ok = false;
  }
  if (!ok) {
    alert(errorMsg);
  }
  
  return ok;
}

// URL safe character encoder
function urlencode(str) {
	str = escape(str);
	str = str.replace(/\+/g, "%2B");
	str = str.replace(/%20/g, "+");
	str = str.replace(/\*/g, "%2A");
	str = str.replace(/\//g, "%2F");
	str = str.replace(/@/g, "%40");
	return str;
}


// Add Social Bookmarking links
$(document).ready(function(){

	// Populate URL and Title for links
	var linkUrl = location.href;
	var linkTitle = $("title").text();
	
	// Add "Camfed" to link title if it doesn't exist 
	if (linkTitle.indexOf('Camfed') == -1) {
		linkTitle = 'Camfed - ' + linkTitle;
	}
	
	linkTitle = urlencode(linkTitle);
	
	// Add URL + Title to social bookmarking links
	$("#socialBookmarking ul li a").each(function() {
		switch (this.className) {
			case 'delicious':
				this.href = this.href + "?url="+linkUrl+"&amp;title="+linkTitle;
				break;
			case 'facebook':
				this.href = this.href + "?u="+linkUrl;
				break;
			case 'digg':
				this.href = this.href + "?phase=2&amp;url="+linkUrl+"&amp;title="+linkTitle;
				break;
			case 'yahoo':
				this.href = this.href + "?u="+linkUrl+"&amp;t="+linkTitle;
				break;
			case 'google':
				this.href = this.href + "&bkmk="+linkUrl+"&amp;title="+linkTitle;
				break;
		}
	});
});

// Accordian in News sidebar

$(function () { // same as $(document).ready(function () { })
  // assuming we have the open class set on the H2 when the HTML is delivered
  $('LI.section H2:not(.open)').next().slideUp();

  $('H2.section-header').click(function () {
    // find the open section, remove the class, move to the UL following it and hide it
    $('H2.open').removeClass('open').next().slideUp();
    
    // add the open class to this H2, move to the next element (the UL) and show it
    $(this).addClass('open').next().slideDown();
    
    return false; // stop the link being followed for jscript enabled users
    
  });
});

// Populate form-fields & re-poplate them if they are clicked, nothing is entered and the user clicks away - populateElement(selector, defvalue)

function populateElement(selector, defvalue) {
    if($.trim($(selector).val()) == "") {
        $(selector).val(defvalue);
    }
  
    $(selector).focus(function() {
        if($(selector).val() == defvalue) {
            $(selector).val("");
        }
    });
    
    $(selector).blur(function() {
        if($.trim($(selector).val()) == "") {
            $(selector).val(defvalue);
        }
    });
 }










