addEvent(window, 'load', init_plate, false);

var format_id = null;
var category = null;
var plate_country = null;
var plate_state = null;
var plate_colour = null;
var plate_digits = null;
var digit_colour = null;
var plate_preview = null;
var country = null;
var user_type_seller = null;
var user_type_heritage = null;
var user_type_buyer = null;
var plate_type = null;
var model_id = null;
var listing_type = null;
var imgDir = null;
var seller_specific = null;
var contact_details = null;
var non_heritage = null;
var confidential = null;
var ebay = null;
var p_ebay = null;
var standard_digits = 9;
var q_digits = 7;
var max_digits = 9;


function init_plate()
{
	var res = null;
	var form = id('form');
	
	if(form != null) {
		// clear all number fields that have the value 0
		res = getElementsByClassName(form, 'gui_number', 'input');
		for (var i = 0; i < res.length; i++) {
			if(res[i].value == 0) {
				res[i].value = '';
			}
		}		

		format_id = id('format_id');
		category = id('category_id');
		plate_country = id('plate_country_id');
		plate_state = id('plate_state_id');
		plate_colour = id('plate_colour');
		plate_preview = id('plate_preview');
		plate_digits = id('plate_digits');
		digit_colour = id('digit_colour');
		country = id('country_id');
		user_type_seller = id('user_type_seller');
		user_type_heritage = id('user_type_heritage');
		user_type_buyer = id('user_type_buyer');
		plate_type = id('plate_type');
		model_id = id('model_id');		
		listing_type = id('listing_type');
		seller_specific = id('seller_specific');
		contact_details = id('contact_details');
		non_heritage = id('non_heritage');
		confidential = id('confidential');
		ebay = id('ebay');
		p_ebay = id('p_ebay');
		
		imgUrl = plate_preview.style.backgroundImage;
		imgDir = imgUrl.substring(imgUrl.indexOf('(') + 1, imgUrl.lastIndexOf('/') + 1);
			
		addEvent(format_id, 'change' , get_listing_types, false);
		addEvent(plate_type, 'change' , get_models, false);
		addEvent(plate_state, 'change' , get_plate_preview, false);
		addEvent(plate_country, 'change' , get_plate_states, false);
		if (country != null){
			addEvent(country, 'change' , get_states, false);
		}
		addEvent(plate_colour, 'change' , get_plate_preview, false);
		addEvent(plate_digits, 'keyup' , set_plate_digits, false);
		addEvent(digit_colour, 'change' , get_plate_preview, false);
		
		
		if(user_type_seller != null){
			addEvent(user_type_seller, 'click' , get_listing_types, false);
			addEvent(user_type_heritage, 'click' , get_listing_types, false);
			addEvent(user_type_buyer, 'click' , get_listing_types, false);
			if (user_type_seller.checked){
				user_type_seller.click();
			} else if (user_type_heritage.checked){
				user_type_heritage.click();
			} else if (user_type_buyer.checked){
				user_type_buyer.click();
			}
			addEvent(category, 'click' , set_max_digits, false);
		}
		
		exp_date = id('expiry_date');
		if (exp_date != null){
			plt_listingTypeChange(id("listing_type"), 'exp_date', "expiry_date");
		}
		set_plate_digits(null);
		get_plate_preview(null);
	}
}

function get_models(e)
{	
	var field = window.event ? window.event.srcElement : e ? e.target : null;	
	ajax_call('module=number_plates&view=model&plate_type=' + field.value, 'plt_type');
}


function get_plate_states(e)
{	
	var field = window.event ? window.event.srcElement : e ? e.target : null;
	ajax_call('module=number_plates&view=country&country_id=' + field.value, 'plt_plate_country');
}

function get_states(e)
{	
	var field = window.event ? window.event.srcElement : e ? e.target : null;	
	ajax_call('module=number_plates&view=country&country_id=' + field.value, 'plt_country');
	get_plate_preview(e);
}

function get_listing_types(e)
{	
	if (user_type_seller.checked){
		user_type = 'seller';
		max_digits = standard_digits;
	} else if (user_type_buyer.checked){
		user_type = 'buyer';
		max_digits = standard_digits;
	} else if (user_type_heritage.checked){
		user_type = 'heritage';
		max_digits = q_digits;
	}
	ajax_call('module=number_plates&view=format&format_id=' + format_id.value + '&user_type=' + user_type, 'plt_format');
}

function get_plate_preview(e)
{
	prefix = state_img_prefix[plate_state.value];
	if (plate_colour.value == ""){
		plt_clr = 'black';
	} else {
		plt_clr = plate_colour.value;
	}
	if (digit_colour.value == ""){
		dig_clr = 'white';
	} else {
		dig_clr = digit_colour.value;
	}
	plate_preview.style.backgroundImage = "url('" + imgDir + prefix + plt_clr + ".gif')";
	plate_preview.style.color = dig_clr;
}

function plt_type(result)
{
	model_id.options.length = 1;
	eval ("var data = " + result);
	for (i = 0; i < data.length; i++){		
		model_id.options[model_id.options.length] = new Option(data[i][1],data[i][0]);
	}
}

function plt_user_type(result)
{
	category.options.length = 0;
	eval ("var data = " + result);	
	for (i = 0; i < data.length; i++){		
		category.options[category.options.length] = new Option(data[i][1],data[i][0]);
	}
}

function plt_plate_country(result)
{
	plate_state.options.length = 0;
	eval ("var data = " + result);
	for (i = 0; i < data.length; i++){		
		plate_state.options[plate_state.options.length] = new Option(data[i][1],data[i][0]);
	}
	get_plate_preview(null);
}

function plt_country(result)
{
	state_id.options.length = 0;
	eval ("var data = " + result);
	for (i = 0; i < data.length; i++){		
		state_id.options[state_id.options.length] = new Option(data[i][1],data[i][0]);
	}
}

function plt_format(result)
{	
	listing_type.options.length = 0;
	eval ("var data = " + result);
	for (i = 0; i < data.length; i++){		
		listing_type.options[listing_type.options.length] = new Option(data[i][1],data[i][0]);
	}
	if (user_type_seller.checked){
		user_type = user_type_seller.value;
	} else if (user_type_buyer.checked){
		user_type = user_type_buyer.value;
	} else {
		user_type = user_type_heritage.value;
	}
	ajax_call('module=number_plates&view=user_type&user_type=' + user_type, 'plt_user_type');
}

function plt_listingTypeChange(select, c_id, ref_id)
{
	if (select.value == "sale"){
		document.getElementById(c_id).style.display = "none";
		document.getElementById(ref_id).value = "";
	} else {
		document.getElementById(c_id).style.display = "block";
	}
}

function set_plate_digits(e)
{
	if (plate_digits.value.length > max_digits){
		plate_digits.value = plate_digits.value.substring(0,max_digits);
	}
	plate_preview.innerHTML = plate_digits.value;
}


var state_img_prefix = {"ACT": "p1","NSW":"p2","NT": "p3","NZ": "p4","QLD": "p5","SA": "p6","TAS": "p7","VIC": "p8","WA": "p9"};

function populate_default(source_name, target_name)
{
	source = document.getElementById(source_name);
	target = document.getElementById(target_name);
	if (source != null && target.value == ''){
		target.value = source.value;
	}
}

function user_typeChange(user_type)
{
	document.getElementById('plate_form').style.display = "block";
	if (user_type == "seller"){
		seller_specific.style.display = "block";
		contact_details.style.display = "block";
		non_heritage.style.display = "block";
		p_ebay.style.display = "block";
		if (confidential != null){
			confidential.style.display = 'none';
		}
		format_id.value = 1;
	} else if (user_type == "buyer"){
		seller_specific.style.display = "none";
		ebay.checked = false;
		p_ebay.style.display = "none";
		if (confidential != null){
			confidential.style.display = 'none';
		}
		format_id.value = 1;
	}  else if (user_type == "heritage"){
		seller_specific.style.display = "block";
		contact_details.style.display = "none";
		non_heritage.style.display = "none";
		ebay.checked = false;
		p_ebay.style.display = "none";
		if (confidential != null){
			confidential.style.display = 'inline';
		}
		format_id.value = 2;
	}
}

function set_max_digits()
{
	if (category.value == 6){
		max_digits = q_digits;
	} else {
		max_digits = standard_digits;
	}
	plate_digits.value = plate_digits.value.substring(0,max_digits);
	set_plate_digits(null);
}
