﻿var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function offer_detail(url)
{
	var x=800; var y=600;
	aa = window.open(url, '', 'resizable,scrollbars,status,menubar=0,width=' + x + ',height=' + y); 
	if (aa) {
		return false;
		aa.focus();
	} else return true;
}

function popup(url)
{
	var x=800; var y=600;
	aa = window.open(url, '', 'resizable,scrollbars,status,menubar=0,width=' + x + ',height=' + y); 
	if (aa) {
		return false;
		aa.focus();
	} else return true;
}

function crypt_mail(s)
{
	var r="";
	for(i=0; i < s.length; i++)
	{ 
		n = s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-4); 
	}
	return r;
}

function attach()
{
	var obj = document.getElementById('attach_file')
	if (obj.style.display == 'none')
		new_display = 'block'
	else
		new_display = 'none'
	obj.style.display = new_display
}

function firm_logo(firm_ID,logo,url)
{
	document.getElementById("top-firmy-logo").src = "/logo/pic_" + firm_ID +"."+ logo
	document.getElementById("top-firmy-url").href = url
}

function firm_logo_text(firm_ID,logo,url,content)
{
	document.getElementById("top-firmy-logo").src = "/logo/pic_" + firm_ID +"."+ logo
	document.getElementById("top-firmy-url").href = url
	document.getElementById("firma-content").innerHTML = content
}

function nahled(oform)
{
	var x=700; var y=600;
	aa = window.open('', 'preview', 'resizable,scrollbars,status,menubar=0,width=' + x + ',height=' + y); 
	aa.focus()

	oform.target = 'preview'
	oform.action = 'index.fcgi'
	oform.a.value= 'offer'
	oform.preview.value = 1
	oform.submit()
}

function nahled_reset(oform)
{
	oform.target = ''
	oform.action = 'adminfirm.fcgi'
	oform.a.value= 'offer_save'
	oform.preview.value = 0
	return true
}

function LogWrite(type,offer_ID,firm_ID)
{
	xmlhttp.open("GET", "/index.fcgi?a=show&type=" + type + "&offer_ID=" + offer_ID + "&firm_ID=" + firm_ID + "&nocache=" + Math.random(), false );
	xmlhttp.send(null);
	var _response = xmlhttp.responseText;
}

function LogWriteInfo(type,offer_ID,firm_ID,info)
{
	xmlhttp.open("GET", "/index.fcgi?a=show&type=" + type + "&offer_ID=" + offer_ID + "&firm_ID=" + firm_ID + "&info=" + info + "&nocache=" + Math.random(), false );
	xmlhttp.send(null);
	var _response = xmlhttp.responseText;
}

function setCheckboxes(form_name, checkbox_name, do_check){
  var elts = (typeof(document.forms[form_name].elements[checkbox_name]) != 'undefined')
           ? document.forms[form_name].elements[checkbox_name]
           : false ;
  var elts_cnt  = (typeof(elts.length) != 'undefined')
                ? elts.length
                : 0 ;
  if (elts_cnt){
    for (var i=0; i<elts_cnt; i++){
      elts[i].checked = do_check;
    } 
  } 
  else{
    elts.checked = do_check;
  } 
  return true;
}

function xmlhttpPost(nabidka_id, action_id) {
	var strURL = "/test.fcgi?a=send_forward&nabidka_id="+nabidka_id+"&action_id="+action_id;
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, false); // 3rd parameter: async
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send();
}

function showIEbubble(){
	var agent = window.navigator.userAgent;
	if(agent.indexOf('Windows NT 6.1') != -1 && agent.indexOf('MSIE 9.0') != -1){
		return 1;
	}
	return 0;
}

var jumpList = {
	/*- onload volani init metody -*/
	loadJumpList : function(){
		window.onload = function(){
			jumpList._init();
		}
	},
	/*- zouseni sitemode, jakmile neni vytvoreni rq pote volame vytvoreni jumplistu -*/
	_init : function(){
		try {
			if(window.external && ("msIsSiteMode" in window.external)){
				if(window.external.msIsSiteMode()){
					jumpList._updateJumpListItems();
					jumpList._makeJumpList();
				} else {
					var bubble = document.getElementById('IEbubble');
					bubble.className = 'active';
					bubble.getElementsByTagName('a')[0].onclick = function(e){
						window.external.msAddSiteMode();
						return false;
					}
				}
			}
		} catch(e) {
			console.log(e)
		};
	},
	/*- vytvoreni samotneho jumplistu jakmile sme v sitemode -*/
	_makeJumpList : function(){
		var ext = window.external,
			lastSearch = LS.get('lastSearch'),
			items = LS.get('lastVisitedItems') || [];
		
		ext.msSiteModeClearJumpList();
		ext.msSiteModeCreateJumplist("Vyhledávání");

		for(var i=(items.length -1);i>=0;i--){
			var item = items[i];
			ext.msSiteModeAddJumpListItem(item[0], item[1],"");
		}

		if(lastSearch) {
			ext.msSiteModeAddJumpListItem("Poslední hledání", lastSearch,"/_img/pes_sprace.ico");
		}
				
		ext.msSiteModeShowJumplist();
		
		/*ext.msSiteModeSetIconOverlay('/_img/pes_sprace.ico', 'Sprace.cz');*/
	},
	/* Update hodnot v localstore pro generovani JumpListu*/
	_updateJumpListItems: function() {
		if(this._localStorageSupport()) {


			if(window.location.href.match("Vyhledat=Hledej") || window.location.href.match("vyhledat=Vyhledat")) {
				LS.set('lastSearch', window.location.href)
			}

			if(window.location.href.match("/nabidka-prace")) {
				var items = LS.get('lastVisitedItems') || [];
				var href = window.location.href;
				var titleEl = document.getElementsByTagName("title")[0];
				var title = titleEl.innerHTML;
				title = title.replace(/\s+/, " ").strip();
				for(var i=0; i< items.length;i++) {
					if(items[i][1] == href) {
						items.splice(i,1);
						break;
					}	
				}
				items.unshift([title, href])
				if(items.length > 10) {
					items.pop()
				}
				LS.set('lastVisitedItems', items);
			}
		}
	},
	_localStorageSupport: function() {
		try {
			return 'localStorage' in window && window['localStorage'] !== null;
		} catch(e){
			return false;
		}
	}
}
// Helper pro ukladani objektu do LocalStorage
LS = {
	ls:window.localStorage,
	set: function(key, value) {
		var value = typeof(value) == "object" ? Object.toJSON(value) : value;
    	this.ls.setItem(key, value);
	},
	get: function(key) {
    	var res = this.ls.getItem(key) && this.ls.getItem(key);
    	try {
    		res = res.evalJSON();
    	} catch(e) {}
    	return res;
	}
}

jumpList.loadJumpList();

var replyFormValidation = {

	// id povinnych poli
	items : ["jmeno", "prijmeni", "mail_from", "file1"],
	
	// vsechny mozne chyby validace
	errMessages : {
		1 : "Zadejte, prosím, jméno.",
		2 : "Zadejte, prosím, příjmení.",
		3 : "Zadejte, prosím, e-mail.",
		4 : "Zadaný e-email nemá správný formát.",
		5 : "Vložte, prosím, životopis."
	},
	
	// uchovava seznam aktualnich chyb
	errors : [],
	
	init : function () {
		var canProceed = false;
		var prevValidation = $$("ul.error");
			
		// vycistit chyby po predchozi validaci	
		for(var i=0; prevValidation.length > i; i++) {
			Element.remove(prevValidation[i]);
			
		}
		this.errors = [];
		
		this.validate();
		
		if(this.errors.length !== 0) {
			this.showErrors();
		} else {
			canProceed = true;
		}
		
		return canProceed;
	},
	
	validate : function() {
		for(var i=0; this.items.length > i; i++) {
			switch(this.items[i]) {
				case "jmeno":
					if($(this.items[i]).value == "") { 
						this.errors.push(1);
					}
					break;
				case "prijmeni":
					if($(this.items[i]).value == "") { 
						this.errors.push(2);
					}
					break;
				case "mail_from":
					if($(this.items[i]).value == "") {
						this.errors.push(3);												
					} else {
						var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
						if(!($(this.items[i]).value).match(re)) { 
							this.errors.push(4);
						}
					}
					break;
				case "file1":
					if($(this.items[i]).value == "") {
						this.errors.push(5);
					}
					break;
				default:
					break;
			}
		}
	},
	
	showErrors : function() {
		var s = "<ul class=\"error\">";
		
		for(var i = 0; this.errors.length > i; i++) {
			s += "<li>"+this.errMessages[this.errors[i]]+"</li>";
		}
		
		$("replyform").insert({'top' : s+"</ul>"});
	}
	
}

