/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href");
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});


function isValidEmailAddress(emailAddress) {
 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 		return pattern.test(emailAddress);
	}
	$(document).ready(function(){
	
	var $zaznaczono=1;
	
	$(".checkbox_koszyk").each(function(){ if(!$(this).is(":checked")) {$zaznaczono=0;}   });

	if($zaznaczono!=0) { $("#zaznacz_wszystkie").text("Odznacz wszystkie"); }
	$("#zaznacz_wszystkie").click(function(){ if($zaznaczono==0) {$zaznaczono=1; $(this).text("Odznacz wszystkie");$(".checkbox_koszyk").each(function(){$(this).attr("checked","checked");}); } else { $zaznaczono=0; $(this).text("Zaznacz wszystkie"); $(".checkbox_koszyk").each(function(){$(this).attr("checked","");});}change_hid(); });
	
		$("#inner_koszyk").css("display","block");
if($(document).getUrlParam("ids")!=null && $("#content_div_2288").text() == "" && $(document).getUrlParam("ids")!="")
{
if($("#koszyk_sent").text()== "") { $(".koszyk_box").css("display","none"); }
if($("#koszyk_sent").text()!= "")  $.cookie("koszyk_submit",null); 
			if($.cookie("koszyk_submit")!=null)
			{
		$params_d = $.cookie("koszyk_submit").split(";");

		$.each($params_d, function(){
		var $p="";
		$p=this;
		$(".checkbox_koszyk").each(function(){
		 if($(this).val()==$p) $(this).attr("checked","checked");}); 
		
	});}
	change_hid();
		$text_c="";
	if($("#form_errors").text()!="") { $text_c+="<li>Przepisany kod jest niepoprawny.</li>"; $("#koszyk_submit_error").html("Napotkane błędy:<ul>"+$text_c+"</ul>");}
	$("#form_email_454").submit(function(){ 
			$text="";
		var $num=0;
		$(".checkbox_koszyk").each(function(){  if(this.checked==true) {$num++; } });
		var $error=0;
	
		if($num <= 0) //zaden checkbox nie zaznaczony
		{	
		//div ze u have to check 1 tick	
		$error++;
		$text += "<li>Musisz zaznaczyć chociaż jedną ofertę!</li>"
		}
		if($("#q454_q4").val()=="" && $("#q454_q5").val()=="") 
		{
			$text+="<li>Musisz wpisać e-mail bądź telefon!</li>"
			$error++;
		}
		if($("#q454_q5").val()!="" && !isValidEmailAddress($("#q454_q5").val()))
		{
		$text+="<li>Wpisany e-mail nie jest poprawny</li>";
		$error++;
		}
		if($error==0)
		{
		
		}
		else
		{
		$text="Napotkane błędy:<ul>"+$text+$text_c+"</ul>";
			$("#koszyk_submit_error").html($text);
	document.location.href="#koszyk_submit_error";
			return false;
		}


		});



	$(".checkbox_koszyk").change(function(){ change_hid(); });
	}
	else
	{ 

	$("#inner_koszyk").html("<p id='#koszyk_submit_error'>Twój koszyk jest pusty</p>"); }
	});

	function change_hid()
	{
	var $ciag="";
	var $ciag_2="";
	$(".checkbox_koszyk").each(function(){  if(this.checked==true) {$ciag+=this.value+"\n";$ciag_2+=this.value+";"; } });
		$.cookie("koszyk_submit",$ciag_2, { path: '/', expires: 30 });
	$("#q454_q1").val($ciag);
	}