﻿/**
 * Internal Portal functions for affiliation links
 **/

 /* global variables */
 current_site_name = '';
 current_url = null;
 changed = false;
 current_site_Data = null;
 current_url_Data = null;
 r = null;
 var dataTabs;
 
 
function autoState() {
 document.addEvent('domready', function() {
 	//var searchfor = document.id('search_site')  + '|' + document.id('zip_state');
	
	var searchfor = document.id('search_site');
	//var zip_state = form1.zip_state.options[form1.zip_state.selectedIndex].value;//document.id('zip_state');
	//var zip_state = new Array(3);
	
	
	new Autocompleter.Request.HTML(searchfor, 'includes/searchDomains.php', {
		postData: {
			'zState': seeState()
		},
		multiple: false,
		minLength: 3,
		maxChoices:50,
		markQuery: true,
		filterSubset: true,
		injectChoice: function(choice) {
			// choice is one <li> element
			var text = choice.getLast();
			// the first element in this <li> is the <span> with the text
			var value = text.get('text');
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}

	});

	addGeneralEvents();
});
}

function seeState() {
	Szip_state = document.id('zip_state').getSelected(); 
	var zip_st = Szip_state[0];
//console.log(zip_st.value);
	return zip_st.value;
}
/* Events */
var addGeneralEvents = function() {
	//button Go in search
//	document.id('go_search').addEvent('click', function(){ getDataSite(); });
	//enter on box
	document.id('search_site').addEvent('keydown', function(event) { if(event.key == 'enter' && this.get('value')!='') {	getDataSite(); } } );
};

/* AJAX */
var getDataSite = function(site_name) {
	site_name = (site_name)? site_name :  document.id('search_site').get('value');
	if(!site_name) return false;
		document.id('search_site').set('value', site_name);
		document.id('current_title').set('html', site_name);

	changed = false;

	showLoading();
	var jsonRequest = new Request({
		url: "includes/getSite.php",
		method: 'post',
		data: {
			'search_site': site_name
		},
		onSuccess: function(response){
			data = JSON.decode(response);
			current_site_name = site_name;
			resizeBody();
			if(data) { current_site_Data = $H(data); }
			insertTableRow(data, site_name);
			hideLoading();
		}
	}).send();
};

var getData = function(data_id, type, target) {
	showLoading();
	site_id = current_site_Data[data_id].id;
	url_id = current_site_Data[data_id].idsite_url;
	if(site_id=='' || url_id=='') return false;
	current_url = data_id;
	switch(type) {
		case "all":
			value_type = "urls_id_url="+current_site_Data[current_url].urls_id_url+"&url_encode_type="+current_site_Data[current_url].url_encode_type;
			break;
	}
	var jsonRequest = new Request({
		url: "includes/getData_sites.php",
		method: 'post',
		data: {
			'site_id': site_id,
			'url_id': url_id,
			'type': type,
			'value': value_type
		},
		onSuccess: function(response){
			data = JSON.decode(response);
			if(data) { current_url_Data = $H(data); }
			else { 
				alert('error getting data\ntry again'); 
				hideLoading();
				return false; 
			}
			if(type == 'all') createFormURL(data);
			else insertDataURL(data, target);
			hideLoading();
		}
	}).send();
};

var updateData = function(id, type, sentvalue, target) {
	r = null;
	changed = true;
	showLoadingUploading(target);
	var jsonRequest = new Request({
		url: "includes/updateData_sites.php",
		method: 'post',
		evalResponse: true,
		data: {
			'id': id,
			'type': type,
			'value': sentvalue
		},
		onFailure: function() {
			alert('Error!!\nan error ocurred, try again');
			hideLoadingUploading(target);
			return false;
		},
		onSuccess: function(response){
			if(r) {
				if(type == 'url_type_update' || type == 'url_type_insert' ) {
					current_site_Data[current_url].urls_id_url = id_url;
					editURL(current_url);
					getDataSite(current_site_name);
					insertURLactive(target, id_url);
				} else if(type == "new_url_site") {
					getDataSite(current_site_name);
				} else if(type == 'url_encode_update') {
					insertURLEncodeActive(target, url_encode_type)
				} else if(type == 'urlvalue_insert') {
					insertUrlValues(target.getPrevious(), 'before', idvalues, name, value);
					document.id('new_url_val_form').fade('in');
					document.search('#url_tabs_nav li.selected a').fireEvent('click');
				} else if(type == 'affiliation_default') {
					getDataSite(current_site_name);
				} else if(type == 'method_redirect') {
					getDataSite(current_site_name);
				} else {
					getDataSite(current_site_name);
				}
			} else {
				alert('error updating data\ntry again'); 
			}
			hideLoadingUploading(target);
		}
	}).send();
};

/* insert data */
var insertTableRow = function(data,site_name) {
	if(data) { insertRowSite($H(data)); }
	else { insertRowNewSite(site_name); }
};

var insertRowNewSite = function(site_name) {
	var tbody = document.id('dataManaged').getElement('tbody');
	tbody.empty();

	newrow = new Element('tr', {'id': 'site_0', 'class': 'new'}).inject(tbody);
		//td: Site IDs
		td = new Element('td', {'class': 'c_id', 'html':'<p class="cID">0</p><p class="site_urlID">0</p>'}).inject(newrow);
		// td: Site Name
		td = new Element('td', {'class': 'Site_name'}).inject(newrow);
			site_n = new Element('span', {'id':'site_name_0', 'class':'editable edit_text'}).inject(td);
				site = new Element('a', {
					'html': site_name,
					'href': 'http://'+site_name,
					'events': {
						'click': function(e) {e.stop(); window.open('http://'+site_name); return false; }
					}
				}).inject(site_n);
		// td: Site Url
		td = new Element('td', {'colspan':'3','class': 'Site_url', 'html':'<p>This site does not exist in the DB. Do you want to insert it?</p>'}).inject(newrow);
			buttons = new Element('p').inject(td);
				button = new Element('a', {
					'href' : 'javascript:;;',
					'class': 'button confirm',
					'html':'create',
					'events': {
						'click': function(e) {
							e.stop();
								updateData(0, 'new_site', site_name, td);
							return false;
						}
					}
				}).inject(buttons);
				button = new Element('a', {
					'href' : 'javascript:;;',
					'class': 'button reset',
					'html':'NO',
					'events': {
						'click': function(e) {
							e.stop();
								cleanAll();
							return false;
						}
					}
				}).inject(buttons);
};

var insertRowSite = function(data) {
	var tbody = document.id('dataManaged').getElement('tbody');
	tbody.empty();
	var oddrow = true;

	data.each(function(row, ind) {
		classOdd = (oddrow)? ' odd' : '';
		oddrow = !oddrow;
		siteActive = (row.active==1)? ' active' : 'inactive';
		newrow = new Element('tr', {'id': 'site_'+row.idsite_url, 'class': siteActive+classOdd}).inject(tbody);
			//td: Site IDs
			td = new Element('td', {'class': 'c_id', 'html':'<p class="cID">'+row.id+'</p><p class="site_urlID">'+row.idsite_url+'</p>'}).inject(newrow);
			// td: Site Name
			td = new Element('td', {'class': 'Site_name'}).inject(newrow);
				site_n = new Element('span', {'id':'site_name_'+row.id, 'class':'editable edit_text'}).inject(td);
					site = new Element('a', {
					'html': row.name,
					'href': 'http://'+row.name,
					'events': {
						'click': function(e) {e.stop(); window.open('http://'+row.name); return false; }
					}
				}).inject(site_n);
				order_index = new Element('p', {'class':'order_index', 'html':'Order Index: '}).inject(td);
					order_index_n = new Element('span', {'id':'site_order_'+row.idsite_url,'class':'editable edit_text', 'html': row.order_index}).inject(order_index);
			// td: Site Url
			td = new Element('td', {'class': 'Site_url'}).inject(newrow);
				url_site = new Element('div', {'id':'url_site_'+row.idsite_url, 'class':'active_'+row.urls_id_url, 'html': remarkValues(row.url)}).inject(td);
				if(row.idsite_url!=0) {
					// Url Type
					url_site_block = new Element('div', {
						'class': 'block_cont urlType',
						'events': {
							'mouseleave': function() {
								this.getFirst('span').set('html', 'Url Type  &#8658;');
								this.getFirst('span').removeClass('subdataOver');
								this.getLast('div').setStyle('display', 'none');
							}
						}
					}).inject(td);
						url_site_block_b = new Element('span', {
							'class':'subdata button',
							'html': 'Url Type &#8658;',
							'events': {
								'mouseenter': function() {
									this.set('html', 'Url Type  &#8659;');
									this.addClass('subdataOver');
									this.getNext('div').setStyle('display', 'block');
								}
							}
						}).inject(url_site_block);
						url_site_block_extra = new Element('div', {'class': 'extra_data'}).inject(url_site_block);
							dl = new Element('dl', {'class': 'urls_type'}).inject(url_site_block_extra);
								dt = new Element('dt', {'html':'Url Type: <strong>'+row.urls_id_url+'</strong>'}).inject(dl);
									dd = new Element('dd').inject(dl);
										ul = new Element('ul', {'id': 'urlType_'+row.idsite_url, 'class': 'comboList'}).inject(dd);
											li = new Element('li', {
												'id': 'urlTypeList_'+row.url_types[row.urls_id_url],
												'class': 'alignRight',
												'html':'<span class="url_list_id">'+row.urls_id_url+'</span>'
											}).inject(ul);
											pre_span = new Element('span', {
												'id' : 'urlType_'+row.urls_id_url,
												'class' : 'url_list_code'
											}).inject(li);
											pre = new Element('pre', {'text':( $chk(row.url_types[row.urls_id_url])? row.url_types[row.urls_id_url].url : '' )}).inject(pre_span);
						_s = url_site_block_b.getDimensions();
						url_site_block_extra.setStyle('left', (_s.width.toInt() / 2) );
					// Url Values
					url_site_block = new Element('div', {
						'class': 'block_cont urlValues',
						'events': {
							'mouseleave': function() {
								this.getFirst('span').set('html', 'Url Values &#8658;');
								this.getFirst('span').removeClass('subdataOver');
								this.getLast('div').setStyle('display', 'none');
							}
						}
					}).inject(td);
						url_site_block_b = new Element('span', {
							'class':'subdata button',
							'html': 'Url Values &#8658;',
							'events': {
								'mouseenter': function() {
									this.set('html', 'Url Values &#8659;');
									this.addClass('subdataOver');
									this.getNext('div').setStyle('display', 'block');
								}
							}
						}).inject(url_site_block);
						url_site_block_extra = new Element('div', {'class': 'extra_data'}).inject(url_site_block);
							dl = new Element('dl', {'class': 'url_values'}).inject(url_site_block_extra);
								row.url_values.each(function(url_val, index_urlV) {
									dt = new Element('dt', {'html':url_val.name}).inject(dl);
									dd = new Element('dd', {'html':url_val.value}).inject(dl);
								});
						_s = url_site_block_b.getDimensions();
						url_site_block_extra.setStyle('left', (_s.width.toInt() / 2) );
					// Encode Type
					url_site_block = new Element('div', {
						'class': 'block_cont encodeType',
						'events': {
							'mouseleave': function() {
								this.getFirst('span').set('html', 'Encode Type &#8658;');
								this.getFirst('span').removeClass('subdataOver');
								this.getLast('div').setStyle('display', 'none');
							}
						}
					}).inject(td);
						url_site_block_b = new Element('span', {
							'class':'subdata button',
							'html':'Encode Type &#8658;',
							'events': {
								'mouseenter': function() {
									this.set('html', 'Encode Type &#8659;');
									this.addClass('subdataOver');
									this.getNext('div').setStyle('display', 'block');
								}
							}
						}).inject(url_site_block);
						url_site_block_extra = new Element('div', {'class': 'extra_data'}).inject(url_site_block);
							dl = new Element('dl', {'class': 'url_encodeType'}).inject(url_site_block_extra);
								dt = new Element('dt', {'html':'encode type id: <strong>'+row.url_encode_type_list.function_id+'</strong>'}).inject(dl);
									dd = new Element('dd').inject(dl);
										pre = new Element('pre', {'text':row.url_encode_type_list.function_code}).inject(dd);
								dt = new Element('dt', {'html':'comment:'}).inject(dl);
									dd = new Element('dd', {'html':row.url_encode_type_list.comment}).inject(dl);
						_s = url_site_block_b.getDimensions();
						url_site_block_extra.setStyle('left', (_s.width.toInt() / 2) );
				}

				url_site_block = new Element('p').inject(td);
					url_site_block_b = new Element('a', {
						'href': 'javascript:;;',
						'class':'button',
						'html': 'Edit',
						'events': {
							'click': function(e) {
										e.stop();
										editURL(ind);
										return false;
							}
						}
					}).inject(url_site_block);


			// td: Site needs
			td = new Element('td', {'class': 'Site_needs'}).inject(newrow);
				if(row.idsite_url!=0) {
					p = new Element('p', {'class':'list'}).inject(td);
						label = new Element('label', {'for':'needs_other_'+row.idsite_url, 'title':'reserved value (bin 001)', 'html':'Other: '}).inject(p);
						input = new Element('input', {
							'id':'needs_other_'+row.idsite_url,
							'name':'needs_other_'+row.idsite_url,
							'type':'checkbox',
							'value':'1',
							'events': {
								'change': function() {
									document.id('sendNeeds_'+row.idsite_url).setStyle('display', 'block');
									if(this.get('checked')) this.addClass('active');
									else this.removeClass('active');
								}
							}
						}).inject(label);
							if(row.needs & 1) {
								label.addClass('active');
								input.set('checked','checked')
							};
					p = new Element('p', {'class':'list'}).inject(td);
						label = new Element('label', {'for':'needs_path_'+row.idsite_url, 'title':'Path is needed (bin 010)', 'html':'Path: '}).inject(p);
						input = new Element('input', {
							'id':'needs_path_'+row.idsite_url,
							'name':'needs_path_'+row.idsite_url,
							'type':'checkbox',
							'value':'2',
							'events': {
								'change': function() {
									document.id('sendNeeds_'+row.idsite_url).setStyle('display', 'block');
									if(this.get('checked')) this.addClass('active');
									else this.removeClass('active');
								}
							}
						}).inject(label);
							if(row.needs & 2) {
								label.addClass('active');
								input.set('checked','checked')
							};
					p = new Element('p', {'class':'list'}).inject(td);
						label = new Element('label', {'for':'needs_query_'+row.idsite_url, 'title':'Query is needed (bin 100)', 'html':'Query: '}).inject(p);
						input = new Element('input', {
							'id':'needs_query_'+row.idsite_url,
							'name':'needs_query_'+row.idsite_url,
							'type':'checkbox', 'value':'4',
							'events': {
								'change': function() {
									document.id('sendNeeds_'+row.idsite_url).setStyle('display', 'block');
									if(this.get('checked')) this.addClass('active');
									else this.removeClass('active');
								}
							}
						}).inject(label);
							if(row.needs & 4) {
								label.addClass('active');
								input.set('checked','checked');
							};
					p = new Element('p', { 'id':'sendNeeds_'+row.idsite_url, 'class':'buttonBlock', 'styles':{'display':'none'} }).inject(td);
						button = new Element('a', {
							'href' : 'javascript:;;',
							'class': 'button',
							'html': 'send_changes',
							'events': {
								'click': function(){
									others = document.id('needs_other_'+row.idsite_url);
									path = document.id('needs_path_'+row.idsite_url);
									query = document.id('needs_query_'+row.idsite_url);
									value = (others.get('checked')? others.get('value').toInt() : 0) + (path.get('checked')? path.get('value').toInt() : 0) + (query.get('checked')? query.get('value').toInt() : 0);
									if(updateData(row.idsite_url, 'needs', value, document.id('sendNeeds_'+row.idsite_url)) == true) {
										this.setStyle('display', 'none');
									}
								}
							}
						}).inject(p);
				}
			// td: Site Actions
			td = new Element('td', {'class': 'Site_actions'}).inject(newrow);
				if(row.idsite_url!=0) {
					p = new Element('p').inject(td);
						button = new Element('a', {
							'href' : 'javascript:;;',
							'class': 'button activateSite',
							'html': ((row.active==1)? 'deactivate site' : 'activate site'),
							'events': {
								'click': function(e){
									e.stop();
									activate = (row.active==1)? 0 : 1;
									activateSite(row.id, activate, this.getParent(), row.name)
									return false;
								}
							}
						}).inject(p);

					p = new Element('p').inject(td);
					button = new Element('a', {
						'href': 'javascript:;;',
						'class':'button confirm',
						'html': 'add new url',
						'events': {
							'click': function(e) {
										e.stop();
										updateData( row.id , 'new_url_site', 4, tbody);
										return false;
							}
						}
					}).inject(p);

					p = new Element('p').inject(td);
						button = new Element('a', {
							'href' : 'javascript:;;',
							'class': 'button reset',
							'html': 'remove url',
							'events': {
								'click': function(e){
									e.stop();
									removeUrl(row.id, row.idsite_url, this.getParent());
									return false;
								}
							}
						}).inject(p);
					p = new Element('p').inject(td);
						t = { 0: '<span class="remark">deeplinking &#10004;</span> / <span class="buttonselection">cookie</span>', 1: '<span class="buttonselection">deeplinking</span> / <span class="remark">cookie &#10004;</span>'};
						button = new Element('a', {
							'href' : 'javascript:;;',
							'class': 'button',
							'html': t[row.method_redirect],
							'events': {
								'click': function(e){
									e.stop();
									updateData( row.id , 'method_redirect', (row.method_redirect==0 ? 1 : 0), tbody);
									return false;
								}
							}
						}).inject(p);
					p = new Element('p').inject(td);
						t = { 0: 'affiliation: <span class="buttonselection">&#10004;</span> / <span class="remark">&#10006;</span>', 1: 'affiliation: <span class="remarkYes">&#10004;</span> / <span class="buttonselection">&#10006;</span>'};
						button = new Element('a', {
							'href' : 'javascript:;;',
							'class': 'button',
							'html': t[(row.affiliation_default & 1)],
							'events': {
								'click': function(e){
									e.stop();
									updateData( row.id , 'affiliation_default', ((row.affiliation_default & 1)? parseInt(row.affiliation_default) - 1 : parseInt(row.affiliation_default) + 1), tbody);
									return false;
								}
							}
						}).inject(p);
					p = new Element('p').inject(td);
						t = { 0: 'overwrite: <span class="buttonselection">&#10004;</span> / <span class="remark">&#10006;</span>', 2: 'overwrite: <span class="remarkYes">&#10004;</span> / <span class="buttonselection">&#10006;</span>'};
						button = new Element('a', {
							'href' : 'javascript:;;',
							'class': 'button',
							'html': t[(row.affiliation_default & 2)],
							'events': {
								'click': function(e){
									e.stop();
									updateData( row.id , 'affiliation_default', ((row.affiliation_default & 2)? parseInt(row.affiliation_default) - 2 : parseInt(row.affiliation_default) + 2), tbody);
									return false;
								}
							}
						}).inject(p);
//					p = new Element('p').inject(td);
//						button = new Element('a', {'href' : 'javascript:;;', 'class': 'button convertLink', 'html':'convert links'}).inject(p);
				}
	});
};

//insert data for the url
var insertDataURL = function(data, target) {
	d = $H(data);
	d.each(function(row, ind) {
		li = new Element('li', {
			'id': target.id+'_'+ind,
			'html':'<span class="url_list_id">'+row.id+'</span>'
		}).inject(target);
			pre = new Element('pre', {'class': 'url_list_val', 'text': row.val}).inject(li);
	});
};

//creates the url edition interface
var createURLinterface = function(data_id) {
	if(!document.id('URLinterface_container')) {
		body = document.id('body');
		bg = new Element('div', {
			'id': 'URLinterface_container',
			'styles' : {
				'opacity':0
			}
		}).inject(body);
		bg.fade('in');
			close = createCloseX(bg);
	} else {
		document.id('URLinterface_container').fade('in');
	}
};

var dataURLinterface = function(data_id) {
	if(document.id('formUrl')) { document.id('formUrl').destroy(); }
	getData(data_id, 'all', document.id('URLinterface_container'));
};

var createFormURL = function(data) {
	if(!document.id('URLinterface_container')) {
		createURLinterface(current_url);
		dataURLinterface(current_url);
	} else {
		container = document.id('URLinterface_container');
		id_url = (current_site_Data[current_url].urls_id_url)? current_site_Data[current_url].urls_id_url : 0;
		encodeType = (current_site_Data[current_url].url_encode_type)? current_site_Data[current_url].url_encode_type : 0;
		row = $H(data);
			form = new Element('form', {'id': 'formUrl', 'name': 'formUrl', 'method': 'post', 'action':''}).inject(container, 'top');
				bL = new Element('div', {'class': 'blockLeft'}).inject(form);
				ul_tab = new Element('ul', {'id':'url_tabs_nav', 'class': 'tabs-nav'}).inject(form);
					li_tab = new Element('li').inject(ul_tab);
						a = new Element('a', {'href': '#', 'text':'Url Type'}).inject(li_tab);
					li_tab = new Element('li').inject(ul_tab);
						a = new Element('a', {'href': '#', 'text':'Url Values'}).inject(li_tab);
					li_tab = new Element('li').inject(ul_tab);
						a = new Element('a', {'href': '#', 'text':'Encode Type'}).inject(li_tab);

				ul_tab = new Element('ul', {'id':'url_tabs_body', 'class':'tabs-body'}).inject(form);
				// urlType
					li_tab = new Element('li', {'class':'tab_content'}).inject(ul_tab);
						dl = new Element('dl', {'class': 'urls_type'}).inject(li_tab);
							dt = new Element('dt', {'html':'Url Type <span class="notesmall">Click in the url to change it</span>'}).inject(dl);
								dd = new Element('dd', {
									'id': 'url_type_active',
									'events' : {
										'click': function() {
											document.id('urlType_list').fade('in');
										}
									}
								}).inject(dl);
								insertURLactive(dd, id_url);

								dd_list_urlType = new Element('dd', {'id':'urlType_list' ,'class':'list_url'}).inject(dl);
									ul_list = new Element('ul', {'class':'comboList'}).inject(dd_list_urlType);
										$each(row.url_type, function(url_t, index_t) {
											classActive = (id_url == url_t.id_url)? ' activeli ' : '';
											li = new Element('li', {
												'id' : 'urlType_list_'+url_t.id_url,
												'class': 'alignRight'+classActive,
												'html': '<span class="url_list_id">'+url_t.id_url+'</span>',
												'events': {
													'click': function(e) {
														e.stop();
														if(id_url != url_t.id_url) {
															this.erase('style');
															document.search('#urlType_list li.activeli').removeClass('activeli');
															activeThisUrlType(current_url, url_t.id_url, document.id('url_type_active'));
															this.addClass('activeli');
															this.erase('style');
														}
													}
												}
											}).inject(ul_list);
												pre = new Element('pre', {'class': 'url_list_code', 'text': url_t.url}).inject(li);
										});
								document.id('urlType_list').fade('out');

				// url Values
					li_tab = new Element('li', {'class':'tab_content'}).inject(ul_tab);
						dl = new Element('dl', {'class': 'url_values'}).inject(li_tab);
							k_avoid = [];
								row.url_values.each(function(url_val, index_urlV) {
									insertUrlValues(dl, 'bottom', url_val.idvalues, url_val.name, url_val.value);
									k_avoid.push(url_val.name);
								});
								dt = new Element('dt', {'id': 'new_url_values', 'html': 'new values'}).inject(dl);
								dd = new Element('dd').inject(dl);
									if( current_site_Data[current_url].urls_id_url != 0 ) {
										insertNewUrlValueForm(dd, k_avoid);
									} else {
										dd.set('html', '<strong>You must select an URL Type before add these values</strong>');
									}

				// Encode Type
					li_tab = new Element('li', {'class':'tab_content'}).inject(ul_tab);
						dl = new Element('dl', {'class': 'url_encodeType'}).inject(li_tab);
								dt = new Element('dt', {'html':'encode type <span class="notesmall">Click in the function to change it</span>'}).inject(dl);
									dd = new Element('dd', {
										'id':'url_encode_active',
										'events' : {
											'click': function() {
												document.id('urlencode_list').fade('in');
											}
										}
									}).inject(dl);
									insertURLEncodeActive(dd, encodeType);
							
							dt = new Element('dt').inject(dl);
								dd_list_urlencode = new Element('dd', {'id':'urlencode_list' ,'class':'list_url'}).inject(dl);
										ul_list = new Element('ul', {'class':'comboList'}).inject(dd_list_urlencode);
											$each(row.encode_type, function(url_e, index_e) {
												classActive = (encodeType == url_e.function_id)? ' activeli ' : '';
												li = new Element('li', {
													'id' : 'urlencode_list_'+url_e.function_id,
													'class': 'alignRight'+classActive,
													'html': '<span class="url_list_id">'+url_e.function_id+'</span>',
													'events': {
														'click': function(e) {
															e.stop();
															if(!this.hasClass('activeli')) {
																this.erase('style');
																document.search('#urlencode_list li.activeli').removeClass('activeli');
																activeThisUrlEncode(current_url, url_e.function_id, document.id('url_encode_active'));
																this.addClass('activeli');
																this.erase('style');
															}
														}
													}
												}).inject(ul_list);
													pre = new Element('pre', {'class': 'url_list_code', 'text': url_e.function_code}).inject(li);
													p = new Element('p', {'class': 'url_list_comment', 'text': url_e.comment}).inject(li);
											});
									document.id('urlencode_list').fade('out');

		//activate tabs...
		new nuTabs(document.search('#url_tabs_nav a'), document.search('#url_tabs_body .tab_content'), {transition: 'bounce:out'});
	}

	new Fx.Scroll(window).toElement('URLinterface_container');
	
	activeTab(0);
};

var activeTab = function(tab) {
	var t = document.search('#url_tabs_nav li');
	t[tab].getFirst().fireEvent('click');
};

var insertURLactive = function(where, id_url) {
	where.empty();
	url = (id_url!=0)? current_url_Data.url_type[id_url].url : '** Click here to select one **';
	span = new Element('span', {'html':id_url, 'class':'url_list_id',}).inject(where);
	pre = new Element('pre', {
		'id': 'urlType_value_text',
		'class': 'url_list_code',
		'html':remarkValues(url)
	}).inject(where);
};

var insertURLEncodeActive = function(where, encodeType) {
	where.empty();
	if(encodeType!=0) {
		span = new Element('span', {'html':encodeType, 'class':'url_list_id',}).inject(where);
		pre = new Element('pre', {'class': 'url_list_code', 'text':current_url_Data.encode_type['f_'+encodeType].function_code}).inject(where);
		p = new Element('p', {'class': 'url_list_comment', 'html':current_url_Data.encode_type['f_'+encodeType].comment}).inject(where);
	} else {
		p = new Element('p', {'class': 'url_list_comment', 'html': 'No function selected! - click here to select one'}).inject(where);
	}
};

var insertUrlValues = function(target, where, idvalues, name, value) {
	where = (where)? where : 'bottom';
	dt = new Element('dt', {'id': 'url_values_dt_'+idvalues,'html':name}).inject(target, where);
		dd = new Element('dd', {'id': 'url_values_dd_'+idvalues,'html':value}).inject(dt, 'after');
		a = new Element('a', {
				'href': 'javascript:;;',
				'class':'button reset',
				'alt': 'Delete this value',
				'title': 'Delete this value',
				'html': '&#10006;',
				'events': {
					'click': function(e) {
								e.stop();
								if( confirm("This action will remove this Url Value.\nAre you sure?") ) {
									updateData( current_site_Data[current_url].id, "remove_url_value", idvalues, document.id('url_values_dt_'+idvalues) );
									document.id('url_values_dd_'+idvalues).destroy();
									document.id('url_values_dt_'+idvalues).destroy();
									document.search('#url_tabs_nav li.selected a').fireEvent('click');
								}
								return false;
					}
				}
			}).inject(dt);
};

var insertNewUrlValueForm = function(where, k_avoid) {
	d1 = new Element('div', {'id':'new_url_val_form'}).inject(where);
		d = new Element('div').inject(d1);
				label = new Element('label', {'for':'urlvalue_name', 'title':'variable name.', 'html':'Name: '}).inject(d);
					field = new Element('select', {'id':'urlvalue_name'}).inject(label);
						options = extractUrlValues(current_url_Data.url_type[current_site_Data[current_url].urls_id_url].url, k_avoid);
						if(options) {
							options.each( function(vN, vIndex) {
								op = new Element('option', {'value':vN, 'html': vN}).inject(field);
							});
						}
//					s = new Element('strong', {'text':'{$'}).inject(label)
//					field = new Element('input', {'id' : 'urlvalue_name', 'size': '10'}).inject(label);
//					s = new Element('strong', {'text':'}'}).inject(label)
		d = new Element('div').inject(d1);
				label = new Element('label', {'for':'urlvalue_value', 'title':'value', 'html':'Value: '}).inject(d);
					field = new Element('input', {'id' : 'urlvalue_value', 'size': '15'}).inject(label);
		d = new Element('div').inject(where);
			a = new Element('a', {
				'href': 'javascript:;;',
				'class':'button confirm',
				'alt': 'Add this value',
				'title': 'Add this value',
				'html': '&#10004;',
				'events': {
					'click': function(e) {
								e.stop();
									document.id('new_url_val_form').fade('out');
									insertNewUrlValue( where, document.id('urlvalue_name').get('value'), document.id('urlvalue_value').get('value') );
									document.id('urlvalue_name').set('value', '');
									document.id('urlvalue_value').set('value', '');
								return true;
					}
				}
			}).inject(d);

			a = new Element('a', {
				'href': 'javascript:;;',
				'class':'button reset',
				'alt': 'Cancel',
				'title': 'Cancel',
				'html': '&#10006;',
				'events': {
					'click': function(e) {
								e.stop();
								document.id('urlvalue_name').set('value', '');
								document.id('urlvalue_value').set('value', '');
								return false;
					}
				}
			}).inject(d);
	d = new Element('div', {'class':'clearFix'} ).inject(where);
};

var createCloseX = function(where){
	close = new Element('div', {
		'class': 'close',
		'html': '<span>X</span>',
		'events': {
			'click': function(e) {
				e.stop();
				where.fade('out');
				where.destroy();
				if(changed)
					getDataSite(current_site_name);
			},
			'mouseenter': function(e) {
				e.stop();
				this.setStyle('color', '#000');
				this.getLast().fade('in');
			},
			'mouseleave': function(e) {
				e.stop();
				this.setStyle('color', '#CCC');
				this.getLast().fade('out');
			}
		}
	}).inject(where);
		s = new Element('span', {'class':'bg'}).inject(close);
		s.fade('out');
		hideLoading();
	return close;
};

/* functions various */
var remarkValues = function(str) {
	if(str) {
		return str.replace(/(\{\$[^\}]*\})/gim, '<span class="remarked">$1</span>');
	} else {
		return str;
	}
};
var extractUrlValues = function(str, k_avoid) {
	var notake = ['{$aid}','{$bid}','{$url}','{$url_1}','{$url_2}','{$url_3}', '{$lastID}'];
	var v = [];
	var pattern = /(\{\$[^\}]*\})/gim;
	var valuesinurl = str.match(pattern);
	if(valuesinurl) {
		valuesinurl.each(function(val, index) {
			if( !notake.contains(val) && !k_avoid.contains(val) ) { v.push(val); }
		});
	}
	if(v.length > 0) {
		return v;
	} else {
		return false;
	}
};

var makeEditable = function() {

};

/* Actions */
var cleanAll = function() {
	current_site_name = '';
	current_site_Data = null;
	var tbody = document.id('dataManaged').getElement('tbody');
	tbody.empty();
	document.id('search_site').set('value', '');
	document.id('current_title').set('html', '-');
	document.id('search_site').focus();
	hideLoading();
};

var activateSite = function(site_id, value, target, site_name) {
	updateData(site_id, 'active', value, target);
};

var removeUrl = function(site_id, idsite_url, target) {
	if( confirm("This action will remove this URL and url values for this site.\nAny other url asociated with this site will remain intact.\nAre you sure you want delete this?") ) {
		updateData(site_id, 'remove', idsite_url, target);
	}
};

var editURL = function(data_id){
	createURLinterface(data_id);
	dataURLinterface(data_id);
};

var activeThisUrlType = function(data_id, id_url, target) {
	site_id = current_site_Data[data_id].id;
	if(current_site_Data[data_id].idsite_url!=0 && current_site_Data[data_id].urls_id_url!=0) {
		id = current_site_Data[data_id].idsite_url;
		type = 'url_type_update';
	} else {
		id = current_site_Data[data_id].id;
		type = 'url_type_insert';
	}
	updateData(id, type, id_url, target);
	document.id('URLinterface_container').destroy();
	return r;
};

var activeThisUrlEncode  = function(data_id, encodeType, target) {
	site_id = current_site_Data[data_id].id;
	id = current_site_Data[data_id].idsite_url;
	type = 'url_encode_update';
	updateData(id, type, encodeType, target);
	return r;
};

var insertNewUrlValue = function(target, urlvalue_name, urlvalue_value) {
	v = 'current_url='+current_site_Data[current_url].idsite_url+'&urlvalue_name='+urlvalue_name+'&urlvalue_value='+urlvalue_value;
	site_id = current_site_Data[current_url].id;
	updateData(site_id, 'urlvalue_insert', v, target);
	document.id('urlvalue_name').set('value', '');
	document.id('urlvalue_value').set('value', '');
	document.id('new_url_val_form').fade('in');
};