var url = '/option/exthelper.php';

var totalXml;

var optionData = Array(); //this will be an array build from the xml
var categoryData = Array(); //this too
var categoryOblSelect = Array();

function clearCategory(catid)
{
	var x;
	for(x in optionData)
	{
		if( optionData[x]['categoryid'] == catid )
		{
			optionData[x]['number'] = 0;
			var optionrow = $('#option' + x);
			optionrow.children('.itemnumber').html('0');
			optionrow.children('.itempricetotal').html('0');
		}
	}
}

function sumoptions()
{
	var x;
	var total = 0;
	for(x in optionData)
	{
		total += (optionData[x]['number'] * optionData[x]['price']) ;
	}

	$('#optionsum').html(total.toString());
}

function updatetable()
{
	var x;
	for(x in optionData)
	{
		num = optionData[ x ]['number'];
		var optionrow = $('#option' + x);
		optionrow.children('.itemnumber').html(num.toString());
		optionrow.children('.itempricetotal').html( (num * optionData[x]['price']).toString() );
	}
}

function addoption(id)
{
	var num = optionData[ id ]['number'];
	var type = optionData[ id ]['type'];

	if(type == 'bin')
	{
		if(num==0)
		{
			num = 1;
		}
	}

	if(type == 'multi')
	{
		num = num + 1;
	}

	var catid = optionData[ id ]['categoryid'];

	if( categoryData[ catid ] == 'single' )
	{
		clearCategory( catid );
	}

	optionData[ id ]['number'] = num;
	var optionrow = $('#option' + id);
	optionrow.children('.itemnumber').html(num.toString());
	optionrow.children('.itempricetotal').html( (num * optionData[id]['price']).toString() );

	sumoptions();

	return false;
}

function removeoption(id)
{
	var num = optionData[ id ]['number'];
	var catid = optionData[ id ]['categoryid'];

	if(num > 0)
	{
		num = num - 1;

		if( num == 0 )
		{
			if(categoryOblSelect[ catid ] == "yes" && categoryData[ catid ] == 'single')
			{
				num = 1;
			}
			if(categoryOblSelect[ catid ] == "yes" && categoryData[ catid ] == 'multi')
			{
				var other = 0;
				var x;
				for(x in optionData)
				{
					if( optionData[x]['categoryid'] == catid && optionData[x]['number'] > 0 && x != id )
					{
						other++
					}
				}
				if(other == 0)
				{
					num = 1;
				}
			}
		}
	}

	optionData[ id ]['number'] = num;
	var optionrow = $('#option' + id);
	optionrow.children('.itemnumber').html(num.toString());
	optionrow.children('.itempricetotal').html( (num * optionData[id]['price']).toString() );

	sumoptions();

	return false;
}

function B_loadOptionsTable(targetId,skin,template,preset)
{
	var requestdata = { skin: skin, template: template, a: 'total'};
	var xml;

	if(preset)
	{
		requestdata.preset = preset;
	}

	$.ajax({
		type: "POST",
		url: url,
		data: requestdata,
		dataType: "xml",
		success: function(xml) {
			var skinObject = new defaultskin('red');

			var skinItems = Array('table','sumrow','optionsum','header','text','description','detail','itemnumber','itempricetotal','addoption','removeoption','addoptionImage','removeoptionImage');

			for(i=0;i<skinItems.length;i++)
			{
				var item = skinItems[i];
				$("/optionsdata/skin/" + item, xml).each(function(){
					var snode = $(this);

					if(snode.attr('class'))
					{
						skinObject[item + 'Class'] = snode.attr('class');
					}

					if(snode.attr('style'))
					{
						skinObject[item + 'Style'] = "style=\"" + snode.attr('style') + "\"";
					}

				});
			}

			$("/optionsdata/skin/addoptionText", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.addoptionText = snode.attr('value');
				}
			});

			$("/optionsdata/skin/removeoptionText", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.removeoptionText = snode.attr('value');
				}
			});

			$("/optionsdata/skin/addoptionImageURL", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.addoptionImageURL = snode.attr('value');
				}
			});

			$("/optionsdata/skin/removeoptionImageURL", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.removeoptionImageURL = snode.attr('value');
				}
			});

			var tablehtml = skinObject.buildTableStart();
			$("/optionsdata/category", xml).each(function(){
				var title = $('title', this).text();
				var catid = $(this).attr('id');
				var cattype = $(this).attr('type');
				var catoblselect = $(this).attr('obligatoryselect');

				categoryData[ catid ] = cattype;
				categoryOblSelect[ catid ] = catoblselect;

				tablehtml += skinObject.buildCategoryHeader(title);

				$('option', this ).each( function(j){
					var option = $(this);
					var pricetext = $('pricedescription', this).text();
					var description = $('description', this).text();
					var detail = $('detail', this).text();
					var id = option.attr('id');
					var type = option.attr('type');
					var price = option.attr('price');
					optionData[ id ] = Array();
					if(j == 0 && catoblselect == 'yes')
					{
						optionData[ id ][ 'number' ] = 1;
					}
					else
					{
						optionData[ id ][ 'number' ] = 0;
					}
					optionData[ id ][ 'type' ] = type;
					optionData[ id ][ 'price' ] = price;
					optionData[ id ][ 'categoryid' ] = catid;

					tablehtml += skinObject.buildOptionRow(pricetext,id,description,detail);
				});//option each
			}); //category each

			tablehtml += skinObject.buildTableEnd();
			$('#' + targetId).html(tablehtml);

			sumoptions();
			updatetable();

			if( preset )
			{
				$("/optionsdata/preset/option", xml).each(function(){
					var poption = $(this);
					for(var q = 0; q < poption.attr('number'); q++)
					{
						addoption(poption.attr('id'));
					}
				});//each

				sumoptions();
				updatetable();
			}
			//
			//table build end
			//
		} //load succes
	}); //ajax
}  //load
   


function loadOptionsTable(targetId,skin,template,preset)
{
	var xml;
	var requestdata = {template: template, a: 'total'};

	if(!$.browser.msie)
	{
		requestdata.skin = skin;
	}

	if(preset)
	{
		requestdata.preset = preset;
	}


	$.ajax({
		type: "POST",
		url: url ,
		data: requestdata,
		dataType: "xml",
		success: function(xml){
			var skinObject = new defaultskin('red');
			var skinItems = new Array('table','sumrow','optionsum','header','text','description','detail','itemnumber','itempricetotal','addoption','removeoption','addoptionImage','removeoptionImage');
			
			for(i=0;i<skinItems.length;i++)
			{
				var item = skinItems[i];
				$("/optionsdata/skin/" + item, xml).each(function(){
					var snode = $(this);

					if(snode.attr('class'))
					{
						skinObject[item + 'Class'] = snode.attr('class');
					}
					if(snode.attr('style'))
					{
						skinObject[item + 'Style'] = "style=\"" + snode.attr('style') + "\"";
					}
				});
			}
			alert('y');

			$("/optionsdata/skin/addoptionText", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.addoptionText = snode.attr('value');
				}
			});

			$("/optionsdata/skin/removeoptionText", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.removeoptionText = snode.attr('value');
				}
			});

			$("/optionsdata/skin/addoptionImageURL", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.addoptionImageURL = snode.attr('value');
				}
			});

			$("/optionsdata/skin/removeoptionImageURL", xml).each(function(){
				var snode = $(this);
				if(snode.attr('value'))
				{
					skinObject.removeoptionImageURL = snode.attr('value');
				}
			});

			var tablehtml = skinObject.buildTableStart();
			$("/optionsdata/category", xml).each(function(){
				var title = $('title', this).text();
				var catid = $(this).attr('id');
				var cattype = $(this).attr('type');
				var catoblselect = $(this).attr('obligatoryselect');

				categoryData[ catid ] = cattype;
				categoryOblSelect[ catid ] = catoblselect;

				tablehtml += skinObject.buildCategoryHeader(title);
           
				$('option', this ).each( function(j){
					var option = $(this);
					//var text = option.text();
					var pricetext = $('pricedescription', this).text();
					var description = $('description', this).text();
					var detail = $('detail', this).text();
					var id = option.attr('id');
					var type = option.attr('type');
					var price = option.attr('price');
					optionData[ id ] = Array();
					if(j == 0 && catoblselect == 'yes')
					{
						optionData[ id ][ 'number' ] = 1;
					}
					else
					{
						optionData[ id ][ 'number' ] = 0;
					}
					optionData[ id ][ 'type' ] = type;
					optionData[ id ][ 'price' ] = price;
					optionData[ id ][ 'categoryid' ] = catid;

					tablehtml += skinObject.buildOptionRow(pricetext,id,description,detail);
				});//option each
			});//category each

			tablehtml += skinObject.buildTableEnd();

			$('#' + targetId).html(tablehtml);


			sumoptions();
			updatetable();

			if( preset )
			{
				$("/optionsdata/preset/option", xml).each(function(){
					var poption = $(this);
					for(var q = 0; q < poption.attr('number'); q++)
					{
						addoption(poption.attr('id'));
					}
				});//each

				sumoptions();
				updatetable();
			}
		} //load succes
	}); //ajax
}

function loadOptionsPrice(targetId,template,preset)
{
	var optionData = Array(); //this will be an array build from the xml
	var categoryData = Array(); //this too
	var categoryOblSelect = Array();
  
	var xml;

	var requestdata = {template: template, a: 'price'};

	if(preset)
	{
		requestdata.preset = preset;
	}

	$.ajax({
		type: "POST",
		url: url ,
		data: requestdata,
		dataType: "xml",
		success: function(xml){
			$("/optionsdata/category", xml).each(function(){
				var catid = $(this).attr('id');
				var cattype = $(this).attr('type');
				var catoblselect = $(this).attr('obligatoryselect');

				categoryData[ catid ] = cattype;
				categoryOblSelect[ catid ] = catoblselect;

				$('option', this ).each( function(j){
					var option = $(this);
					var id = option.attr('id');
					var type = option.attr('type');
					var price = option.attr('price');
					optionData[ id ] = Array();
					if(j == 0 && catoblselect == 'yes')
					{
						optionData[ id ][ 'number' ] = 1;
					}
					else
					{
						optionData[ id ][ 'number' ] = 0;
					}
					optionData[ id ][ 'type' ] = type;
					optionData[ id ][ 'price' ] = price;
					optionData[ id ][ 'categoryid' ] = catid;
				});//option each
			});//category each

			if( preset )
			{
				$("/optionsdata/preset/option", xml).each(function(){
					var poption = $(this);
					optionData[ poption.attr('id') ]['number'] = poption.attr('number');
				});//each
			}

			var x;
			var total = 0;
			for(x in optionData)
			{
				total += (optionData[x]['number'] * optionData[x]['price']);
			}

			$('#' + targetId).html(total.toString());
		} //load succes
	}); //ajax
}
