
function SetTab(sTabName) {

	if (document.getElementById(sTabName)) {
		document.getElementById(sTabName).className = 'current';
	}
	
}

function ShowModal(Url, Title, Width, Height) {
	Modalbox.show(Url, {title: Title, width: Width, height: Height, loadingString: 'Trwa ładowanie, proszę czekać', closeString: 'Zamknij', afterLoad: initTabs}); 

	
}

function initTabs() {
	new Control.Tabs('tab_group_one');
	
	/*
		sortowanie
	
	if ($('AddedFeatures_1')) {
		Sortable.create('AddedFeatures_1', { ghosting:true, constraint:false } );
		Sortable.create('AddedFeatures_2', { ghosting:true, constraint:false } );
		Sortable.create('AddedFeatures_3', { ghosting:true, constraint:false } );
	}
	*/
}

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


function CheckOptionField(Id, NoPermittedValue) {
	
	var FieldName = document.getElementById(Id).value;
	if (FieldName==NoPermittedValue) {
		document.getElementById(Id).style.backgroundColor = '#FFCCCC';
		return '1';
	}
	else {
		document.getElementById(Id).style.backgroundColor = '#FFFFFF';	
		return '0';
	}
}

function CheckTextField(Id)
{
	var FieldName = document.getElementById(Id).value;
	if (FieldName=='') {
		document.getElementById(Id).style.backgroundColor = '#FFCCCC';
		return '1';
	}
	else {
		document.getElementById(Id).style.backgroundColor = '#FFFFFF';	
		return '0';
	}
}

function CheckTextFieldByValue(Id, Value)
{
	var FieldName = document.getElementById(Id).value;
	if (FieldName=='' || FieldName == Value) {
		document.getElementById(Id).style.backgroundColor = '#FFCCCC';
		return '1';
	}
	else {
		document.getElementById(Id).style.backgroundColor = '#FFFFFF';	
		return '0';
	}
}


function CheckForm(sId) {
	var ErrorCounter  = 0;
	HideAllMessageInTab();
	
	switch (sId) {
		case 'edit-announcement':
			ErrorCounter += CheckTextField('ReferenceNumber');
			ErrorCounter += CheckTextField('Position');
			ErrorCounter += CheckTextField('AnnDescription');
			ErrorCounter += CheckTextField('JobTypeAddittion');
			ErrorCounter += CheckTextField('ClientDescription');
			ErrorCounter += CheckTextField('ClientContact');
			ErrorCounter += CheckTextField('Localization');
			ErrorCounter += CheckTextField('DateStart');
			ErrorCounter += CheckTextField('DateStop');
		break;
		
		case 'edit-user':
			ErrorCounter += CheckTextField('Login');
			ErrorCounter += CheckTextField('Password');
			ErrorCounter += CheckTextField('Surname');
		break;
		
		case 'new-branch':
			ErrorCounter += CheckTextField('NewBranchName');
		break;
		
		case 'new-client':
			ErrorCounter += CheckTextField('NewClientName');
		break;
		
		case 'new-type':
			ErrorCounter += CheckTextField('NewTypeName');
		break;
		
		case 'new-category':
			ErrorCounter += CheckTextField('NewCategoryName');
		break;
		
		case 'new_feature':
			ErrorCounter += CheckTextField('NewFeatureDescription');
		break;
	}
	
	if (ErrorCounter>0) {
		if (!UpdateMessageInTab(1, 'Nie wszystkie wymagane pola zostały wypełnione lub ich wartości są niepoprawne')) {
			alert('Nie wszystkie wymagane pola zostały wypełnione lub ich wartości są niepoprawne')
		}
		return false;
	}
	else {
		return true;
	}
}


function ValidateEmail(Id) {
	var FieldName = document.getElementById(Id).value;
	if (FieldName!='') {
		if ((FieldName.indexOf(".") > 2) && (FieldName.indexOf("@") > 0)) {
			document.getElementById(Id).style.backgroundColor = '#FFFFFF';	
			return '0';
		}
		else {
			document.getElementById(Id).style.backgroundColor = '#FFCCCC';
			return '1';
		}
	}
	else {
		document.getElementById(Id).style.backgroundColor = '#FFCCCC';
		return '1';
	}
}

function HideAllMessageInTab() {
	$('tab-errmsg').hide();
	$('tab-infmsg').hide();
}

function RefreshAlert() {
	alert('x');
}

function UpdateMessageInTab(Type, Message) {
	
	switch(Type) {
		case 1: //error
			Field = 'tab-errmsg';
		break;
		
		case 2: // info
			Field = 'tab-infmsg';
		break;
		
		default:
			return false;
	}
	
	if (document.getElementById(Field)) {
		if (Message != '') {
			$(Field).show();
			$(Field).update(Message);
		}
		else {
			$(Field).hide();
			$(Field).update(Message);
		}
		return true;	
	}
	else {
		return false;
	}
}

function InitLoader() {
	document.getElementById('loader').style.display = '';
}

function DestroyLoader() {
	document.getElementById('loader').style.display = 'none';
}
