var ConstPopupLoaderPopupContent = 'PopupLoaderPopupContent';
var _width, _height;
var _openedUrl;
var _popupObject;
var _documentHead;
var _loaded = false;

$(document).ready(
	function(){
		_loaded = true;
	}
);

		
function OpenPopupWindow(url, width, height) {
	if(_loaded) {
		_width = width;
		_height = height;
		_openedUrl = url;
		_popupObject = $('<div id="' + ConstPopupLoaderPopupContent + '"></div>').modal({
			zIndex: 20000,
			close: true,
			positionStyle: 'absolute',
			containerCss: {width: _width, height: _height},
			containerId: 'PopupLoaderContainer',
			position: [73,]
		});
		
		$('body').animate({scrollTop:0}, 'slow');

		GetPage(url);
	}
}

function GetPage(url) {
	var xmlHttp;
	try {// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();		
	} catch (e) {// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			//Get the response from the server and extract the section that comes in the body section of the second html page avoid inserting the header part of the second page in your first page's element
			AttachPageToDocument(xmlHttp.responseText);
		}
	}

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}		

function PostFrom(formId) {
	var xmlHttp;
	try {// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();		
	} catch (e) {// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			//Get the response from the server and extract the section that comes in the body section of the second html page avoid inserting the header part of the second page in your first page's element
			AttachPageToDocument(xmlHttp.responseText);
		}
	}

	xmlHttp.open("POST", _openedUrl, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
	var params = $("#" + formId).serialize();
	xmlHttp.send(params);
}

function AttachPageToDocument(textToAttach) {
	_documentHead = document.getElementsByTagName("head")[0];
	var elem = document.getElementById(ConstPopupLoaderPopupContent);
	if (!elem) {
		alert('The element with id="' + ConstPopupLoaderPopupContent + '" doesn\'t exists in your page');
		return;
	}
	elem.innerHTML = '';
	var respHead = textToAttach;
	var start = textToAttach.indexOf('<head');
	if (start > -1) {
		respHead = textToAttach.substring(textToAttach.indexOf('>', start) + 1);
	}
	respHead = respHead.split('</head>')[0];

	$('script', '<div>' + respHead + '</div>').each(function() {
			if (typeof(this.src) != 'undefined' && this.src != '') {
				var script = document.createElement('script');
				script.setAttribute('type', 'text/javascript');
				script.setAttribute('language', 'javascript');
				script.setAttribute('src', this.src);
				_documentHead.appendChild(script);
			}
			if (this.innerHTML != '') {
				$(elem).append($('<script type=\'text/javascript\' language=\'javascript\'>' + this.innerHTML + '</script>'));
			}
		});

	$('link', '<div>' + respHead + '</div>').each(function() {
			var link = document.createElement('link');
			link.setAttribute('type', 'text/css');
			link.setAttribute('rel', 'stylesheet');
			link.setAttribute('href', this.href);
			_documentHead.appendChild(link);
		});

	$('style', '<div>' + respHead + '</div>').each(function() {
			var style = document.createElement('style');
			style.setAttribute('type', 'text/css');
			style.setAttribute('href', this.innerHTML);
			_documentHead.appendChild(style);
		});

	var respBody = textToAttach;
	var start = textToAttach.indexOf('<body');
	if (start > -1) {
		respBody = textToAttach.substring(textToAttach.indexOf('>', start) + 1);
	}
	respBody = respBody.split('</body>')[0];

	$(elem).append($(respBody));
	
	$('.ClosePopup').bind('click.simplemodal', function (e) {
			e.preventDefault();
			_popupObject.close();
		});	
}