
var przesX = 20;
var przesY = 10;
var _app = navigator.appName;


function calc_options()
{
	var program = '';
	
	var radio =  document.forms['calc_frm'].elements['c_prog'];
	for (i = 0; i < radio.length; i++)
	{
		if (radio[i].checked) program = radio[i].value;
	}

	if (program == 'light')
	{
		$('c_portale_row').style.display = 'table-row';
		$('c_website_row').style.display = 'table-row';
		$('c_limit_row').style.display = 'table-row';
	} else if (program == 'stand') {
		$('c_portale_row').style.display = 'table-row';
		$('c_website_row').style.display = 'none';
		$('c_limit_row').style.display = 'none';
	} else {
		$('c_portale_row').style.display = 'table-row';
		$('c_website_row').style.display = 'none';
		$('c_limit_row').style.display = 'none';
	}
}

function calc_solve()
{	
	var cena = 0;	
	var program = '';
	
	var konta   = trim(document.forms['calc_frm'].elements['c_loginy'].value);
	var portale = trim(document.forms['calc_frm'].elements['c_portale'].value);
	var website =  document.forms['calc_frm'].elements['c_strona'].checked;
	var extlimit =  document.forms['calc_frm'].elements['c_limit'].checked;
		
	var radio =  document.forms['calc_frm'].elements['c_prog'];
	for (i = 0; i < radio.length; i++)
	{
		if (radio[i].checked) program = radio[i].value;
	}
	
	$('c_message').style.display = 'none';  
	
	if (program == '')
	{
		$('c_message').innerHTML = 'wybierz wersję programu!';
		$('c_message').style.display = 'block';
		return false;
	}
	
	if (konta != '' && konta.match(/^[0-9]+$/)) 
	{
		if(konta > 0) 
		{
			var q = Math.floor(konta / 10);
			var l = konta % 10;
			
			if (program == 'light')
			{
				if (konta == 1)
				{
					cena = 0;
				} else if (konta == 2 || konta == 3) {
					cena = 30;
				} else {
					$('c_message').innerHTML = 'możesz wykupić maksymalnie 3 konta';
					$('c_message').style.display = 'block';
					return false;
				}
				
				if (portale > 0) {
					cena += portale * 19;
				}
				
				if (website) cena += 10;
				if (extlimit) cena += 30;
				
			} else if (program == 'stand') {
				
				if(konta <= 10)
				{
					cena = 49;
					if(l) cena += (l-1)*24;
					if(q) cena += 9*24;
				} else { 
					cena = 49 + 9*24; // pierwsze 10
					d  = konta - 10;
					if(d) cena += d*19;
				} 
				
				if (portale > 3) {
					cena += (portale - 3) * 19;
				}
			} else {
				
				if(konta <= 10)
				{
					cena = 99;
					if(l) cena += (l-1)*49;
					if(q) cena += 9*49;
				} else if(11 <= konta && konta <= 20) { 
					cena = 99 + 9*49;
					if(l) cena += (l)*44;
					if(q) cena += 44*(q-1)*10;
				} else if( 20 < konta && konta <= 40) {
					cena = 99 + 9*49 + 10*44;
					if(l) cena += (l)*39;
					if(q) cena += (q-2)*10*39;
				} else if( 40 < konta && konta <= 100) { 
					cena = 99 + 9*49 + 10*44 + 20*39;
					if(l) cena += (l)*34;
					if(q) cena += (q-4)*10*34;
				} else if( konta > 100) { 
					cena = 99 + 9*49 + 10*44 + 20*39 + 6*10*34; //do 100
					d  = konta - 100;
					if(d) cena += d*29;
				}
				
				if (portale > 6) {
					cena += (portale - 6) * 19;
				}
				
			}

			$('c_cena').innerHTML = cena + ' PLN/mc';
			$('c_display_cena').style.display = 'block';

		} else {
			$('c_display_cena').style.display = 'none';
		} 
		
	} else {
		$('c_display_cena').style.display = 'none';
	}
  
	return false;
	
}

function openImagePopup(img,wid,hei){
   if(!wid) wid=500;
   if(!hei) hei=500; 
   
   var photoB = window.open('','fwindow','left=100,top=100,width='+(wid+5)+',height='+(hei+5)+',innerheight='+(hei+5)+',innerwidth='+(wid+5)+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
   photoB.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Agencja 3000<'+'/title><'+'/head><body style="background-color:white;margin:0;padding:0"><a href="javascript:self.close();"><img src="'+img+'" border="0" alt="Zamknij okno" /><'+'/a><'+'/body><'+'/html>');
   photoB.focus();
   photoB.document.close();
  
}
function open_win(url, ramka, szer, wys) {

	przesX += 40;
	przesY += 20;
	if (przesX > 300) {
		przesX = 20;
		przesY = 10;
	}

	okno = window.open(url, ramka, 'left=' + przesX + ',top=' + przesY + ',width='+szer+',height='+wys+',innerheight='+wys+',innerwidth='+szer+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
	okno.focus();

	return okno;
}


function $() {
  if (arguments.length == 1) {
    return get$(arguments[0]);
  }
  var elements = [];
  $c(arguments).each(function(el){
    elements.push(get$(el));
  });
  return elements;

  function get$(el){
    if (typeof el == 'string') {
      el = document.getElementById(el);
    }
    return el;
  }
}

function check_email(string) {
    if (trim(string) == '') {
        return false;
    }
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(string))) {
        return false;
    } else {
        var illegalChars= /[\(\)\<\>\,\;\:\\"\[\]]/
        if (string.match(illegalChars)) {
            return false;
        }
    }
    
    if(!string.match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/)){
        return false;
    }	
    return true;
}

function hide_div(id) {
    document.getElementById(id).style.display = 'none';
}

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

function is_int(val) {
    return !isNaN(val) && trim(val) != '';
}

function jsTrim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
	strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
	strText = strText.substring(0, strText.length-1);

    return strText;
}

function validate_nip(inputVal) {
	var odp = false;
    var ar = inputVal.replace(/-/g,'');
    var arg = inputVal.replace(/-/g,'');
    var org = inputVal.replace(/-/g,'');
    
    arg = arg.substring(0,10);
    ar = ar.substring(0,10);
    
    var coefficients = '657234567';
    var sum=0;
    var index=8;
    for (index=8; index>=0 ;index--) {
        sum += (parseInt(coefficients.charAt(index)) * parseInt(arg.charAt(index)));
    }

    if ((sum % 11) == 10 ? false : ((sum % 11) == parseInt(arg.charAt(9)))) {
        odp = true;
    }
	//sprawdzenie czy sa takie same liczby w stringu
    if(arg.match(/^[0]+$/) || arg.match(/^[1]+$/) || arg.match(/^[2]+$/) || arg.match(/^[3]+$/) || arg.match(/^[4]+$/) 
	   || arg.match(/^[5]+$/) || arg.match(/^[6]+$/) || arg.match(/^[7]+$/) || arg.match(/^[8]+$/) ||
	   arg.match(/^[9]+$/)
	){
	    odp = false;
	}
	
	//sprawdzenie dlugosci nipu
	if(org.length > 10){
	   odp = false;
	}
	
    return odp;
}

function turnOn(name,active){ 
	fullname = 'img/a3k/'+name+'on.gif';
	obj = document.getElementById(name);
	if(!active){
	  obj.src = fullname;
	}
}

function turnOff(name,active){ 
	fullname = 'img/a3k/'+name+'off.gif';
	obj = document.getElementById(name);
	if(!active){
	  obj.src = fullname;
	}
}


function httpRequest() {
  var httpRequest;

  if (window.XMLHttpRequest) {
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) {
      httpRequest.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) {
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!httpRequest) {
    alert('Cannot create an XMLHTTP instance');
    return false;
  }
  return httpRequest;
}


function wyslij_kontakt(){
  var error = false;
  var focus = false;
  
  ereg = /^[0-9a-zA-Z.\-_\+]+\@[0-9a-zA-Z]+\.[0-9a-zA-Z.\-_\+]+$/;
  if (ereg.test(document.forms['zg_frm'].elements['email'].value) != true || document.forms['zg_frm'].elements['email'].value == 0) {
    $('email_blad').innerHTML = 'Wpisz poprawny adres email.';
    $('email_blad').style.display = 'block';
    error = 'email';
    if (!focus) {
      focus = 'email';
    }
  } else {
    $('email_blad').innerHTML = '';
    $('email_blad').style.display = 'none';
  }
  
  if (trim(document.forms['zg_frm'].elements['name'].value) == '') {
    $('name_blad').innerHTML = 'Proszę o wpisa�ćcć nazwę firmy';
    $('name_blad').style.display = 'block';
    error = 'name';
    if (!focus) {
      focus = 'name';
    }
  } else {
    $('name_blad').innerHTML = '';
    $('name_blad').style.display = 'none';
  }
  
  
  if (trim(document.forms['zg_frm'].elements['notes'].value) == '') {
    $('notes_blad').innerHTML ='Proszę wpisać komentarz'
    $('notes_blad').style.display = 'block';
    error = 'notes';
    if (!focus) {
      focus = 'notes';
    }
  } else {
    $('notes_blad').innerHTML = '';
    $('notes_blad').style.display = 'none';
  }
  
  if (error){
      return false;	
   }
    
	ajax = httpRequest();
    ajax.open('POST','send.php', true);
    
	var parameters = 
	 'zg_nazwa='+
     encodeURI(document.forms['zg_frm'].elements['name'].value)+
	 '&zg_email='+
     encodeURI(document.forms['zg_frm'].elements['email'].value)+
	 '&zg_telefon='+
     encodeURI(document.forms['zg_frm'].elements['phone'].value)+
     '&zg_notes='+
     encodeURI(document.forms['zg_frm'].elements['notes'].value)+
	 '&zg_type=kontakt';
	 
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.setRequestHeader("Content-length", parameters.length);
    ajax.setRequestHeader("Connection", "close");
	 
   
    ajax.onreadystatechange = function(){
    if (ajax.readyState == 4) {
    if (ajax.status == 200) {
      if (ajax.responseText == 1) {
		 /*alert("Zam�wienie zosta�o wys�ane");*/
		 $('zg_ret').innerHTML = 'Zapytanie zostało wysłane';
		 $('zg_ret').style.display = 'block';
		 $('zg_frm').style.display = 'none';
		 
      } else {
	  	 alert('Nie udało się wysłać informacji' +"\n"
		 +'Proszę spróbować jescze raz'); 
      }
     }
    }
  };
  ajax.send(parameters);
  return false;
  
  	
}
function wyslij(){
    
  var error = false;
  var focus = false;

  
  ereg = /^[0-9a-zA-Z.\-_\+]+\@[0-9a-zA-Z]+\.[0-9a-zA-Z.\-_\+]+$/;
  if (ereg.test(document.forms['zg_frm'].elements['email'].value) != true || document.forms['zg_frm'].elements['email'].value == 0) {
    $('email_blad').innerHTML = 'Wpisz poprawny adres email.';
    $('email_blad').style.display = 'block';
    error = 'email';
    if (!focus) {
      focus = 'email';
    }
  } else {
    $('email_blad').innerHTML = '';
    $('email_blad').style.display = 'none';
  }
  
  /*
  if (trim(document.forms['zg_frm'].elements['nazwa'].value) == '') {
    $('nazwa_blad').innerHTML = 'Proszę wpisać nazwę firmy';
    $('nazwa_blad').style.display = 'block';
    error = 'nazwa';
    if (!focus) {
      focus = 'nazwa';
    }
  } else {
    $('nazwa_blad').innerHTML = '';
    $('nazwa_blad').style.display = 'none';
  }
  */
 
  if (trim(document.forms['zg_frm'].elements['wojewodztwo'].value) == '') {
    $('wojewodztwo_blad').innerHTML = 'Proszę wybrać województwo';
    $('wojewodztwo_blad').style.display = 'block';
    error = 'wojewodztwo';
    if (!focus) {
      focus = 'wojewodztwo';
    }
  } else {
    $('wojewodztwo_blad').innerHTML = '';
    $('wojewodztwo_blad').style.display = 'none';
  }
  
  
  if (trim(document.forms['zg_frm'].elements['kod'].value) == '') {
    $('kod_blad').innerHTML = 'Proszę wpisać kod pocztowy';
    $('kod_blad').style.display = 'block';
    error = 'kod';
    if (!focus) {
      focus = 'kod';
    }
  } else {
    $('kod_blad').innerHTML = '';
    $('kod_blad').style.display = 'none';
  }
  
  
  if (trim(document.forms['zg_frm'].elements['miasto'].value) == '') {
    $('miasto_blad').innerHTML = 'Proszę podać nazwę miasta';
    $('miasto_blad').style.display = 'block';
    error = 'miasto';
    if (!focus) {
      focus = 'miasto';
    }
  } else {
    $('miasto_blad').innerHTML = '';
    $('miasto_blad').style.display = 'none';
  }
  
  
  if (trim(document.forms['zg_frm'].elements['adres'].value) == '') {
    $('adres_blad').innerHTML = 'Proszę wpisać nazwę ulicy';
    $('adres_blad').style.display = 'block';
    error = 'adres';
    if (!focus) {
      focus = 'adres';
    }
  } else {
    $('adres_blad').innerHTML = '';
    $('adres_blad').style.display = 'none';
  }
  
  /* 
  if (
  	   !validate_nip(trim(document.forms['zg_frm'].elements['nip'].value))
	  ) {
    $('nip_blad').innerHTML = 'Proszę wprowadzić prawidłowy nip';
    $('nip_blad').style.display = 'block';
    error = 'nip';
    if (!focus) {
      focus = 'nip';
    }
  } else {
    $('nip_blad').innerHTML = '';
    $('nip_blad').style.display = 'none';
  }
  */
  
  if (
        !trim(document.forms['zg_frm'].elements['loginy'].value).match(/^[0-9]+$/)   
  	  ) { 
    $('loginy_blad').innerHTML = 'Proszę wpisać prawidłową liczbę loginów';
    $('loginy_blad').style.display = 'block';
    error = 'loginy';
    if (!focus) {
      focus = 'loginy';
    }
  } else {
    $('loginy_blad').innerHTML = '';
    $('loginy_blad').style.display = 'none';
  }
  
  
  if (trim(document.forms['zg_frm'].elements['osoba_kontaktowa'].value) == '') {
    $('osoba_kontaktowa_blad').innerHTML = 'Proszę wpisać imię i nazwisko';
    $('osoba_kontaktowa_blad').style.display = 'block';
    error = 'osoba_kontaktowa';
    if (!focus) {
      focus = 'osoba_kontaktowa';
    }
  } else {
    $('osoba_kontaktowa_blad').innerHTML = '';
    $('osoba_kontaktowa_blad').style.display = 'none';
  }
  
  
   if (error){
      return false;	
   }
    
	ajax = httpRequest();
    ajax.open('POST','send.php', true);
    
    
	
	var parameters = 
	 //'zg_nazwa='+ encodeURI(document.forms['zg_frm'].elements['nazwa'].value)+
	 'zg_wojewodztwo='+
     encodeURI(document.forms['zg_frm'].elements['wojewodztwo'].value)+ 
	 '&zg_kod='+
     encodeURI(document.forms['zg_frm'].elements['kod'].value)+
     '&zg_miasto='+
     encodeURI(document.forms['zg_frm'].elements['miasto'].value)+
     '&zg_adres='+
     encodeURI(document.forms['zg_frm'].elements['adres'].value)+
     '&zg_telefon='+
     encodeURI(document.forms['zg_frm'].elements['telefon'].value)+
     '&zg_loginy='+
     encodeURI(document.forms['zg_frm'].elements['loginy'].value)+
     '&zg_osoba_kontaktowa='+
     encodeURI(document.forms['zg_frm'].elements['osoba_kontaktowa'].value)+
     '&zg_email='+
     encodeURI(document.forms['zg_frm'].elements['email'].value)+
	 '&zg_type=zgloszenie';
     
	 
	 
	 ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     ajax.setRequestHeader("Content-length", parameters.length);
     ajax.setRequestHeader("Connection", "close");
	 
   
   ajax.onreadystatechange = function(){
    if (ajax.readyState == 4) {
    if (ajax.status == 200) {
      if (ajax.responseText == 1) {
		 
		 $('zg_ret').innerHTML = 'Zamówienie zostało wysłane.';
		 $('zg_ret').style.display = 'block';
		 $('zg_frm').style.display = 'none';
		 
      } else {
	  	 alert('Nie udało się wysłać zamówienia' +"\n"
		 +'Proszę spróbować jeszcze raz'); 
      }
     }
    }
  };
  ajax.send(parameters);
  return false;	
}

function parse_XML(file){
  //alert(file);
  var xml;
  var xsl;
  
	
	if (window.XMLHttpRequest){

      var xmlDoc;
      var testTransform;

      var processor;
      var xslStylesheet;
      var xsltProcessor = new XSLTProcessor();

      // load the xslt file
      var myXMLHTTPRequest = new XMLHttpRequest();
          myXMLHTTPRequest.open("GET", "help/test.xsl", false);
          myXMLHTTPRequest.send(null);

     // get the XML document and import it
          xslStylesheet = myXMLHTTPRequest.responseXML;
          xsltProcessor.importStylesheet(xslStylesheet);

// load the xml file
		 myXMLHTTPRequest = new XMLHttpRequest();
		 myXMLHTTPRequest.open("GET", "help/toc.xml", false);
		 myXMLHTTPRequest.send(null);

		 var xmlSource = myXMLHTTPRequest.responseXML;
		 var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);

		document.getElementById('tree').appendChild(resultDocument);
		
		}
		
		else if (window.ActiveXObject){
		// load XML source document
		var source = new ActiveXObject("Msxml2.DOMDocument.4.0");
		source.async = false;
		source.load("help/toc.xml");

		// load XSLT stylesheet document
		var stylesheet = new ActiveXObject("Msxml2.DOMDocument.4.0");
		stylesheet.async = false;
		stylesheet.load("help/test.xsl");

		// transform the source using the XSLT stylesheet
	  tree.innerHTML = source.transformNode(stylesheet);
    }

}


