// JavaScript Document

var selectedRow = '';

function selectRow(rowNum, rowCount, colCount) {
	selectedRow = rowNum;
	var rowColor;
	for (var i = 1; i <= rowCount; i++ ) {
		if (i == rowNum) {
			rowColor = '#FFCC00';
		} else if (i % 2 == 1) {
			rowColor = '#EEEEEE';
		} else {
			rowColor = '#DDDDDD';
		}
		for (j = 1; j <= colCount; j++) {
			document.getElementById(j + '_row_' + i).style.background = rowColor;
		}
	}
}

function selectGroup(rowNum, rowCount, colCount) {
	document.getElementById('group_id').value = document.getElementById('0_row_' + rowNum).innerHTML;
	selectRow(rowNum, rowCount, colCount);
}

function selectContact(rowNum, rowCount, colCount) {
	document.getElementById('contact_id').value = document.getElementById('0_row_' + rowNum).innerHTML;
	selectRow(rowNum, rowCount, colCount);
}

function selectUser(rowNum, rowCount, colCount) {
	document.getElementById('user_id').value = document.getElementById('0_row_' + rowNum).innerHTML;
	selectRow(rowNum, rowCount, colCount);
}

function editRow(url) {
	if (selectedRow == ''){
		alert('Lūdzu iezīmējiet ierakstu!');
	} else {
		window.location = url + '?id=' + document.getElementById('0_row_' + selectedRow).innerHTML;
	}
}

function deleteRow(action) {
	if (selectedRow == ''){
		alert('Lūdzu iezīmējiet ierakstu!');
	} else {
		//window.location = 'save.php?id=' + document.getElementById('0_row_' + selectedRow).innerHTML + '&action='+ action;
	}
}

function viewRow(url) {
	if (selectedRow == ''){
		alert('Lūdzu iezīmējiet ierakstu!');
	} else {
		window.location = url + '?id=' + document.getElementById('0_row_' + selectedRow).innerHTML;
	}
}

function editGroup() {
	if (document.getElementById('ch_' + selectedRow).checked) {
		editRow('edit_group.php');
	} else {
		alert('Predefinētu grupu nedrīkst labot!');
	}
}

function deleteGroup() {
		alert('a');
	if (document.getElementById('ch_' + selectedRow).checked) {
		deleteRow('delete_group');
	} else {
		alert('Predefinētu grupu nedrīkst dzēst!');
	}
}

function deleteContact() {
	if (document.getElementById('ch_' + selectedRow).checked) {
		deleteRow('delete_contact');
	} else {
		alert('Predefinētu kontaktu nedrīkst dzēst!');
	}
}

function editContact(url, group_id) {
	if (selectedRow == ''){
		alert('Lūdzu iezīmējiet ierakstu!');
	} else {
		window.location = url + '?id=' + document.getElementById('0_row_' + selectedRow).innerHTML + '&group_id=' + group_id;
	}
}

function addContact(url, group_id) {
		window.location = url + '?group_id=' + group_id;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
