/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function isNumeric(formElement, message)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	formElement.value = trim(formElement.value);
	sText = formElement.value;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
				alert(message);
				formElement.focus();
			}
		}
	return IsNumber;
}

function isEqualPass(formElement, formElement2, message) 
{
	formElement.value = trim(formElement.value);
	formElement2.value = trim(formElement2.value);
	
	sText = formElement.value;
	sText2 = formElement2.value;
	
	var isEqual = true;
	var Char;
	
	if(sText == sText2) {
		isEqual = true;
	}
	else {
		isEqual = false;
		alert(message);
		formElement.focus();
	}
	return isEqual;
}

function validateCheckbox(chk){
	var error = "";
  if (chk.checked == 0)
    error = "Musisz zaakceptowaę regulamin\n";
  return error;
}

/*********************************************************
*                 FAN FUNCTIONS 
*********************************************************/


function checkAddFanForm()
{
	with (window.document.frmAddFan) {
		if (isEmpty(txtLogin, 'Wprowad¼ login')) {
			return;
		} 
		
		else if (isEmpty(txtPass, 'Wprowad¼ has³o')) {
			return;
		}
		else if (isEmpty(txtPass2, 'Powtórz has³o')) {
			return;
		}
		else if (!isEqualPass(txtPass, txtPass2, 'Has³a s± róæne' )) {
			return;
		}
		
		else if (isEmpty(txtEmail, 'Wprowad¼ e-mail')) {
			return;
		}
		
		else {
			submit();
		}
	}
}

function checkAddFanShopForm()
{
	with (window.document.frmAddFan) {
		if (isEmpty(txtLogin, 'Wprowad¼ login')) {
			return;
		} 
		
		else if (isEmpty(txtPass, 'Wprowad¼ has³o')) {
			return;
		}
		else if (isEmpty(txtPass2, 'Powtórz has³o')) {
			return;
		}
		else if (!isEqualPass(txtPass, txtPass2, 'Has³a s± róæne' )) {
			return;
		}
		else if (isEmpty(txtEmail, 'Wprowad¼ e-mail')) {
			return;
		}

		else if (isEmpty(txtImie, 'Wprowad¼ imiź')) {
			return;
		}
		else if (isEmpty(txtNazwisko, 'Wprowad¼ nazwisko')) {
			return;
		}
		else if (isEmpty(txtUlica, 'Wprowad¼ ulicź i numer domu')) {
			return;
		}
		else if (isEmpty(txtMiasto, 'Wprowad¼ miasto')) {
			return;
		}
		else if (isEmpty(txtKodPocztowy, 'Wprowad¼ kod pocztowy')) {
			return;
		}
		else if (isEmpty(txtWojewodztwo, 'Wprowad¼ województwo')) {
			return;
		}
		else if (isEmpty(txtKraj, 'Wprowad¼ kraj')) {
			return;
		}
		else if (isEmpty(txtTelefon, 'Wprowad¼ numer telefonu')) {
			return;
		}
		else if(validateCheckbox(akceptacja1) != "") {
			var message = validateCheckbox(akceptacja1);
			alert(message);
			return;
		}
		
		else {
			submit();
		}
	}
}

function checkModifyFanForm() 
{
	with (window.document.frmModifyFan) {
		if (isEmpty(txtEmail, 'Wprowad¼ e-mail')) {
			return;
		} 

		else {
			submit();
		}
	}
}

function checkModifyFanShopForm() 
{
	with (window.document.frmModifyFan) {
		if (isEmpty(txtEmail, 'Wprowad¼ e-mail')) {
			return;
		} 
		
		else if (isEmpty(txtImie, 'Wprowad¼ imiź')) {
			return;
		}
		else if (isEmpty(txtNazwisko, 'Wprowad¼ nazwisko')) {
			return;
		}
		else if (isEmpty(txtUlica, 'Wprowad¼ ulicź i numer domu')) {
			return;
		}
		else if (isEmpty(txtMiasto, 'Wprowad¼ miasto')) {
			return;
		}
		else if (isEmpty(txtKodPocztowy, 'Wprowad¼ kod pocztowy')) {
			return;
		}
		else if (isEmpty(txtWojewodztwo, 'Wprowad¼ województwo')) {
			return;
		}
		else if (isEmpty(txtKraj, 'Wprowad¼ kraj')) {
			return;
		}
		else if (isEmpty(txtTelefon, 'Wprowad¼ numer telefonu')) {
			return;
		}
		
		else {
			submit();
		}
	}
}

function addFan()
{
	window.location.href = 'index.php?view=add';
}

function changeFan(fanId)
{
	window.location.href = 'index.php?view=modify&fanId=' + fanId;
}

function deleteFan(fanId)
{
	if (confirm('Usun±ę kibica?')) {
		window.location.href = 'processFan.php?action=delete&fanId=' + fanId;
	}
}


/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


function checkPlainText(textBox)
{
	var alphaExp = /^[0-9a-zA-Z±ęź³ńó¶¼æ”ĘŹ£ŃÓ¦¬Æ\-\_\,\.\-\_\s\;\:\)\(\?\!]+$/;
	
	while (textBox.value.length > 0 && !textBox.value.match(alphaExp)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
}

function checkPlainTextUserName(textBox)
{
	var alphaExp = /^[0-9a-zA-Z\-\_]+$/;
	
	while (textBox.value.length > 0 && !textBox.value.match(alphaExp)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
}
