/* cms ajax actions */

function generateRaport() {
	var c = getValue( 'client' );
	var s = getValue( 'status' );
	var dateFrom = getValue( 'calendar_field_from' );
	var dateTo = getValue( 'calendar_field_to' );
	var regex = /(\d){4}\/(\d){2}\/(\d){2}/;
	if( !regex.test( dateFrom ) || !regex.test( dateTo ) ) {
		alert( 'Proszę wprowadzić daty z kalendarza' );
	} else {
		var ajax = new myAjax();
		ajax.action = 'generateRaport';
		ajax.post( 'c='+c+'&df='+dateFrom+'&dt='+dateTo+'&s='+s, 'responseRaport' );
	}
}

function getCompare( source, target, id ) {
	if( trim( getValue( source ) ) != '' ) {
		var ajax = new myAjax();
		ajax.action = 'getCompare';
		ajax.post( 'c_n='+getValue( source )+'&id='+id, target );
	}
}

function togglePublish( id, c_n ) {
	var ajax = new myAjax();
	ajax.action = 'togglePublish';
	ajax.post( 'id='+id+'&c_n='+c_n, 'publishIcon_'+id.toString()+'_'+c_n );
}

function toggleConfirm( id, c_n ) {
	var ajax = new myAjax();
	ajax.action = 'toggleConfirm';
	ajax.post( 'id='+id+'&c_n='+c_n, 'confirmIcon_'+id.toString()+'_'+c_n );
}

function updateAjaxIcon( target, sel ) {
	var s = getValue( sel );
	
	//alert( s );
	
	if( document.buffer_icon == undefined ) { document.buffer_icon = s; }
	if( document.buffer_icon != s ) {
		document.buffer_icon = s;
		var ajax = new myAjax();
		ajax.action = 'iconPreview';
		ajax.post( 'icon='+s, target );
	}
	setTimeout( 'updateAjaxIcon( \''+target+'\', \''+sel+'\' );', 1500 );
}

function markRelation( cn, cid, pc, pid ) {
	get( 'icon_'+cid ).innerHTML = '';
	var ajax = new myAjax();
	ajax.action = 'markRelation';
	ajax.post( 'cn='+cn+'&cid='+cid+'&pc='+pc+'&pid='+pid, 'icon_'+cid );
}

function unmarkRelation( cn, cid, pc, pid ) {
	get( 'icon_'+cid ).innerHTML = '';
	var ajax = new myAjax();
	ajax.action = 'unmarkRelation';
	ajax.post( 'cn='+cn+'&cid='+cid+'&pc='+pc+'&pid='+pid, 'icon_'+cid );	
}

function toggleCheckboxList( id, pc, pid ) {
	var obj = get( id ).value;
	get( 'list' ).innerHTML = '';
	if( obj != '' ) {
		var ajax = new myAjax();
		ajax.action = 'toggleCheckboxList';
		ajax.post( 'class='+obj+'&parent_class='+pc+'&parent_id='+pid, 'list' );
	}
}

function niceUrl( str, target ) {
	var ajax = new myAjax();
	var string = getValue( str );
	//string = string.split( '&' ).join( '&amp;' );
	string = string.split( '&' ).join( '_amp_' );
	ajax.action = 'nice_url';
	ajax.post( 'string='+string );
	ajax.onLoad = function() {
		var t = get( target );
		t.value = this.response;
	}
}
