//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}

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

function MoveToCenter(element) {
   // pa - PageArray
   var pa = getPageSize();
   var ele = $(element);
   var pa_c = new Array(Math.round(pa[0]/2),Math.round(pa[1]/2),Math.round(pa[2]/2),Math.round(pa[3]/2));
   var ea = new Array(Math.round(ele.getWidth()/2),Math.round(ele.getHeight()/2));
   var cx=$(element).style.left;
   if ( cx.indexOf('px') > -1 ) cx=cx.substring(0,cx.indexOf('px'));
   var cy=pa_c[3] - ea[1]; 
/*   new Effect.Move(ele,{ x: cx, y: 0, duration: 0, mode: 'absolute' }); */
   new Effect.Move(ele,{ x: cx, y: cy, duration: 0.8, transition: Effect.Transitions.linear, mode: 'absolute', delay: 0.5}); 
}

function SetCookie(k,v) {
    var cj = new CookieJar();
    cj.put(k,v);
}

function getCookie(s) {
    var cj = new CookieJar({domain : '.eurobike.com', path : '/'});
    return cj.get(s);
}

function createOverlay()
{
        var pa = getPageSize();
        var objBody = document.getElementsByTagName("body").item(0);
        var objSels = document.getElementsByTagName("select");
	for (i=0;i < objSels.length; i++) {
	   objSels[i].style.visibility = "hidden";
        }
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	if ( ! document.getElementById('overlay') )
	{
	  var objOverlay = document.createElement("div");
	  objOverlay.setAttribute('id','overlay');
	  objOverlay.style.display = 'block';
	  objOverlay.style.position = 'absolute';
	  objOverlay.style.top = '0';
	  objOverlay.style.left = '0';
	  objOverlay.style.zIndex = '90';
 	  objOverlay.style.width = '100%';
 	  objOverlay.style.height = pa[1]+'px';
 	  objOverlay.style.backgroundColor='black'; 
 	  objOverlay.style.opacity = 0.7;
 	  objOverlay.style.filter="alpha(opacity=70)";
	  objBody.insertBefore(objOverlay, objBody.firstChild);
          $('em_win').style.zIndex=200;
	} else {
	  document.getElementById('overlay').style.display='';
	} 
	self.scrollTo(0,0);
}


function showSurvey(f) {

     new Ajax.Updater('em_win','/web_survey', { evalScripts:true,
          parameters: {'f':f},
	  onComplete:function () { MoveToCenter($('ebt_outer')); } });
}

function hideOverlay() {
        var objSels = document.getElementsByTagName("select");
	$('ebt_outer').hide();
	for (i=0;i < objSels.length; i++) {
	   objSels[i].style.visibility = "visible";
        }
	$('overlay').hide();
}

function checkForm(form,s) {
 var q = $(form).getElementsByClassName('a').length + 1;
 var flag=1;
 for (var k=1;k<q;k++) {
   var e = $('q'+k);
   switch (e.type.toLowerCase()) { 
      case 'checkbox':
      case 'radio':
	 var tv = Form.getInputs(form,e.type,e.name).find(function(y){ return y.checked; });
	 if ( tv == null ) flag=null;
         break;
      default:
	 if ( $(e).getValue() == null || $(e).getValue() == '' ) 
	 {
	    flag=null;
	    $(e).className='redborder';
	    $('_req').className='redborder';
	 } else {
           $(e).className='';
	 }
         break;
   }
 }
 if ( flag != null ) {
    $(form).request({ onSuccess:function(trans){ $('thanks').innerHTML=trans.responseText;$('ovly_form').hide();$('thanks').show();} });
    if ( s ) SetCookie('s',s);
    return true;
 }
 return false;
}














