//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var ajaxRequest; 
var nextFormURL;
var curUrlParam;

$(document).ready(function(){
	
	if(document.getElementById('member_set').style.display=="block") {
		bindMemberSet();
	}

	links = $('a[rel=lightbox]');
	linksFull = $( jQuery('a[rel^=lightboxfull]') );
	backgroundPopup=$( jQuery( '<div id="backgroundPopup"></div>' ) );
	popupContact=$( jQuery( '<div id="popupContact"></div>' ) );
	popupContactClose=$( jQuery( '<a id="popupContactClose">x</a>') );
	
	$( 'body' ).append( backgroundPopup ).append( popupContact );
	
	 //Click out event! Close popup!
	 backgroundPopup.click(function(){  
	 disablePopup();  
	 });
	
	links.each( function( index ) {
    var link = $( this );
    link.click( function( c ) {
        c.preventDefault();
		//load popup
		loadPopup($(this).attr('href'));
    } );
	} );
	
	linksFull.each( function( index ) {
    var link = $( this );
    link.click( function( c ) {
        c.preventDefault();
		//load popup
		loadFullScreenPop($(this).attr('href'));
    } );
	} );
	
});

function bindPopForm() {
	var formObj=document.getElementById("membershipFormID");
	var formAction=formObj.submitAction.value;
	$("#membershipFormID").bind("submit", function(event) {
	event.preventDefault();
	if(formAction!=="") {
		eval(formAction);
	}
});
}

function bindMemberSet() {
	links = $( 'a[rel^=signout]' );
	links.each( function( index ) {
		var link = $( this );
		 link.click( function( c ) {
			c.preventDefault();
			//load and populate edit detail page form
			logout($(this).attr('href'));
		} );
	} );
}

//loading popup with jQuery magic!
function loadPopup(virtual_url){
	
	//process virtual url htaccess style
	start_url=virtual_url.split("/")[1];
	curUrlParam=virtual_url.split("/")[2];
	if(start_url=="member") {
		url=siteRoot+"shared/membership/membership.php?view="+curUrlParam;
	}else if(start_url=="load") {
		url=siteRoot+"loadlightbox.php?url="+curUrlParam;
	} else{
		url=virtual_url;
	}
	
//loads popup only if it is disabled
if(popupStatus==0){
	$("#popupContact").load(url, function() { 
		//load popup stylesheets - safari won't recogonize it otherwise
		var headID = document.getElementsByTagName("head")[0];
		//load form css 
		var cssNode_bpm = document.createElement('link');
		cssNode_bpm.type = 'text/css';
		cssNode_bpm.rel = 'stylesheet';
		cssNode_bpm.href = siteRoot+'shared/membership/form/css/bpmform.css';
		cssNode_bpm.media = 'screen';
		
		//load css form validation
		var cssNode_valid = document.createElement('link');
		cssNode_valid.type = 'text/css';
		cssNode_valid.rel = 'stylesheet';
		cssNode_valid.href = siteRoot+'shared/membership/form/css/validationEngine.jquery.css';
		cssNode_valid.media = 'screen';
		
		//load js form validation        
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.src = siteRoot+'shared/membership/form/js/jquery.validationEngine.js';

		//add dynamic scripts and css
		headID.appendChild(cssNode_bpm);
		headID.appendChild(cssNode_valid);
		headID.appendChild(newScript);

		$("#popupContact").append(popupContactClose);
		//centering with css
		centerPopup(100,150);
		//Click the x event! Close popup!
		popupContactClose.click(function(){  
		 disablePopup();  
		  });
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		bindPopForm();
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	});
}
}

//loading popup with jQuery magic!
function loadFullScreenPop(url){
if(popupStatus==0){
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");	
}
//loads popup only if it is disabled
if(popupStatus==0){
	$("#popupContact").load(url, function() { 
		//centering with css
		centerPopup(0,75);
		popupStatus = 1;
		$("#popupContact").css({
		"background-color": "#000000"
		});

		$("#popupContact").fadeIn("slow");
	});
}
}

function exitReg() {
	disablePopup();
}

function forgot() {
	var formObj=document.getElementById("membershipFormID");
	var login=formObj.login.value;
	var phpURL=$("#membershipFormID").attr('action'); 
	
	if (typeof asynchronous == 'undefined') asynchronous = true;
	
	var dataString=$('#membershipFormID').serialize();
	
	$.ajax({
	    type: "POST",
	    url: phpURL,
	    data: dataString,
	    success: function(result) {  
			
			$("#contentForm").html(result);			
   		},
   		error: function (xmlHttpRequest, textStatus, errorThrown) {
   			//alert("error:"+result+":end");
   			$("#errorDisplay").html(errorThrown);
   		}
	}); // end ajax
	return false;
}

function logUser() {
	var formObj=document.getElementById("membershipFormID");
	var email=formObj.email.value;
	var password=formObj.password.value;
	var phpURL=$("#membershipFormID").attr('action'); 
	
	if (typeof asynchronous == 'undefined') asynchronous = true;
	
	
	virtual_url=document.getElementById("membershipFormID").nexturl.value;
	//process virtual url htaccess style
	start_url=virtual_url.split("/")[1];
	url_param=virtual_url.split("/")[2];
	if(start_url=="member") {
		nextFormURL=phpURL+"?view="+url_param;
	}else{
		nextFormURL=virtual_url;
	}

	var dataString=$('#membershipFormID').serialize();
	
	$.ajax({
	    type: "POST",
	    url: phpURL,
	    data: dataString,
	    success: function(result) {  
			//alert("post success:"+result);
			result=jQuery.trim(result);
			if(result=="1") {
				window.location.reload(true);
				//document.getElementById('member_set').style.display='block';
				///document.getElementById('login_set').style.display='none';
			} else {
				$("#errorDisplay").html(unescape(result));
			}
			
   		},
   		error: function (xmlHttpRequest, textStatus, errorThrown) {
   			//alert("error:"+result+":end");
   			$("#errorDisplay").html(errorThrown);
   		}
	}); // end ajax
	return false;
}

function logout(url) {
	var phpURL=siteRoot+"shared/membership/membership.php";
	start_url=url.split("/")[2];
	var dataString="view="+start_url;
	$.ajax({
	    type: "POST",
	    url: phpURL,
	    data: dataString,
	    success: function(result) {  
			//console.log("post success:"+result);
			del_cookie(result);
			window.location.reload(true);			 	
   		},
   		error: function (xmlHttpRequest, textStatus, errorThrown) {
   			//console.log("error:"+result+":end");
   		}
	}); // end ajax
	return false;
}

function del_cookie(name) {
	document.cookie = name+'=; expires=Wed, 1 Mar 2006 00:00:00';
} 

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function submitReg() {	
	var dataString=$("#membershipFormID").serialize();

	var phpURL=$("#membershipFormID").attr('action'); 
	
	if (typeof asynchronous == 'undefined') asynchronous = true;
	
	virtual_url=document.getElementById("membershipFormID").nexturl.value;
	//process virtual url htaccess style
	start_url=virtual_url.split("/")[1];
	url_param=virtual_url.split("/")[2];
	if(start_url=="member") {
		nextFormURL=phpURL+"?view="+url_param;
	}else{
		nextFormURL=virtual_url;
	}
	
	$.ajax({
	    type: "POST",
	    url: phpURL,
	    data: dataString,
	    success: function() {  
			//alert("post success:"+nextFormURL);
			if(popupStatus==1) {
			$("#popupContact").load(nextFormURL, function() { 
					centerPopup(100,150); 
			});
			}else {
					loadPopup(nextFormURL);
			}	
			return false;
   		},
   		error: function (xmlHttpRequest, textStatus, errorThrown) {
   			
   			if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) {
   				return false;
               // it's not really an error
   			} else {
              // Do normal error handling
				return false;
   			}
   		}
	}); // end ajax
	return false;
}

function moreReg() {
	var formObj=document.getElementById("membershipFormID");
	var action=formObj.action.value;
	
	var dataString=$("#membershipFormID").serialize();
	
	var phpURL=$("#membershipFormID").attr('action');
	
	virtual_url=document.getElementById("membershipFormID").nexturl.value;
	
	//process virtual url htaccess style
	start_url=virtual_url.split("/")[1];
	url_param=virtual_url.split("/")[2];
	if(start_url=="member") {
		nextFormURL=phpURL+"?view="+url_param;
	}else{
		nextFormURL=virtual_url;
	}
	
	nextFormURL+="&"+dataString;
	

	if(popupStatus==1) {
			$("#popupContact").load(nextFormURL, function() { 
					centerPopup(100,150); 
			});
	}else {
			loadPopup(nextFormURL);
	}	
}

//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
if($.validationEngine) {
$.validationEngine.closePrompt(".formError",true);
}
$("#backgroundPopup").fadeOut("slow");
$("#popupContact").fadeOut("slow");
popupStatus = 0;
}
}

//centering popup
function centerPopup(topPix,leftPix){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();

//centering
$("#popupContact").css({
"position": "absolute",
"top": topPix,
"left": leftPix
});
//only need force for IE6

$("#backgroundPopup").css({
"height": windowHeight
});

}

//CLOSING POPUP    
 //Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
