/**
 * hideObj('seletor1', 'selector2', ...)
 */
function hideObj() 
{
	for (i =0 ; i < arguments.length; i++)
	{
		$(arguments[i]).addClass('hidden');
	}
}

/**
* showObj('seletor1', 'selector2', ...)
*/
function showObj() 
{
	for (i =0 ; i < arguments.length; i++)
	{
		$(arguments[i]).removeClass('hidden');
	}
}

/**
@param type default 'text' supported type refer to: http://docs.jquery.com/Selectors
*/
function getFormFieldValue(name, type)
{
	var $field;
	if('radio'==type)
	{
		var $field = $("input:radio[name="+name+"][checked]");
		if($field.length>0) return $field.val();
	}
	else
	{
		$field = $(":input[name="+name+"]");
	}
	
	if($field && $field.length)return $field.val();
	else return "";
}

//validat
function isEmpty(value)
{
	return /^[\s]*$/.test(value);
}

function isPositiveNumber(value)
{
	return parseInt(value)>0;
}

function updateWarning(country, suffix)
{
	hideObj('#caWarning_'+suffix, '#usWarning_'+suffix);
	$(':text').each(function(){this.disabled=false});
	$(':radio').each(function(){this.disabled=false});
	$(':button').each(function(){this.disabled=false});
	$(':submit').each(function(){this.disabled=false});
	
	if('Canada'==country)
	{
		showObj("#caWarning_"+suffix);
		$(':text').each(function(){this.disabled=true});
		$(':radio').each(function(){this.disabled=true});
		$(':button').each(function(){this.disabled=true});
		$(':submit').each(function(){this.disabled=true});
	}
	else if('United States'==country)
	{
		showObj("#usWarning_"+suffix);
		$(':text').each(function(){this.disabled=true});
		$(':radio').each(function(){this.disabled=true});
		$(':button').each(function(){this.disabled=true});
		$(':submit').each(function(){this.disabled=true});
	}
}

function syncAjaxResponse(url, params, method)
{
	$.ajaxSetup({async:false});
	
	var ajaxMethod = $.get;
	if('post'==method) ajaxMethod = $.post;
	
	var data = null;
	
	ajaxMethod(url, params, function(response){data = response;});
	$.ajaxSetup({async:true});	//set back
	
	return data;
}

/**
	Display errors on displayBlock
	@errors array
	@tabId int
	@displayBlock string selector, default #errors-block
*/
function displayError(errors, tabId, displayBlock)
{
	if(!displayBlock) displayBlock="#errors-block";
	
	$(displayBlock).html("");	//clear error messages
	
	if(errors.length>0)
	{
		tabOn(tabId);	//go to tab
		for(var i=0; i<errors.length; i++)
		{
			$("#errors-block").append("<li>"+errors[i]+"</li>");
		}
	}
}

/**
	Case insensitive
@return '' if success, else return the supposed initials
*/
function checkInitials(firstName, lastName, initial)
{
	if(!firstName)firstName = '';
	if(!lastName)lastName = '';
	if(!initial)initial = '';
	
	i = firstName.charAt(0) + lastName.charAt(0);
	if(initial.toUpperCase() != i.toUpperCase())
	{
		return i;
	}
	
	return '';
}

function popUp(url)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800, height=600');");
}

function changeCaptcha(containerId){
	var url='/captcha/default?t='+new Date().getTime();
	$("#"+containerId+">img")[0].src=url;
}