function WBGModal()
{
	var instance = this;
	instance.wx = 620;
	instance.formURLRequest=null;
	instance.targetURLRequest=null;
	instance.validationFunction=null;
	instance.currentValueObject = null;
	instance.formTitle = null;
	instance.form=null;
	instance.totalExtraHeight = 180;
	instance.capchaValidationString = "Invalid capcha code";
	
	//RESIZING Window
	instance.resizeWBGModal = function()
	{
		var W = jQuery(window).width();
		var H = jQuery(window).height();
		var targetHeight = (H - instance.totalExtraHeight);
		
		jQuery('.WBGpopup #innerDiv').stop().animate({height:targetHeight},1000,function(){
																						   jQuery('.WBGpopup #innerDiv').css('overflow','auto');
																						  });
		
	}
	
	//Used to recolate the WBGModal popup window
	instance.reLocate = function()
	{
		W = jQuery(window).width();
		H = jQuery(window).height();
		tgtLeft = (W-instance.wx)/2;
		
		jQuery(".WBGpopupContent").css('left',tgtLeft);
		jQuery(".WBGpopupContent").css('top',20);
		jQuery(".shadowbg").css('width',W);
		jQuery(".shadowbg").css('height',H);
		jQuery(".shadowbg").css('opacity',0.6);
		
	}
	
	//Used to reset the WBGModal popup window
	instance.resetModal = function()
	{
		
		jQuery('.WBGpopup #innerDiv').show();
		jQuery('.WBGpopup #innerDiv').html("<div class='preloader_container'><div class='preloader'></div></div>");
	}
	
	
	//Used to close the WBGModal popup window
	instance.closeModal = function()
	{
	  instance.currentValueObject = null;
	  jQuery('.WBGpopup #message_table').html("");
	  jQuery('.WBGpopup').hide();	
	  instance.resetModal();	
	}
	
	instance.alert = function(type,message_string)
	{
	  	switch(type)
		{
		 case "error":
		 jQuery('.WBGpopup #message_table').html("");
		 jQuery('.WBGpopup #message_table').append('<td  width="40" valign="top"><img src="WBGPopup/err.jpg"/></td>');
		 jQuery('.WBGpopup #message_table').append('<td><span class="redtext">'+message_string+'</span></td>');
		 jQuery('.WBGpopup #message_table').append('<td class="msg_close"><img src="WBGPopup/cross_bt.png"/></td>');
		 jQuery('.msg_close').bind('click',function(){jQuery('.messageBox').slideUp('fast');});
		 jQuery('.messageBox').slideDown('slow');
		 break;
		 
		 case "message":
		 jQuery('.WBGpopup #message_table').html("");
		 jQuery('.WBGpopup #message_table').append('<td  width="40" valign="top"><img src="WBGPopup/tick.jpg"/></td>');
		 jQuery('.WBGpopup #message_table').append('<td><span class="greentext">'+message_string+'</span></td>');
		 jQuery('.WBGpopup #message_table').append('<td class="msg_close"><img src="WBGPopup/cross_bt.png"/></td>');
		 jQuery('.msg_close').bind('click',function(){jQuery('.messageBox').slideUp('fast');});
		 jQuery('.messageBox').slideDown('slow');
		 break;
		}
	}
	
	instance.getMessageBoxHTML = function()
	{
		var table_html = "<div class='messageBox'><table width='100%' cellpadding='2' cellspacing='2'>";
		table_html = table_html+"<tr id='message_table'></tr>";
		table_html = table_html+"</table></div>";
		
		return table_html;
	}
	
	//Used to create the WBGModal popup window
	instance.createModal = function()
	{
	   var htmlc="";
	   htmlc = htmlc+"<div class='WBGpopup'><div class='shadowbg'></div><div class='WBGpopupContent'>"
	   htmlc = htmlc+"<div class='popupHead'><div id='h2div'>Popup content</div><div class='crossbt'></div></div><div class='popupBody'>"
	   htmlc = htmlc+"<div id='innerDiv'><b>Loading</b></div></div><div class='popupTail'></div></div></div>";	   
	   jQuery('body').append(htmlc);
	   jQuery('.popupBody').append(instance.getMessageBoxHTML());
	   instance.reLocate();
	   jQuery('.WBGpopup .crossbt').eq(0).bind('click',function(){instance.closeModal();});
	   jQuery(window).resize(function(){
									     instance.reLocate();
									     instance.resizeWBGModal();
									   });
	   
	   instance.resizeWBGModal();
	}
	
	
	//Used to show the WBGModal popup window
	instance.openModal = function()
	{
	  instance.resetModal();
	  jQuery('.WBGpopup').fadeIn(250);	
	  	
	}
	
	instance.refillForm = function()
	{
	   instance.form.find('input').each(function(inp){
												     	var v = instance.currentValueObject[""+jQuery(this).attr('name')+""];
														jQuery(this).val(v);
													 
													 });
	   
	   instance.form.find('textarea').each(function(inp){
												     	var v = instance.currentValueObject[""+jQuery(this).attr('name')+""];
														jQuery(this).val(v);
													 
													 });
	   
	   instance.form.find('select').each(function(inp){
												     	var v = instance.currentValueObject[""+jQuery(this).attr('name')+""];
														jQuery(this).val(v);
													 
													 });
	  
	}
	
	
	//Used to load the javascript form in the WBGModal popup window
	instance.loadForm = function(pageURL,pageTitle,targetPageURL,validateFun)
	{
	  instance.validationFunction = validateFun;
	  instance.targetURLRequest = targetPageURL;
	  instance.openModal();
	  instance.formURLRequest = pageURL;
	  instance.formTitle = pageTitle;
	  jQuery('#h2div').html(instance.formTitle);
	  jQuery('.WBGpopup #innerDiv').load(instance.formURLRequest,instance.onLoadComplete);
	  
	  
	}
	
	instance.checkCapcha = function(output_str)
	{
	  var code_str = new String(output_str);
	  code_str = code_str.toLowerCase();
	  var idx = code_str.indexOf("capcha");
	  
	  if(idx==-1){ return true;}
	  else{ return false;}
	}
	
	
	instance.postComplete = function(data)
	{
		if(instance.checkCapcha(String(data)))
		{
			jQuery('.WBGpopup #innerDiv').slideUp('fast');
			instance.alert("message",String(data));
		}
		else
		{
		  instance.alert("error",String(data));
		  instance.loadForm(instance.formURLRequest,instance.formTitle,instance.targetURLRequest,instance.validationFunction)
		}
		
	}
	
	
	//Used to post form
	instance.formPost = function(e)
	{
	  	
		e.preventDefault();
		if(instance.validationFunction())
		{
			
			var obj = instance.createDataObject();
			jQuery.ajax({async:true});
			jQuery.post(instance.targetURLRequest,{form:obj},instance.postComplete);
			
		}
	}
	
	instance.createDataObject = function()
	{
		var obj = new Object();
		instance.form.find('input').each(function(inp){  obj[""+jQuery(this).attr('name')+""] = jQuery(this).val();	  });
		instance.form.find('select').each(function(se){  obj[""+jQuery(this).attr('name')+""] = jQuery(this).val();	  });
		instance.form.find('textarea').each(function(se){  obj[""+jQuery(this).attr('name')+""] = jQuery(this).val();	  });
		instance.currentValueObject = obj;
		return obj;
	}
	
	//Used to add listeners
	instance.applyListeners = function()
	{
	 instance.form = null;
	 if(jQuery('.WBGpopup #innerDiv').find('form').length)	
	 {
		instance.form = jQuery('.WBGpopup #innerDiv').find('form').eq(0);
		if(instance.form.find('input[type="submit"]').length)
		{
			instance.form.find('input[type="submit"]').eq(0).bind('click',instance.formPost);
		}
		else
		{ instance.alert("error","No Submit button found! in the form"); }
		
	 }
	 else
	 {instance.alert("error","No form found in the page. Please check the page, from where the form is.");}
	 
	}
	
	instance.onLoadComplete = function()
	{
	 	instance.applyListeners();
		if(instance.currentValueObject!=null)
		{
			instance.refillForm();	
		}
		
	}
	
	
	
	
	
}

var WBG_MODAL;
jQuery(document).ready(function(){
								
								
								WBG_MODAL = new WBGModal();
								WBG_MODAL.createModal();
								
								});
