var recordcounter = 0;

var wflag = 0;
var w='';

function checkSize() {
    if (navigator.appName=="Netscape") {
	winW = window.innerWidth;
	winH = window.innerHeight;
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
	winW = document.body.offsetWidth;
	winH = document.body.offsetHeight;
    }
    if (winW < 632) {
	window.resizeTo(640,480);
    }
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {	
	//if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			//document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
			document.getElementById(changeImages.arguments[i]).src = changeImages.arguments[i+1];
		}
	//}
}


var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		orszag_01_komarom_esztergom_over = newImage("http://www.e-x.hu/images/map/orszag_01-komarom-esztergom.gif");
		orszag_01_veszprem_over = newImage("http://www.e-x.hu/images/map/orszag_01-veszprem_over.gif");
		orszag_01_gyor_moson_sopron_over = newImage("http://www.e-x.hu/images/map/orszag_01-gyor-moson-sopron.gif");
		orszag_01_vas_over = newImage("http://www.e-x.hu/images/map/orszag_01-vas_over.gif");
		orszag_01_zala_over = newImage("http://www.e-x.hu/images/map/orszag_01-zala_over.gif");
		orszag_01_nograd_over = newImage("http://www.e-x.hu/images/map/orszag_01-nograd_over.gif");
		orszag_01_heves_over = newImage("http://www.e-x.hu/images/map/orszag_01-heves_over.gif");
		orszag_01_jasz_nagykun_over = newImage("http://www.e-x.hu/images/map/orszag_01-jasz-nagykun_over.gif");
		orszag_01_baranya_over = newImage("http://www.e-x.hu/images/map/orszag_01-baranya_over.gif");
		orszag_01_baz_over = newImage("http://www.e-x.hu/images/map/orszag_01-baz_over.gif");
		orszag_01_szabolcs_over = newImage("http://www.e-x.hu/images/map/orszag_01-szabolcs_over.gif");
		orszag_01_hajdu_bihar_over = newImage("http://www.e-x.hu/images/map/orszag_01-hajdu-bihar_over.gif");
		orszag_01_bekes_over = newImage("http://www.e-x.hu/images/map/orszag_01-bekes_over.gif");
		orszag_01_csongrad_over = newImage("http://www.e-x.hu/images/map/orszag_01-csongrad_over.gif");
		orszag_01_tolna_over = newImage("http://www.e-x.hu/images/map/orszag_01-tolna_over.gif");
		orszag_01_fejer_over = newImage("http://www.e-x.hu/images/map/orszag_01-fejer_over.gif");
		orszag_01_bacs_kiskun_over = newImage("http://www.e-x.hu/images/map/orszag_01-bacs-kiskun_over.gif");
		orszag_01_somogy_over = newImage("http://www.e-x.hu/images/map/orszag_01-somogy_over.gif");
		orszag_01_pest_over = newImage("http://www.e-x.hu/images/map/orszag_01-pest_over.gif");
		orszag_01_budapest_over = newImage("http://www.e-x.hu/images/map/orszag_01-budapest_over.gif");
		preloadFlag = true;
	}
}

function isEmpty (inputField)
{
	if (!inputField.value) return true;
	else return false;
}

function CharCounter(textarea, charcount, max) {
   if (textarea.value.length > max) {
      textarea.value = textarea.value.substring(0, max);
   } else {
      charcount.value = max - textarea.value.length;
   }
}

function buildSelect2(selectedItem, list, listvalues, childid, selectedCityId)
{
    theMakeIndex=selectedItem.selectedIndex;
		    strModelList = list[theMakeIndex];		    
		    strModelValues = listvalues[theMakeIndex]
             document.getElementById(childid).length = 1          //reset model option list to show only "no preference"
            arrModel = new Array();
            arrModelValues = new Array();
            tstModel = new Array();
            if (selectedItem.value != '-1')
			{
			    strModelList = strModelList.replace(/&#337;/g,"ő");
			    strModelList = strModelList.replace(/&#369;/g,"ű");
			    arrModel = strModelList.split("|");
			    arrModelValues = strModelValues.split("|");
			}                        
                        
			document.getElementById(childid).length = (1)
            document.getElementById(childid).options[0].text = 'Mind';
            document.getElementById(childid).options[0].value = '-1'; 
            document.getElementById(childid).options[0].id = childid + '0';
        	if (selectedCityId == '-1')
            	document.getElementById(childid).options[(0)].selected = 'selected';
			
            if (selectedItem.value != '-1')
            {
	            for (i=0; i<arrModel.length; i++) 
	            { 
	                if(arrModel[i].value==0 || arrModelValues[i].value==0) {return}                
	                document.getElementById(childid).length = (i+1)    //start at 1 to leave 'no preference' option untouched
	                if(arrModel[i] != "")  //test for blank model name 
	                {
	                    document.getElementById(childid).options[(i)].text = arrModel[i];
	                    document.getElementById(childid).options[(i)].value = arrModelValues[i]; 
	                    document.getElementById(childid).options[(i)].id = childid + arrModelValues[i]; 
	                    if (arrModelValues[i] == selectedCityId)
	                    {
		                    document.getElementById(childid).options[(i)].selected = 'selected';
	                    }
	                }
	             }
			}               
}

function buildSelect3(selectedItem, list, listvalues, childid)
{

			theMakeIndex = selectedItem.selectedIndex+1;	
			strModelList = list[theMakeIndex];
		    strModelList = strModelList.replace(/&#337;/g,"ő");
		    strModelList = strModelList.replace(/&#369;/g,"ű");
		    strModelValues = listvalues[theMakeIndex]
            document.getElementById(childid).length = 1          
            
            arrModel = new Array();
            arrModelValues = new Array();
            tstModel = new Array();
            arrModel = strModelList.split("|");
            arrModelValues = strModelValues.split("|");


		   if(strModelList != ""){
				document.getElementById(childid).length = (1);
	            document.getElementById(childid).options[0].text = 'Nincs kiválasztva';
	            document.getElementById(childid).options[0].value = '0'; 
	            document.getElementById(childid).options[0].id = childid + '0';
	        }  
			
			for (i=0; i<arrModel.length; i++) { 
				if(arrModel[i].value==0 || arrModelValues[i].value==0) {return}                
                document.getElementById(childid).length = (i+1)    //start at 1 to leave 'no preference' option untouched
                if(arrModel[i] != "") { //test for blank model name 
                    document.getElementById(childid).options[(i)].text = arrModel[i];
                    document.getElementById(childid).options[(i)].value = arrModelValues[i]; 
                    document.getElementById(childid).options[(i)].id = childid + arrModelValues[i]; 
                }
             }     
					
           
}

function popup(mylink, windowname, width, height)
{
	if (!window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;

	leftPos = 0;
	topPos = 0;

	if (screen) {
   		x = ((screen.width / 2) - (width / 2));
   		y = ((screen.height / 2) - (height / 2));
   		if (x>0) {
      		leftPos = x;
   		}
   		if (y>0) {
      		topPos = y;
   		}
	}

	w = window.open(href, windowname, 'width=' + width +',height='+ height + ',scrollbars=yes, resizable=yes, left=' + leftPos + ',top=' + topPos);
	w.focus();
	return false;
}

function scrollpopup(mylink, windowname, width, height)
{
	if (!window.focus)return true;
		var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;

	leftPos = 0;
	topPos = 0;

	if (screen) {
   		x = ((screen.width / 2) - (width / 2));
   		y = ((screen.height / 2) - (height / 2));
   		if (x>0) {
      		leftPos = x;
   		}
   		if (y>0) {
      		topPos = y;
   		}
	}

	w = window.open(href, windowname, 'width=' + width +',height='+ height + ',scrollbars=yes, resizable=yes, left=' + leftPos + ',top=' + topPos);
	w.focus();
	return false;
}


function createpopup(mylink, windowname, width, height)
{
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;

	leftPos = 0;
	topPos = 0;

	if (screen) {
   		x = ((screen.width / 2) - (width / 2));
   		y = ((screen.height / 2) - (height / 2));
   		if (x>0) {
      		leftPos = x;
   		}
   		if (y>0) {
      		topPos = y;
   		}
	}

	w = window.open(href, windowname, 'width=' + width +',height='+ height + ',scrollbars=no, resizable=yes, left=' + leftPos + ',top=' + topPos);
	w.focus();
	return w;

}

function closepopup(flag, popupwindow) {
	if (flag == 1) {
		if (popupwindow.closed == false) {
	    	popupwindow.close();
		}
	}
}

function CharCounter(textarea, charcount, max) {
   if (textarea.value.length > max) {
      textarea.value = textarea.value.substring(0, max);
   } else {
      charcount.value = max - textarea.value.length;
   }
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function categoryCheck(input)
{
	if ( input.value != '0')
	{
		document.searchForm.submit();
	
	}
	else
	{
		alert ("Kérem válasszon kategóriát!");
	}
}

function generateDirectUrl()
{
	var firmName = document.getElementById('companyFirmName').value;
	firmName = firmName.toLowerCase();	
	Ref="1234567890qwertzuiopasdfghjklyxcvbnmíöüóőúéáű?:-_?!=()"
	Ref2="1234567890qwertzuiopasdfghjklyxcvbnmiouooueau---_-----"
	Result=""	
	for (Count=0; Count<firmName.length; Count++) 
	{
		Char=firmName.substring (Count, Count+1); 
		Num=Ref.indexOf (Char);		
		EncodeChar=Ref2.substring(Num, Num+1) 
		Result += EncodeChar;
	}
	document.getElementById('companyDirectUrl').value=Result;
}

function craftSelected()
{
	var areaCraftId = document.getElementById('companyTiparId').value;
	var professionalCraftId = document.getElementById('companySziparId').value;
	if((areaCraftId==0) || (professionalCraftId==0))
	{
		document.getElementById("approverCraft").style.display='none';		
		document.getElementById("approverLabel").style.display='none';
		if(areaCraftId==0)
			document.getElementById("approverCraft").value='2';
		if(professionalCraftId==0)
			document.getElementById("approverCraft").value='1';
	}
	else
		{
			document.getElementById("approverCraft").style.display='block';		
			document.getElementById("approverLabel").style.display='block';			
		}
	
	if((areaCraftId==0) && (professionalCraftId==0))
	{
		document.getElementById("craftPriceDiv").style.display='none';
		document.getElementById("noCraftPriceDiv").style.display='block';	
	}
	else
	{
		document.getElementById("craftPriceDiv").style.display='block';
		document.getElementById("noCraftPriceDiv").style.display='none';
	}
}

// Lecseréli a \r\n karaktert <br/> -re

function cserelo(str)
{
	while(str.indexOf("\r\n") != -1)
		{
			str=str.replace("\r\n","&lt;br/&gt;");
		}
	return str;
}

// Rámuatásnál autómatikusan betöltődik a nagyított kép

function showProductImage(imageName, object) {
	var image_top = findPosY(object);
	var image_left = findPosX(object);
	
	document.getElementById('productImageLayer').style.display = 'block';
	document.getElementById('productImage').src = imageName;
    }

function hideProductImage() {
    document.getElementById('productImage').src = 'images/spacer.gif';
    document.getElementById('productImageLayer').style.display = 'none';
    }
    
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function craftTypeModified()
{
    if (document.getElementById('craftCorpType').value == '1')
    {
        document.getElementById('areaCraftTr').style.display='block';
	document.getElementById('professionCraftTr').style.display='none';
    }
    else
    {
	document.getElementById('areaCraftTr').style.display='none';
	document.getElementById('professionCraftTr').style.display='block';
    }
}

function craftSelected()
{
	var areaCraftId = document.getElementById('companyTiparId').value;
	var professionalCraftId = document.getElementById('companySziparId').value;
	
	if((areaCraftId==0) && (professionalCraftId==0))
	{
		document.getElementById("craftPriceDiv").style.display='none';
		document.getElementById("noCraftPriceDiv").style.display='block';	
	}
	else
	{
		document.getElementById("craftPriceDiv").style.display='block';
		document.getElementById("noCraftPriceDiv").style.display='none';
	}
}

function loadAllCounty(countyNumber)
{
     loadXml("countyCityDistrict?parentId=" + countyNumber + "&type=3", writeXMLToComboBox, document.getElementById('county'))
    
    if (document.getElementById('selectedCounty').value!='0')
    {
	//alert("-->Load city: " + document.getElementById('selectedCounty').value);
	
	for(i=0;i<document.getElementById('county').length;i++)
	{
	    if (document.getElementById('county').options[(i)].value == document.getElementById('selectedCounty').value)
	    {
		document.getElementById('county').options[(i)].selected = 'selected';		
	    }
	}
	loadAllCity();
    }
    
    if (document.getElementById('selectedCity').value!='0')
    {
	for(i=0;i<document.getElementById('city').length;i++)
	{
	    if (document.getElementById('city').options[(i)].value == document.getElementById('selectedCity').value)
		document.getElementById('city').options[(i)].selected = 'selected';
	}
	loadAllDistrict();
    }
    if (document.getElementById('selectedDistrict').value!='0')
    {
	for(i=0;i<document.getElementById('district').length;i++)
	{
	    if (document.getElementById('district').options[(i)].value == document.getElementById('selectedDistrict').value)
		document.getElementById('district').options[(i)].selected = 'selected';
	}
    }
}

function loadAllCity()
{
    if (document.getElementById('county').value != "0")
    {
	if( ! loadXml("countyCityDistrict?type=2&parentId=" + document.getElementById('county').value, writeXMLToComboBox, document.getElementById('city')) )
        {
	}
    }
    document.getElementById('district').length=1;
//    document.getElementById('district').options[(0)].text = "Válasszon";
    document.getElementById('district').options[(0)].value = "0";
    document.getElementById('city').options[(0)].selected = 'selected';
}

function loadAllDistrict()
{
    if (document.getElementById('city').value != "0")
    {
	if( ! loadXml("countyCityDistrict?type=1&parentId=" + document.getElementById('city').value, writeXMLToComboBox, document.getElementById('district')) )
        {
	    alert("Browser doesn't support external XML loading");
	}
    }    
}

function loadUsedCounty(countyCode)
{
    loadXml("usedArea?parentId=" + countyCode + "&type=3", writeXMLToComboBox, document.getElementById('county'))

    if (document.getElementById('selectedCounty').value!='0')
    {
	//alert("-->Load city: " + document.getElementById('selectedCounty').value);
	
	for(i=0;i<document.getElementById('county').length;i++)
	{
	    if (document.getElementById('county').options[(i)].value == document.getElementById('selectedCounty').value)
	    {
		document.getElementById('county').options[(i)].selected = 'selected';		
	    }
	}
	loadUsedCity();
    }
    
    if (document.getElementById('selectedCity').value!='0')
    {
	for(i=0;i<document.getElementById('city').length;i++)
	{
	    if (document.getElementById('city').options[(i)].value == document.getElementById('selectedCity').value)
		document.getElementById('city').options[(i)].selected = 'selected';
	}
	loadUsedDistrict();
    }
    if (document.getElementById('selectedDistrict').value!='0')
    {
	for(i=0;i<document.getElementById('district').length;i++)
	{
	    if (document.getElementById('district').options[(i)].value == document.getElementById('selectedDistrict').value)
		document.getElementById('district').options[(i)].selected = 'selected';
	}
	document.getElementById("districtdiv").style.display='block';
    }
    else
	document.getElementById("districtdiv").style.display='none';
        
}

function loadUsedCity()
{
    if (document.getElementById('county').value != "0")
    {
	if( ! loadXml("usedArea?type=2&parentId=" + document.getElementById('county').value, writeXMLToComboBox, document.getElementById('city')) )
        {
	    alert("Browser doesn't support external XML loading");
	}
    }
    document.getElementById('district').length=1;
//    document.getElementById('district').options[(0)].text = "Válasszon";
    document.getElementById('district').options[(0)].value = "0";
    document.getElementById('city').options[(0)].selected = 'selected';
}

function loadUsedDistrict()
{
    if (document.getElementById('city').value != "0")
    {	
		if( ! loadXml("usedArea?type=1&parentId=" + document.getElementById('city').value, writeXMLToComboBox, document.getElementById('district')) )
	        {
		    alert("Browser doesn't support external XML loading");
		}
		if (document.getElementById("district").length>1)
		    document.getElementById("districtdiv").style.display='block';
		else
		    document.getElementById("districtdiv").style.display='none';
    }
	
}

function loadXml(xmlFile, fun, comboBox)
{
    var xdoc;
    if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
    {
	xdoc = new ActiveXObject("Microsoft.XMLDOM");
	       
	xdoc.async = false;
	xdoc.load(xmlFile);
			   
	fun(xdoc, comboBox);
				 
	return true;
    }
    else if( document.implementation && document.implementation.createDocument )
    {
	xdoc = document.implementation.createDocument("", "", null);
	xdoc.async = false;
							    
	xdoc.onload = function()
	{
	    fun(xdoc, comboBox);
	}
	xdoc.load(xmlFile);		
								       
	return true;
    }
    else
    {
	return false;
    }
}

function writeXMLToComboBox(xdoc, comboBox)
{
    var movies = xdoc.getElementsByTagName("name");
    comboBox.length = movies.length + 1;
//    comboBox.options[(0)].text = "Válasszon";
    comboBox.options[(0)].value = "0";
    for(var i = 0; i < movies.length; i++)
    {
	comboBox.options[(i+1)].text = movies[i].firstChild.nodeValue;
	comboBox.options[(i+1)].value = movies[i].getAttribute("id");
    }
}

function helpBox(code) {
	if (code == 0)
		document.getElementById('helpBox').style.display='none';
	else
		document.getElementById('helpBox').style.display='block';
}

function extraHelpBox(code) {
	if (code == 0)
		document.getElementById('extraHelpBox').style.display='none';
	else
		document.getElementById('extraHelpBox').style.display='block';
}