
function checkFields(fields, form)
{
	if(!fields) return false;
	
	var errors = '';
	var styleError = '1px solid red';
	var styleOK = '1px solid #9A9A9A';	
	
	for(var i = 0; i < fields.length; i++)
	{
		//depends
		if(document.getElementById(fields[i][3])){	
			if(validAgree(fields[i][3], fields[i][4], form)){
			
				if(!validAgree(fields[i][0], fields[i][2], form))
				{
					if(document.getElementById(fields[i][0])){
						errors += ' - ' + fields[i][1] + "\r\n";
						if(typeof(classError) != 'undefined'){
							document.getElementById('label_' + fields[i][0]).className = classError;
						}else{
							document.getElementById(fields[i][0]).style.border = styleError;
						}
					}
				}
				else
				{
					if(typeof(classOK) != 'undefined'){
						document.getElementById('label_' + fields[i][0]).className = classOK;
					}else{				
						document.getElementById(fields[i][0]).style.border = styleOK;
					}
				}
			}else{
				if(fields[i][5]){				
					errors += ' - ' + fields[i][5] + "\r\n";
					
					if(typeof(classError) != 'undefined'){
						document.getElementById('label_' + fields[i][3]).className = classError;
					}else{
						document.getElementById(fields[i][3]).style.border = styleError;
					}				
				}
			}
			
		}else{
			if(document.getElementById('label_' + fields[i][0])) {
				
				if(!validAgree(fields[i][0], fields[i][2], form))
				{
					errors += ' - ' + fields[i][1] + "\r\n";
	
					if(typeof(classError) != 'undefined'){
					
						document.getElementById('label_' + fields[i][0]).className = classError;
					}else{
						document.getElementById(fields[i][0]).style.border = styleError;
					}
				}
				else
				{
					if(typeof(classOK) != 'undefined'){
						document.getElementById('label_' + fields[i][0]).className = classOK;
					}else{				
						document.getElementById(fields[i][0]).style.border = styleOK;
					}
				}
			}
		}
	}
	

	
	return errors;
}

//walidacja pol
function validAgree(id, type, form)
{
	var el = document.getElementById(id);

	if(!el && id != 'nip') return false;

	switch(type)
	{
		case 'email':
			rx = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;
			if(!rx.test(el.value))
			{
				return false;
			}
			break;
		
		case 'num':	
			rx = /^[0-9]+$/i;
			if(!rx.test(el.value))
			{
				return false;
			}			
			break;
			
			
		case 'phone':
			rx = /^0[0-9]{9}$/i;
			if(!rx.test(el.value))			
			//if(!el.value)
			{
				return false;
			}
			break;

		case 'phone2':
			rx = /^[0-9]{9}$/i;
					
			el.value = format_phone(el.value);
					
			if(!rx.test(el.value))			
			{
				return false;
			}
			
			break;			
			
		case 'notEmpty':
			if(!el.value)
			{
				return false;
			}
			break;
			
		case 'zip':
			rx = /^[0-9]{2}\-[0-9]{3}$/i;
			if(!rx.test(el.value))
			{
				return false;
			}			
			break;
			
		case 'zip2':
			rx = /^[0-9]{2}\-[0-9]{3}$/i;
			if(!rx.test(el.value +'-'+ document.getElementById('zip1').value))
			{
				return false;
			}
			break;			
			
		case 'checkbox':
			return el.checked;
			break;
		
		case 'nip':
	
			for(var i = 2; i <= 6; i++)
			{
				el = document.getElementById('customerType' + i + '_' + id);
				if(typeof(el) != 'undefined')
				{
					if(el.value) {
						
						if(!check_nip(el.value, 1)) return false;
					
						el.value = format_nip(el.value);
					}
				}
			}
		
			break;
			
		case 'nip_short':

				if(typeof(el) != 'undefined')
				{
					if(el.value) {

						if(!check_nip(el.value, 1)) return false;

						el.value = format_nip(el.value);
					}
				}

			break;				
		
		case 'select':
			if(!el.options.selectedIndex) return false;
			break;
		case 'radioArray':
			
			el = eval("document.forms['"+form.id+"']."+id);
			if(el){
				for(var i=0;i<el.length;i++){
					if(el[i].checked){
						return true;
					}
				}
				
				return false;
			}
			break;
		
		case 'pesel':
			return validSpecifyData(0, el.value);
			break;
			
		case 'regon':
			return validSpecifyData(1, el.value);
			break;	
			
		case 'checkboxArray':
			//el = eval("document.forms['"+form.id+"']."+id);
			el = document.getElementById(id).getElementsByTagName('input');
			if(el){
				for(var i=0;i<el.length;i++){
					if(el[i].checked){
						return true;
					}
				}
				
				return false;
			}
			break;				
			
		default:
			alert('Nieznany typ: ' + type); 
			break;		
	}
	
	return true;
}

//krokowa nawigacja
function navigTpaForm(act, next)
{
	var a = document.getElementById(act);
	var n = document.getElementById(next);
	
	if(!a || !n) return false;
	
	a.style.display = 'none';
	n.style.display = 'block';
	
	return true;
}

//zmiana typu klienta z resetem pol
function navigTpaFormTypes(act)
{
	if(isNaN(act)) 
	{
		if(act) act = Number(act.replace("customerType", ""));
	}
	
	if(isNaN(act))  return false;
	
	for(var i = 1; i <= customerTypeOptions; i++)
	{
		var el = document.getElementById('customerType' + i);
		
		if(el)
		{
			//reset pol
			if(el.style.display == 'block')
			{
				resetFields('customerType' + i);
			}
			
			el.style.display = 'none';
		}
	}
	
	document.getElementById('customer_type' + act).checked = 'true';
	var el = document.getElementById('customerType' + act);
	
	if(el) el.style.display = 'block';
	
	return true;
}

//rese pol po ukrywaniu i odrkywaniu
function resetFields(box)
{
	var el = document.getElementById(box);
	
	if(!el) return false;
	
	var tmp = el.getElementsByTagName('input');
	for(var x = 0; x < tmp.length; x++) tmp[x].value = '';
				
	var tmp = el.getElementsByTagName('select');
	for(var x = 0; x < tmp.length; x++) tmp[x].value = '';				

	tmp = el.getElementsByTagName('checkbox');
	for(var x = 0; x < tmp.length; x++) tmp[x].checked = 'false';
}

//np adres korespondencyjny
function switchBox(val, id, checkbox)
{
	var el = document.getElementById(id);

	if(!el) return false;
	
	el.style.display = (val || checkbox.checked) ? 'block' : 'none';

	if(!val) resetFields(id);

	return true;
}



function switchBoxSimple(id)
{
	var el = document.getElementById(id);

	if(!el) return false;
	
	el.style.display = (el.style.display == 'none') ? 'block' : 'none';
	
	return true;
}

//kopiowane wartosci do dalszych etapow
function copyValues(e)
{
	if(!e) return false;

	for(var i = 1; i <= customerTypeOptions; i++)
	{
		var el = document.getElementById('customerType' + i + '_' + e.name);
		
		if(el)
		{
			el.innerHTML = e.value; 
		}
	}	
}

function checkAndCopyValues()
{
	var f = document.forms['fagreement'];
	for (i=0;i<standardTab.length;i++) {
		
		if (f[standardTab[i]].value != '') {
			for(var j = 1; j <= customerTypeOptions; j++) 	{
				var el = document.getElementById('customerType' + j + '_' + standardTab[i]);
				if(el) {
					el.innerHTML = f[standardTab[i]].value;
				}
			}	
		} else {
			for(var j = 1; j <= customerTypeOptions; j++) 	{
				var el = document.getElementById('customerType' + j + '_' + standardTab[i]);
				if(el) {
					el.parentNode.style.display="none";
				}
			}
		}	
	}
}

function nextCompanyOwner(source, dest)
{
	var source = document.getElementById(source);
	var dest = document.getElementById(dest);
	
	if(!source || !dest) return false;
	
	companyOwnerCounter++;
	
	var tmpCode = '';
	
	var newBox = document.createElement('span');
	newBox.setAttribute('id', "ownerAdded_"+ companyOwnerCounter);
	newBox.innerHTML = "<strong>Wspólnik</strong>";
	newBox.innerHTML += "<input type='button' class='dodajwspolnika' onclick=\"javascript:nextCompanyOwner('contactPersonBlock', 'contactPersonBlockAdded')\" value='Dodaj wspólnika' />"
	newBox.innerHTML += " <input type='button' class='usunwspolnika' value='Usuń' onclick=\"document.getElementById('"+ newBox.id +"').innerHTML = '';\"";
	dest.appendChild(newBox);

	tmpCode = source.cloneNode(true);
	var tags = tmpCode.getElementsByTagName('input');
	
	if(tags.length)
	{
		for(var i=0; i<tags.length; i++)
		{
			tags[i]['value'] = '';
			tags[i]['id'] = tags[i]['id'].replace(/customerType3\_/g, 'customerType3_'+ companyOwnerCounter +'_');
			tags[i]['name'] = tags[i]['name'].replace(/customerType3\[owner\]\[0\]/g, 'customerType3[owner]['+ companyOwnerCounter +']');
		}
	}
	
	newBox.appendChild(tmpCode);

	return true;
}


function validSpecifyData(validType, str) {	

	var  strLen  = new Array  (11,9);
	var  mod  = new Array  (10,11);
	var  weights  = new Array  (
		new Array( 1,3,7,9,1,3,7,9,1,3,0),	
		new Array( 8,9,2,3,4,5,6,7,0,0,0)
		);

	var  tmp = new Array  (0,0,0,0,0,0,0,0,0,0,0);

	var L = str.length;

	for ( i=0; i<L; i++ ) {
		tmp[i] = parseInt(str.charAt(i));
	} 

	if (L == strLen[validType]){
		var sum = 0;   
		
		for (i=0; i<L-1; i++) {
		    sum += (weights[validType][i]*tmp[i]);
		}

		sum = sum % mod[validType];
	
		switch(validType){
			case 0:	 sum = (10-sum) % 10; break;
			case 1:	 sum = sum  % 10; 	 break;	
			default: return false; break;
		}
			  
		var result = (sum == tmp[L-1]);

		return result;
	} 

	return false;
} 

function checkArea(city, zip, form){

	jq.ajax({
	   type: "POST",
	   url: "/tools/location/locations_zip.php",
	   data: "customer=zip||"+zip+"||city||"+city+"&from=form",
	   success: function(msg){
		 //console.log(msg);
			switch(msg){
				case '1': form.submit(); break;
				case '2': alert('Usługa niedostępna w podanej lokalizacji'); break;
			}
			
	   }
	 });
	
	return true;
}

var applicationAttachments = {
	counterBox : 'attachCounter',
	attachList : 'attachList',
	uploaderBtn: 'uploadForm',
	parentF : document,
	formType: '',
	filesLimit : 1,
	updateInput : function(value){
		if(this.uploaderInput){
			if(typeof(document.getElementById(this.uploaderInput)) == 'object'){
				
				if(!value){
					value = '';
				}
				
				document.getElementById(this.uploaderInput).value = value;
			}
		}
	},
	updateCounter : function(value){
		var p = this.parentF.getElementById(this.counterBox);
		if(p && value){
			var quantity = parseInt(p.innerHTML) + parseInt(value);
			
			p.innerHTML = quantity;
			
			this.updateInput(quantity);
			
			var displayForm = "block";
			if(parseInt(p.innerHTML) >= this.filesLimit){
				displayForm = "none";
			}
			this.parentF.getElementById(this.uploaderBtn).style.display = displayForm;
		}
	},
	
	addLink : function(html){
		var aList = this.parentF.getElementById(this.attachList);
		if(aList) aList.innerHTML += html;

	},
	deleteLink : function(id){
		var el = this.parentF.getElementById(id);

		if(el){
			el.style.display = 'none';
		}

		return false;
	},

	deleteFile : function(url){
		if(!confirm("Potwierdzam usunięcie")){
			return false;
		}

		if(!url){
			return false;
		}

		jq.get(url, function(data) {
			eval(data);
		});

		return false;
	},
	error : function(msg){
		alert(msg);
	},
	popup : function(){
		window.o = window.open('/tools/applicationUpload.php?type='+this.formType,'aaa','width=450,height=350');
		return false;
	},
	run : function(config){
		if(config){
			for(a in config){
				eval('this.'+a+'='+'"'+ config[a] +'";');
			}
		}
		//applicationAttachments.updateInput(0);
	}
};
