/*
 * Used to set properties of and create an onclick functionality using the
 * ui.dialog library of jQuery
 * Used in conjunction with the Publisher PHP class to edit standards
 * @Author Rhonda Kammer
**/

//NOTE: these globals are bad style and will be changed as soon as a (timely) technique is found to replace them rlk
var orig_element_id = "";
var orig_text = "";
var url_sid  = "";
var script_uri = "";
var web_str_pn = "";
var web_str_id = "";
var web_str_ts = "";
var edit_object = "";
var edit_path   = "";
var edit_data   = new Array();
var xpos = "";
var ypos = "";

/**
*
* This function is for the FIRST CLICK on an element
* which has been outfitted (through php class publisher)
* Any clicks on the callout are processed in process_form_click below
*/
function edit_standard_callout( url, element_id ){		

	// Clean textarea for new input
    $("#esct_edit_form_field").empty();
    $("#esct_edit_alert").empty();
    
    // store original element id so other functions can use it
	orig_element_id = element_id;

	// parse session and web_string ids
	pub_task =  get_url_variable("pub_task", url);
	url_sid = get_url_variable("session_id", url);
	web_str_id = get_url_variable("web_string_id", url);
	web_str_ts = get_url_variable("web_string_ts", url);
	web_str_pn = get_url_variable("property_name", url);
	
	// parse script uri
	script_uri = get_script_uri( url );
	edit_path = script_uri + "?";		
	
	// if adding standard change submit button to Add
	if( pub_task == "esct_edit_create" ){		
		url_esn = get_url_variable("parent_esn", url);
		$("#esct_edit_form_field").val("Title");				
		$("#esct_edit_save").val("Add");		
	}else{
		// set button back to save for recursive back and forth between add/save
		$("#esct_edit_save").val("Save");
		
		// fetch contents from database for editing
		var p = {};
    	p[name] = "id";
        p['id'] = element_id;
                
		//alert( "edit standard: " + url );
		$.post(url, p, function( response ) {
			
			var jsonData = new Array();						   	   	 
   			json_parse(response, function (key, value){				
						
				if(typeof(value) == "string"){ 
					jsonData[key] = value; 
				} 	   	        	    		
			});
   			
			//alert( "Res: " + jsonData[0] + "TEXT--" + jsonData[1] + "--" );	
			if( jsonData[0] == "text available" ){				
				orig_text = jsonData[1];
						
				// tranfer database content to callout textarea
    			$("#esct_edit_form_field").val( orig_text );					
			}
			//alert(orig_text);
	
    	});//post call
	
	}//pub_task edit_create

    // highlight field we are editing same as emporis class form_text_120
    edit_object = document.getElementById( element_id );
    edit_object.style.fontFamily = 'arial';
    edit_object.style.borderBottom = '1px solid #183961';
    edit_object.style.borderRight = '1px solid #183961';
    edit_object.style.borderLeft = '1px solid #819DC0';
    edit_object.style.borderTop = '1px solid #819DC0';
 	edit_object.style.backgroundColor = '#CCD2DA';

	/************************   Make callout container   ***************************/
	$("#inner_container").dialog({
				position: [806,340],
    			width:  375,
    			height: 500,
    			modal: true,
				close: function(event, ui) { unhighlight_edit_field(); }
	});
	$("#esct_edit_form_field").focus();

}//fun esc


function process_form_click( element_id ){

	if( element_id == "esct_edit_save" ){
		action = $("#esct_edit_save").val();
					
		
		/******************** ADD CLICKED ********************/
		if( action == "Add" ){

			standard_title = $("#esct_edit_form_field").val();
			
			//build post vars
			var p = {};
	    	p[name] = "id";
	        p['id'] = element_id;
	        p[name] = "standard_title";
	    	p['standard_title'] = standard_title;
	    	
	    	url = edit_path + "pub_task=esct_edit_create&session_id=" + url_sid + "&parent_esn=" + url_esn;
	    	//alert("Add clicked: " + url );
	    	
	    	$.post(url, p, function( response ) {
	    	   
	    	    var response = response.split(":");
	    	    ajx_res = response[0];
	    	    new_esn = response[1];
	    	    new_nav = response[2];
	    	    user_lang = response[3];
	    	    
	    		//alert( "Response: " + response + " splitted resp: " + response[0] + " esn: " + new_esn + " nav: " + new_nav );

	    		if( response[0] == "Title empty" ){	    			
	    			alert_text = 'Title must not be empty.';
					$("#esct_edit_alert").html( alert_text );
	    		}
	    		if( response[0] == "Added"){
	    			$("#inner_container").dialog( "destroy" );
	    			
	    			//refresh page	
	    			new_url = edit_path + "nav=" + new_nav + "&lng=" + user_lang + "&esn=" + new_esn;
					window.location.href = new_url;						    			
	    		}
	    		if( response[0] == "Not added"){
	    			alert_text = 'Standard was not added. Please try again.';
					$("#esct_edit_alert").html( alert_text );
	    		}
    		
	    	});//post call
	    	
	    			
		
		/******************** SAVE CLICKED ********************/
		}else{
			   	    
			text_to_save = $("#esct_edit_form_field").val();			
			//prepare element id for ajax processing
			var p = {};
	    	p[name] = "standards_text";
	    	p['standards_text'] = text_to_save;
	    	p[name] = "id";
	        p['id'] = element_id;
	        p[name] = "web_string_ts";
	        p['web_string_ts'] = web_str_ts;
	        p[name] = "property_name";
	        p['property_name'] = web_str_pn;			
	        
	        //NOTE: this chained get query will be converted to pure post query using var p above when time allows
			url =   edit_path + "pub_task=esct_edit_save&session_id=" + url_sid + "&web_string_id=" + web_str_id;
			
			/********************  GET AJAX RESPONSE  ********************/
			//alert("Save clicked: " + url );
			//NOTE: better to use $.ajax(), will convert soon as time is available, rlk
			$.post(url, p, function( response ) {
				
				//alert(response);
				
				var jsonData = new Array();						   	   	 
	   			json_parse(response, function (key, value){				
					//alert( "value: " + typeof(value) );			
					if(typeof(value) == "string"){ 
						jsonData[key] = value; 
					} 	   	        	    		
				});//json parse
				//data available here:
				//alert( "Res: " + jsonData[1]);
				$("#esct_edit_alert").empty();
				if( jsonData[0] == "saved" ){				
					//update field in standards_show.tpl cannot use jQuery due to : in our ids
	    			updated_text = document.getElementById(orig_element_id);
	        		updated_text.innerHTML = jsonData[1];	        		
	        		//alert( "Text saved!" );
				}else{		
					//alert( "Text not saved! " );				
					alert_text  = '<b>Text not saved! </b><br>';
					alert_text += 'Most likely, this text has been changed by someone else while you were editing.  ';
					alert_text += 'Copy and save your changes on your pc if you wish.  ';
					alert_text += 'Then refresh your browser to see the new changes made to the text by the other person.  ';
					alert_text += 'After this you may try again to edit.';
					$("#esct_edit_alert").html( alert_text );
				}//json data saved	
	    	});//post call
			//$("#inner_container").dialog( "destroy" );
			unhighlight_edit_field();
	    }//action is add	    
	    
	    
	}//save click
	
    /******************** CANCEL CLICKED ********************/
	if( element_id == "esct_edit_cancel" ){
		//alert("cancel clicked.");
		$("#inner_container").dialog( "destroy" );
	}
	
	/******************** UNDO CLICKED ********************/
	if( element_id == "esct_edit_undo" ){
		
		//alert("undo clicked: " + orig_text );
		$("#esct_edit_form_field").val(orig_text);
		
	}
	
}//fun pfc

function unhighlight_edit_field(){
		var orig_id = '#' + orig_element_id;
		$(orig_id).css({ "borderBottom":"0px solid #183961" });
        $(orig_id).css({ "borderRight":"0px solid #183961" });
        $(orig_id).css({ "borderLeft":"0px solid #819DC0" });
        $(orig_id).css({ "borderTop":"0px solid #819DC0" });
        $(orig_id).css({ "backgroundColor":"#FFFFFF" });                
		$("#inner_container").dialog( "destroy" );
	}
