/* ----------------------------------------------------------------------
	$Id: form.js,v 1.3 2009-03-12 15:25:39 rbernaczek Exp $
---------------------------------------------------------------------- */

var attachedFiles = 0;

$(document).ready( function() {
	$("#f_file").css("display", "block");
	addButonOnFile();
});

function addNextFile(){
	if (attachedFiles == 0){
		$("#f_file_area").css("display", "block");
		$("#f_hint").css("display", "block");
	}
	var hp = $("<p id='ddd'><input type='file' name='attachment_" + Math.random() + "' size='45' /></p>").appendTo("#f_file_area");
	var ha = $("<a href='#1' title='Usu\u0144 plik'>Usu\u0144</a>").appendTo(hp);
	$(ha).click( function(e) {
		$(e.target.parentNode).remove();
		attachedFiles --;
		if (attachedFiles == 0){
			$("#f_file_area").css("display", "none");
			$("#f_hint").css("display", "none");
			addButonOnFile();
		}
	});
	attachedFiles ++;
	if (attachedFiles == 1)
		addButonMoreFile();
}

function addButonOnFile(){
	$("#f_next_file_button").remove();
	var hd = $("<div class='db_button db_attach_file' id='f_file_button'></div>").appendTo("#f_file");
	var ha = $("<a href='#1' title='Za\u0142\u0105cz plik'>Za\u0142\u0105cz plik</a>").appendTo(hd);
	$(ha).click( function(e) {
		addNextFile();
	});
}

function addButonMoreFile(){
	$("#f_file_button").remove();
	var hd = $("<div class='db_button db_attach_next_file' id='f_next_file_button'></div>").appendTo("#f_file");
	var ha = $("<a href='#1' title='Dodaj kolejny plik'>Dodaj kolejny plik</a>").appendTo(hd);
	$(ha).click( function(e) {
		addNextFile();
	});
}


function makeSpamBotTest() {
	var tc = document.getElementById('spambot_test');
	var tv = document.getElementById('form_request_test').value;
	var ti = document.createElement("INPUT");
	ti.setAttribute('type', 'hidden');
	ti.setAttribute('id', 'form_request_tested');
	ti.setAttribute('name', 'tested');
	ti.setAttribute('value', tv);
	tc.appendChild(ti);
}


/* ----------------------------------------------------------------------
	DEFAULT VALIDATION FORMS
 ---------------------------------------------------------------------- */
// class
function addClass(el, c) {
	if (!containsClass(el, c))
		el.className += " " + c;
}
function removeClass(el, c) {
	el.className = el.className.replace(new RegExp(c, "g"), "");
}
function containsClass(el, c) {
	return el.className.indexOf(c) != -1
}

function checkInput(inputId, errorMessage) {
	var input = document.getElementById(inputId);
	if (!input) {
		alert("Input " + inputId + " not found!");
		return false;
	}

	if (input.value == "")
		return focusFailedInput(inputId, errorMessage);

	return true;
}

function compareFields(fieldId1, fieldId2, errorMessage) {
	var field1 = document.getElementById(fieldId1);
	if (!field1) {
		alert("Element " + fieldId1 + " not found!");
		return false;
	}

	var field2 = document.getElementById(fieldId2);
	if (!field2) {
		alert("Element " + fieldId2 + " not found!");
		return false;
	}

	if (field1.value != field2.value)
		return focusFailedInput(fieldId2, errorMessage);

	return true;
}

function compareFieldsNotEmpty(fieldId1, fieldId2, errorMessage) {
	var field1 = document.getElementById(fieldId1);
	if (!field1) {
		alert("Element " + fieldId1 + " not found!");
		return false;
	}

	var field2 = document.getElementById(fieldId2);
	if (!field2) {
		alert("Element " + fieldId2 + " not found!");
		return false;
	}

	if (field1.value == '' || field1.value != field2.value)
		return focusFailedInput(fieldId2, errorMessage);

	return true;
}

function checkTextarea(inputId, errorMessage) {
	var input = document.getElementById(inputId);
	if (!input) {
		alert("Textarea " + inputId + " not found!");
		return false;
	}

	if (input.value.length <= 3)
		return focusFailedInput(inputId, errorMessage);

	return true;
}

function checkEmail(inputId, errorMessage) {
	var input = document.getElementById(inputId);
	if (!input) {
		alert("Input " + inputId + " not found!");
		return false;
	}

	if (!isValidEmail(input.value)) {
		focusFailedInput(inputId, errorMessage);
		return false;
	}
	return true;
}

function isValidEmail(email) {
	var template = /^[0-9a-z]+[0-9a-z._-]*\@[0-9a-z]+[0-9a-z._-]*\.[0-9a-z]{2,}$/i;
	if (template.test(email) == false)
		return false;
	return true;
}

function clearError() {
	var labels = document.getElementsByTagName("label");

	for ( var i = 0; i < labels.length; i++) {
		var label = labels[i];
		removeClass(label, "error");
	}
	return true;
}

function focusFailedInput(inputId, errorMessage) {
	var labels = document.getElementsByTagName("label");

	var tmplabel;
	// set error class to correct label and remove error class from others
	for ( var i = 0; i < labels.length; i++) {
		var label = labels[i];
		removeClass(label, "error");
		// if anything will be wrong, remove break statement
		// KCI -> KCI i think it is wrong 'couse it coulnd not loop to the end
		if (label.htmlFor == inputId) {
			label.className += " error";
			break;
		}
	}

	var element = document.getElementById(inputId);
	if (errorMessage)
		myAlert(errorMessage, element);

	return false;
}

function checkRadio(form, input, errorMessage, inputId) {
	var tmpForm = document.getElementById(form);
	if (tmpForm)
		form = tmpForm;

	if (!form[input]) {
		alert("Element " + input + " not found!");
		return false;
	}

	if (!form[input].length) // 1 input
	{
		if (form[input].checked)
			return true;
		return focusFailedInput(inputId, errorMessage);
	}

	var i = 0;
	for (i; i < form[input].length; i++)
		if (form[input][i].checked == true)
			break;

	if (i == form[input].length)
		return focusFailedInput(inputId, errorMessage);

	return true;
}

function groupFields(name, id, cnt, message) {
	var count = parseInt(cnt);
	for ( var i = 1; i <= count; i++)
		if (document.getElementById(id + "_" + i, message).value == "")
			return true;
	return focusFailedInput(id + "_1", message);
}

function groupEmails(name, id, cnt, message) {
	var template = /^[0-9a-z]+[0-9a-z._-]*\@[0-9a-z]+[0-9a-z._-]*\.[0-9a-z]{2,}$/i;
	var count = parseInt(cnt);
	for ( var i = 1; i <= count; i++)
		if (template.test(document.getElementById(id + "_" + i, message).value) == true)
			return true;
	return focusFailedInput(id + "_1", message);
}

//prettier dialog
//errorMesssage - display message
//elementFocus - set focus to element after hide alert (optional)
function myAlert(errorMessage, elementFocus)
{
	var box = new AlertBox();
	box.addMessage(errorMessage);
	box.addPostElement(elementFocus);
	box.addButton("OK","Zamknij","button",false);
	box.show();
	box.addShadow();
	return false;
}

function AlertBox()
{
	var dialogAlertId = "dialog_alert";
	
	var elementFocus = null;
	var buttons = new Array();
	var returnVal = null;
	
	var alertBg = document.createElement("div");
	alertBg.setAttribute("id", dialogAlertId);
	document.getElementsByTagName("body").item(0).appendChild(alertBg);

	var dialog = document.createElement("div");
	dialog.className = "dialog";
	dialog.onclick = function(e) { e = e||event; e.cancelBubble = true; return false; }
	alertBg.appendChild(dialog);
	
	this.addShadow = function()
	{
		// shadow
		var shadow = document.createElement("div");
		shadow.className = "shadow";
		shadow.style.position = "absolute";
		shadow.style.background = "#000";
		shadow.style.top = (dialog.offsetTop + 3) + "px";
		shadow.style.left = (dialog.offsetLeft + 3) + "px";
		shadow.style.width = dialog.offsetWidth + "px";
		shadow.style.height = dialog.offsetHeight + "px";
		shadow.style.opacity = 0.15;
		shadow.style.filter = "alpha(opacity=15)";
		alertBg.appendChild(shadow);		
	}
	
	this.addPostElement = function(elem)
	{
		elementFocus = elem;
	}
	
	this.addMessage = function(msg)
	{
		var text = document.createElement("div");
		text.className = "text";
		text.innerHTML = msg;
		text.onselectstart = new Function("return false");
		dialog.appendChild(text);
	}
	
	this.addButton = function(text, title, className, ret)
	{
		var button = document.createElement("a");
		button.setAttribute("href","#");
		button.innerHTML = text;
		button.className = className;
		button.onfocus = function() { addClass(this,"focus"); }
		button.title = title;
		button.onblur = function() { removeClass(this,"focus"); }
		dialog.appendChild(button);
		button.onclick = function()
		{
			alertBg.parentNode.removeChild(alertBg);
			if(elementFocus) elementFocus.focus();
			return false;
		}
		buttons[buttons.length] = button;
	}
	
	this.show = function()
	{
		var scroll = getPageScroll();
	
		alertBg.style.width = document.documentElement.scrollWidth + "px";
		alertBg.style.height = document.documentElement.scrollHeight + "px";
		
		var innerHeight = window.innerHeight ? window.innerHeight : ( screen.availHeight - 100 )
		
		dialog.style.position = "absolute";
		dialog.style.top = ( ( scroll.y + ( innerHeight/2 )  ) - ( dialog.offsetHeight / 2 ) ) + "px";
		dialog.style.left = ( (parseInt(alertBg.style.width) / 2) - ( dialog.offsetWidth / 2 ) ) + "px";
		
		alertBg.onclick = function()
		{
			dialog.className += " alert";
			setTimeout( function()
			{
				removeClass(dialog,"alert");
				if(buttons.length == 1) buttons[0].focus();
			}, 1000 );
		}
		
		buttons[buttons.length - 1].focus();
	}
	
	function getPageScroll()
	{
		var scroll = new Object();
		if (self.pageYOffset)
		{
			scroll.x = self.pageXOffset;
			scroll.y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			scroll.x = document.documentElement.scrollLeft;
			scroll.y = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			scroll.x = document.body.scrollLeft;
			scroll.y = document.body.scrollTop;
		}
		return scroll;
	}
}