// Call this method to create a new hidden field in the specified form, and concatenate
// its value from two other fields.
function AddConcatField(form, dstName, srcName1, srcName2)
{
	// Check if destination field already exists
	var field = document.getElementsByName(dstName);
	if (field==null || field.length==0)
	{
		// Add a new hidden field
		if (document.all)
		{
			field = document.createElement("<input name='"+dstName+"'>");
		}
		else
		{
			field = document.createElement("input");
			field.name = dstName;
		}
		field.type = "hidden";
		form.appendChild(field);
	}
	else
	{
		field = field[0];
		
		// Ensure that the existing field is empty
		if (field.value.length!=0)
			return;
	}
	
	// Find fields to concatenate
	var src1 = document.getElementsByName(srcName1);
	var src2 = document.getElementsByName(srcName2);
	if (src1==null || src1.length==0 || src2==null || src2.length==0)
		return;
	
	// Build the value for the destination from the two source fields
	field.value = src1[0].value;
	if (field.value.length>0)
		field.value += " ";
	field.value += src2[0].value;
}

function OnDaysChange(txtDays, txtPricePerDay, tbl, ddl)
{
	if ((txtDays.value.length == 0) || (isNaN(txtDays.value)))
	{
		txtPricePerDay.value = "";
		return;
	}
	
	var intervalsAndPrices = new Array(tbl.rows.length);
	for (var i = 0; i < tbl.rows.length; i++)
	{
		var cells = tbl.rows.item(i).cells;
		var interval = cells[0].innerHTML.split("-");
		
		var temp = new Array(3);
		temp[0] = interval[0];			// Lower part of interval
		temp[1] = interval[1];			// Higher part of interval
		temp[2] = cells[1].innerHTML;	// Price for interval
		
		intervalsAndPrices[i] = temp;
	}
	
	intervalsAndPrices[tbl.rows.length - 1][1] = parseInt(99999);

	var price = 0.0;
	var days = txtDays.value;
	var j = 0;
	while (days > 0)
	{
		var currentSpan = (intervalsAndPrices[j][1] - intervalsAndPrices[j][0]) + 1;
		
		var currentPrice = parseFloat(intervalsAndPrices[j][2].replace(/,/, "."));
		
		if (days >= currentSpan)
		{
			//price += intervalsAndPrices[j][1] * currentPrice;
			price += currentSpan * currentPrice;
		}
		else
		{
			price += days * intervalsAndPrices[j][2].replace(/,/, ".");
		}
		
		//days -= intervalsAndPrices[j][1];
		days -= currentSpan;
		j++;
	}
	
	var pricePerDay = 0.00;
			
	pricePerDay = parseFloat(price / txtDays.value);
	pricePerDay = Math.round(pricePerDay * 100) / 100;
	
	txtPricePerDay.value = pricePerDay.toString().replace(".", ",");
	document.getElementById('Data').value = "...Traficdev..." + pricePerDay.toString().replace(".", ",");
}

// Called when the information in a Quantity2 field, or a corresponding drop down list, is changed.
function OnQuantity2Change(obj)
{
	if (obj.tagName=="INPUT")
	{
		// Find corresponding drop down list with prices for different intervals
		var textBox = obj;
		var dropList = obj.nextSibling;
		while (dropList!=null && (dropList.tagName==null || dropList.tagName!="SELECT"))
			dropList = dropList.nextSibling;
		var thisValue = parseInt(textBox.value);
		
		// Extract all intervals from option texts in drop down box
		var intervalExp = new RegExp("([0-9]+)-([0-9]+)?", "i");
		for (var i = 0; i<dropList.options.length; i++)
		{
			var optionText = dropList.options[i].text;
			var found = intervalExp.exec(optionText);
			if (found!=null)
			{
				var firstValue = parseInt(found[1]);
				var lastValue = (found[2]!=null && found[2].length>0) ? parseInt(found[2]):9999999;
				if (thisValue>=firstValue && thisValue<=lastValue)
				{
					dropList.selectedIndex = i;
				}
			}
		}
	}
	else if (obj.tagName=="SELECT")
	{
		// Find corresponding text box with the length of the interval
		var textBox = obj.previousSibling;
		var dropList = obj;
		while (textBox!=null && (textBox.tagName==null || textBox.tagName!="INPUT"))
			textBox = textBox.previousSibling;
		
		var intervalExp = new RegExp("([0-9]+)-([0-9]+)?", "i");
		var optionText = dropList.options[dropList.selectedIndex].text;
		var found = intervalExp.exec(optionText);
		if (found!=null)
		{
			textBox.value = found[1];
		}
	}
}

// Call this method to make sure certain checkboxes are filled when submitting a form
function ValidateCheckBoxes(formId, fieldName)
{
	var inputElements = document.getElementsByTagName("input");
	
	// Don't bother to validate if we have no input-elements at all
	if (inputElements.length == 0)
	{
		return true;
	}
	
	// 'DB_Comment' is our default field name
	if (fieldName == undefined)
	{
		fieldName = "DB_Comment";
	}
	
	// Variable used to keep track of what should be sent along in the customer comment field
	var appendBoxes = new Array();
	
	// Find only those checkboxes that fullfill certain requirements
	for (var element = 0; element < inputElements.length; element++)
	{
		var obj = inputElements[element];
		
		if (obj.type == "checkbox")
		{
			var reqAttr = obj.getAttribute("required");
		
			if (reqAttr != null)
			{
				if (!obj.checked)
				{
					alert(reqAttr);
					
					return false;
				}
				else
				{
					appendBoxes.push(obj);
				}
			}
			else
			{
				if (obj.checked)
				{
					appendBoxes.push(obj);
				}
			}
		}
	}
	
	if (fieldName.length<3 || fieldName.substr(0, 3)!="DB_")
	{
		fieldName = "DB_" + fieldName;
	}
	
	var field = document.getElementsByName(fieldName);

	if (field != null && field.length > 0 && field[0].type == "text")
	{
		for (var box = 0; box < appendBoxes.length; box++)
		{
			field[0].value = field[0].value + "\r\n" + appendBoxes[box].getAttribute("notification") + "\r\n";
		}
	}
	else
	{
		var form = document.getElementById(formId);
		
		var tag = document.createElement("input");
		
		tag.id = "CreatedComment";
		
		tag.name = fieldName;
		
		tag.type = "hidden";
		
		for (var box = 0; box < appendBoxes.length; box++)
		{
			tag.value = tag.value + "\r\n"  + appendBoxes[box].getAttribute("notification") + "\r\n";
		}
		
		form.appendChild(tag);
	}

	return true;
}
function SaveLastVisited(type, id)
{
	if (document.cookie)
	{
		if (type=="sc" || type=="sp")
		{	
			var d = new Date();
			d.setDate(d.getDate() + 1);
			if (isNaN(id)==false && id<1000000)		
				document.cookie = "lv"+type+"="+id + ";expires=" + d;
		}
	}	
}

function GetLastVisited(type)
{
	if (document.cookie && document.cookie.length>0)
	{
		if (type=="sc" || type=="sp")
		{
			var start=document.cookie.indexOf("lv"+type+"=");
			if (start>-1)
			{ 
				
				start=start+5; 
				var end=document.cookie.indexOf(";",start);
				
				if (end==-1) 
					end=document.cookie.length;
				
				return document.cookie.substring(start,end);
			} 

		}	
	}
	
	return 0;
}
