/*
 * File		: script.js (www.leab.biz v2.0)
 * Name		: MouseOver
 * Author	: Stefan Westling <stefan@westlingit.com>
 * Date		: 2001-10-15
 */

// Fix trim function for strings
String.prototype.trim = function() { return this.replace(/^\r+|^\n+|^\s+|\s+$|\n+$|\r+$/g, ''); }

// Preload images
var overimg = new Array();
var outimg = new Array();
var names = new Array("btn_company","btn_stock","btn_owners","btn_services");
for(i=0; i<names.length; i++)
{
	overimg[names[i]] = new Image(); 
	overimg[names[i]].src = "images/"+names[i]+"_on.gif";
	outimg[names[i]] = new Image();
}

// Functions for mouse over effect
function over(img){
	outimg[img].src = document.images[img].src;
	document.images[img].src = overimg[img].src;
}
function out(img){
	document.images[img].src = outimg[img].src;
}

// Functions for mouse over table effect
function overtd(td) {
	td.style.background = "#f0f0f0";
}
function outtd(td) {
	td.style.background = "#ffffff";
}

// Functions for validating and sending of form data
function validate() {
	if(document.form1.company.value.length==0) {
		document.form1.company.focus();
		return "Please, fill in your company name";
	}
	else if(document.form1.fname.value.length==0) {
		document.form1.fname.focus();
		return "Please, fill in your first name";
	}
	else if(document.form1.lname.value.length==0) {
		document.form1.lname.focus();
		return "Please, fill in your last name";
	}
	else if(document.form1.phone.value.length==0) {
		document.form1.phone.focus();
		return "Please, fill in your phone number";
	}
	else if(document.form1.fax.value.length==0) {
		document.form1.fax.focus();
		return "Please, fill in your fax number";
	}
	else if(document.form1.email.value.search(/[\w-\.]+\@[\w-\.]+\.\w{2,3}/gi)) {
		document.form1.email.focus();
		return "Please, fill in a valid email address";
	}
	else
		return ""
}
function send() {
	var msg = validate();
	if(msg.length==0) {
		document.form1.submit();
	}
	else {
		alert(msg);
	}
}
// Functions for validating and sending of form data
function sendRequest() {
	if(document.form1.pn.value.length==0) {
		document.form1.pn.focus();
		alert("Please, fill in the partnumber you are interested in");
	}
	else if(document.form1.quantity.value.length == 0 || isNaN(document.form1.quantity.value)) {
		document.form1.quantity.focus();
		alert("Please, fill in the quantity");
	}
	else if(document.form1.email.value.search(/[\w-\.]+\@[\w-\.]+\.\w{2,3}/gi)) {
		document.form1.email.focus();
		alert("Please, fill in a valid email address");
	}
	else
		document.form1.submit();
}

/* Tab-Menu */

if (document.all) {navigator.family = "ie4"}
if (window.navigator.userAgent.toLowerCase().match(/gecko/)) {navigator.family = "gecko"}
if (window.navigator.userAgent.toLowerCase().indexOf('opera') != -1) {navigator.family = 'opera';}

toplevel="4";
tabcount = 9;

function DoChanges(num)
{
	toplevel++;
	var box = "box" + num;

	if (navigator.family == "ie4")
	{
		document.all[box].style.zIndex = toplevel;
		i=1;
		for (i=1; i<=tabcount; i++)
		{
			var mytab="tab" + i;
			document.all(mytab).className = "tab";
			if (i == num) 
			{
				document.all(mytab).className = "thistab";
			}
		}
	}

	if (navigator.family == "opera" || navigator.family == "gecko")
	{
		document.getElementById(box).style.zIndex = toplevel;
		i=1;
		for (i=1; i<=tabcount; i++)
		{
			var mytab="tab" + i;
			document.getElementById(mytab).className = "tab";
			if (i == num) 
			{
				document.getElementById(mytab).className = "thistab";
			}
		}
	}
}

function showClock() {
	var clock = document.getElementById("clock");
	clock.style.visibility = "visible";
	clock.style.left = (document.body.clientWidth/2) - 190;
	clock.style.top = (document.body.clientHeight/2) - 100;
}

function hideClock() {
	var clock = document.getElementById("clock")
	clock.style.visibility = "hidden";	
}

function preSelectItem(selectList, itemToSelect){  // Get a reference to the drop-down
	for (iLoop = 0; iLoop< selectList.options.length; iLoop++)  {    
		if (selectList.options[iLoop].value == itemToSelect)    {
			// Item is found. Set its selected property, and exit the loop      
			selectList.options[iLoop].selected = true;      
			break;    
		}  
	}
}

function startMultiSearch() {
	limit = 50;
	secondsPerSearch = 0.136;
	partcount = document.getElementById('pns').value.trim().split('\n').length;
	if(partcount > limit) {
		if(!confirm("The limit for the number of parts in one search is att the moment "+limit+" pcs!\nDo you want to continue the search with just the first "+limit+" pcs?"))
			return false;
		partcount = limit;
	}
	parts = document.getElementById('pns').value.split('\n').slice(0,limit);
	document.getElementById('pns').value = parts.join('\n');
	form = document.getElementById('form1');
	form.submit();
	document.getElementById('busy').style.display = 'block';
	document.getElementById('indicator').src = 'images/rotating_arrow.gif';
	seconds = Math.round(partcount * secondsPerSearch);
	if(seconds < 45) {
		time = seconds + " seconds";
	}
	else if(seconds < 90) {
		time = "1 minute";
	}
	else {
		minutes = Math.round(seconds/60);
		time = minutes + " minutes";
	}
	document.getElementById('count').innerHTML = time;
}

function addrow() {
	var tbl = document.getElementById('parts');
	var lastRow = tbl.rows.length-1;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);
	row.id = "row_"+iteration;
	// left cell
	//var cellLeft = row.insertCell(0);
	//var textNode = document.createTextNode(iteration);
	//cellLeft.appendChild(textNode);
	
	// left cell
	var cellLeft = row.insertCell(0);
	var deleteButton = document.createElement('input');
	deleteButton.type = 'button';
	deleteButton.value = '-';
	deleteButton.onclick = function() {
		tbl.deleteRow(tbl.rows.length-2);
		document.getElementById('number_of_parts').value = document.getElementById('number_of_parts').value - 1;
	};
	cellLeft.appendChild(deleteButton);
	
	// partno cell
	var cellPartNo = row.insertCell(1);
	txtPartNo = getTextBox("partno", iteration, "");
	cellPartNo.appendChild(txtPartNo);
	
	// qty cell
	var cellPartNo = row.insertCell(2);
	txtQty = getTextBox("qty", iteration, "1");
	cellPartNo.appendChild(txtQty);
	
	// price cell
	var cellPartNo = row.insertCell(3);
	txtPrice = getTextBox("price", iteration, "0");
	cellPartNo.appendChild(txtPrice);

	// currency
	var cellCur = row.insertCell(4);
	var textNode = document.createTextNode("EUR");
	cellCur.appendChild(textNode);
	
	document.getElementById('number_of_parts').value = iteration-1;
}

function getTextBox(name, iteration, value) {
 	var txtBox = document.createElement('input');
	txtBox.type = 'text';
	txtBox.name = name + "_" + iteration;
	txtBox.id = name + "_" + iteration;
	txtBox.value = value
	txtBox.size = 20;
	txtBox.className = "textfield";
	return txtBox;	
}

function sendCheckPart() {
	f = document.getElementById('checkform');
	if(f.company.value.length==0) {
		f.company.focus();
		alert("Please, fill in your company name");
	}
	else if(f.email.value.search(/[\w-\.]+\@[\w-\.]+\.\w{2,3}/gi)) {
		f.email.focus();
		alert("Please, fill in a valid email address");
	}
	else if(f.supplier_name.value.length==0) {
		f.supplier_name.focus();
		alert("Please, fill in the suppliers name");
	}
	else if(f.supplier_email.value.search(/[\w-\.]+\@[\w-\.]+\.\w{2,3}/gi)) {
		f.supplier_email.focus();
		alert("Please, fill in a valid supplier email address");
	}
	else if(f.partno_1.value.length == 0) {
		f.partno_1.focus();
		alert("Please, fill in the partnumber!");
	}
	else
		f.submit();
	/*	
	else {
		var ok = true;
		for(i=1; i<f.number_of_parts; i++) {
			if(f['partno_'+i].value.length==0) {
				f['partno_'+i].focus();
				alert("Please, fill in the partno!");
				ok = false;
			}
		}
		if(ok) {
			f.submit();
		}
	}
	*/
}
