function addRowToTable() {
	var tbl = document.getElementById('rma_items_table');
	var lastRow = tbl.rows.length;
	// if there's no header row in the table, then iteration = lastRow + 1
	var iteration = lastRow;
	var row = tbl.insertRow(lastRow);
	
	// left cell
	var cellLeft = row.insertCell(0);
	var el_left = document.createElement('input');
	el_left.type = 'text';
	el_left.name = 'rma_quantity_' + iteration;
	el_left.className = 'textfield_singleline rma_reg_textfield';
	el_left.id = 'rma_quantity_' + iteration;
	cellLeft.appendChild(el_left);
	
	// right cell
	var cellMiddle = row.insertCell(1);
	var el_middle = document.createElement('input');
	el_middle.type = 'text';
	el_middle.name = 'rma_code_' + iteration;
	el_middle.className = 'textfield_singleline rma_textfield';
	el_middle.id = 'rma_code_' + iteration;
	cellMiddle.appendChild(el_middle);
	
	// select cell
	var cellRight = row.insertCell(2);
	var el_right = document.createElement('input');
	el_right.type = 'text';
	el_right.name = 'rma_name_' + iteration;
	el_right.className = 'textfield_singleline rma_textfield';
	el_right.id = 'rma_name_' + iteration;
	cellRight.appendChild(el_right);
	
	// vis fjern link
	document.getElementById('rma_remove_line').style.visibility = 'visible';
	
	document.getElementById('number_items').value = parseInt(document.getElementById('number_items').value)+1;
}


function removeRowFromTable() {
	var tbl = document.getElementById('rma_items_table');
	var lastRow = tbl.rows.length;
	if (lastRow > 2) {
		tbl.deleteRow(lastRow - 1);
		if (lastRow == 3) {
			document.getElementById('rma_remove_line').style.visibility = 'hidden';
		}
	}
	
	document.getElementById('number_items').value = parseInt(document.getElementById('number_items').value)-1;
}

function saveRMA() {
	var error = false;
	for (field_id in required_fields['new']) {
		if (!required_fields['new'][field_id]['param'].test(document.getElementById(field_id).value)) {
			error = true;
			alert(required_fields['new'][field_id]['error']);
			document.getElementById(field_id).className = 'rma_textfield textfield_singleline_error';
			document.getElementById(field_id).onkeyup = function () {
				this.className = 'rma_textfield textfield_singleline';
				this.onkeyup = null;
			}
			
			document.getElementById(field_id).focus();
			break;
		}
	}
	
	if (!error) {
		document.forms['rma_form'].submit();
	}
}

function sendRMA() {
	var error = false;
	for (field_id in required_fields['send']) {
		if (!required_fields['send'][field_id]['param'].test(document.getElementById(field_id).value)) {
			error = true;
			alert(required_fields['send'][field_id]['error']);
			document.getElementById(field_id).className = 'rma_textfield textfield_singleline_error';
			document.getElementById(field_id).onkeyup = function () {
				this.className = 'rma_textfield textfield_singleline';
				this.onkeyup = null;
			}
			
			document.getElementById(field_id).focus();
			break;
		}
	}
	
	if (!error) {
		document.forms['rma_send_email_form'].submit();
	}
}

function openRMA(id, checksum) {
	w = 726;
	h = 800;
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	leftpos = (pageWidth-w)/2;
	toppos = (pageHeight-h)/2;
	
	eval("page = window.open('/rma_print.php?RMAnum="+id+"&Checksum="+checksum+"', 'RMA', 'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,width="+w+",height="+h+",left="+leftpos+",top="+toppos+"');");
}
