var ajaxImages = new Array();
ajaxImages['squares'] = new Image();
//ajaxImages['squares'].src = '/questionnaire/img/loader.gif'
ajaxImages['squares'].src = '/img/loader.gif'

function block(id) {
	$(id).block({
		message: '<img src="' + ajaxImages['squares'].src + '"/>',  
		css: { 
			border: '0px',
			backgroundColor: 'transparent'
		},
		overlayCSS:  {  
			opacity: '0',
			backgroundColor: 'transparent' 
		}
	});

	$('.blockElement').css('opacity', 1);
}

function unblock(id) {
	$(id).unblock();
}

function isNumeric(strString) {
   return /^\d+$/.test(strString);
}

function isFloat(strString) {
   return /^\d+\.?\d*$/.test(strString);
}

function isValidEmail(strString) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(strString);
}

function isNotBlank(strString) {
	return $.trim(strString).length > 0;	
}


function validQuestionnaireForm(questions_info) {

	var questions = questions_info.questions;
	var total_question = questions_info.question_count;
	var msg = '';
	var question_focus_id = '';
	var valid = true;
	for (var question_id in questions) {
		var question = questions[question_id];
		if (question.option_count != null && question.required == 1){
			var option_checked = false;
			var options = question.options;
			for (var option_id in options) {
				var option = options[option_id];
				var option_id = option.id
				var option_obj = $('input[id="'+option_id+'"]');
				if (option_obj.attr('checked')) {
					option_checked = true;
					if (option.suboption_count != undefined) {
						var suboption_checked = false;
						var suboptions = option.suboptions
						for (var suboption_id in suboptions) {
							var suboption = suboptions[suboption_id];
							var suboption_id = suboption.id
							var suboption_obj = $('input[id="'+suboption_id+'"]');
							if (suboption_obj.attr('checked')) {
								suboption_checked = true;
							}
						}
						if (!suboption_checked) {
							msg += '<label class="error">Question ' + question.order + ': ' + 'require at least one additional info selection.</label>';
						}
					}
				}
			}

			if (!option_checked) {
				msg += '<label class="error">Question ' + question.order + ' is required.</label>';
			}
			
			var td = $('td[id="question_error_'+question_id+'"]');
			var tr = td.parent();
			td.html('');
			if (msg != '') {
				if (question_focus_id == '') {
					question_focus_id = question_id;
				}
				valid = false;
				td.html(msg);
				msg = '';
			}

		}
	}

	if (question_focus_id != '') {
		window.location.href = '#question_error_'+question_focus_id;
	}
	return valid;

}


// STYLING FILE INPUTS 1.0 | Shaun Inman <http://www.shauninman.com/> | 2007-09-07
if (!window.SI) { var SI = {}; };
SI.Files =
{
	htmlClass : 'SI-FILES-STYLIZED',
	fileClass : 'file',
	wrapClass : 'cabinet',
	
	fini : false,
	able : false,
	init : function()
	{
		this.fini = true;
		
		var ie = 0 //@cc_on + @_jscript_version
		if (window.opera || (ie && ie < 5.5) || !document.getElementsByTagName) { return; } // no support for opacity or the DOM
		this.able = true;
		
		var html = document.getElementsByTagName('html')[0];
		html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
	},
	
	stylize : function(elem)
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		elem.parentNode.file = elem;
		elem.parentNode.onmousemove = function(e)
		{
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.pageY == 'undefined' &&  typeof e.clientX == 'number' && document.documentElement)
			{
				e.pageX = e.clientX + document.documentElement.scrollLeft;
				e.pageY = e.clientY + document.documentElement.scrollTop;
			};

			var ox = oy = 0;
			var elem = this;
			if (elem.offsetParent)
			{
				ox = elem.offsetLeft;
				oy = elem.offsetTop;
				while (elem = elem.offsetParent)
				{
					ox += elem.offsetLeft;
					oy += elem.offsetTop;
				};
			};

			var x = e.pageX - ox;
			var y = e.pageY - oy;
			var w = this.file.offsetWidth;
			var h = this.file.offsetHeight;

			this.file.style.top		= y - (h / 2)  + 'px';
			this.file.style.left	= x - (w - 30) + 'px';
		};
	},
	
	stylizeById : function(id)
	{
		this.stylize(document.getElementById(id));
	},
	
	stylizeAll : function()
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			var input = inputs[i];
			if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1)
			{
				this.stylize(input);
			};
		};
	}
};


function checkAll(checkbox, stub) {

	var c = 0;
	var cbs = $('input[name^="data['+stub+'][id]"]').filter(':checkbox');
	cbs.each(function() {
		this.checked = checkbox.checked;
		c += (this.checked == true ? 1 : 0);
	});
	$('input[name="data['+stub+'][boxchecked]"]').val(c);
	return true;
}

function isChecked(isitchecked, stub) {
	
	var count = $('input[name="data['+stub+'][boxchecked]"]').val();
	if (isitchecked == true) {
		count++;
		$('input[name="data['+stub+'][boxchecked]"]').val(count);
	} else {
		count--;
		$('input[name="data['+stub+'][boxchecked]"]').val(count);
	}
}






/**
 * Toggle the visibility of a fieldset using smooth animations
 */
toggleFieldset = function(fieldset) {
  if ($(fieldset).is('.collapsed')) {
    // Action div containers are processed separately because of a IE bug
    // that alters the default submit button behavior.
    var content = $('> div:not(.action)', fieldset);
    $(fieldset).removeClass('collapsed');
    content.hide();
    content.slideDown( {
      duration: 'fast',
      easing: 'linear',
      complete: function() {
        collapseScrollIntoView(this.parentNode);
        this.parentNode.animating = false;
        $('div.action', fieldset).show();
      },
      step: function() {
        // Scroll the fieldset into view
		collapseScrollIntoView(this.parentNode);
      }
    });
  }
  else {
    $('div.action', fieldset).hide();
    var content = $('> div:not(.action)', fieldset).slideUp('fast', function() {
      $(this.parentNode).addClass('collapsed');
      this.parentNode.animating = false;
    });
  }
};

/**
 * Scroll a given fieldset into view as much as possible.
 */
collapseScrollIntoView = function (node) {
  var h = self.innerHeight || document.documentElement.clientHeight || $('body')[0].clientHeight || 0;
  var offset = self.pageYOffset || document.documentElement.scrollTop || $('body')[0].scrollTop || 0;
  var posY = $(node).offset().top;
  var fudge = 55;
  if (posY + node.offsetHeight + fudge > h + offset) {
    if (node.offsetHeight > h) {
      window.scrollTo(0, posY);
    } else {
      window.scrollTo(0, posY + node.offsetHeight - h + fudge);
    }
  }
};

