var rkont=new Array('nie_wiem','citikonto','citione','citikontodirect','citigold','saver','supersaver');
var rpro=new Array('brak','chf','pakiet_medyczny','b2t');
if (top != self) {
top.location = self.location;
}
var urlPath = top.location.href; 
var urlWoProtocol = urlPath.substring(5,urlPath.length); 
if(self.location.protocol=='http:'){ 
	var redirectPath = 'https:'+urlWoProtocol; 
	top.location.href=redirectPath; 
}
if((navigator.userAgent.indexOf('Mac')==-1) && (top!=self)) top.location=self.location;

function getUParam(npar)
{
npar=npar.toLowerCase();
qstr=new String(window.top.location.href);
qstr=qstr.toLowerCase();
if(qstr.indexOf('?')==-1||qstr.indexOf(npar+'=')==-1)return '';
pstr=qstr.substring(qstr.indexOf('?')+1).substring(qstr.substring(qstr.indexOf('?')+1).indexOf(npar+'=')+(npar.length+1));
return (pstr.indexOf('&')==-1)?pstr:pstr.substring(0,pstr.indexOf('&'));
}

var getVar1=getUParam('prID');
var getAcc=getUParam('acctype');
var getPro=getUParam('campaign');
var getCity=getUParam('city');

function checkpesel(v) {
	var tab_pesel = v.split('');
	var w = new Array(1,3,7,9);
	var wk = 0;
	for (var l=0; l<=9; l++) wk = (wk + tab_pesel[l]*w[l%4]) % 10;
	var k = (10-wk) % 10;
	if ( tab_pesel[10] != k ) return false;
	return true;
}
function Walidacja(){
var pola=document.forms[0].elements;
	var errmsg='';
	for (var i=0;i<pola.length;i++){
	if(pola[i] && pola[i]!=undefined && pola[i].type && pola[i].type!='submit'){
			if ( pola[i].name=='name' ){
					if (pola[i].value.toLowerCase()=='imiê' || pola[i].value.toLowerCase()=='imi' || pola[i].value.toLowerCase()=='imie' || pola[i].value==''){
						pola[i].value='Imiê';
						errmsg+="Wpisz imiê\n";
					}
					else if(!/^[a-zA-Z±æê³ñó¶¼¿¡ÆÊ£ÑÓ¦¬¯\s]{3,30}$/.test(pola[i].value)){
						errmsg+="Niepoprawna warto¶æ pola imiê\n";
					}
				}	
			if ( pola[i].name=='surname' ){
					if (pola[i].value.toLowerCase()=='nazwisko' || pola[i].value==''){
						pola[i].value='Nazwisko';
						errmsg+="Wpisz nazwisko\n";
					}
					else if(!/^[a-zA-Z±æê³ñó¶¼¿¡ÆÊ£ÑÓ¦¬¯\-\s]{3,30}$/.test(pola[i].value)){
						errmsg+="Niepoprawna warto¶æ pola nazwisko\n";
					}
				}
			if ( pola[i].name=='telefon_komorkowy' || pola[i].name=='home_phone' ){
					if (pola[i].value.toLowerCase()=='telefon' || pola[i].value.toLowerCase()=='telefon kontaktowy' || pola[i].value.toLowerCase()=='numer telefonu' || pola[i].value==''){
						pola[i].value='Telefon';
						errmsg+="Wpisz telefon\n";
					}
					else if(!/^\+*[0-9\s\-\(\)\+]{9,24}$/.test(pola[i].value)){
						errmsg+="Niepoprawna warto¶æ pola telefon\n";
					}
				}		
			if ( pola[i].name=='pesel' ){
				if (typeof getVar1 == 'undefined' || getVar1 != '44401000') {
					if (pola[i].value.toLowerCase()=='pesel' || pola[i].value==''){
						pola[i].value='Pesel';
						errmsg+="Wpisz pesel\n";
					}
					else if(!/^[0-9]{11}$/.test(pola[i].value) || !checkpesel(pola[i].value) || pola[i].value==00000000000){
						errmsg+="Niepoprawna warto¶æ pola pesel\n";
					}
				}
			}				
			if ( pola[i].name=='agreed' || pola[i].name=='zgoda' ){
				if(!pola[i].checked){
					errmsg+="Musisz wyraziæ zgodê na przetwarzanie danych\n";
				}
			}
			if (czyMiasto()) {
				if ( pola[i].name=='disclaimerFile' || pola[i].name=='zgoda' ){
					if(!pola[i].checked){
						errmsg+="Musisz zaznaczyæ opcjê wype³nij formularz identyfikacyjny";
					}
				}	
			}
		}
	}
	if(errmsg!='') {
				alert(errmsg);
				return false;
	} else {
		$('#acctype').val(getAcc);
		$('#campaign').val(getPro);
		if (typeof send2Crm == 'function') {
			  send2Crm(document.forms[0]);
		} else {
			  document.forms[0].submit(); 
		}
		return false;
	}
}

function czyMiasto() {
	if ((getCity == 'lodz')) {
		$('#form_cf').css('background','url("img/tlo_lodz.jpg") no-repeat scroll 0 0 transparent');
		$('#form_cf').css('height','440px');
		$('#check_file').show();
		return true;
	}
	else if ((getCity == 'trojmiasto') || (getCity == 'wroclaw')) {
		$('#form_cf').css('background','url("img/tlo_trojmiasto_wroclaw.jpg") no-repeat scroll 0 0 transparent');
		$('#form_cf').css('height','390px');
		$('#check_file').show();
		return true;
	}
	else {
		return false;
	}	
}

$(document).ready(function(){	
	var rozwiniete=0;
	var d_name='Imiê';
	var d_surname='Nazwisko';
	var d_pesel='Pesel';
	var d_phone='Telefon';
	var d_email='Adres e-mail';
	
	czyMiasto();	
	$('input').focus(function(){
		if(this.name=='name' && this.value==d_name) this.value='';
		if(this.name=='surname' && this.value==d_surname) this.value='';
		if((this.name=='telefon_komorkowy' || this.name=='home_phone') && this.value==d_phone) this.value='';
		if(this.name=='pesel' && this.value==d_pesel) this.value='';
		if(this.name=='email' && this.value==d_email) this.value='';
	});
	$('input').blur(function(){
		if(this.name=='name' && this.value=='') this.value=d_name;
		if(this.name=='surname' && this.value=='') this.value=d_surname;
		if((this.name=='telefon_komorkowy' || this.name=='home_phone') && this.value=='') this.value=d_phone;
		if(this.name=='pesel' && this.value=='') this.value=d_pesel;
		if(this.name=='email' && this.value=='') this.value=d_email;
	});
			
	$('form').submit(function(){		
		return Walidacja();		
	});
	$('.showhide').click(function(){
		if($('#'+this.id+'_more').css('display')=='none') {
			if (this.id=='options' || this.id=='options2') {
				$(this).css('background','url(img/select-right1.jpg) no-repeat');
				setTimeout(function(){rozwiniete=1;},50);
				}
			$('#'+this.id+'_more').css('display','block');
			return false;
		} else {
			if (this.id=='options' || this.id=='options2') {
				$(this).css('background','url(img/select-right.jpg) no-repeat');
				rozwiniete=0;
				}
			$('#'+this.id+'_more').css('display','none');
			return false;
		}
	});
	$(document).click(function(){
		if(rozwiniete) {
			$('#options_more').css('display','none');
			rozwiniete=0;
		}
	});
	$('.clk > div').click(function(){
		$(this).parent().parent().find('.showhide').css('background','url(img/select-right.jpg) no-repeat');
		if ( $(this).parent().attr('id') == 'options2_more' ) {
			$('#poraKontaktu').val ( $(this).attr('value') );
			$('#options2').html($(this).html());
		} else {
			getAcc=$(this).attr('value');
			$('#options').html($(this).html());
		}
		$(this).parent().css('display','none');
		$(this).parent().find('div').css('display','block');
		$(this).css('display','none');
	});
	$('.clk > div').mouseover(function(){
		$(this).css('text-decoration','underline');
	});
	$('.clk > div').mouseout(function(){
		$(this).css('text-decoration','none');
	});
	$('#emailinfo').mouseover(function(){
		if ($('#info_more').css('display')!='block') $('#'+this.id+'_more').css('display','block');
	});
	$('#emailinfo').mouseout(function(){
		$('#'+this.id+'_more').css('display','none');
	});
	document.forms.form_cf.reset();
	var jt=0;
	if (getVar1!=''){
		$('#OTH1').val(getVar1);
	}
	if (getAcc!='') {
		for (i in rkont){
			if (getAcc==rkont[i]) {
				$('#options').css('display','none');
				if (getAcc=='citikontodirect' && getPro=='b2t')  $('#form_cf').css('background','url(img/tlo_b2t.jpg) no-repeat');
				else $('#form_cf').css('background','url(img/tlo1.jpg) no-repeat');
				jt=1;
				break;
			}
		}
		if (jt==0) getAcc='nie_wiem';
	}
	if (jt==0 || getAcc=='') getAcc='nie_wiem';
	var jp=0;
	if(getPro!=''){
	for (i in rpro){
	if(getPro==rpro[i]){
	jp=1;
	}
	}
	}
	if (jp==0) getPro='brak';

	$('#cancel').click(function(){
		$('form')[0].reset();
		return false;
	});
	
	// psesl out dla prid = 44401000
	if (typeof getVar1 != 'undefined' && getVar1 == '44401000') {
		var uniqueRandom = Math.abs(new Date())+'';
		$('#pesel').val(uniqueRandom).parent().hide();
		$('#options').hide();
		$('#form_cf').css('background','url("img/swiat_nalezy.jpg") no-repeat scroll 0 0 transparent');
		getAcc = 'citione';
	}

	// zmiany dla prid = 99901121
	if ( (typeof getVar1 != 'undefined' && getVar1 == '99901121') || (typeof getVar1 != 'undefined' && getVar1 == '22301005') || (typeof getVar1 != 'undefined' && getVar1 == '77701252') ) {		
		$('#options').hide();
		$('#form_cf').css('background','url("img/rodzinka.jpg") no-repeat scroll 0 0 transparent');
		getAcc = 'citione';
		form2crm.fields['CAMPAIGN_ID']=form15minCitiOne;
		form2crm.fields['WAVE_ID']=form15minCitiOneFamily_wave;
	}
	// zmiany dla prid = 99901122
	if ( (typeof getVar1 != 'undefined' && getVar1 == '99901122') || (typeof getVar1 != 'undefined' && getVar1 == '22301004') || (typeof getVar1 != 'undefined' && getVar1 == '77701251') ) {		
		$('#options').hide();
		$('#form_cf').css('background','url("img/swiat_nalezy.jpg") no-repeat scroll 0 0 transparent');
		getAcc = 'citione';
		form2crm.fields['CAMPAIGN_ID']=form15minCitiOne;
		form2crm.fields['WAVE_ID']=form15minCitiOneWorld_wave;
	}
	
	if (getPro=='chf'){
		$('#options').css('display','none');
		$('#form_cf').css('background','url(img/tlo_franki.jpg) no-repeat');
		postData['ASSIGN_TO']='KB82996';
		form2crm.fields['CAMPAIGN_ID']=form15minTanieFranki;
		form2crm.fields['WAVE_ID']=form15minTanieFranki_wave;

	}
	
	if (getCity == 'lodz') {
		form2crm.fields['CAMPAIGN_ID']=form15minACC_lodz;
		form2crm.fields['WAVE_ID']=form15minACC_ZTM_waveShort_lodz;
		form2crm.fields['ASSIGN_TO']='KB82996'		
	}
	else if (getCity == 'trojmiasto') {
		form2crm.fields['CAMPAIGN_ID']=form15minACC_ZTM;
		form2crm.fields['WAVE_ID']=form15minACC_ZTM_waveShort_T;		
	}
	else if (getCity == 'wroclaw') {
		form2crm.fields['CAMPAIGN_ID']=form15minACC;
		form2crm.fields['WAVE_ID']=form15minACC_waveShort_ztmwro;		
	}
	
	$(document.body).show();
	
});
