function completeSubmit() {

	filltags();
	if( savePhoto()    ) {
		startUpload();
	}
	$('datafile').value = '' ;

}

function updatephotos() {

	new Ajax.Updater (
	'thumb_images' , '/media/browse/tagname' + "/" + $( 'im_tag' ).value ,
	{onComplete : function() {

		a = $( 'thumb_images' ).getElementsByClassName( 'ajax-img' ) ;
		for( i = 0 ; i < a.length ; i++ ) {
			Event.observe( a[ i ] , 'click' , webaprove.bindAsEventListener(this,    a[ i ].id ) )   ;
		}

	}
	} ,
	{ method : 'get' }

	) ;

}


function webaprove( a , b ) {

	add_and_aprove_photo(  b , $( 'ar_id' ).value  ) ;


}


function savePhoto(){
	/*new Ajax.Request('/photo/ajax-save/format/html' ,
	{
	method:'post',
	parameters: {
	ok: 10
	} ,
	onSuccess: function(transport){
	$('uploadFormDiv').innerHtml='ok';
	},
	onFailure: function(){
				window.location = "/"; 
			}
	});*/



	if( $( 'datafile' ).value.blank() )
	{
		alert( " Alegeți un fișier " ) ;
		return ;
	}
	a = $( 'agree' ).checked ;
	if( !a ) { alert( ' Trebuie sa fii de acord cu termenii și condițiile site-ului pentru a uploada ! ' )   ;  return  ;    }
	else
	$('upload-photo').submit() ;
	return 1 ;

}

function startUpload(){

	$( 'photo_result' ).innerHTML = "<img src=\"/images/preloader.gif\" />" ;

}

function aprove_photo( div_number ,  pid , rid , ph_name , ph_path  ) {
	new Ajax.Request('/default/recipe/ajax-aprove-photo/format/json/' ,
	{
		method:'post',
		parameters: {
			p_id : pid ,
			r_id : rid ,
			im_name: ph_name ,
			im_path: ph_path ,
			divnum: div_number
		} ,
		onSuccess: function(transport){
			response = transport.responseJSON;
			if( response.response )             {  complete_div( response.bname , response.sname )
			$( 'aprove_photos' + response.div_number ).innerHTML = "" ;
			}


		},
		onFailure: function(){
				window.location = "/"; 
			}
	});

}



function add_and_aprove_photo(  pid , rid  ) {
	new Ajax.Request('/default/recipe/ajax-add-and-aprove-photo/format/json/' ,
	{
		method:'post',
		parameters: {
			p_id : pid ,
			r_id : rid

		} ,
		onSuccess: function(transport){
			response = transport.responseJSON;
			if( response.response )             {
				complete_div( response.bname , response.sname )
			}


		},
		onFailure: function(){
				window.location = "/"; 
			}
	});

}


function disaprove_photo( div_number ,  pid , rid , div     ) {
	if( confirm( "sigur doriţi să renunţaţi la poza aceasta ?" ))
	new Ajax.Request('/default/recipe/ajax-disaprove-photo/format/json/' ,
	{
		method:'post',
		parameters: {
			p_id : pid ,
			r_id : rid ,
			div_number: div_number
		} ,
		onSuccess: function(transport) {
			response = transport.responseJSON;
			if( $( 'aprove_photos' + div_number ) )
			$( 'aprove_photos' + div_number ).innerHTML = "" ;
			else
			$( 'admin' + response.divnum ).innerHTML = "" ;
			if( $( 'li'  + pid ) ) { $( 'li' + pid ).innerHTML = "" ; image_nr-- ; }
			if( global_images ) {
				for( i = 0 ; i < global_images.length ; i++ )
				if( global_images[ i ][ "im_id" ] == pid ) {
					global_images[ i ] = null ;
					global_images = global_images.compact() ;
				}
			}
		},
		onFailure: function(){
				window.location = "/"; 
			}
	});

}



// for tags

function photoInitialize(){
	if( !document.getElementById( 'results' ) ) {
		if( $( 'result' ) ) {
			$( 'result' ).className = "autocomplete" ;
			$( 'result' ).id = "results" ;
		}
	}
	if ( $( "photoAutocomplete" ) )
	new Ajax.Autocompleter( $("photoAutocomplete"), $("results"), "/media/tags/format/html", {
	});

}


Event.observe( window,'load', photoInitialize )    ;


function filltags() {
	$( 'Photohiddentags' ).value = vectTag2str() ;
}



function isValid(parm,val) {
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}

function getSelectionId(        ) {
}



function isAlphanum( str ) {
	var numb = '0123456789';
	var lwr = 'abcdefghijklmnopqrstuvwxyz';
	var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	return isValid(str,lwr+upr+numb) ;

}



var photoTagListVect = new Array() ;
var photocontor=0;
function print_r ( a ) {
	for ( i = 0 ; i < a.length ; i++ ) alert ( a[i] ) ;
}
function realSplice ( a , i ) {
	var b = new Array  ;
	for ( j = 0 ; j < i ; j++ ) b[j] = a[j] ;
	for ( j = i+1 ; j < a.length ; j++ ) b[j-1] = a[j] ;
	return b ;
}


function vectTag2str() {
	str = "" ;
	for ( j = 0 ; j < photoTagListVect.length ; j++ ) { str+= photoTagListVect[j] + ";"  ; } return str ;
}


function existsTag( str ) {
	for ( i=0; i<photoTagListVect.length ; i++  ) if ( photoTagListVect[ i ] == str ) return 1 ;
	return 0 ;

}


function photoaddTagsToList() {
	var str = $("photoAutocomplete").value ;
	str = str.replace(/^\s*|\s*$/g,'');
	if  ( existsTag( str ) == 0 &&  str && isAlphanum( str )  ) {  ;
	photoTagListVect[ photocontor++ ] = str ;
	photoMakeDivList() ;            }
}

function photoMakeDivList() {
	var str="" ;
	for ( i = 0 ; i < photoTagListVect.length ; i++ )
	str+=photoTagListVect[i]+"<img onclick='removeTag(\""+photoTagListVect[i]+"\")' src='/images/del.gif' >"+"<br>";
	$( "photo_tagListdiv").innerHTML = str ;
}



function removeTag( str ) {
	for ( i = 0 ; i < photoTagListVect.length ; i++ ) if( photoTagListVect[ i ] == str ) {
		photoTagListVect=realSplice(photoTagListVect, i ) ; photocontor--  }
		photoMakeDivList() ;

}






// end
