var page = 0;

function goods_next()
{
	page++;
	if (page >= totalpages)
		page = totalpages;
	goods_load();
	return false;
}

function goods_prev()
{
	page--;
	if (page < 0)
		page = 0;
	goods_load();
	return false;
}

function goods_first()
{
	page = 1;
	goods_load();
	return false;
}

function goods_last()
{
	page = totalpages;
	goods_load();
	return false;
}

function goods_load() //function starts request
{
	JsHttpRequest.query("/ajax/goods/",
	{
		'page': page,
		'gid': gid,
		'search': search,
		'name': name,
		'code': code,
		'minprice': minprice,
		'maxprice': maxprice,
		'discount': discount,
		'printing': printing,
		'colors': colors,
		'discontinued': discontinued,
		'new': anew
	},
	function(result, errors)
	{
		document.getElementById("goods").innerHTML = errors;
		if (result)
		{
			document.getElementById("goods").innerHTML = result["content"];
			document.getElementById("curpage").innerHTML = (page+1);
		}
	},
	false); //queries server with selected page index and places results into box
	return false; //stop
}

function cart_put(id) //function starts request
{
	JsHttpRequest.query("/ajax/cart/",
	{
		'action': 'put',
		'add': id
	},
	function(result, errors)
	{
		if (result)
		{
			notify(result["msg"]);
		}
	},
	false);
	return false; //stop
}

function notify(msg)
{
	document.getElementById("notify").innerHTML = msg;
	document.getElementById("notify").style.display = 'block';
	setTimeout('notify_hide()', 1800);
}

function notify_hide()
{
	document.getElementById("notify").style.display = 'none';
}




function validateFormOnSubmit(theForm)
{
	var reason = "";
	reason += validateUsername(theForm.name);
	reason += validateEmail(theForm.email);
	reason += validatePhone(theForm.phone);
	reason += validateUsername(theForm.cname);
	reason += validateUsername(theForm.caddress);
	reason += validateUsername(theForm.pib);

	if (reason != "")
	{
		//alert("Some fields need correction:\n" + reason);
		return false;
	}

	return true;
}

function validateFormOnSubmitCont(theForm)
{
	var reason = "";
	reason += validateEmpty(theForm.to);
	reason += validateUsername(theForm.Name);
	reason += validateEmail(theForm.Email);
	reason += validateEmpty(theForm.Message);
	reason += validateEmpty(theForm.antibot);

	if (reason != "")
	{
		//alert("Some fields need correction:\n" + reason);
		return false;
	}

	return true;
}

function validateEmpty(fld)
{
	var error = "";

	if (fld.value.length == 0)
	{
		fld.style.background = 'Yellow';
		error = "The required field has not been filled in.\n"
	}
	else
	{
		fld.style.background = 'White';
	}
	return error;
}


function validateUsername(fld)
{
	var error = "";

	if (fld.value == "")
	{
		fld.style.background = 'Yellow';
		error = "You didn't enter a username.\n";
	}
	else
	{
		fld.style.background = 'White';
	}
	return error;
}

function trim(s)
{
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld)
{
	var error="";
	var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	if (fld.value == "")
	{
		fld.style.background = 'Yellow';
		error = "You didn't enter an email address.\n";
	}
	else if (!emailFilter.test(tfld))
	{              //test email for illegal characters
		fld.style.background = 'Yellow';
		error = "Please enter a valid email address.\n";
	}
	else if (fld.value.match(illegalChars))
	{
		fld.style.background = 'Yellow';
		error = "The email address contains illegal characters.\n";
	}
	else
	{
		fld.style.background = 'White';
	}
	return error;
}


function validatePhone(fld)
{
	var error = "";
	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

	if (fld.value == "")
	{
		error = "You didn't enter a phone number.\n";
		fld.style.background = 'Yellow';
	}
	else if (isNaN(parseInt(stripped)))
	{
		error = "The phone number contains illegal characters.\n";
		fld.style.background = 'Yellow';
	}
	else if (!(stripped.length > 7))
	{
		error = "The phone number is the wrong length.\n";
		fld.style.background = 'Yellow';
	}
	return error;
}
