var http_request = false;
function makePOSTRequest(url, parameters, callback, actionAfterComplete) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			// http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = function() {
		callback(actionAfterComplete);
	};
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertContents(actionAfterComplete) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			// alert('request recieved' + result);
			$.fn.colorbox( {
				html : result,
				maxWidth : '950px',
				maxHeight : '85%',
				close : 'Zamknij',
				onComplete : function(element) {
					if (actionAfterComplete) {
						actionAfterComplete();
					}
				}
			});
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function getColorbox(form) {
	var poststr = $(form).serialize();
	// alert(form.action)

	makePOSTRequest(form.action, poststr, alertContents);
}

function postFormAndOpenColorbox(form) {
	var poststr = $(form).serialize();
	$.post(form.action, poststr, function(data) {
		$.fn.colorbox( {
			html : data,
			maxWidth : '950px',
			maxHeight : '85%',
			close : 'Zamknij'
		})
	});
	return false;
}

function postForm(form, actionAfterComplete) {
	if (!form) {
		alert ('undefined form');
		return;
	}
	var poststr = $(form).serialize();
	// alert(form.action + "; " + poststr)
	$.post(form.action, poststr, function(data) {
		// alert('post success' + data);
			if (!actionAfterComplete) {
				actionAfterComplete = createActionFunction(form.action, '_onComplete');
			}
			var onCleanupFunction = createActionFunction(form.action, '_onCleanup');
			
			var onClosedFunction = createActionFunction(form.action, '_onClosed');
		
			$.fn.colorbox( {
				html : data,
				maxWidth : '640px',
				maxHeight : '85%',
				close : 'Zamknij',
				onComplete : function() {
					actionAfterComplete();
				},
				onCleanup:function(){
					onCleanupFunction();
				},
				onClosed:function(){			
					onClosedFunction();
				}
			
			});
			
			// alert('post success 2');

		})
	return false;
}

function openColorBox(uri, checkLogged) {
	return openColorBox3(uri, null, checkLogged);
}

function openColorBox3(uri, data, checkLogged) {
	if(checkLogged == undefined) {
		checkLogged = true;
	}
	
	if(checkLogged && !isUserLogged()){
		return openColorBox('notlogged.htm', false);
	}	
	
	var actionAfterComplete = createActionFunction(uri, '_onComplete');
	var onCleanupFunction = createActionFunction(uri, '_onCleanup');
	var onClosedFunction = createActionFunction(uri, '_onClosed');
	
	if (uri == "notlogged.htm" || 
		uri.indexOf("message.htm") == 0 ||
		uri.indexOf("igdelete.htm") == 0 ||
		uri.indexOf("igLimitExceeded.htm") == 0 ||
		uri.indexOf("userDelete.htm") == 0) {
		maxW = '600px';
	} else if (uri == "registerclientSimple.htm"){
		maxW = '580px';
	} else {
		maxW = '950px';
	}
	
	if (!data) {
		$.fn.colorbox( {
			href : uri,
			maxWidth : maxW,
			maxHeight : '85%',
			close : 'Zamknij',
			onComplete : function() {
			actionAfterComplete();
			},
			onCleanup:function(){
				onCleanupFunction();
			},
			onClosed:function(){
				onClosedFunction();
			}
		});
	} else {
		$.fn.colorbox( {
			html : data,
			maxWidth : maxW,
			maxHeight : '85%',
			close : 'Zamknij',
			onComplete : function() {
			actionAfterComplete();
			},
				onCleanup:function(){
					onCleanupFunction();
			},
			onClosed:function(){
				onClosedFunction();
			}
		});
	}
	return false;
}

function createActionFunction(url, postFix) {
	return function() {
		var splitUrl = url.split("/");
		var splitAction = splitUrl[splitUrl.length - 1].split(".");
		var functionName = splitAction[0] + postFix;
		var actionFunction = window[functionName];
		if (actionFunction) {
			actionFunction.call();
		}
	}
}

// function postForm(form, actionAfterComplete) {
// var poststr = $(form).serialize();
// // alert(form.action)
//      
// makePOSTRequest(form.action, poststr, alertContents, actionAfterComplete);
// return false;
// }

function ajaxFileUpload(formId, onSuccessForm, onFailureForm) {
	$("#loading").ajaxStart(function() {
		$(this).show();
	}).ajaxComplete(function() {
		$(this).hide();
	});

	$.ajaxFileUpload( {
		url : 'uploadFile.htm',
		secureuri : false,
		fileElementId : 'fileToUpload',
		dataType : 'json',
		success : function(data, status) {
			if (typeof (data.error) != 'undefined') {
				if (data.msg != '') {
					alert(data.msg);
				} else {
					alert(data.error);
				}
				postForm(onFailureForm);
			} else {
				postForm(onSuccessForm);
			}
		},
		error : function(data, status, e) {
			alert(e);
		},
		uploadFormId : formId
	});
	return false;
}

function ajaxFileUpload2(formId, fileElementID, uploadURL, action) {
	$("#loading").ajaxStart(function() {
		$(this).show();
	}).ajaxComplete(function() {
		$(this).hide();
	});
	$.ajaxFileUpload({
		url : uploadURL,
		secureuri : false,
		fileElementId : fileElementID,
		dataType : 'json',
		success : function(data, status) {
			if (data.uploadOk != 'true') {
				if (data.msg != '') {
					alert(data.msg);
				} else {
					alert(data.error);
				}
			}
			action();
		},
		error : function(data, status, e) {
			alert(e);
		},
		uploadFormId : formId
	});
	return false;
}

function companyDocUpload(formId) { 
	$("#loading").ajaxStart(function() {
		$(this).show();
	}).ajaxComplete(function() {
		$(this).hide();
	});
	$.ajaxFileUpload( {
		url : 'registerCompanyStageTwo.htm',
		secureuri : false,
		fileElementId : 'uploadedFile',
		dataType : 'json',
		success : function(data, status) {
			if (typeof (data.error) != 'undefined') {
				if (data.msg != '') {
					alert(data.msg);
				} else {
					alert(data.error);
				}
			} else {
				postForm(document.refreshCompanyDoc);	
			}
		},
		error : function(data, status, e) {
			alert(e);
		},
		uploadFormId : formId
	});
	return false;
}

