var rkart=new Array('nie_wiem','world','silver','gold','platinum','bp','plus','lot','elle','bp_payback');
var rpro=new Array('brak');
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;

var getVars = new Array();
//var locvartemp = ( window.top.location.href.indexOf( "?" ) + 1 ) ? window.top.location.href.substr( window.top.location.href.indexOf( "?" ) + 1 ) : "";
var locvartemp = window.top.location.search.substr(1);
locvartemp = locvartemp.split( "&" );
for( var x = 0; x < locvartemp.length; x++ ) {
    var lvTempVar = locvartemp[x].split( "=" );
    getVars[ unescape( lvTempVar[0] ) ] = unescape( lvTempVar[1] );
}
var getVar1;
var getCc;
var getPro;

if(getVars['prID']){
	getVars['prID'] = getVars['prID'].replace(/[\<\"\' \%\;\(\)\&\+\\\/\=]/g,'');
	getVar1 = getVars['prID'];
}
else
{
	getVar1 = '';
}
if(getVars['cctype']){
	getVars['cctype'] = getVars['cctype'].replace(/[\<\"\' \%\;\(\)\&\+\\\/\=]/g,'');
	getCc = getVars['cctype'];
}
else
{
	getCc= '';
}
if(getVars['campaign']){
	getVars['campaign'] = getVars['campaign'].replace(/[\<\"\' \%\;\(\)\&\+\\\/\=]/g,'');
	getPro = getVars['campaign'];
}
else
{
	getPro= '';
}


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 CheckAge(pesel)
{
	var data=new Date();
	var year=data.getFullYear();
	var wiek = (year-(1900+parseInt(pesel.substring(0,2))));
	if(wiek<21) return 'nie';
	else return 'tak';
}


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=='phone' || 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 (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";
					}
				else if (CheckAge(pola[i].value)=='nie')
				{
					errmsg+="Musisz mieæ skoñczone 21 lat\n";
				}
			}
			if ( pola[i].name=='agreed' || pola[i].name=='zgoda' ){
				if(!pola[i].checked){
					errmsg+="Musisz wyraziæ zgodê na przetwarzanie danych\n";
				}
			}			
		}
	}
	if(errmsg!='') {
				alert(errmsg);
				return false;
	}
	else {
		$('#cctype').val(getCc);
		$('#campaign').val(getPro);
		
		if (typeof send2Crm == 'function') {
			send2Crm(document.forms[0]);
		}
		else {
			document.forms[0].submit(); 
		}
	
		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';
$('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=='phone' || 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=='phone' || 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, #options2_more').css('display','none');
	rozwiniete=0;
}
});
$('.clk > div').click(function(){
					var selectID = $(this).parent('.clk').attr('id');
					if ( selectID == 'options_more' ) {
						$('#options').html($(this).html());	
						getCc=$(this).attr('value');						
					} else if ( selectID == 'options2_more' ) {
						$('#options2').html( $(this).html());
						$('#poraKontaktu').attr('value', $(this).attr('value') );
					}
					
					$(this).parent().parent().find('.showhide').css('background','url(img/select-right.jpg) no-repeat');
					// $(this).parent().parent().find('.showhide').html($(this).html());
					// getCc=$(this).attr('value');
					
					$(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_cc.reset();
var jt=0;
if(getVar1!=''){
	$('#OTH1').val(getVar1);
}
if(getCc!=''){
for (i in rkart){
if (getCc==rkart[i]){
$('#options').css('display','none');
$('#form_cc').css('background','url(img/tlo1.jpg) no-repeat');
jt=1;
break;
}
}
if (jt==0) getCc='nie_wiem';
}
if (jt==0 || getCc=='') getCc='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;
});

});	

