var oTopInit = 150;

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}

function openInNewWindow(url){
	window.open (url, "mywindow",		"location=1,status=1,scrollbars=1,height=500");
}

function openPrivacyPolicy(){
	window.open ("/privacy-policy-pop.html", "mywindow","location=1,status=1,scrollbars=1,height=500");
}

function getPageScroll(){
     var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;   
     var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;   
	
	var arrayScroll = new Array(scrollx, scrolly);
	return arrayScroll;	
}

function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

 
function showOverlayer(){
	var size = getPageSize();
	var scroll = getPageScroll();

	var obg = document.getElementById('page-bg');
	obg.style.width = size[0]+'px';
	obg.style.height = size[1]+'px';
	
	var ovr = document.getElementById('overlayer');
//	ovr.style.top = oTopInit + scroll[1]+'px';
	
	obg.style.display = 'block';
	ovr.style.display = 'block';

	sizes = getPageSize();
	left = parseInt((sizes[0] - 941) / 2) + 10;
	ovr.style.left = left + 'px'; 
	
}

function hideOverlayer(){
	var obg = document.getElementById('page-bg');
	var ovr = document.getElementById('overlayer');

	ovr.style.display = 'none';
	obg.style.display = 'none';

}

function show_product_content(show_id, hide1, hide2){
	
	$(show_id).show();
	$(hide1).hide();
	$(hide2).hide();
	
	$(show_id+'_link').className = 'selected';
	$(hide1+'_link').className = '';
	$(hide2+'_link').className = '';
}


function product_addToCart(product_id, qty){
	
	var url = '/cart/index.html?add_cart=1&product_id='+product_id+'&qty='+qty;
	ajax_doRequest(url, 'overlayer');	
	showOverlayer();
}

function product_deleteFromCart(product_id){
	
	var url = '/cart/index.html?delete=1&product_id='+product_id;
	ajax_doRequest(url, 'overlayer');	
	showOverlayer();
}

function seminar_addToCart(product_id, qty){
	
	var url = '/cart/index.html?add_cart=1&seminar_id='+product_id+'&qty='+qty;
	ajax_doRequest(url, 'overlayer');	
	showOverlayer();
}

function seminar_deleteFromCart(product_id){
	
	var url = '/cart/index.html?delete=1&seminar_id='+product_id;
	ajax_doRequest(url, 'overlayer');	
	showOverlayer();
}

function product_deleteFromCartOnCheckout(product_id){
	
	var url = '/cart/index.html?delete=1&product_id='+product_id;
	ajax_doRequest(url, 'overlayer');	
	//showOverlayer();
}

function seminar_deleteFromCartOnCheckout(product_id){
	
	var url = '/cart/index.html?delete=1&seminar_id='+product_id;
	ajax_doRequest(url, 'overlayer');	
	//showOverlayer();
}

function showCart(){
	var url = '/cart/index.html';
	ajax_doRequest(url, 'overlayer');
	showOverlayer();	
}

function showOrderDetails(order){
	var url='/member/account/show_order.php?order_id='+order;
	ajax_doRequest(url, 'overlayer');
	showOverlayer();
}

function cart_changeShipping(name, cost){
	var url = '/cart/shipping.php?shipping='+name+'&cost='+cost;
	ajax_doRequest(url, 'review-total');	
}

function ajax_doSyncRequest(url, container_id){
	new Ajax.Request(url , {
      method: 'get',
	  asynchronous: false,
      onSuccess: function(transport) {
        $(container_id).innerHTML = transport.responseText;
      }
    });	
}

function ajax_doRequest(url, container_id){
	new Ajax.Request(url , {
      method: 'get',
      onSuccess: function(transport) {
        $(container_id).innerHTML = transport.responseText;
      }
    });	
}

function sameAsBilling(){
	if( document.getElementById('same').checked ){
		document.getElementById('shipp_fname').value = document.getElementById('bill_fname').value;
		document.getElementById('shipp_lname').value = document.getElementById('bill_lname').value;
		document.getElementById('shipp_address').value = document.getElementById('bill_address').value;
		document.getElementById('shipp_city').value = document.getElementById('bill_city').value;
		document.getElementById('shipp_state').value = document.getElementById('bill_state').value;
		document.getElementById('shipp_zipcode').value = document.getElementById('bill_zipcode').value;
		document.getElementById('shipp_phone1').value = document.getElementById('bill_phone1').value;
		document.getElementById('shipp_phone2').value = document.getElementById('bill_phone2').value;
		document.getElementById('shipp_phone3').value = document.getElementById('bill_phone3').value;
		document.getElementById('shipp_email').value = document.getElementById('bill_email').value;
	}
	else
	{
		document.getElementById('shipp_fname').value = '';
		document.getElementById('shipp_lname').value = '';
		document.getElementById('shipp_address').value = '';
		document.getElementById('shipp_city').value = '';
		document.getElementById('shipp_state').value = '';
		document.getElementById('shipp_zipcode').value = '';
		document.getElementById('shipp_phone1').value = '';
		document.getElementById('shipp_phone2').value = '';
		document.getElementById('shipp_phone3').value = '';
		document.getElementById('shipp_email').value = '';
	}
}



var secs;
var timerID = null;
var fadeTimerID = null;
var timerRunning = false;
var delay = 5000;
var idx = 0; 

function InitializeTimer()
{
  StopTheClock();
  StartTheTimer();
}

function StopTheClock()
{
	if(timerRunning){
		clearTimeout(timerID);
		if( fadeTimerID != null ){
			clearTimeout(fadeTimerID);
			fadeTimerID = null;
		}
	}
	timerRunning = false;
}

function StartTheTimer()
{
  timerRunning = true;
  timerID = self.setTimeout("FadeIn()", delay);
}

function FadeIn(){
  timerRunning = true;
  fade('rotator');
  timerID = self.setTimeout("FadeOut()", TimeToFade);
}

function FadeOut(){
  timerRunning = true;
  fade('rotator');
  img = document.getElementById('rotator');

  idx++;
  if( idx == ImgCount )
      idx = 0;
  
  if( img != null )
      {
	  img.src = images[idx];
	  img.setAttribute("usemap","#" + images[idx]);
	  }

  timerID = self.setTimeout("StartTheTimer()", TimeToFade);
}

function nextImg(){
	timerWasRunnig = timerRunning;
	if( timerRunning ) 
		StopTheClock();
		
	idx++;
	if( idx == ImgCount )
      idx = 0;

	img = document.getElementById('rotator');
	if( img != null )
	      {
	  img.src = images[idx];
	  	  img.setAttribute("usemap","#" + images[idx]);

	  }

	
	if( timerWasRunnig )
		InitializeTimer(); 
}

function prevImg(){
	timerWasRunnig = timerRunning;
	if( timerRunning ) 
		StopTheClock();
		
	if( idx == 0 )
      idx = ImgCount;
	  
	idx--;
	
	img = document.getElementById('rotator');
	if( img != null )
		      {
	  img.src = images[idx];
	  	  img.setAttribute("usemap","#" + images[idx]);
	  }


	if( timerWasRunnig )
		InitializeTimer(); 
  
}

function playPause(){
	if( timerRunning ){ 
		StopTheClock(); 
	}else{ 
		InitializeTimer(); 
	}
}

/*Fade*/

var TimeToFade = 200.0;

function fade(eid)
{
 var element = document.getElementById(eid);
 if(element == null)
  return;

 if(element.FadeState == null)
 {
  if(element.style.opacity == null
      || element.style.opacity == ''
      || element.style.opacity == '1')
  {
    element.FadeState = 2;
  }
  else
  {
    element.FadeState = -2;
  }
 }

 if(element.FadeState == 1 || element.FadeState == -1)
 {
  element.FadeState = element.FadeState == 1 ? -1 : 1;
  element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
 }
 else
 {
  element.FadeState = element.FadeState == 2 ? -1 : 1;
  element.FadeTimeLeft = TimeToFade;
  if( fadeTimerID != null )
	clearTimeout(fadeTimerID);
  fadeTimerID = setTimeout("animateFade(" + new Date().getTime()
      + ",'" + eid + "')", 40);
 }
}

function animateFade(lastTick, eid)
{
 var curTick = new Date().getTime();
 var elapsedTicks = curTick - lastTick;

 var element = document.getElementById(eid);

 if(element.FadeTimeLeft <= elapsedTicks)
 {
  element.style.opacity = element.FadeState == 1 ? '1' : '0';
  element.style.filter = 'alpha(opacity = '
      + (element.FadeState == 1 ? '100' : '0') + ')';
  element.FadeState = element.FadeState == 1 ? 2 : -2;
  return;
 }

 element.FadeTimeLeft -= elapsedTicks;
 var newOpVal = element.FadeTimeLeft/TimeToFade;
 if(element.FadeState == 1)
  newOpVal = 1 - newOpVal;

 element.style.opacity = newOpVal;
 element.style.filter =
    'alpha(opacity = ' + (newOpVal*100) + ')';

 fadeTimerID = setTimeout("animateFade(" + curTick
    + ",'" + eid + "')", 40);
}


function showLogin(){
	var url = '/member/member-login.php';
	ajax_doRequest(url, 'overlayer');	
	showOverlayer();
}
