//
//	Functions for AJAX use
//	Copyright OLC Systems s.r.o.
//	Modified by: David pokorny
//	With source:  http://www.sitepoint.com/article/take-command-ajax

/*
 * @param async if false then a client waits for a server's response
 */
function makeHttpRequest(httpServer,paramsString, requestName, elementId, targetdiv, async) {
   var http_request = false;

   paramsString = encodeURI(paramsString);

   if (targetdiv==undefined) {
	   targetdiv = true;
   }

   if (async==undefined) {
	   async = true;
   } else {
	   if (async!=false) {
		   async = true;
	   }
   }

   var mytime = "&antiCache="+new Date().getTime();
   url = "http://www.wfc2010.cz/fis/fis_proxy.php"+"?request="+requestName;
   // prepare url add cookie identification

   var targetdivvalue="true";
   if (targetdiv==true) {
   		targetdivvalue = "true";
   } else {
   		targetdivvalue = "false";
   }
   
   var asyncvalue="true";
   if (async==true) {
   		asyncvalue = "true";
   } else {
   		asyncvalue = "false";
   }
   
   
   url = url + "&ajax=1"+paramsString+mytime+"&elementid="+elementId+"&targetdiv="+targetdivvalue+"&async="+asyncvalue;
  
   if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
              
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
           http_request.overrideMimeType('text/xml');
       }
   } else if (window.ActiveXObject) { // <=IE6
       try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
           try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
       }
   }

   if (!http_request) {
       alert('Váš prohlížeč nepodporuje techniku AJAX\'t Použíjte prosím kompatibilní prohlížeč.');
       return false;
   }
   
   if (async==true) {
     http_request.onreadystatechange = function() {
         if (http_request.readyState == 4) {
             if (http_request.status == 200) {
             		  if (targetdiv) {
   	              	eval('setContent("'+elementId+'",http_request.responseText)');
   	              } else {
  	 	             eval('fillObject("'+elementId+'",http_request.responseText)');
   	              }
             } else {
               //  alert('There is problem with communication. (Return code: ' + http_request.status + ')');
             }
         }
     }
   }

   http_request.open('GET', url, async);
   http_request.send(null);
   
   // Async call
   if (async==false) {
      if (http_request.status == 200) {
       		  if (targetdiv) {
            	eval('setContent("'+elementId+'",http_request.responseText)');
            } else {
              eval('fillObject("'+elementId+'",http_request.responseText)');
            }
       } else {
           // alert('There is problem with communication. (Return code: ' + http_request.status + ')');
       }
   }
}

// callback function
function fillObject(objectId,htmlresponse) {

    targetObject = document.getElementsByName(objectId);

    if (!targetObject[0]) {
    	window.alert("Neplatný cílový object "+objectId);
    	return;
    }
        
    targetObject[0].value = htmlresponse;
    // externallinks();
}

// callback function
function setContent(elementId,htmltext) {
   // check if elementid exist
    targetObject = document.getElementById(elementId);
    if (htmltext.substr(0,1)!="<") {
    	if (htmltext.length>0) {
    		window.alert(htmltext);
    	}
    } else {
    	if(htmltext.length>0) { 
		    if (!targetObject) {
			  window.alert("Target object "+elementId+" doesnot exist.");
		    } else {
			  document.getElementById(elementId).innerHTML=htmltext;
			  //externallinks();
		    }
		}
	}
}

//function for hiddin and appearing search box in multiform
// Honza Novak & Jakub Ferenc
function searchBoxAction(element){
	if(document.getElementById(element).style.display=="none")
		expand(element)
	else
		collapse(element)
}

function collapse(element){
	document.getElementById(element).style.display="none";
}
	
function expand(element){
	document.getElementById(element).style.display="block"
}

function resetToolTip(object) {
	if(object.value.search("...")>0) {
		object.value = "";
	}
}

 function openw(atributerel,wlocation){
   // Separate resolution and type
  	var height="", width="", type="", windowoptions="";
  	width = atributerel.substr(9,3);
  	height  = atributerel.substr(13,3);
  	type   = Number(atributerel.substr(17,1));	

  	if (type > 0) {
  		if ((Number(height)==0) || (Number(width)==0)) {
  		window.alert("Invalid height or width of window. Please check external parameter");
  		type = 0;
  		}
  	}
	
  	switch (type) {
  		case 1: // with toolbar resizable
        	params="location=yes,menubar=no,toolbar=yes,scrollbars=yes,resizable=yes,width="+width+",height="+height
  			break;
  		case 2: // onlyborder no resizable
       		params="location=no,menubar=no,toolbar=no,scrollbars=no,resizable=no,width="+width+",height="+height
    		break;
  		case 3: // onlyadress resizable
    		params="location=yes,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height
  			break;
		default:
  			params="location=yes,menubar=no,toolbar=yes,scrollbars=yes,resizable=yes,width=1000,height=768"     			
  			break;
  	}
  	window.open(wlocation,"_blank",params)
 }   
 
