
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


jQuery(document).ready(function() {					   
/**
 * Security lock popup in Login box 
 */
	jQuery('#login a#security').click(function(){
		var href = $(this).attr('href');
		window.open(href, 'secure','width=565,height=450,scrollbars=yes,location=no,dependent=yes,resizable=no,top=25,left=25');return false; 
	});			

/** 
  * Set focus to either the user name field by default, or the password field if user has been 
  *	remembered and username has been prepopulated
  */	
	if($('#Userid') != null && $('#Pword') != null) {
		if ($('#Userid').val() == '') {
			$('#Userid').focus();
		} else {
			$('#Pword').focus();
		}
	}
	
/*******************************************************************************
=== INCREASE/DECREASE TEXT======================================================
	* INCREASE/DECREASE TEXT 
*******************************************************************************/
// For graceful degredation, this script reveals these tools that the CSS hides by default.
	//$('#tools').css('display', 'block');
	
// Read the cookie. Depending on what it is, apply the inline style to the body tag, and the selected class to the link that indicates the size.
	var size = jQuery.cookie('fontSize');
	if (size == 'exsmall') {
	$('#mainTxt').css('font-size','100%');
		$('#text-size a').removeClass('selected');
		$('#text-size #exsmall').addClass('selected');
	};
	if (size == 'small') {
	$('#mainTxt').css('font-size','110%');
		$('#text-size a').removeClass('selected');
		$('#text-size #small').addClass('selected');
	};
	if (size == 'medium') {
	$('#mainTxt').css('font-size','130%');
		$('#text-size a').removeClass('selected');
		$('#text-size #medium').addClass('selected');
	};
	if (size == 'large') {
	$('#mainTxt').css('font-size','150%');
		$('#text-size a').removeClass('selected');
		$('#text-size #large').addClass('selected');
	};
	if (size == 'exlarge') {
	$('#mainTxt').css('font-size','180%');
		$('#text-size a').removeClass('selected');
		$('#text-size #exlarge').addClass('selected');
	};
	
// To increase and decrease the text-size, adding inline styles to the body tag.
	$('#text-size a').click(function(event) {
									 
// This nullifies the font-size value by default.
	$('#mainTxt').css('font-size', '100%');
	jQuery.cookie('fontSize', 'exsmall', { expires: 365, path: '/'});
	
// This nullifies the font-size value by default.
	if (this.id == 'small') {
      $('#mainTxt').css('font-size', '110%');
	  $.cookie('fontSize', 'small', { expires: 365, path: '/'});
    }
	
// This is the "smaller" font-size link.
    if (this.id == 'medium') {
      $('#mainTxt').css('font-size', '130%');
	  $.cookie('fontSize', 'medium', { expires: 365, path: '/'});
    }
	
// This is the "larger" font-size link.
    else if (this.id == 'large') {
      $('#mainTxt').css('font-size', '150%');
	  $.cookie('fontSize', 'large', { expires: 365, path: '/'});
    }
	
// This is the "larger" font-size link.
    else if (this.id == 'exlarge') {
      $('#mainTxt').css('font-size', '180%');
	  $.cookie('fontSize', 'large', { expires: 365, path: '/'});
    }
	
// Add the selected class to the link, if the user has selected it.
    $('#text-size a').removeClass('selected');
    $(this).addClass('selected');
	event.stopPropagation();
	return false;
	});  

/*******************************************************************************
=== PRINT THIS PAGE ============================================================
	* Initiates a regular browser print job when user clicks.
*******************************************************************************/
  $('#print-page a').click(function() {
    window.print(); return false;
  });

/*******************************************************************************
=== PDF LINKS ==================================================================
	* Makes any link to a .PDF open in a smaller cromeless window, 
	  and displays the PDF icon image after the link.
*******************************************************************************/
	$('a[href$=".pdf"]').attr("target","blank");
	$('a[href$=".pdf"]').after(' <img src="/global/images/pdf-ico.gif" class="pdf-icon" title="This is a PDF document" />');

/*****
 * Add event handler on find an advisor forms, calls function to validate it 
 *
******/
	$('.find-an-advisor form').attr('onsubmit','return validateZIP(this.zip.value)');

});// Jquery onload handler ends 


/*******************************************************************************
=== TOP NAVIGATION =============================================================
	* Suckfish method to show the parent tab's sub navigation.
*******************************************************************************/
/*
 * sfDelay - Suckerfish CSS menu with JS delay.
 *
 * Copyright (c) 2008 Peter Ryan (peter-ryan.co.uk)
 * This function is free for anyone to use, modify and redistribute.
 *
 * $Id: functions.js,v 1.1.1.1.4.7 2010/03/20 06:05:44 btfide Exp $
 */

/* http://simonwillison.net/2004/May/26/addLoadEvent/ */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
};


sfHover = function() {
	var timeout = 400;
	var cssClass = "sfhover";

	var queue = [];
	var reCSS = new RegExp("\\b" + cssClass + "\\b");
	//var sfEls = document.getElementById("nav").getElementsByTagName("li");
	var sfEls = document.getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
  
		// mouseover and mouseout handlers for regular mouse based interface.
		sfEls[i].onmouseover = function() {
			queueFlush();
			this.className += " " + cssClass;
		}
		sfEls[i].onmouseout = function() {
			queue.push([setTimeout(queueTimeout, timeout), this]);
		}

		// focus and blur handlers for keyboard based navigation.
		sfEls[i].onfocus = function() {
			queueFlush();
			this.className += " " + cssClass;
		}
		sfEls[i].onblur = function() {
			queue.push([setTimeout(queueTimeout, timeout), this]);
		}

		// click event handler needed for tablet type interfaces (e.g. Apple iPhone).
		sfEls[i].onclick = function(e) {
			if (this.className.search(reCSS) == -1) {
				// CSS not set, so clear all sibling (and decendants) menus, and then set CSS on this menu...
				var elems = this.parentNode.getElementsByTagName("li");
				for (var i=0; i<elems.length; i++) {
					elems[i].className = elems[i].className.replace(reCSS, "");
				}
				this.className += " " + cssClass;
			} else {
				// CSS already set, so clear all decendant menus and then this menu...
				var elems = this.getElementsByTagName("li");
				for (var i=0; i<elems.length; i++) {
					elems[i].className = elems[i].className.replace(reCSS, "");
				}
				this.className = this.className.replace(reCSS, "");
			}
			if (e && e.stopPropagation)
				e.stopPropagation();
			else
				window.event.cancelBubble = true;
		}
	}

	queueFlush = function () {
		while (queue.length) {
			clearTimeout(queue[0][0]);
			queueTimeout();
		}
	}

	queueTimeout = function() {
		if (queue.length) {
			var el = queue.shift()[1];
			el.className = el.className.replace(reCSS, "");
		}
	}
}
addLoadEvent(sfHover);


/////////////////////////////////////////////////////////////////////////

//    Script for cookie setting/getting                                //

/////////////////////////////////////////////////////////////////////////
var intdays = 366;
var now = new Date();
now.setTime(now.getTime() + intdays * 24 * 60 * 60 * 1000);
var cookie_path = "/"; //"/" as root
var cookie_domain = document.domain; //"www.ameriprise.com"

function getCookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((path) ? "; path=" + path : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

/**
 * Check to see if the value is blank, 
 * If not, redirect to the url, otherwise, display the error message
 * This is the fix for the onlicks being called for the dropdown forms for jobs/schools etc..
 * @author Bao
 * @date 2009.04.01
 * 
 * @param url - the url this will redirect to, if value isnt blank
 * @param value - the value to test to see if its blank or not
 * @param errorMessage - the error message to display if value is blank
 */
function validateDropdown(url, value, errorMessage) {
	if(value == "") {
		alert(errorMessage);
	} else {
		location = url;
	}
}


/* Validate find an advisor box */
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	
	if (field.length!=5 && field.length!=10) {
	alert("Please enter a valid Zip/Postal code.");
	return false;
	}
	for (var i=0; i < field.length; i++) {
	temp = "" + field.substring(i, i+1);
	if (temp == "-") hyphencount++;
	if (valid.indexOf(temp) == "-1") {
	alert("Invalid characters in your zip code.  Please try again.");
	return false;
	}
	if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
	alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
	return false;
	   }
	}
	return true;
}
