/**
 * agiliance common scripts, used by all forms
 *
 */

$(window).load(function () {
	if ($('#agiliance_form #00N60000001noPw')) { $('#agiliance_form #00N60000001noPw').triggerHandler('change'); }
});

$(document).ready(function() {
	// check for previously registered user
	var registered = $.cookie("registered");

	// if registered, update all links to go right to documents
	if (registered == 'true') {

		if ($('.rightcolcontainer').length != 0) {
			$('.rightcolcontainer').find('a').map(function(){
				var oldlink = $(this).attr('href');
				var newlink = oldlink.replace(/(.*)Registration\.html(.*)/, "$1Registration_confirm.html$2"); 
				$(this).attr({href: newlink});
			});                     
		}

		if ($('.sidebarlinks').length != 0) {
			$('.sidebarlinks').find('a').map(function(){
				var oldlink = $(this).attr('href');
				var newlink = oldlink.replace(/(.*)Registration\.html(.*)/, "$1Registration_confirm.html$2"); 
				$(this).attr({href: newlink});
			});                     
		}

		if ($('.centercol').length != 0) {
			$('.centercol').find('a').map(function(){
				var oldlink = $(this).attr('href');
				var newlink = oldlink.replace(/(.*)Registration\.html(.*)/, "$1Registration_confirm.html$2"); 
				$(this).attr({href: newlink});
			});                     
		}
	}

	// turn on form validation
	if ($('#agiliance_form').length != 0) { $('#agiliance_form').validate(); }

	// take state menu value and copy into hidden state field
	$('#agiliance_form #00N60000001noPm').change(function() {
		$('#state').val($(this).val());
	});
	
	// if form has country, and state/province, state is only 
	// required if US or Canada selected
	// in SF, country is 00N60000001noPw, state is 00N60000001noPm
	$('#agiliance_form #00N60000001noPw').change(function() {
		var state = $('#agiliance_form #00N60000001noPm');
		if (state) {
			if (($(this).val() != "United States") && ($(this).val() != "Canada")) {
				// turn off required for state/province
				state.removeClass("required");
				// previous td, with span.show-required
				state.parent().prev().children('span.show-required').hide();
				state.parent().prev().children('span.label').removeClass('bold');
			} else {
				state.addClass("required");
				state.parent().prev().children('span.show-required').show();
				state.parent().prev().children('span.label').addClass('bold');
			}
		}
	});
});
