// payprocessor.js
var loading = new Image();
loading.src = "../img/loading.gif";

function funcProcesarPago()
{
	var firstname = document.getElementById("firstname");
	var lastname = document.getElementById("lastname");
	var zip = document.getElementById("postcode");
	var city = document.getElementById("city");
	var country = document.getElementById("country");
	var email = document.getElementById("email");
	
	var email_regex = /^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/;
	
	var err_msg = "";
	
	// Verify firstname
	if(firstname.value == "")
		err_msg += "Specify your firstname. \n";
	// VErify lastname
	if(lastname.value == "")
		err_msg += "Specify your lastname. \n";
	// Verify e-mail
	if(email.value == "")
		err_msg += "Specify an e-mail address \n";
	else if(!validateRegEx(email_regex, email.value))
		err_msg += "Specify a valid e-mail address \n";
	// Verify selected country	
	if(country.selectedIndex == 0)
		err_msg += "Select a country of residence\n";
		
	if(err_msg != "")
	{
		var message_header = "In order to proceed you must:\n";
		alert(message_header+err_msg);
	}
	else
		document.order_form.submit();
}


function validateRegEx(regex, inputStr)
{
	if(!regex.test(inputStr))
		return false;
	else 
		return true;
}


// Simply submits the corresponding form
function processClientRegistryPayment()
{
	document.registry_payment.submit();
}



function cancel(id)
{
	var message = "Are you sure you want to cancel your subscription process?\n";
	message += "This will remove all data you've entered permantenly";
	
	var checkCancel = confirm(message);
	
	if(checkCancel == true)
		cancelSubscription(id);
}


function keepIt()
{
	document.body.removeChild(document.getElementById("cancelContainer"));
}



function cancelSubscription(id)
{
	var cancelPopUp = document.createElement("div");
	cancelPopUp.setAttribute("id","cancelContainer");
	
	var content = "<p style='font-family:Verdana, Arial;font-size:16px;color:#FFFFFF;'><b>Cancelling subscription...</b></p>";
	content += "<div style='text-align:center;'><img src='"+loading.src+"' /></div>";
	
	cancelPopUp.innerHTML = content;
	
	document.body.appendChild(cancelPopUp);
	
	centerPopUp(cancelPopUp);
	
	var oXMLHttp = checkAndCreateXMLHttp();
	
	var url = "../ajxcall/cancel_suscription.php";
	var query = "?userid="+id;
	
	oXMLHttp.open("GET", url+query, true);
	
	oXMLHttp.onreadystatechange = function()
		{
			if(oXMLHttp.readyState == 4)
			{
				if(oXMLHttp.status == 200)
				{
					keepIt();
					location.href = "http://www.bookingdiscounter.com";
				}
			}
		
		}
	
	oXMLHttp.send(null);

}



/* Devuelve el desplazamiento vertical del scroller en pixels */
function getTopScroll()
{
	var topScroll;
	// Check for Internet Explorer or Netscape
	if(navigator.appName.indexOf("Microsoft")!=-1)
	{	
		topScroll = document.body.scrollTop;
	}
	else
	{
		topScroll = window.pageYOffset;
	}
	
	return topScroll;
}


/* Esta función devuelve el ancho de la ventana del browser */
function getClientWidth()
{
	var cwidth;
	// Check for Internet Explorer or Netscape
	if(navigator.appName.indexOf("Microsoft")!=-1)
	{	
		cwidth = screen.availWidth;
	}
	else
	{
		cwidth = window.innerWidth;
	}
	
	return cwidth;
}



function getClientHeight()
{
	var theHeight;
	
	// Obtaining the client's height
	if (window.innerHeight) 
	{
		theHeight=window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) 
	{
		theHeight=document.documentElement.clientHeight;
	}
	else if (document.body) 
	{
		theHeight=document.body.clientHeight;
	}
	
	return theHeight;
}




function centerPopUp(elemObj)
{
	var topScroll;
	var wwth;
	var theHeight;
	var topDistance;
	var theObjWidth;
	
	theHeight = getClientHeight();
	
	// Check for Internet Explorer or Netscape
	if(navigator.appName.indexOf("Microsoft")!=-1)
	{	
		topScroll = document.body.scrollTop;
		wwth = screen.availWidth;
	}
	else
	{
		topScroll = window.pageYOffset;
		wwth = window.innerWidth;
	}
	
	// Getting the popUp object width
	theObjWidth = elemObj.offsetWidth;
	
	// Setting a top distance. TheHieght represents the the clientHeight. 
	// The second number is an aprox popUp Object Height
	topDistance = ((theHeight/2) - 80);
	// Setting the popUp top position	
	elemObj.style.top = topScroll + topDistance + "px";
	// Setting the popUp left position - (wwth/2) = half of the screen => 150 = half container width
	elemObj.style.left = ((wwth/2)-theObjWidth/2) + "px";
}

