
var $ = jQuery.noConflict();
var getJson;

$(document).ready(function() {


	url = 'https://www.tujaus.net/tujaushosted/vuokraus4/css/style.css?update=50';
	url2 = 'https://www.tujaus.net/tujaushosted/vuokraus4/tyylit/scandiarent/style.css?update=50';
	if (document.createStyleSheet) {
    	document.createStyleSheet(url);
    	document.createStyleSheet(url2);
    } else {
   	 	jQuery('<link rel="stylesheet" type="text/css" href="' + url + '" />').appendTo('head'); 
   	 	jQuery('<link rel="stylesheet" type="text/css" href="' + url2 + '" />').appendTo('head'); 
    }

lataa_kaavake();

});
   

function lataa_kaavake(paivita, hae_autot) {
	
	 var hae_autot = hae_autot;
     var $ = jQuery.noConflict();
	 var tujaus_id = Cookies.get('tujaus_id'); 	 
	 var yhteenveto = '';
	 
	 var varausid = $('.tu_jatkavahvistus').attr('alt');
	 
	 var cargroup = gup( 'cargroup' );
	 var carclass = gup( 'carclass' );
	 var begin = gup( 'begin' );
	 var end = gup( 'end' );
	 var begintime = gup( 'begintime' );
	 var endtime = gup( 'endtime' );
     var coupon = gup( 'coupon' );

     var debug = gup( 'debug' );

    //var coupon = gup( 'tu_kuponki' );

	 var km = gup( 'km' );
	 var type = gup( 'type' );
	 var language = gup( 'language' );
	 var orders = gup( 'orders' );
	 var url = $(location).attr('href');
	 if (url.indexOf("#") != -1)
	 var url = url.substring(0, url.indexOf('#'));
	 
	 	 
	 var km_upgrade_check = $('#km_upgrade_check').val();
	 var km_upgrade = $('#km_upgrade:checked').val();
	 var automaatti_upgrade = $('#automaatti_upgrade:checked').val();
	 
 	//if (paivita != 1) {

    if (cargroup != '') yhteenveto = yhteenveto+'&cargroup='+cargroup;
    if (carclass != '') yhteenveto = yhteenveto+'&carclass='+carclass;
    
    if (begin != '')    yhteenveto = yhteenveto+'&begin='+begin;
    if (begintime != '')yhteenveto = yhteenveto+'&begintime='+begintime;
    if (end != '')      yhteenveto = yhteenveto+'&end='+end;
    if (endtime != '')  yhteenveto = yhteenveto+'&endtime='+endtime;

    if (debug != '')   yhteenveto = yhteenveto+'&debug='+debug;
    
    if (coupon != '')   yhteenveto = yhteenveto+'&coupon='+coupon;
    if (km != '')       yhteenveto = yhteenveto+'&km='+km;
    if (type != '')     yhteenveto = yhteenveto+'&type='+type;
    if (language != '') yhteenveto = yhteenveto+'&language='+language;
    if (orders != '') yhteenveto = yhteenveto+'&orders='+orders;

    if (km_upgrade_check) yhteenveto = yhteenveto+'&km_upgrade_check='+km_upgrade_check;
    if (km_upgrade) yhteenveto = yhteenveto+'&km_upgrade='+km_upgrade;
    if (automaatti_upgrade) yhteenveto = yhteenveto+'&automaatti_upgrade='+automaatti_upgrade;
            
     if ($('#tu_yhteystiedot').length != 0)
     yhteenveto = yhteenveto+'&yhteenveto=1&varausid='+varausid;
     
     //}
          
 	 if (paivita != 1)
	 $('#tujausform').html('<div class="tu_loader" style="display:block"></div>'); 
	 else
     $('#tujausform #tu_formwrapper').html('<div class="tu_loader" style="display:block"></div>');
     
     var style = $('#tujausform').data('tu_style');
     
     if(getJson && getJson.readyState != 4)
	 getJson.abort();
                           
     getJson = $.getJSON("https://www.tujaus.net/tujaushosted/vuokraus4//vuokrauskaavake.php?id="+tujaus_id+"&renter=scandiarent&site="+yhteenveto+"&url="+url+"&paivita="+paivita+"&style="+style+"&jsoncallback=?", function(data){
     
     var $ = jQuery.noConflict();
     
		 //var tujaus_id = $.cookie('tujaus_id', data.id, { expires: 1 });
		 var tujaus_id = Cookies.set('tujaus_id', data.id, { expires: 1 });
			
		 if (paivita != 1)
         $('#tujausform').html(data.html);
		 else
         $('#tujausform #tu_formwrapper').html(data.html);
         
         if (hae_autot == 1) {
		 	$('#tu_autoluokat_hae').trigger('click');	
		 }
		 
		 


   });
}



/*!
 * jQuery Cookie Plugin
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2011, Klaus Hartl
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/GPL-2.0
 */
(function($) {
    $.cookie = function(key, value, options) {

        // key and at least value given, set cookie...
        if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
            options = $.extend({}, options);

            if (value === null || value === undefined) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

        var pairs = document.cookie.split('; ');
        for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
            if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
        }
        return null;
    };
})(jQuery);

/*!
 * JavaScript Cookie v2.2.0
 * https://github.com/js-cookie/js-cookie
 *
 * Copyright 2006, 2015 Klaus Hartl & Fagner Brack
 * Released under the MIT license
 */
 
;(function(factory){var registeredInModuleLoader;if(typeof define==='function'&&define.amd){define(factory);registeredInModuleLoader=true;}
if(typeof exports==='object'){module.exports=factory();registeredInModuleLoader=true;}
if(!registeredInModuleLoader){var OldCookies=window.Cookies;var api=window.Cookies=factory();api.noConflict=function(){window.Cookies=OldCookies;return api;};}}(function(){function extend(){var i=0;var result={};for(;i<arguments.length;i++){var attributes=arguments[i];for(var key in attributes){result[key]=attributes[key];}}
return result;}
function decode(s){return s.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent);}
function init(converter){function api(){}
function set(key,value,attributes){if(typeof document==='undefined'){return;}
attributes=extend({path:'/'},api.defaults,attributes);if(typeof attributes.expires==='number'){attributes.expires=new Date(new Date()*1+attributes.expires*864e+5);}
attributes.expires=attributes.expires?attributes.expires.toUTCString():'';try{var result=JSON.stringify(value);if(/^[\{\[]/.test(result)){value=result;}}catch(e){}
value=converter.write?converter.write(value,key):encodeURIComponent(String(value)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent);key=encodeURIComponent(String(key)).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var stringifiedAttributes='';for(var attributeName in attributes){if(!attributes[attributeName]){continue;}
stringifiedAttributes+='; '+attributeName;if(attributes[attributeName]===true){continue;}
stringifiedAttributes+='='+attributes[attributeName].split(';')[0];}
return(document.cookie=key+'='+value+stringifiedAttributes);}
function get(key,json){if(typeof document==='undefined'){return;}
var jar={};var cookies=document.cookie?document.cookie.split('; '):[];var i=0;for(;i<cookies.length;i++){var parts=cookies[i].split('=');var cookie=parts.slice(1).join('=');if(!json&&cookie.charAt(0)==='"'){cookie=cookie.slice(1,-1);}
try{var name=decode(parts[0]);cookie=(converter.read||converter)(cookie,name)||decode(cookie);if(json){try{cookie=JSON.parse(cookie);}catch(e){}}
jar[name]=cookie;if(key===name){break;}}catch(e){}}
return key?jar[key]:jar;}
api.set=set;api.get=function(key){return get(key,false);};api.getJSON=function(key){return get(key,true);};api.remove=function(key,attributes){set(key,'',extend(attributes,{expires:-1}));};api.defaults={};api.withConverter=init;return api;}
return init(function(){});}));


function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );

  if ( jQuery("#tujaus_"+name).length ) {
  	return jQuery("#tujaus_"+name).val();
  }

  if( results == null )
    return "";
  else
    return results[1];
    
    
}