var draggables = new Array();

function findPosX(obj)
{
	var curleft = 0;
	if (obj && obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj && obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj && obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj && obj.y)
		curtop += obj.y;
	return curtop;
}

function toggleQ(qid){
	var botLink = $(qid+'_link_2');
	var q = $(qid);
	
	if(!q.hasClassName('noheight')){
		q.addClassName('noheight');
		botLink.innerHTML='<i><b>shrink</b>...</i>';
	}else{
		q.removeClassName('noheight')
		botLink.innerHTML='<i><b>read more</b>...</i>';
	}
}

function toggleCatMenu(){
	if ($('cats_alphabetic').style.display == 'none'){
		$('category_menu_link').innerHTML = 'a grouped';
	}else{
		$('category_menu_link').innerHTML = 'an alphabetic';
	}
	
	$('cats_alphabetic').style.display = ($('cats_alphabetic').style.display=='none')?'':'none';
	$('cats_grouped').style.display = ($('cats_alphabetic').style.display=='none')?'':'none';
}

function displayFAQ(link){
	displayDialog('FAQ',0.5,0.4);
}

function displayAbout(){
	var newx = (screen.availWidth - 500 )/2;
	
	var newy = (screen.availHeight - 475)/1.1;
	new Effect.Appear('about_blue_box');
	new Effect.Move('about_blue_box',{ x: newx, y: newy, mode: 'absolute', duration: 0.0 });
}

function displayFAQC(link){
	displayDialog('blue_box',0.5,0.4);
}

function showConsumerTerms(link){/*
	var newx = (screen.availWidth - 525 )/2;
	var newy = findPosY( link );
	
	if( newy < 700 )
		newy = 50;
	else
		newy = newy - 600;
	
	new Effect.Appear('consumer_terms');
	new Effect.Move('consumer_terms',{ x: newx, y: newy, mode: 'absolute', duration: 0.0 });*/
	displayDialog('consumer_terms',0.5,0.5);
}

function tabE(obj,e){

	var e=(typeof event!='undefined')?window.event:e;// IE : Moz
	if(e.keyCode==13)
	{
		var ele = document.forms[0].elements;
		for(var i=0;i<ele.length;i++)
		{
			var q=(i==ele.length-1)?0:i+1;// if last element : if any other
			if(obj==ele[i])
			{
				ele[q].focus();
				break
			}
		}
		return false;
	}
	return e;
}
	
function fadeScreen(fade){
	if(!$('overlay') || !$('pagemain'))
		return;
		
	if(fade){
		new Effect.Move('overlay',{ x: 0, y: 0, mode: 'absolute', duration: 0.0 });
		new Effect.Fade('pagemain',{to: 0.5, duration:0.2});
		new Effect.Appear('overlay',{to: 0.3, duration:0.2});
	}else{
		new Effect.Fade('overlay',{duration: 0.1});
		new Effect.Appear('pagemain',{duration: 0.1});
	}

}

function stopEvent(event){
	Event.stop(event);
	return false;
}

function displayDialog(el_id,x_pos,y_pos,fade){
	if(!el_id)
		return;
	if(!draggables[el_id])
		draggables[el_id] = new Draggable(el_id);
	//prevent scrolling of pane from dragging the dialog
	if($$('#'+el_id+' .pane')[0]){
		Event.observe($$('#'+el_id+' .pane')[0],'mousedown',stopEvent);
	}
	var dims = $(el_id).getDimensions();
	var fade = fade || false;
	var x_pos = (x_pos) ? x_pos : 0.5;	var y_pos = (y_pos) ? y_pos : 0.5;
	var w = 0;	var h = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	x_pos = Math.round((w * x_pos - dims.width / 2 ));
	y_pos = Math.round((h - dims.height) * y_pos) + findPosY($('submit_payment'))/2;
	if(navigator.appName=='Microsoft Internet Explorer' && $('radius')){
		if(findPosY($('radius'))>=y_pos && findPosY($('radius')) <= dims.height+y_pos  ){
			$('radius').hide();
		}
	}
	

	fadeScreen(fade);
			   
	new Effect.Move(el_id,{ x: x_pos, y: y_pos, mode: 'absolute', duration: 0.0 });
	new Effect.Appear(el_id,{duration: 0.2});
}

function closeDialog(dlg){
	dlg = dlg[2];
	
	fadeScreen();
	
	if(draggables[dlg.id]){
		draggables[dlg.id].destroy();
		draggables[dlg.id] = null;
	}
	Event.stopObserving($$('#'+dlg.id+' .pane')[0],'mousedown',stopEvent);
	

	if(navigator.appName=='Microsoft Internet Explorer' && $('radius')){
		$('radius').show();
	}
	
	new Effect.Fade(dlg.id);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function clearEmail(r){
	if(r=='Email'){
		return '';
	}else{
		return r;
	}
}

function setColor(obj){
	if($(obj.id).hasClassName('grey')){
		$(obj.id).removeClassName('grey');
		$(obj.id).addClassName('black');
	}
}

/*window.onload = function() {
	$('overlay').setStyle({
		height: document.body.getHeight()+'px',
		width: document.body.getWidth()+'px',
	});
}*/

function showTooltip(text_div,x,y)
{
	$('tooltip_body').innerHTML = $(text_div).innerHTML;
	
	$('tooltip').makePositioned();
	$('tooltip').style.left = x - 25 + 'px';
	$('tooltip').style.top = y-$('tooltip').getHeight() + 'px';
	$('tooltip').show(0);
}

function hideTooltip()
{
	$('tooltip').hide();
}

function showTooltipQA(text_div,x,y)
{
	$('tooltipqa_bodyq').innerHTML = $(text_div+'q').innerHTML;
	$('tooltipqa_bodya').innerHTML = $(text_div+'a').innerHTML;
	
	$('tooltipqa').makePositioned();
	$('tooltipqa').style.left = x - 245 + 'px';
	$('tooltipqa').style.top = y-$('tooltipqa').getHeight() + 'px';
	
	$('tooltipqa').show(0);
}

function hideTooltipQA()
{
	$('tooltipqa').hide();
}