function InitCalendar(prefix)
{
	Date.firstDayOfWeek = 1;

	var idDay = prefix + '_day';
	var idMonth = prefix + '_month';
	var idYear = prefix + '_year';

	var idImg = 'idImgCalendar_' + prefix;
	
	$('#'+idImg).mouseover(function(){
		this.style.cursor = 'pointer';
	});

	var startYear = $('#'+idYear+'>option:first').get(0).value;
	var endYear = $('#'+idYear+'>option:last').get(0).value;

	$('#'+idImg)
		.datePicker(
			// associate the link with a date picker
			{
				createButton:false,
				startDate:'01/01/'+startYear,
				endDate:'31/12/'+endYear,
				showHeader: 2
			}
		).bind(
			// when the link is clicked display the date picker
			'click',
			function()
			{
				UpdateCalendarSelects($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		).bind(
			// when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				UpdateCalendarSelects(selectedDate);
			}
		).bind(
			'dpClosed',
			function(e, selected)
			{
				UpdateCalendarSelects(selected[0]);
			}
		)

	var UpdateCalendarSelects = function (selectedDate)
	{
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth()+1;
		var y = selectedDate.getFullYear();

		($('#'+idDay)[0]).value = ZeroPad(d);
		($('#'+idMonth)[0]).value =  ZeroPad(m) ;
		($('#'+idYear)[0]).value = y;
	}

	// listen for when the selects are changed and update the picker
	$('#' + idDay + ', #' + idMonth + ', #' + idYear)
		.bind(
			'change',
			function()
			{
				var d = new Date(
							$('#'+idYear).val(),
							$('#'+idMonth).val() - 1,
							$('#'+idDay).val()
						);
				
				//alert(d.asString());
				$('#'+idImg).dpSetSelected(d.asString());
			}
		);
	
	// and update the datePicker to reflect it...
	$('#'+idDay).trigger('change');


}


function ZeroPad(num) 
{
	var s = '0'+num;
	return s.substring(s.length-2)
	//return ('0'+num).substring(-2); // doesn't work on IE :(
};

$(document).ready(function () {

	//Template for confirm message
	var confirmStr = ""+
		"<div id='confirm' style='display:none;'>"+
			"<a href='#' title='Close' class='modalCloseX modalClose'>x</a>"+
			"<div class='header'><span>Confirm</span></div>"+
			"<p class='message'></p>"+
			"<div class='buttons'>"+
			"<div class='no modalClose'>No</div><div class='yes'>Yes</div>"+
		"</div>";
	$("body").append(confirmStr);

	//Template for Alert Message
	var alertStr = ""+
		"<div id='alert' style='display:none;'>"+
		"<table>"+
		"<tr>"+
			"<td width='48'><img src='img/system/alert.jpg'/></td>"+
			"<td>"+
				"<ul style='padding:3px;margin:3px;'>"+
					"<li style='color:#ff0000;' class='alert_message'></li>"+
				"</ul>"+
			"</td>"+
		"</tr>"+
		"</table>"+
		"</div>";
	$("body").append(alertStr);

	
	// Show alert && notify message on SetError or SetNotify


	//if(msg_error && msg_error.length > 1)
	if(eval("(typeof(msg_error) != 'undefined')") && msg_error.length > 1)
	{
		$('#alertMessage').modal();
		$('#modalContainer').draggable();

		var containerWidth = parseInt($('#modalContainer').width());
		var documentWidth = parseInt($(document).width());

		var left = parseInt(documentWidth/2 - containerWidth/2);
		$('#modalContainer').css('left', left);

		jQuery('#modalContainer a.modalCloseImg').pngfix();
	}


	$(".cols2 .column").each(function(i){
		if(i%2 == 0){
			$(".cols2 .column:eq("+i+"), .cols2 .column:eq("+(i+1)+")").wrapAll("<div class='columns datarow'></div>");
		}
	});


	$("#excol-gr").toggle(
		function(){ explode("collaps","gr"); return false; },
		function(){ explode("explode","gr"); return false; }
	);
	$("#excol-red").toggle(
		function(){ explode("collaps","red"); return false; },
		function(){ explode("explode","red"); return false; }
	);

	$(".steps_doors").toggle(
		function(){ $("#edit"+$(this).attr('lid')+"_step"+$(this).attr('step')).slideDown("fast"); },
		function(){ $("#edit"+$(this).attr('lid')+"_step"+$(this).attr('step')).slideUp("fast");  }
	);

	$(".show_edit_form").click(function(){
		var _p = "#lead"+$(this).attr('fid')+"_edit";
		$(this).text(($(_p).css('display') == "none") ? "- EDIT LEAD" : "+ EDIT LEAD");
		$(_p).toggle();
		return false;
	});

	$(".show_survey_form").click(function(){
		var _p = "#survey"+$(this).attr('fid')+"_form";
		$(this).text(($(_p).css('display') == "none") ? "Hide" : "View");
		$(_p).toggle();
		return false;
	});


});

function explode(type,id){
	if(type == 'collaps'){
		$.cookie("show-excol-"+id, 0);
		$("#excol-"+id+" span").text("Expand");
		$("#excol-"+id+" img").attr("src",'img/ico-expand-gr.png');
		$("#excol-"+id+"-div").slideUp("fast");
	}else{
		$.cookie("show-excol-"+id, 1);
		$("#excol-"+id+" span").text("Collapse");
		$("#excol-"+id+" img").attr("src",'img/ico-collaps-gr.png');
		$("#excol-"+id+"-div").slideDown("fast");
	}
}

function confirm(message, callback)
{
	$('#confirm').modal({
		close:false,
		containerId:'confirmModalContainer',
		onShow: function (dialog) {
			dialog.data.find('.message').append(message);
			dialog.data.find('.yes').click(function () {
				$.modal.close();

				//check callback
				if (eval("(typeof(callback) != 'function') && (typeof(callback) != 'object')")) return false;

				//link
				if (callback.href)
				{
					location.href = callback.href;
					return true;
				}

				//form
				if(1==1)
				{

				}

				//function
				if ($.isFunction(callback)) {
					callback.apply();
				}
			});
		}
	});
	$('#confirmModalContainer').draggable();

	return false;
}

function alert(message) {
	$('#alert').modal({
		onShow: function (dialog) {
			dialog.data.find('.alert_message').append("<b>"+message+"</b>");
		}
	});
	$('#modalContainer').draggable();

	var w = $('#modalContainer').width();
	w = parseInt(w);
	var left = ($(window).width()/2) - w/2;
	left = parseInt(left);
	//$('#modalContainer').css('position', 'fixed');
	$('#modalContainer').css('left', ''+left+'px');

	jQuery('#modalContainer a.modalCloseImg').pngfix();
	
}

function popupWindow(url,width,height,scroll) {
	var popUpWin = 0;

	if (scroll==null) scroll=false;

	if(popUpWin) {
		if(!popUpWin.closed) popUpWin.close();
	}

	var left = (screen.width/2) - width/2;
  	var top = (screen.height/2) - height/2;
  	var scrolling = (scroll)?'yes':'no';

	popUpWin = open(url, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrolling+',resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin.focus();
}

function alertObj(obj, showValues)
{
	showValues = (showValues) ? true : false;
	var buf = '';
	for (var prop in obj)
	{
		if (showValues) buf += ' ' + prop + '=' + obj[prop] + ', ';
		else buf += ' ' + prop + ' ';
	}
	alert(buf);
}
