/* usefull util functions */

function putHtml( id, str ) {
	var h = ( typeof( id ) == 'object' ) ? id : get( id );
	h.innerHTML = str;
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;
	for (var intLoop=0; intLoop < opt.length; intLoop++) {
		if ( opt[ intLoop ].selected ) {
			index = selected.length;
			selected[ index ] = new Object;
			selected[ index ].value = opt[ intLoop ].value;
			selected[ index ].index = intLoop;
	 }
  }
  return selected[ index ];
}

function addEvent(obj, type, fn){
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
	}
	else if (obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function() { obj['e'+type+fn](window.event); }
		obj.attachEvent('on'+type, obj[type+fn]);
	}
}

function checkNumber(e)
{
	var numbers = '0123456789- ';
	var key     = window.event ? window.event.keyCode : e ? e.which : null;
	var keychar = String.fromCharCode(key);
	return ( numbers.indexOf(keychar)!=-1 || key==null || key==0 || key==8 || key==9 || key==13 || key==27 );
}

function validateNumber(e){
	var numbers = new RegExp( "^[0-9]+$" );
	return numbers.test( e );
}

function clearLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == 'adres e-mail' ) {
				get( id ).value = '';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == 'hasło' ) {
				get( id ).value = '';
			}
			break;
	}
}

function fillLogin( id ) {
	switch( id ) {
		case 'public_login':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'adres e-mail';
			}
			break;
		case 'public_password':
			if( trim( getValue( id ) ) == '' ) {
				get( id ).value = 'hasło';
			}
			break;
	}
}


function get( id ) {
	return document.getElementById( id );
}

function getValue( str ) {
	var st = get( str );
	if( st != null ) {
		return trim( st.value );
	} else {
		return ' ';
	}
}


function swapHashCode() {
	var h		= get( 'hashcode' );
	h.value = 'foto';
}

function hide( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		h.style.display = "none";
	} else {
//		alert( 'błąd (hide)' );
	}
}

function show( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		h.style.display = "block";
	}
}

function changeVisible( str ) {
	var h = ( typeof( str ) == 'object' ) ? str : get( str );
	if( h != undefined ) {
		if( h.style.display == 'none' ) {
			h.style.display = 'block';
		} else {
			h.style.display = 'none';
		}
	} else {
		alert( 'błąd (changeVisible)' );
	}
}

function trimLeft( s ) {
	while( s.substring( 0, 1 ) == " " ){
		s = s.substr( 1 );
	}
	return s;
}

function inArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return true;
			}
	}
	return false;
}

function indexOfArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return i;
			}
	}
	return -1;
}

function trimRight( s ) {
	while( s.charAt( s.length - 1 ) == " " ) {
		s = s.substr( 0, s.length - 1 );
	}
	return s;
}

function trim( s ) {
	s = trimLeft( s );
	s = trimRight( s );
	return s;
}

/*BKR - do ankiety */
function fillFromChecklist(element,limit,question) {


	var position, value, br, pos;
	var chbxy = new Array();
	var text_id = "l"+element.id;
	value = element.value;
	pos = element.id.indexOf('_q');
	pos = element.id.substring(pos);
	var el_lab = document.getElementById(text_id);
	var v_text = document.getElementById(text_id).innerHTML;

	if (limit == 0) {
		if(element.checked) {
			el_lab.className = "checkbox_checked";
		}
		else if (element.checked == false) {
			el_lab.className = "checkbox_unchecked";
		}
		return false;
	}

	if(element.checked) {
		for(i=1;i<=limit;i++) {
			position = document.getElementById('pozycja'+i+'_q'+question);
			if(position.innerHTML == '' || position.innerHTML == undefined) {
				//position.innerHTML = value;
				position.innerHTML = v_text;
				i = eval(limit+"+"+1);
			}
		}
		el_lab.className = "checkbox_checked";
	}
	else {
		for(i=1;i<=limit;i++) {
			position = document.getElementById('pozycja'+i+'_q'+question);
			//if(position.innerHTML == value) {
			if(position.innerHTML == v_text) {

				position.innerHTML = '';
				i = eval(limit+"+"+1);
			}
		}
		el_lab.className = "checkbox_unchecked";
	}
	checkowane = 0;
	chbxy = document.getElementsByTagName('input');
	for(i=1;i<=limit;i++) {
		position = document.getElementById('pozycja'+i+'_q'+question);
		if(position.innerHTML == '' || position.innerHTML == undefined) {	}
		else {
			checkowane++;
		}
	}

	for(i=0;i<chbxy.length;i++) {
		if(chbxy[i].type == 'checkbox' && chbxy[i].id.substring(chbxy[i].id.indexOf('_q')) == pos) {
			if(!(chbxy[i].checked)) {
				if(checkowane == limit) {
					chbxy[i].disabled = true;
				}
				else {
					chbxy[i].disabled = false;
				}
			}
		}
	}

	document.getElementById('pusty_a').click();
}

function znaki(textar, liczba) {
	if(liczba == '') { liczba = 350;}
	var ilosc = (liczba - textar.value.length);
	if(ilosc < 0) {
		textar.value = textar.value.substring(0,liczba);
		ilosc = 0;
	}
	//if(div == '') { div ='znaczki';	}
	//document.getElementById(div).innerHTML = ilosc;
}

function karta_skarbonka(element,element_text) {
	if(element.checked) {
		element_text.readonly = false;
	}
	if(element.checked == false) {
		element_text.readonly = true;
	}
}

function valid() {
	var form = document.getElementById('forma');
	var fake = 1;
	var faked = 0;
	var faking = new Array();
	for(i=0;i<form.length;i++) {
		if(form[i].tagName.toUpperCase() == 'INPUT' && form[i].name.substr(0,1) == 'v' && form[i].value == '1' ) {
			var qid = form[i].name.substring(1);
			faking[qid] = 1;
			for(ii=0;ii<form.length;ii++) {
				var re = new RegExp("(a[0-9]*)?_q"+qid+"$", "g");

				if(form[ii].name.match(re) != null ) {
					//alert(form[ii].type);
					switch(form[ii].type) {
						case 'radio':
							if (form[ii].checked == true) {
								faking[qid] = 0;
							}
							break;
						case 'checkbox':
							if (form[ii].checked == true) {
								faking[qid] = 0;
							}
							break;
						case 'text':
							if (form[ii].value != '') {
								faking[qid] = 0;
							}
							break;
					}
				}
			}
			if(faking[qid] == 1) {	faked++;	}
		}
	}
	if(faked > 0) {
		alert("Jedno z pytań jest wymagane.\nProszę wypełnić/zaznaczyć wymagane pola!")
		return false;
	}
	return true;
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function changeRigipsSelect( index ){
	if( ct[ index ] != undefined && index != '') {
		var al = ct[ index ].length;
		get( 'company_spec_sel' ).innerHTML = '';
		var opt = new Option( 'wybierz', '' );
		get( 'company_spec_sel' ).options[0] = opt; 
		for( var l=0; l<al; l++ ){
			opt = new Option( ct[ index ][l], ct[ index ][l] );
			get( 'company_spec_sel' ).options[l+1] = opt;
		}
	}
	else {
		get( 'company_spec_sel' ).innerHTML = '';
		var opt = new Option( 'wybierz rodzaj dzialalności firmy', '' );
		get( 'company_spec_sel' ).options[0] = opt; 
	}
}

function changeShowHideInput( value, wanted , id ){
	if( value == wanted ){
		get( id ).style.display = 'block';
		get( id ).focus();
	}
	else{
		get( id ).style.display = 'none';
	}
}

function validateFormRigips( form_name ) {
	var forma = document.forms[ form_name ];
	var wanted = "first_name|input|Imię|last_name|input|Nazwisko|phone|input|Telefon|email|email|E-mail|company_name|input|Nazwa firmy|company_street|input|Adres firmy - ulica|company_number|input|Adres firmy - numer|company_zip|input|Adres firmy - kod pocztowy|company_city|input|Adres firmy - miejscowość |company_phone|input|Telefon do firmy|account_contact|radio|Czy jest się zainteresowanym spotkaniem|account_contact_type|select|Rodzaj kontaktu|zgoda_mark|radio|Pytanie o dane osobowe|zgoda_info|radio|Pytanie dotyczące otrzymywania informacji handlowych";
	var wanted_a = new Array();
	wanted_a = wanted.split( '|' );
	var wl = wanted_a.length;
	var error = false;
	var uzup_error = false;
	var wyp_error = false;
	var rad_error = false;
	var uzupelnij = 'Proszę uzupełnić pola:'+"\n";
	var wybierz = 'Proszę wybierz pola:'+"\n";
	var radyjo = 'Proszę zazczyć odpowiedzi na:'+"\n";
	var email = '';
	for( var i=0; i<wl; i = i+3 ){
		var elem = eval( "forma."+wanted_a[i] );
		
		if( wanted_a[i+1] == 'input' ){
			if( elem.value == '' || elem.value == undefined ){
				uzupelnij += wanted_a[i+2]+"\n";
				uzup_error = true;
				error = true;
			}
		}
		if( wanted_a[i+1] == 'email' ){
			if( elem.value == '' || elem.value == undefined ){
				uzupelnij += wanted_a[i+2]+"\n";
				uzup_error = true;
				error = true;
			}
			else{
				if( !email_check( elem.value ) ){
					email += "Podany e-mail jest nieprawidłowy\n";
					error = true;
				}
			}
		}
		if( wanted_a[i+1] == 'select' ){
			if( elem.value == '' || elem.value == undefined ){
				wybierz += wanted_a[i+2]+"\n";
				wyp_error = true;
				error = true;
			}
		}
		if( wanted_a[i+1] == 'radio' ){
			var vartosc = getCheckedValue( elem );
			if( !vartosc ){
				radyjo += wanted_a[i+2]+"\n";
				rad_error = true;
				error = true;
			}
		}
	}
	var elx = forma.company_type;
	if( elx != undefined ){
		if( elx.value == '' || elx.value == undefined ){
			wybierz += "Rodzaj działalności firmy"+"\n";
			wyp_error = true;
			error = true;
		}
	}
	
	var els = forma.company_spec_sel;
	var elt = forma.company_spec_text;
	if( elt.style.display == 'none' ){
		if( els.value == '' || els.value == undefined ) {
			wybierz += "Specjalizacja firmy"+"\n";
			wyp_error = true;
			error = true;
		}
		else {
			get( 'company_spec' ).value = els.value;
			get( 'company_spec_type' ).value = 'sele';
		}
	}
	else {
		if( elt.value == '' || elt.value == undefined ) {
			uzupelnij += "Specjalizacja firmy"+"\n";
			uzup_error = true;
			error = true;
		}
		else {
			get( 'company_spec' ).value = elt.value;
			get( 'company_spec_type' ).value = 'text';
		}
	}
	
	if( error ){
		var text = '';
		if( uzup_error )
			text += uzupelnij+"\n";
		if( wyp_error )
			text += wybierz+"\n";
		if( rad_error )
			text += radyjo+"\n";
			
		alert( text + email);
	}
	else {
		submitForm( form_name );
	}
	
}

function email_check(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)	{	return false;	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)		{	return false;	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)	{	return false;	}
	if (str.indexOf(at,(lat+1))!=-1){	return false;	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){	return false;	}
	if (str.indexOf(dot,(lat+2))==-1){	return false;	}
	if (str.indexOf(" ")!=-1){			return false;	}

	return true					
}

function getCheckedValue(radioObj) {
	if(!radioObj) return false;
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return false;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return false;
}
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function validateFormRigipsPoll( form_name, field_counter ) {
	error = true;
	error_string = 'Proszę zaznaczyć minimum 1 pole!';
	counter = 0;
	$("INPUT[type='checkbox']").each(function(){
		if( $(this).attr('checked') ) {
			error = false;
			counter++;
		} 
	});
	
	if( counter > 2 ) {
		error = true;
		error_string = 'Proszę zaznaczyć nie więcej niż 2 pola!';
	}
	
	if( error ) {
		alert( error_string );
		return false;
	} else {
		submitForm( form_name );
	}

}