var numCommands = 0

function processPrice(price)
{
	newPrice = price.replace(",",".")
	pattern = /^\d{1,2}.\d{1,2}$/
	if(newPrice.search(pattern) == -1)
	{
		alert("Maximum price exceeded or incorrect price format")
		return ""
	}
	euros = newPrice.substring(0,newPrice.indexOf("."))
	cents = newPrice.substring(newPrice.indexOf(".")+1)
	if(euros.length != 2)
	{
		euros = "0" + euros
	}
	if(cents.length != 2)
	{
		cents += "0"
	}
	return euros + cents
}

function clearInputs()
{
	$("input[type=radio]").prop('checked',false)
	$("input[name=datepicker").val("")
	$("input[name=channela6").val("")
	$("input[name=producta6").val("")
	$("input[name=price").val("")
	$("input[name=channela7").val("")
	$("input[name=producta7").val("")
	$("input[name=productname").val("")
}

function addCommand()
{
	command=$("input[name=command]:checked").val()
	if(typeof command == 'undefined')
	{
		alert("No command selected")
		return
	}
	else if(command == "Hello")
	{
		//NOTHING TO DO
	}
	else if(command == "A5")
	{
		date=$("input[name=datepicker]").val()
		if(date == "")
		{
			alert("Date not set. Please check that BOTH date and time are set in the field")
			return
		}
		command += date.substring(8,10)
		command += date.substring(3,5)
		command += date.substring(0,2)
		command += date.substring(11,13)
		command += date.substring(14,16)
		command += date.substring(17,19)
	}
	else if(command == "A6")
	{
		channel=$("input[name=channela6]").val()
		product=$("input[name=producta6]").val()
		price=$("input[name=price]").val()
		if(channel == "" || product == "" || price == "")
		{
			alert("Missing required parameters for Product Price Change command. Please check that Channel, Product and Price fields are set")
			return
		}
		command += channel
		command += product
		newPrice = processPrice(price)
		if(newPrice == "")
		{
			return
		}
		command += newPrice
	}
	else if(command == "A7")
	{
		channel=$("input[name=channela7]").val()
		product=$("input[name=producta7]").val()
		productName=$("input[name=productname]").val()
		if(channel == "" || product == "" || productName == "")
		{
			alert("Missing required parameters for Product Name Change command. Please check that Channel, Product and Product Name fields are set")
			return
		}
		command += channel
		command += product
		command += productName
	}
	else if(command == "Bye")
	{
		//NOTHING TO DO
	}
	$("select[name=commandlist]").append(new Option(command,command))
	numCommands++
	$("select[name=commandlist]").attr('size',numCommands)
	clearInputs()
}

function removeCommand()
{
	numCommands -= $("select[name=commandlist] option:selected").length
	$("select[name=commandlist] option:selected").remove()
	$("select[name=commandlist]").attr('size',numCommands)
}

function submitForm()
{
	name = $("input[name=name]").val()
	machineID = $("input[name=machineid]").val()
	commands = ""
	commandList = $("select[name=commandlist] option")
	if(commandList.length == 0)
	{
		alert("No commands added in command list")
		return
	}
	var i
	for(i = 0; i < commandList.length; i++)
	{
		commands += commandList[i].text + ","
	}
	commands = commands.slice(0,-1)
	$("input[name=commands]").val(commands)
	document.getElementById("btvendor").submit()
}
	
$(function() 
{
	$('#datetimepicker1').datetimepicker(
	{
        	language: 'es-ES'
        });
});
