$.extend( {
	getUrlVars : function() {
		var vars = [], hash;
		var hashes = window.location.href.slice(
				window.location.href.indexOf('?') + 1).split('&');
		for ( var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar : function(name) {
		return $.getUrlVars()[name];
	}
});

$.extend({
	parseJSON: function( data ) {
	if ( typeof data !== "string" || !data ) {
		return null;
	}

	// Make sure leading/trailing whitespace is removed (IE can't handle it)
	data = jQuery.trim( data );
	
	// Make sure the incoming data is actual JSON
	// Logic borrowed from http://json.org/json2.js
	if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
		.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
		.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {

		// Try to use the native JSON parser first
		return window.JSON && window.JSON.parse ?
			window.JSON.parse( data ) :
			(new Function("return " + data))();

	} else {
		jQuery.error( "Invalid JSON: " + data );
	}
}
});

function createPopup(uri, popupParam) {
	$.fn.colorbox({href:uri, maxWidth: '950px', maxHeight: '85%', onComplete:function(){
		var igFlowRequestParam = $.getUrlVar("igFlowRequest");
		if(igFlowRequestParam){
			getIgStep();
			//$.fn.colorbox({href:popupParam, maxWidth: '950px', maxHeight: '100%', close: 'Zamknij'});
		}
	}, close: 'Zamknij', 
	onCleanup:function(){
		var onCleanupFunction = window[popupParam.split(".")[0] + '_onCleanup'];
		if (onCleanupFunction) {
			onCleanupFunction.call();
		}
	},
	onClosed:function(){
		var onClosedFunction = window[popupParam.split(".")[0] + '_onClosed'];
		if (onClosedFunction) {
			onClosedFunction.call();
		}
	}
	});
}

function createPopup2(uri, popupParam) {
	$.fn.colorbox({
		href:uri, 
		maxWidth: '950px', 
		maxHeight: '85%', 
		onCleanup:function() {
			window.location = 'http://212.75.102.69';
		}, 
		onClosed:function() {
			window.location = 'http://212.75.102.69';
		},
		overlayClose: false,
		escKey:	false
	});
}

$(document).ready(function()
{
//	$('#form_add button, #form_check button').click(function()
/*	$('#form_add button').click(function()
	{
		$.fn.colorbox({href: 'igadd.htm', maxWidth: '950px', maxHeight: '100%', close: 'Zamknij'});
		return false;
	});
*/
	
	$(document).ajaxError(function(e, xhr, settings, exception) {
		alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
		$.fn.colorbox({href:'message.htm?title=ERROR&msg=UNEXPECTED_ERROR', width:'500px', height:'500px', maxWidth: '950px', maxHeight: '85%', close: 'Zamknij'});
	});
	
	var popupParam = $.getUrlVar("popup");	
	
	if(popupParam){
		uri = popupParam;
		
		var urlVars = $.getUrlVars();
		if (urlVars.length > 1) {
			uri = uri + "?";
			for (var i = 0; i < urlVars.length; i++) {
				var name = urlVars[i]; 
				if (name != 'popup') {
					uri = uri + name + '=' + urlVars[name] + '&';
				}
			}
		}
		createPopup(uri, popupParam);
	}
	
	var igFlowRequestParam = $.getUrlVar("igFlowRequest");	
	if(igFlowRequestParam){
		var productId = $.getUrlVar("productId");
		getIgStep(productId);
		//$.fn.colorbox({href:popupParam, maxWidth: '950px', maxHeight: '100%', close: 'Zamknij'});
	}

	var irFlowRequestParam = $.getUrlVar("irFlowRequest");	
	if(irFlowRequestParam){
		var productId = $.getUrlVar("productId");
		getIrStep(productId);
		//$.fn.colorbox({href:popupParam, maxWidth: '950px', maxHeight: '100%', close: 'Zamknij'});
	}
	
	var changePasswordParam = $.getUrlVar("changePassword");
	if(changePasswordParam){
		changePassword();
	}

	var igCheckPaymentParam = $.getUrlVar("checkPayment");	
	if(igCheckPaymentParam){
		checkPayments();
	}

	var checkCapchaParam = $.getUrlVar("checkCapcha");	
	if(checkCapchaParam){
		checkCapcha();
	}
	
	
	$('#resetPassword_link').colorbox({onComplete:function(element){
		resetPassword_complete();
	},maxWidth: '950px', maxHeight: '85%', close: 'Zamknij'});
	
	$('#igaddform_link').colorbox({onComplete:function(element){
		;
	},maxWidth: '594px', maxHeight: '85%', close: 'Zamknij'});
	

	$('#registerclientSimple_link').click(function() {
		return openColorBox("registerclientSimple.htm", false);
	});
	
	
	$('a.colorbox').click(function(){
//		$.fn.colorbox.resize
//		$.fn.colorbox({href: this.href, height: '100%', width: '700', maxWidth: '950px', maxHeight: '100%', close: 'Zamknij'});
		$.fn.colorbox({href: this.href, maxWidth: '950px', maxHeight: '85%', close: 'Zamknij'});
		return false;
	});		
	
	
//	$('#igadd').submit(function() {
////		$.fn.colorbox({href: 'igadd.htm', maxWidth: '950px', maxHeight: '100%'});
//		get(this);
//		return false;
//	});
//	
//	$("form.colorbox").submit(function(){
//		get(this);
//		return false;
//	});
	
	$("#questions .box").hide();
	$("#questions .box:first").show();
	$("#questions li:first").addClass('selected');
	
	$("#questions li a").click(function()
	{
		id = $(this).attr('href');
		$("#questions .box").hide();
		$("#questions li").removeClass('selected');
		$(this).parent().addClass('selected');
		$("#questions .box"+id).show('slow');
		return false;
	});
	
	$('#logout_button').click(function() {		
		window.location.href="logout.htm";
	});
	
	
	$('#clientpanel_button').click(function(){
		openColorBox('clientpanel.htm');
		//$.fn.colorbox({href: 'clientpanel.htm', maxWidth: '950px', height: '500px', close: 'Zamknij'});
	});
	
	$('#belka a').click(function()
	{
		if(isUserLogged()){
			$.fn.colorbox({href: this.href, innerWidth: '880px', height: '90%', iframe: true, close: 'Zamknij'});
			return false;
		}else{
			return openColorBox("notlogged.htm", false);
		}
	});

	$('button#print').click(function()
	{
		window.print();
		return false;
	});

	$('#numRepresentatives').change(function() {
		refreshRepresentatives();
	});
	refreshRepresentatives();
});

var deleteProduct = function(productId, isConfirmed) {
	var url = 'igdelete.htm?productId=' + productId + '&isConfirmed=' + isConfirmed;
	openColorBox(url);
}

var showGetProductDetails = function(productId, isConfirmed) {
	var url = 'productgetdetails.htm?productId=' + productId + '&isConfirmed=' + isConfirmed;
	$.fn.colorbox({href: url, close: 'Zamknij', onClosed: function(){
		$.fn.colorbox({href: 'clientpanel.htm', maxWidth: '950px', height: '500px', close: 'Zamknij'});
	}});
}

var changePassword = function() {
	var url = 'passwordChange.htm';
	openColorBox(url);
}

var changeEmail = function() {
	var url = 'emailChange.htm';
	openColorBox(url);
//	$.fn.colorbox({href: url, close: 'Zamknij', 		
//		onClosed: function(){
//			$.fn.colorbox({href: 'clientpanel.htm', maxWidth: '950px', height: '500px', close: 'Zamknij'});
//		},
//		onComplete:function(element){
//			emailChange_complete();	
//		}
//	});
}

var userDelete = function() {
	var url = 'userDelete.htm';
	openColorBox(url);
}
	
var showMessage = function(title, msg) {
	var url = 'message.htm?title=' + title + '&msg=' + msg;
	openColorBox(url);
}

function refreshRepresentatives() {
	var idx = $('#numRepresentatives').attr('selectedIndex');
	for (i = 0; i < 4; i++) {
		if (i <= idx) {
			$('#representative_' + i).show();
		} else {
			$('#representative_' + i).hide();
		}
	}
}

function changeOperatorStatus(id, status) {
	$('#acceptOperator_' + id + '_controls').hide();
	$('#acceptOperator_' + id + '_loader').show();
	$.getJSON('changeOperatorStatus.htm', {id: id, status: status}, function() {
		window.location = 'mainpage.htm?popup=clientpanel.htm';
	});
}

function refreshCorrespAddress(checkbox) {
	if (checkbox.checked) {
		$('[id^="corresp_"]').css("display", "none");
	} else {
		$('[id^="corresp_"]').css("display", "table-row");
	}

}

function refreshRegisterCompanyRows() {
	if ($('#attachToExistingCompany').val() == 'true') {
		$('[id^="registerCompany_"]').css("display", "none");
		$('[id^="attachCompany_"]').css("display", "table-row");
	} else {
		$('[id^="registerCompany_"]').css("display", "table-row");
		$('[id^="attachCompany_"]').css("display", "none");
	}
}

function refreshRegisterCompanyEdgKrs() {
	var val = $('#client\\.companyType').val(); 
	if (val == "") {
		$('#registerCompany_edg').css("display", "none");
		$('#registerCompany_krs').css("display", "none");
	} else if (val == "DG" || val == "SPOLKA_CYWILNA") {
		$('#registerCompany_edg').css("display", "table-row");
		$('#registerCompany_krs').css("display", "none");
	} else {
		$('#registerCompany_edg').css("display", "none");
		$('#registerCompany_krs').css("display", "table-row");
	}
}

function showHideExecutiveTitleDetailsFields() {
	var value = $('#executiveTitle').val();
	if (value && value == 'EXECUTIVE_TITLE_00') {
		$('[id^="executiveTitleDetails_"]').css("display", "table-row");
		$('#currency1').removeAttr('disabled');
		$('#currency2').attr('disabled', 'disabled');
		$('#currency1').show();
		$('#currency2').hide();
	} else {
		$('[id^="executiveTitleDetails_"]').css("display", "none");
		$('#currency1').attr('disabled', 'disabled');
		$('#currency2').removeAttr('disabled');
		$('#currency1').hide();
		$('#currency2').show();
	}
}


