function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
	if(ok.indexOf(e.charAt(i))<0){ 
	return (false);
	}
	}

	if (document.images) {
	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!e.match(re) && e.match(re_two)) {
	return (-1);
	}
	}
}

function popup(url, p_width, p_height)
{
 var width  = p_width;
 var height = p_height;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}


function ValidateNewsletterForm()
{
	if ($('#frmRegTypeDesigner').prop("checked") == false && $('#frmRegTypeBasic').prop("checked") == false && $('#frmRegTypeCompany').prop("checked") == false){
		alert ("Określ typ wpisu.");
		$('#frmRegTypeDesigner').css("background-color", "#FFA0A0");
		$('#frmRegTypeBasic').css("background-color", "#FFA0A0")
		$('#frmRegTypeCompany').css("background-color", "#FFA0A0")
		return false;
	}
	
	if (document.getElementById('emailEntered').value != document.getElementById('emailConfirmation').value || document.getElementById('emailEntered').value == "") {
		alert("Błąd. Podaj dwukrotnie identyczny adres e-mail w obu polach.");
		$('#emailEntered').css("background-color", "#FFA0A0");
		$('#emailEntered').focus();
		$('#emailConfirmation').css("background-color", "#FFA0A0");
		return false;
	}
	
	if ($('#newsletterRulesAccepted').prop("checked")	== false) {
		alert("Aby kontynuować, musisz wyrazić zgodę na otrzymywanie korespondencji.");
		$('#newsletterRulesAccepted').css("background-color", "#FFA0A0");
		$('#newsletterRulesAccepted').focus();
		return false;
	}
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else
		countfield.value = maxlimit - field.value.length;
}

function ShowDesignerActivitiesDesc(){
	if ($('#frmTypDzialalnosci99').prop("checked") == true)
		$('#frmDescription').html('<textarea id="frmDescription" name="frmDescription" cols="30" rows="4"></textarea>');
	else
		$('#frmDescription').html('');
}

function ShowDesignerExtendedForm(){
	if ($('#frmWpisDoKataloguTak').prop("checked") == true) //jesli architekt robi wpis do katalogu
	{
		$('#frmHidden').show();
		$('#legalLine2').show();
		$('#legalLine1').hide();
	}
	else
	{
		$('#frmHidden').hide();
		$('#legalLine2').hide();
		$('#legalLine1').show();
	}
}

function ShowNewsletterForm(){
	$('#frmNewsletterDetails').show();
	$('#frmNewsletterArchitectForm').hide();
}


// AJAX FUNCTIONS
//Gets the browser specific XmlHttpRequest Object
function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		xmlhttp = false;
		}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp = false;
	}
	}
	return xmlhttp;
}

var searchReq = getHTTPObject();

//Starts the AJAX request.
function displaySubcategories() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('formCategoryListbox').value);
	
		//wyswietlenie subkategorii z zakresu wybranej kategorii
		searchReq.open("GET", '/inc/showSubcategories.php?search=' + str, true);
		searchReq.onreadystatechange = handleDisplaySubcategories;
		searchReq.send(null);
	}
}

function displayRating() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('formCurrentSubcategories').value);
		var company_id = escape(document.getElementById('formCompanyIdForRating').value);
		var suboffer_id = escape(document.getElementById('formSubofferIdForRating').value);
	
		//wyswietlenie ratingu dla wybranej podkategorii
		searchReq.open("GET", '/inc/ajaxGetSubcategoryRating.php?search=' + str +'&companyId=' + company_id +'&subofferId=' + suboffer_id, true);
		searchReq.onreadystatechange = handleDisplayRating;
		searchReq.send(null);
	}
}

function sendEmailMessage() {
	//walidacja
	if (document.getElementById('formContent').value == "") {
		alert("Wpisz treść wiadomości");
		document.getElementById('formContent').style.backgroundColor="#FFE070";
		$('#formContent').focus();
		return false;
	}
	
	if (document.getElementById('formSignature').value == "") {
		alert("Wprowadź podpis");
		document.getElementById('formSignature').style.backgroundColor="#FFE070";
		$('#formSignature').focus();
		return false;
	}
	
	if (!check_email(document.getElementById('formContactEmail').value)){
		alert("Błędny adres e-mail");
		document.getElementById('formContactEmail').style.backgroundColor="#FFE070";
		$('#formContactEmail').focus();
		return false;
	}

	//wysłanie wiadomości
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var messageContents = document.getElementById('formContent').value;
		var signature = document.getElementById('formSignature').value;
		var fromEmail = document.getElementById('formContactEmail').value;
	
		//wyswietlenie subkategorii z zakresu wybranej kategorii
		searchReq.open("POST", '/emailer.php?a=EMAIL_CONTACT_FORM', true);
		searchReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
		searchReq.onreadystatechange = handleSendEmailMessage;
		searchReq.send("SendEmailMessage=" + "true" +"&"+ "formContent=" + messageContents +"&"+ "formSignature=" + signature +"&"+ "formEmail=" + fromEmail);
	}
}


function sendEmailMessageA(email, msgid) {
	//walidacja
	if ($('#formContent'+msgid).val() == "") {
		alert("Wpisz treść wiadomości");
		document.getElementById('formContent'+msgid).style.backgroundColor="#FFE070";
		$('#formContent'+msgid).focus();
		return false;
	}

	if ($('#formSignature'+msgid).val() == "") {
		alert("Wprowadź podpis");
		document.getElementById('formSignature'+msgid).style.backgroundColor="#FFE070";
		$('#formSignature'+msgid).focus();
		return false;
	}

	if (!check_email($('#formFromEmail'+msgid).val())){
		alert("Błędny adres e-mail");
		document.getElementById('formFromEmail'+msgid).style.backgroundColor="#FFE070";
		$('#formFromEmail'+msgid).focus();
		return false;
	}

	//wysłanie wiadomości
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var messageContents = $('#formContent'+msgid).val();
		var signature = $('#formSignature'+msgid).val();
		var fromEmail = $('#formFromEmail'+msgid).val();

		searchReq.open("POST", '/emailer.php?a=EMAIL_CONTACT_A&cid='+email, true);
		searchReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
		searchReq.onreadystatechange = function(msgid) //handleSendEmailMessageA(msgid);
		{
			if (searchReq.readyState == 4) {
				var str = searchReq.responseText;
	
				if (str.length > 0)	{
						$('#ProgressIndicator'+msgid).hide();
						$('#StatusTxt'+msgid).html(str);
				}
				else {
					document.getElementById('SendEmailMessage'+msgid).style.backgroundColor="red";
				}
			}
			else
			{
				if (document.getElementById('SendEmailMessage'+msgid) != null)
				{
					document.getElementById('SendEmailMessage'+msgid).parentNode.removeChild(document.getElementById('SendEmailMessage'+msgid));
				}
	
				document.getElementById('ProgressIndicator'+msgid).style.visibility = "visible";
				document.getElementById('StatusTxt'+msgid).innerHTML="trwa wysyłanie wiadomości...";
			}
		}
		searchReq.send("SendEmailMessage=" + "true" +"&"+ "formContent=" + messageContents +"&"+ "formSignature=" + signature +"&"+ "formFromEmail=" + fromEmail);
	}
}


function sendEmailMessageToCompany(id) {
	//walidacja
	if (document.getElementById('formContent').value == "") {
		alert("Wpisz treść wiadomości");
		document.getElementById('formContent').style.backgroundColor="#FFE070";
		document.getElementById('formContent').focus();
		return false;
	}
	
	if (document.getElementById('formSignature').value == "") {
		alert("Wprowadź podpis");
		document.getElementById('formSignature').style.backgroundColor="#FFE070";
		document.getElementById('formSignature').focus();
		return false;
	}
	
	if (!check_email(document.getElementById('formFromEmail').value)){
		alert("Błędny adres e-mail");
		document.getElementById('formFromEmail').style.backgroundColor="#FFE070";
		document.getElementById('formFromEmail').focus();
		return false;
	}

	//wysłanie wiadomości
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var messageContents = $('#formContent').val();
		var signature = $('#formSignature').val();
		var fromEmail = $('#formFromEmail').val();
	
		searchReq.open("POST", '/emailer.php?a=EMAIL_CONTACT_COMPANY&cid='+id, true);
		searchReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
		searchReq.onreadystatechange = handleSendEmailMessage;
		searchReq.send("SendEmailMessage=" + "true" +"&"+ "formContent=" + messageContents +"&"+ "formSignature=" + signature +"&"+ "formFromEmail=" + fromEmail);
	}
}

function sendEmailMessageToCompanyNew(id) {
	//walidacja
	if ($('#formContent').val() == "") {
		alert("Wpisz treść wiadomości");
		document.getElementById('formContent').style.backgroundColor="#FFE070";
		$('#formContent').focus();
		return false;
	}
	
	if (document.getElementById('formSignature').value == "") {
		alert("Wprowadź podpis");
		document.getElementById('formSignature').style.backgroundColor="#FFE070";
		$('#formSignature').focus();
		return false;
	}
	
	if (!check_email(document.getElementById('formFromEmail').value)){
		alert("Błędny adres e-mail");
		document.getElementById('formFromEmail').style.backgroundColor="#FFE070";
		$('#formFromEmail').focus();
		return false;
	}

	//wysłanie wiadomości
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var messageContents = $('#formContent').val();
		var signature = $('#formSignature').val();
		var fromEmail = $('#formFromEmail').val();
	
		searchReq.open("POST", '/emailer.php?a=EMAIL_CONTACT_COMPANY_NEW&cid='+id, true);
		searchReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
		searchReq.onreadystatechange = handleSendEmailMessage;
		searchReq.send("SendEmailMessage=" + "true" +"&"+ "formContent=" + messageContents +"&"+ "formSignature=" + signature +"&"+ "formFromEmail=" + fromEmail);
	}
}

function sendEmailMessageToDesigner(id) {
	//walidacja
	if (document.getElementById('formContent').value == "") {
		alert("Wpisz treść wiadomości");
		document.getElementById('formContent').style.backgroundColor="#FFE070";
		document.getElementById('formContent').focus();
		return false;
	}
	
	if (document.getElementById('formSignature').value == "") {
		alert("Wprowadź podpis");
		document.getElementById('formSignature').style.backgroundColor="#FFE070";
		document.getElementById('formSignature').focus();
		return false;
	}
	
	if (!check_email(document.getElementById('formFromEmail').value)){
		alert("Błędny adres e-mail");
		document.getElementById('formFromEmail').style.backgroundColor="#FFE070";
		document.getElementById('formFromEmail').focus();
		return false;
	}
	
	//wysłanie wiadomości
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var messageContents = document.getElementById('formContent').value;
		var signature = document.getElementById('formSignature').value;
		var fromEmail = document.getElementById('formFromEmail').value;
	
		//wyswietlenie subkategorii z zakresu wybranej kategorii
		searchReq.open("POST", '/emailer.php?a=EMAIL_CONTACT_DESIGNER&cid='+id, true);
		searchReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
		searchReq.onreadystatechange = handleSendEmailMessage;
		searchReq.send("SendEmailMessage=" + "true" +"&"+ "formContent=" + messageContents +"&"+ "formSignature=" + signature +"&"+ "formFromEmail=" + fromEmail);
	}
}

function sendEmailMessageToServiceProvider(id) {
	//walidacja
	if (document.getElementById('formContent').value == "") {
		alert("Wpisz treść wiadomości");
		document.getElementById('formContent').style.backgroundColor="#FFE070";
		document.getElementById('formContent').focus();
		return false;
	}
	
	if (document.getElementById('formSignature').value == "") {
		alert("Wprowadź podpis");
		document.getElementById('formSignature').style.backgroundColor="#FFE070";
		document.getElementById('formSignature').focus();
		return false;
	}
	
	if (!check_email(document.getElementById('formFromEmail').value)){
		alert("Błędny adres e-mail");
		document.getElementById('formFromEmail').style.backgroundColor="#FFE070";
		document.getElementById('formFromEmail').focus();
		return false;
	}
	
	//wysłanie wiadomości
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var messageContents = document.getElementById('formContent').value;
		var signature = document.getElementById('formSignature').value;
		var fromEmail = document.getElementById('formFromEmail').value;
	
		//wyswietlenie subkategorii z zakresu wybranej kategorii
		searchReq.open("POST", '/emailer.php?a=EMAIL_CONTACT_SERVICEPROVIDER&cid='+id, true);
		searchReq.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8' ); 
		searchReq.onreadystatechange = handleSendEmailMessage;
		searchReq.send("SendEmailMessage=" + "true" +"&"+ "formContent=" + messageContents +"&"+ "formSignature=" + signature +"&"+ "formFromEmail=" + fromEmail);
	}
}



function showAlert(MyRequest)
{
	alert("Operacja AJAX nie powiodła się");
}

//Called when the AJAX response is returned.
function handleDisplaySubcategories() {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText.split("\n");
		if (str.length > 0)	{
			document.getElementById('formSubcategoryListbox').options.length = str.length - 2;
	
			var dlugosc = str.length - 2;
	
			for (var i=0; i<= dlugosc; i++) {
				var entry = new Array();
				entry = str[i].split('|');
	
				document.getElementById('formSubcategoryListbox').style.backgroundColor="#FFCC33";
	
				if (i >= 0){
				document.getElementById('formSubcategoryListbox').options[i] = new Option(entry[0] + " (" + entry[1] + ")", entry[1]);
				}
			}
		}
		else {
			document.getElementById('formSubcategoryListbox').style.backgroundColor="cyan";
		}
	}
	else
	{
		document.getElementById('formSubcategoryListbox').options.length = 0; //wyzerowanie pola select
		document.getElementById('formSubcategoryListbox').style.backgroundColor="#EFBC23";
		document.getElementById('formSubcategoryListbox').options[0] = new Option("trwa ładowanie...", "");
	}
}

function handleDisplayRating() {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText;
		if (str.length > 0)	{
				document.getElementById('formSubcategoryRating').style.backgroundColor="55ff55";
				$('#formSubcategoryRating').val(str);
				document.getElementById('formSubcategoryIdForRating').value=document.getElementById('formCurrentSubcategories').value;
		}
		else {
			document.getElementById('formSubcategoryRating').style.backgroundColor="cyan";
		}
	}
	else
	{
		$('#formSubcategoryRating').val("");
		document.getElementById('formSubcategoryRating').style.backgroundColor="red";
		$('#formSubcategoryRating').val("...");
	}
}

function handleSendEmailMessage() {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText;
	
		if (str.length > 0)	{
				$('#ProgressIndicator').hide();
				$('#StatusTxt').html(str);
		}
		else {
			document.getElementById('SendEmailMessage').style.backgroundColor="red";
		}
	}
	else
	{
		if (document.getElementById('SendEmailMessage') != null)
		{
			document.getElementById('SendEmailMessage').parentNode.removeChild(document.getElementById('SendEmailMessage'));
		}
	
		$('#ProgressIndicator').show();
		$('#StatusTxt').html("trwa wysyłanie wiadomości...");
	}
}


function handleSendEmailMessageA(msgid) {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText;

		if (str.length > 0)	{
				$('#ProgressIndicator'+msgid).hide();
				$('#StatusTxt'+msgid).html(str);
		}
		else {
			document.getElementById('SendEmailMessage'+msgid).style.backgroundColor="red";
		}
	}
	else
	{
		if (document.getElementById('SendEmailMessage'+msgid) != null)
		{
			document.getElementById('SendEmailMessage'+msgid).parentNode.removeChild(document.getElementById('SendEmailMessage'+msgid));
		}

		document.getElementById('ProgressIndicator'+msgid).style.visibility = "visible";
		document.getElementById('StatusTxt'+msgid).innerHTML="trwa wysyłanie wiadomości...";
	}
}


function skipToNext(chars, currentField, nextField)
{
	if (document.getElementById(currentField).value.length >= chars)
	{
		document.getElementById(nextField).focus();
		return (false);
	}
	return (true);
}

/* general doc.ready */
$(function(){
	function supports_html5_storage() {
		try {
			return 'localStorage' in window && window['localStorage'] !== null;
		} catch (e) {
			return false;
		}
	}
	
	//ini
	if(supports_html5_storage()){
		if(localStorage.getItem("menuInUse") == null){
			localStorage.setItem("menuInUse","nav_home")
		}
		
		$(".nav_level2 > ul").hide();
		$("."+localStorage.getItem("menuInUse")).show();
		
		$(".nav_level1 > ul > li").removeClass("active");
		$("#"+localStorage.getItem("menuInUse")).addClass("active");
	}
	
	//Click
	$(".nav_level1 > ul > li").click(function(){
		
		$(".nav_level1 > ul > li").removeClass("active");
		$(".nav_level2 > ul").hide();
		$(this).addClass("active");
		
		localStorage.setItem("menuInUse",$(this).prop("id"));
		
		$("."+$(this).prop("id")).show();
		
	});

});
