var directory_load_pre = '';

function directory_magazine_change() {
	category_select = document.getElementById('category_id');
	value = category_select.options[category_select.selectedIndex].value;
	if(value == 305) {
		//do a call
		if(document.getElementById('publisher_choice_row')!=null) {
			
			document.getElementById('publisher_choice_row').style.visibility = 'visible';
			document.getElementById('publisher_choice_row_1').style.visibility = 'visible';
			document.getElementById('publisher_choice_row_2').style.visibility = 'visible';
			
			document.getElementById('publisher_choice_row').style.display = '';
			document.getElementById('publisher_choice_row_1').style.display = '';
			document.getElementById('publisher_choice_row_2').style.display = '';
			
			document.getElementById('publisher_name_row_1').style.visibility = 'visible';
			document.getElementById('publisher_name_row_2').style.visibility = 'visible';
			document.getElementById('publisher_name_row').style.visibility = 'visible';
			
			document.getElementById('publisher_name_row_1').style.display = '';
			document.getElementById('publisher_name_row_2').style.display = '';
			document.getElementById('publisher_name_row').style.display = '';
			//right ajax
			var d = new Date();
			var time = d.getTime();
			var url= SITEURL + "/libs/ajax/ajax_directory_publisher_dropdown.php?timestamp="+time;
			

			params = "";
			xmlHttp_directory = GetXmlHttpObject(directory_magazine_change_helper);//fails on safari 1
			xmlHttp_directory.open("POST", url , true);


			xmlHttp_directory.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp_directory.setRequestHeader("Content-length", params.length);
			xmlHttp_directory.setRequestHeader("Connection", "close");
			xmlHttp_directory.send(params);
			
			
		}
	}
	else {
		//REMOVE ALL SIGNS!
		document.getElementById('publisher_name').value = '';
		document.getElementById('telephone').value = '';
		document.getElementById('email').value = ''; 
		document.getElementById('company_postcode').value = ''; 
		document.getElementById('company_address').value = ''; 
		document.getElementById('company_town').value = '';
		document.getElementById('company_state').value  = '';
		document.getElementById('contact_name').value  = '';
		
		ShowHide('publisher_choice_row','hidden');
		ShowHide('publisher_choice_row_1','hidden');
		ShowHide('publisher_choice_row_2','hidden');
		document.getElementById('publisher_choice_row_2').innerHTML = directory_load_pre;
		
	}
	
	var in_array = false;
	for(i=0;i<directory_free_listing.length&&in_array==false;i++) {
		if(directory_free_listing[i]==value) {
			in_array = true;
		}
	}
	if(in_array==true) {
		document.getElementById('enhanced_yes').checked = true;
		document.getElementById('enhanced_yes').disabled = true;
		document.getElementById('enhanced_no').disabled = true;
	}
	else {
		document.getElementById('enhanced_yes').disabled = false;
		document.getElementById('enhanced_no').disabled = false;
	}
	
}
publish_array = new Array();
String.prototype.strrtrim = function() {
	//Match spaces at end of text and replace with a null string
	return this.replace(/\s+$/,'');
}

function directory_magazine_change_helper() {
	if (xmlHttp_directory.readyState==4 || xmlHttp_directory.readyState=="complete"){
		var return_text = xmlHttp_directory.responseText;
		//ok ideally we want  a a drop down b an array of all the publishers and their attributes 
		//mm
		
		return_text_ar = return_text.split('[code]');

		directory_load_pre = document.getElementById('publisher_choice_row_2').innerHTML;
		document.getElementById('publisher_choice_row_2').innerHTML = return_text_ar[0];

		
		javascript_array = return_text_ar[1].split('[split]');
		//alert(javascript_array[0]);
		
		for(i=1;i<javascript_array.length;i++) {
			//k so now we have the array
			//explode it all and put into the  publish_array array
			//mmm
			javascript = javascript_array[i].replace(' ','');
			
			if(javascript_array[i].replace(' ','')!='') {
				publisher = new Object();
				
				first_split = javascript_array[i].split('[split_publisher_name]');
				
				second_split = first_split[1].split('[split_telephone]');
				publisher.company_name = second_split[0].strrtrim();
				third_split = second_split[1].split('[split_email]');
				publisher.telephone = third_split[0].strrtrim();
				forth_split = third_split[1].split('[split_address]');
				publisher.email = forth_split[0].strrtrim();
				fifth_split = forth_split[1].split('[split_town]');
				publisher.postcode = fifth_split[0].strrtrim();
				sixth_split = fifth_split[1].split('[split_state]');
				publisher.address = sixth_split[0].strrtrim();
				seventh_split = sixth_split[1].split('[split_postcode]');
				publisher.town = seventh_split[0].strrtrim();
				eighth_split = seventh_split[1].split('[split_country]');
				publisher.county = eighth_split[0].strrtrim();
				nineth_split = eighth_split[1].split('[split_contact_name]');
				publisher.country = nineth_split[0].strrtrim();
				publisher.contact_name = nineth_split[1].strrtrim();
				
				
				
				
				var regExp = /\s+/g;
				first_split[0] = first_split[0].replace(regExp,'');

				publish_array[first_split[0]] = publisher;
			}
			
		}
		directory_update_fields();
		
		
	}
}




function directory_update_fields() {
	publisher_choice = document.getElementById('publisher_id');
	value = publisher_choice.options[publisher_choice.selectedIndex].value;
	publisher = publish_array[value];
	
	document.getElementById('publisher_name').value = publisher.company_name;
	document.getElementById('telephone').value = publisher.telephone;
	document.getElementById('email').value = publisher.email; 
	document.getElementById('company_postcode').value = publisher.postcode; 
	document.getElementById('company_address').value = publisher.address; 
	document.getElementById('company_town').value = publisher.town;
	document.getElementById('company_state').value  = publisher.county;
	document.getElementById('contact_name').value  = publisher.contact_name;
	//alert(publisher.country);
	
	country = document.getElementById('company_country');
	for(i=0;i<country.options.length;i++) {
		if(country.options[i].value == publisher.country) {
			country.selectedIndex = i;
			i = country.options;
		
			//start looking here!
		}
	}	
}
function directory_search_control() {
	port = document.getElementById('directory_search_control_ele');
	if(port.className == "control_hide") {
		//ok then hide
		close_recursive('directory_search_options',0,directory_search_control_open);
	}
	else {	
		open_recursive('directory_search_options',0,directory_search_control_close);
	}
}
function directory_search_control_open(div) {
	document.getElementById('directory_search_control_ele').className = 'control_show';
}
function directory_search_control_close(div) {
	document.getElementById('directory_search_control_ele').className = 'control_hide';
	
}

