// for the window
//done window

// Bucur Marius Ovidiu - the script for the shopping list ....

var fatiContAcumPentruSms = '<a title="Cont nou" href="/comunitate/contnou">Fă-ţi un cont acum!</a>';
var adaugaIngredienteInListaTa = "Eşti în grabă? Nu ai un pix la îndemână? Trimite-ţi un SMS! Simplu.GRATUIT. " ;
adaugaIngredienteInListaTa+= "Da click pe ingredientele dorite si adauga-le la lista de cumparaturi.";
adaugaIngredienteInListaTa+= "<div id=\"sms_sending_button\"></div>";
var vect = new Array();

Event.observe( window , 'load' , last_history )       ;
Event.observe( window , 'load' , main_id )      ;
Event.observe( window , 'load' , checkrights) ;
//Event.observe( window , 'load' , initShoppingList); // in checkrights

function showRecaptcha(element, submitButton, themeName) {
	Recaptcha.create("6LeATgMAAAAAADIS2M5x1ilFwgrUD9zm3_qVqgnv", element, {
		theme: 'custom',
		tabindex: 0,
		callback: Recaptcha.focus_response_field
	});
	//hideSubmitButtons();
	//document.getElementById(submitButton).style.visibility = "visible";
}

function hideSubmitButtons() {
	document.getElementById('submit_button_1').style.visibility = "hidden";
	//document.getElementById('submit_button_2').style.visibility = "hidden";
	//document.getElementById('submit_button_3').style.visibility = "hidden";
}

function destroyRecaptchaWidget() {
	hideSubmitButtons();
	Recaptcha.destroy();
}

function validationMessage(){
	$('messages_text').innerHTML="Poza va fi validata in curand" ; $('messages').show();
}

function sendByEmail() {
	//$('').innerHTML = ai;
	new Ajax.Request('/recipe/send-by-email/format/html', {
		asynchronous: true,
		method: 'post',
		parameters:{
		},
		onSuccess: function(transport) {
			var str=transport.responseText;
			createWindow( str, 300, 400, "Trimite pe mail" );
			showRecaptcha('dynamic_recaptcha_1', 'submit_button_1', 'red');
		}

	});
	//document.write('<script src="http://api.recaptcha.net/js/recaptcha.js"></s'+'cript>');
	//$('').innerHTML = "";
}


function ajax_recipe_send_email() {
	//$('').innerHTML = ai;
	recipe_contents = $('recipe_content').innerHTML.stripTags().strip().substr(0,$('recipe_content').innerHTML.stripTags().strip().substr(0,500).lastIndexOf(" ")) + "[..]";
	recipe_title = $('recipe_title').value;
	new Ajax.Request('/recipe/ajax-send-recipe-email/format/html', {
		asynchronous: true,
		method: 'post',
		parameters:{
			email_from:$('email_from').value,
			email_to:$('email_to').value,
			page:$('current_page').value,
			recipe_contents:recipe_contents,
			mesaj:$('recipe_email_mesaj').value,
			recipe_title:recipe_title,
			recaptcha_response_field:$('recaptcha_response_field')?$('recaptcha_response_field').value:"",
			recaptcha_challenge_field:$('recaptcha_challenge_field')?$('recaptcha_challenge_field').value:""
		},
		onSuccess: function(transport) {
			if(transport.responseText!='Cod incorect! Reincercati!'){
				$('dialog' + (idx-1) + '_content').innerHTML = transport.responseText;
				setTimeout( function(){Windows.closeAll()}, 1500 );
			}
			else{
				$('dynamic_recaptcha_1').innerHTML=transport.responseText;
				Recaptcha.reload();
			}
		}

	});
	//$('').innerHTML = "";
}

var i_a_u = 0;

function initShoppingList(s) {
	i_a_u = s;
	$( 'shopping_list_ul' ).innerHTML = createConditionSL(s) ;
	if(!s) $('sms_sending_button').innerHTML=fatiContAcumPentruSms;
}

var drepturi_user;

function getAllPhotos() {
	my_s = "<center><div style=\"overflow:scroll;height:200px;\">" ;
	rid = $( 'ar_id' ).value ;
	for( i = 0 ; i < global_images.length ; i++ ) {
		pid = global_images[ i ][ 'im_id' ] ;
		my_s +="<div id = \"admin" + i + "\" ><img src=\"/"+global_images[ i ][ 'im_path' ] + "s2" + global_images[ i ][ 'im_name' ] + "\" />" ;
		my_s += "<img src=\"/images/del.gif\" onclick = \"disaprove_photo("+i+","+pid+"," + rid +");\" />";
		my_s += "</div>";
	}
	my_s += "</div><center>" ;
	createWindow( my_s , 200 , 200  )
}

function checkrights() {
	if( $$( ".add_to_friend" )[ 0 ] ) {
		Event.observe( $$( ".add_to_friend" )[ 0 ] , 'click' , add_to_friend ) ;
		new Ajax.Request('/default/recipe/ajax-check-rights/format/json' ,
		{
			method:'post',
			parameters: {
				r_id : $( 'ar_id' ).value ,
				div_number: ""
			} ,
			onSuccess: function(response) {
				drepturi_user = response.responseJSON.drepturi;
				switch( response.responseJSON.drepturi ) {
					case "00" : break ;
					case "01" : $( 'is_user' ).show() ; break ;
					case "11" : $( 'is_user' ).show() ; $( 'is_owner' ).show() ; break ;
				}
			},
			onFailure: function(){
				window.location = "/";
			}
		}) ;
	}
}

function main_id() {
	if( $( 'main_id' ) )
	$( 'main_id' ).value = $( 'page_main_id' ).value ;
}

function last_history() {
	// visit recipe.
	if( !$( 'ar_id' ) ) return ;
	new Ajax.Request( '/recipe/ajax-visit-recipe/format/html/rid/' + $( 'ar_id' ).value ,  {
		onSuccess: function(transport){
		},
		onFailure: function() {   }
	});

	new Ajax.Request( '/user/ajax-last-recipe/format/json/id/' + $( 'ar_id' ).value ,  {
		onSuccess: function(transport){
		},
		onFailure: function() {   }
	});

}

function print_recipe(str) {
	str="<iframe id=\"aaa\" style=\"border:none;width:100%;height:100%;\" src='"+str+"' />";
	printRecipeWindow( 400,450,str);
}

function createSemicolonList() {
	window_str = ""  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( i != 0 )
		window_str += ";" ;
		window_str += vect[ i ] ;
		if( cookieVal ) window_str += "-" + cookieVal ;

	}

	return window_str ;

}

function createSemicolonDetails() {
	window_str = ""  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		aux = readCookie( vect[ i ] ) ;
		if( !aux ) aux = "" ;
		window_str += ";" + aux + "" ;

	}

	return window_str ;

}




var ai = "<img src = \"/images/ajax-loader.gif\" />"     ;

function add_to_fav() {
	$( 'ajax-loader' ).innerHTML = ai ;
	new Ajax.Request( '/recipe/add-to-fav/format/json/rid/' + $( 'ar_id' ).value ,{
		onSuccess: function(transport){
			$( 'ajax-loader' ).innerHTML = transport.responseJSON.added ;
		},

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

function add_to_friend() {
	var i = $$( '.add_to_friend' )[0].id.substr(1) ;
	$( 'friend' ).innerHTML = ai ;
	new Ajax.Request( '/user/ajax-request-friend/format/json/autor/' + $( 'autor_page' ).value + '/' + 'uid/' + i  ,{
		onSuccess: function(transport){
			$( 'friend' ).innerHTML = transport.responseJSON.added ;
		},
		onFailure: function(){
			window.location = "/";
		}
	});
}

function createS_L() {

	window_str = "<li>" + adaugaIngredienteInListaTa + "</li>"  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + " <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>    </li>" ;

	}
	if(vect.length&&i_a_u)
	window_str += createSmsButton() ;

	else {
		window_str += "<li><small>Nu ai niciun ingredient adăugat!</small></li>" + createFakeSmsButton();
	}

	return window_str ;

	//-----------------------------------------------------------------

}

function createFakeSmsButton() {
	str = "<li>" ;
	str += "<br clear = \"all\"/>";
	str += "<input style=\"background:#b2e35a;border:2px solid #b2e35a\" class=\"formated_input\" id = \"send_sms_now\" value = \" Trimite SMS \" type = \"button\" />" ;
	str += '<input onclick="window.location=\'/ajutor#sms\'" type="button" value="?"style=';
	str += '"color:white;font-weight:bold;margin-left:10px;background:#992512;border:3px #992512 solid"id="ineedhlp">';
	str += "<div id = \"cart_messages\" ></div></li>" ;
	return str ;
}

function createConditionSL() {

	window_str = "<li>" + adaugaIngredienteInListaTa + "</li>"  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + " <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>    </li>" ;

	}
	if(vect.length&&i_a_u)
	window_str += createSmsButton() ;

	else {
		window_str += "<li><small>Nu ai niciun ingredient adăugat!</small></li>" + createFakeSmsButton();
	}

	return window_str ;

	//-----------------------------------------------------------------

}

function createSmsButton() {
	str = "<li>" ;
	str += "<br clear = \"all\"/>";
	str += "<input class=\"formated_input\" id = \"send_sms_now\" value = \" Trimite SMS \" type = \"button\" onclick = \"trySendSms();\" />" ;
	str += '<input onclick="window.location=\'/ajutor#sms\'" type="button" value="?"style="color:white;font-weight:bold;margin-left:10px;background:#992512;border:3px #992512 solid"id="ineedhlp">'
	str += "<div id = \"cart_messages\" ></div></li>" ;
	return str ;
}

function check_ph( a ) {
	for(i=0;i<a.length;i++)
	if( parseInt(a.charAt(i))!=a.charAt(i)){
		$('nc_phone').value="format gresit";
		setTimeout( function(){ $('nc_phone').value=""; } , 1000 );
		return -1 ;
	}
	if(a.length != 10) {
		$('nc_phone').value="format gresit";
		setTimeout( function(){ $('nc_phone').value=""; } , 1000 );
		return -1 ;
	}
	else return 1 ;

}

function confirm_button_nc(tre){
	var a = 0 ;
	$('nc_confirm_result').innerHTML=ai;
	if( check_ph( $( 'nc_phone' ).value ) < 0 ) { $('nc_confirm_result').innerHTML=""; return false ; }
	else {
		new Ajax.Request('/user/confirm-mobile/format/json/', {
			asynchronous: false,
			method: 'post',
			parameters:{
				mobile : $('nc_phone').value,
				tre : tre
			},
			onSuccess: function(transport) {
				var myJson=transport.responseJSON;
				if( myJson.response ) {
					$('nc_confirm_result').innerHTML = transport.responseJSON.response ;
					//$('dialog'+(idx-1)+'_content').style.height="172px";
				}
				else {
					//$('dialog'+(idx-1)+'_content').style.height="172px";
					$('nc_confirm').innerHTML=nc_old_phone(myJson.u_phone);
				}
				if(tre) {
					if( myJson.u_phone ){
						general_phone=myJson.u_phone;
						$('nc_confirm').innerHTML=nc_old_phone(myJson.u_phone);
					}
					else {
						if( myJson.response )
						$('nc_confirm_result').innerHTML = transport.responseJSON.response ;
					}
				}
			}

		});

	}
}

function nc_new_phone(){
	var str = "<div id=\"nc_confirm\">Introduceti numarul de telefon:<br/>";
	str+="<input type=\"text\" id=\"nc_phone\" style=\"color:black;width:81px;\" /><br/><br/>";
	str+="<input type=\"button\" onclick=\"confirm_button_nc(1);\" id=\"confirm_button\" value=\"trimite\" />";
	str+="<div id=\"nc_confirm_result\"></div>";
	return str;
}

function confirm_code_nc() {
	$('nc_confirm_result').innerHTML=ai;
	new Ajax.Request('/user/confirm-code/format/json/', {
		asynchronous: false,
		method: 'post',
		parameters:{
			mobile : $( 'send_code' ).value
		},
		onSuccess: function(transport) {
			myJson=transport.responseJSON;
			if( myJson.corect ) {
				$('nc_confirm').innerHTML = myJson.response ;
				$('cart_messages').innerHTML="";
				return;
			}
			$('nc_confirm_result').innerHTML = myJson.response ;
			//$('dialog'+(idx-1)+'_content').style.height="172px";
		}
	});
}

function nc_choose_another() {
	$( 'nc_confirm' ).innerHTML = nc_new_phone();
}


function nc_old_phone(phone){
	var str = "<form style=\"padding:10px\"><div id=\"nc_confirm\"><h2>Confirmă-ţi numărul:</h2>";
	str+="Verifică numărul de telefon şi apasă butonul trimite. Introdu în căsuţă codul primit prin SMS.";
	str+="<input disabled type=\"text\" value=\""+phone+"\"id=\"nc_phone\"style=\"color:black;width:70px;\" />";
	str+="<br/><input type=\"button\" onclick=\"confirm_button_nc(0);return false;\" value=\" >> SMS de confirmare\"/>";
	str+="<br/><br/>Codul de confirmare: <input type=\"text\"id=\"send_code\"style=\"color:black;width:40px;\" />";
	str+="<br/><input type=\"button\" onclick=\"confirm_code_nc();\"id=\"confirm_button\"value=\"Confirmă\"/>"
	str+="<br/> ";
	str+="<a href=\"javascript:nc_choose_another();\">Schimbă numărul de telefon!</a>";
	str+="<div id=\"nc_confirm_result\" style=\"color:#FF0000;font-weight:bold;\"></div></form>";
	return str;
}



var general_phone = 0;


function nc_confirm(phone) {
	if( general_phone ) {
		createWindow(nc_old_phone(general_phone),250,250,"Confirmaţi-vă numărul");
		return;
	}
	if(phone) {
		createWindow(nc_old_phone(phone),250,250,"Confirmaţi-vă numărul");
	}
	else {
		createWindow(nc_bew_phone(),250,250,"Confirmaţi-vă numărul");
	}
}

function trySendSms() {
	glue = createSemicolonList()     ;
	alt  = createSemicolonDetails()  ;
	new Ajax.Request( '/user/send-sms/format/json' ,{
		method: 'post',
		parameters:{
			list : glue
		},
		onSuccess: function(transport){
			var res = transport.responseJSON;
			$( 'cart_messages' ).innerHTML = transport.responseJSON.response ;
			if( res.result == "-3" ) {
				if(res.confirm_tries){ // daca a mai trimis un mesaj de confirmare
					var phone=res.u_phone;
				}
				else {
					var phone=null;
				}
				var f = document.createElement('div');
				f.id="not_confirmed";var nc_s="";nc_s+="<a  href=\"javascript:nc_confirm('"+phone+"');\" >";
				nc_s+="Confirmati-va numarul de telefon acum!</a>";
				f.innerHTML=nc_s;
				$('cart_messages').appendChild(f);
			}
		}
	});

}



function eraseIngredIn( str ) {

	eraseCookie( str )   ;
	aux = str + "," ;
	b = readCookie( "ingredCart" )   ;
	modiied = b.replace( aux , "" ) ;
	if( b != modiied )
	b = modiied ;
	else
	b = b.replace( str , "" ) ;
	if( b[ b.length - 1 ] == "," ) b = b.substr( 0 , b.length - 1 ) ;
	createCookie( "ingredCart" , b ) ;
	createS() ;

}


//---------------------------------------------------------


function createS() {
	window_str = "<li>"+adaugaIngredienteInListaTa+"</li>"  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + "  <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>   </li>" ;

	}
	if(vect.length&&i_a_u)
	window_str += createSmsButton() ;

	else {
		window_str += "<li><small>Nu ai niciun ingredient adăugat!</small></li>" + createFakeSmsButton();
	}

	$( 'shopping_list_ul' ).innerHTML = window_str ;
	if(!i_a_u) {
		$('sms_sending_button').innerHTML=fatiContAcumPentruSms;
	}





}

//--------------------------------------------------------


function addToCart( ing , id_of_ing ) {

	justacookie = readCookie( 'ingredCart' );
	if( vect.indexOf( ing ) == -1 ) {
		if( !justacookie ) justacookie = ing ;
		else
		justacookie += "," + ing   ;

		str = "<ul><li>" ;
		str+= " <h4>Adaugă ingredientul <h3>"   +     ing     + "</h3> </h4> <p> " ;
		button = ' <input class = \"ingredient_content\" type = "text" onclick = " this.value = \'\' ; "  value = "Introduceti o descriere" id = "input' + ing + '" onkeyup = " createCookie( \'' + ing + '\' , this.value , 1 ) ; " >   <input type = "button" value = "ok" onclick = "	createCookie( \'ingredCart\' , \'' + justacookie + '\' , 1 );createS();Windows.close( \'dialog' + idx + '\' , event ) ;   " /> ' ;
		//button += '<input type = "button" value = "nu" onclick = "createS();Windows.close( \'dialog' + idx + '\' , event ) ;     "   /> </p>      </div>     '   ;
		str += button  ;
		str+="</li></ul>" ;
	}

	else str = "  <ul> <li> <h4>  Ingredientul " + ing   + " exista deja." + '  <input style = " margin-left : 3em ; " type = "button" value = "ok"onclick = "Windows.close( \'dialog' + idx + '\',event);createS();"</h4>'+'</li></ul>' ;



	str += "<div style = ' background : white ; ' > <ul> "     ;
	window_str = " <ul> " ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal )
		cookieVal = "Introduceti o descriere"         ;

		str+= "<li>" + vect[ i ] + "</li> <input class = \"ingredient_content\" type = \"text\" id = \"input" + vect[ i ] +   "\" " + " value = \"" + cookieVal + "\"  onkeyup = \" createCookie( '"+ vect[ i ] +"' , this.value , 1 ) ; \" " ;
		window_str += "<li>" + vect[ i ] + "</li>" ;

	}

	str+= " </ul>  " ;

	window_str += "</ul> " ;

	if( vect.indexOf( ing ) == -1 ) {
		if( !myCookie ) myCookie = ing ;
		else
		myCookie += "," + ing   ;



		//document.write( str ) ;

	}


	// ( 1 )  derizoriu
	//createLi = createS() ;
	//if( createLi )
	//$( 'shopping_list_ul' ).innerHTML = createS()   ;
	// ( 2 ) se poate sterge ( 1 ) - ( 2 )
	//str += createSmsButton() ;
	showWin ( pos_x , pos_y , str , 350 , 300 ) ;

	ingred_content = $$( ".ingredient_content" ) ;
	for( i = 0 ; i < ingred_content.length ; i++ )
	if(ingred_content[i].value == "Introduceti o descriere" )
	ingred_content[i].onfocus = function(){this.value='';this.style.color="black";};
	else {
		ingred_content[i].onfocus=function(){this.value='';};ingred_content[i].style.color = "black";
	}
}


