var site_path;

function getScroll(){
var scrOfX=0,scrOfY=0;
if(document.body&&(document.body.scrollLeft||document.body.scrollTop))
{
scrOfY=document.body.scrollTop;
scrOfX=document.body.scrollLeft;
}
else if(document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop))
{
scrOfY=document.documentElement.scrollTop;
scrOfX=document.documentElement.scrollLeft;
}
return[scrOfY,scrOfX];
}

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){
xScroll=document.body.scrollWidth;
yScroll=document.body.scrollHeight;
}else{
xScroll=document.body.offsetWidth;
yScroll=document.body.offsetHeight;
}
var windowWidth,windowHeight;
if(self.innerHeight){
windowWidth=self.innerWidth;
windowHeight=self.innerHeight;
}else if(document.documentElement&&document.documentElement.clientHeight){
windowWidth=document.documentElement.clientWidth;
windowHeight=document.documentElement.clientHeight;
}else if(document.body){
windowWidth=document.body.clientWidth;
windowHeight=document.body.clientHeight;
}
if(yScroll<windowHeight){
pageHeight=windowHeight;
}else{
pageHeight=yScroll;
}
if(xScroll<windowWidth){
pageWidth=windowWidth;
}else{
pageWidth=xScroll;
}
arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);
return arrayPageSize;
}

function popUp(top) {
	var size = getPageSize(), w = size[0], h = size[1], scroll = getScroll();

	if (top === '' || top === undefined) {
		top = 8;
	}

	if (scroll[0] > 0) {
		top = top + (scroll[0] * 100) / size[1];
	}

	$("#Underlay, #Overlay", "#POPUP").fadeIn(900);

	document.getElementById('POPUP').style.top=top+'%';
	document.getElementById('Underlay').style.top=top+'%';
	document.getElementById('Overlay').style.top=top+'%';

	document.getElementById('Underlay').style.display='block';
	document.getElementById('Overlay').style.display='block';
	document.getElementById('POPUP').style.display='block';

}

function popUp2(top) {
	var size = getPageSize(), w = size[0], h = size[1], scroll = getScroll();

	if (top === '' || top === undefined) {
		top = 8;
	}

	if (scroll[0] > 0) {
		top = top + (scroll[0] * 100) / size[1];
	}

	$("#Underlay2, #Overlay2", "#POPUP2").fadeIn(900);

	document.getElementById('POPUP2').style.top=top+'%';
	document.getElementById('Underlay2').style.top=top+'%';
	document.getElementById('Overlay2').style.top=top+'%';

	document.getElementById('Underlay2').style.display='block';
	document.getElementById('Overlay2').style.display='block';
	document.getElementById('POPUP2').style.display='block';

}

function popOut() {
	$("#Underlay, #Overlay", "#POPUP").fadeOut(900);

	document.getElementById('POPUP').style.display='none';
	document.getElementById('Overlay').style.display='none';
	document.getElementById('Underlay').style.display='none';
}

function popOut2() {
	$("#Underlay2, #Overlay2", "#POPUP2").fadeOut(900);

	document.getElementById('POPUP2').style.display='none';
	document.getElementById('Overlay2').style.display='none';
	document.getElementById('Underlay2').style.display='none';
}

function validateMyBookingLogin()
{
	var err = 0;

	if(document.loginForm.email.value == '')
	{
		document.loginForm.email.style.border = '1px solid #cb0c0c';
		err = 1;
	}
	else document.loginForm.email.style.border = '1px solid #adaebd';

	if(document.loginForm.password.value == '')
	{
		document.loginForm.password.style.border = '1px solid #cb0c0c';
		err = 1;
	}
	else document.loginForm.password.style.border = '1px solid #adaebd';

	if(err == 1)
	{
		alert('Please fill in your e-mail address and your account password!');
		return false;
	}else{
		if(validateEmailAddress())
			return true;
		else{
			alert('Invalid e-mail address!');
			return false;
		}
	}
}

function MyBooking_login()
{
	var email_addr = document.loginForm.email.value;
	var passw      = document.loginForm.password.value;

	$.ajax({
	   type: "POST",
	   url: "index.php?module=booking_login&cmd=check_login",
	   data: "email="+email_addr+"&pass="+passw,
	   success: function(msg){
	     handleReturnedValue(msg);
	   }
	 });
}

function handleReturnedValue(msg)
{
	if(msg == 1){
		popOut();
		window.location = site_path+'my_booking/account_home.html';
	}else{
		document.getElementById('loginErr').style.display = 'block';
	}
}

function validateForgotPass()
{
	var ok = validate_email('passForm', 'email2');

	if(!ok){
		alert('Invalid e-mail address!');
		return false;
	}

	return true;
}

function MyBooking_forgotPass()
{
	var email_addr = document.passForm.email2.value;
	$.ajax({
	   type: "POST",
	   url: "index.php?module=booking_password&cmd=forgot_passw",
	   data: "email="+email_addr,
	   success: function(msg){
	     handleForgotPassValue(msg);
	   }
	 });
}

/**
 *
 * @access public
 * @return void
 **/
function MyBooking_logout()
{
	$.ajax({
	   url: "index.php?module=booking_logout&cmd=logout",
	   success: function(msg){
	     window.location = site_path+'home.html';
	   }
	 });
}

function handleForgotPassValue(msg)
{
	if(msg == 'ok')
		document.getElementById('msgForgotPass').innerHTML = 'Email with account data sent!';
	else document.getElementById('msgForgotPass').innerHTML = 'The email address you entered doesn\'t exist!';
	document.getElementById('msgForgotPass').style.display = 'block';
}


function validateEmailAddress()
{
	return validate_email('loginForm', 'email');
}

function validate_email(form_name,email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = eval('document.'+form_name+'.'+email+'.value');
   if(reg.test(address) == false) {
      return false;
   }
   return true;
}
