// AJAX functions

var xHRObject = false;


 var xHRObject;
  try
    {
    xHRObject=new XMLHttpRequest();
    }
  catch (e)
    {
    try
      {
      xHRObject=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xHRObject=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("A böngészõd nem képes az AJAX technológia használatára!!");
//        return false;
        }
      }
    }

function loadPage(id)
{
  params = escape(id) + ".php";
  parent.foodframe.location = params;
}

function enSel(id,args)
{
  document.getElementById("ing"+id).disabled = false;
  params = escape(id) + ".php?" + args;
  parent.foodframe.location = params;
}


function loadPageState(id,args)
{
  params = escape(id) + ".php?" + args;
  parent.foodframe.location = params;
}

function getList(id)
{
  if (id=="12") {
    loadPage('custom');
  }
  if (id=="999") {
    loadPage('magnet0');
  }  
   else {
    parent.foodframe.location = "foodlist.php#" + escape(id);
  }
}

function getPage(id,args)
{
  params = escape(id) + ".php";
  xHRObject.open("POST", params, true);
  xHRObject.onreadystatechange = getData;
//  xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-2");
  xHRObject.send(null);
}


function sendRequest(id)
{
  params = "food.php?cat=" + escape(id);
  xHRObject.open("POST", params, true);
  xHRObject.onreadystatechange = getData;
//  xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-2");
  xHRObject.send(null);
}

function getData()
{
  if (xHRObject.readyState == 4 && xHRObject.status == 200)
  {
    var serverText = xHRObject.responseText;
//    frames['foodframe'].getElementById('lister').innerHTML = serverText;
    d = document.getElementById("foodframe");
    d.contentWindow.document.getElementById("lister").innerHTML = serverText;
  }
}

function sendToCart(action,id,sub)
{

  //  alert('->' + id + '\n->' + sub);
  cdata = id + '|' + sub;
  params = "cart.php?action=" + escape(action) + "&data=" + encodeURIComponent(cdata);
  parent.cart.location = params;
//  xHRObject.open("POST", params, true);
//  xHRObject.onreadystatechange = refreshCart;
//  xHRObject.send(null);
}

function sendCustomToCart(fId) {

  cString = '';  
  cF = document.customform;
  switch (fId)  {
    case 1000 :        
      	for (i=1;i<18;i++) {
      	   eName = 'ing'+i; 
      	   if (document.forms[0].elements[i].checked) { 
            cString+=document.forms[0].elements[i].value + ',';       
           }	   
        }
        
        fSub =   '|' + cF.price.value + '|' + cF.dressing.value + ',' + cString;
        break;
  
    case 999 :        
      	for (i=1;i<28;i++) {
      	   eName = 'chk'+i; 
      	   if (document.forms[0].elements[i].checked) { 
            cString+=document.forms[0].elements[i].value + ',';       
           }	   
        }
        
        fSub =   cF.size.value + '|' + cF.price.value + '|' + cF.base.value + ',' + cString;
        break;
    case 998 :              
        fSub =   document.halfform.size.value + ' cm|' + document.halfform.price.value + '|' + document.halfform.selected1.value + ',' + document.halfform.selected2.value + ',';
        break;        
    case 990 :       
        cString = cF.ing1.value + ',' + cF.ing2.value + ',' + cF.ing3.value + ',' + cF.udito.value + ',';
        fSub =   '|' + cF.price.value + '|' + cF.alap.value + ',' + cString;                                
        break;            
    case 991 :       
        cString = cF.ing1.value + ',' + cF.ing2.value + ',' + cF.ing3.value + ',' + cF.ing4.value + ',' + cF.f2.value;
        fSub =   '|' + cF.price.value + '|' + cF.alap.value + ',' + cString;
        break;            
    case 992 :       
        cString = cF.ing1.value + ',' + cF.ing2.value + ',' + cF.ing3.value + ',' + cF.ing4.value + ',' + cF.f2.value;
        fSub =   '|' + cF.price.value + '|' + cF.alap.value + ',' + cString;
        break;            
    case 993 :       
        cString = cF.ing1.value + ',' + cF.ing2.value + ',' + cF.ing3.value + ',' + cF.ing4.value + ',' + cF.f2.value;
        fSub =   '|' + cF.price.value + '|' + cF.alap.value + ',' + cString;
        break;
    case 994 :       
        cString = cF.selecteds.value + ',' + cF.f2.value + ',' + cF.udito.value;
        fSub =   '|' + cF.price.value + '|' + cString;
        break;                              
    case 980 :       
        cString = cF.alap.value + ',' + cF.ing1.value + ',' + cF.ing2.value + ',' + cF.ing3.value + ',' + cF.ing4.value + ',' + cF.csipos.value;
        cString = cString + ',' + cF.alap1.value + ',' + cF.ing12.value + ',' + cF.ing13.value + ',' + cF.ing14.value + ',' + cF.ing15.value + ',' + cF.csipos1.value;
        cString = cString + ',' + cF.alap2.value + ',' + cF.ing21.value + ',' + cF.ing22.value + ',' + cF.ing23.value + ',' + cF.ing24.value + ',' + cF.csipos2.value;                        
        fSub =   '|' + cF.price.value + '|' + cString;
        break;            
    case 979 :      
         xItem = '' // levett feltétek
         pItem = '' // feltett
         xC = 0;           
         tn = cF.toppingnum.value;

        // levett feltétek 
      	for (i=0;i<tn;i++) {
      	    eName = 'top'+i;
      	    if (!cF.elements[eName].checked) { 
              xItem += '-' + cF.elements[eName].value + ',';       
            }	   
        }
        // felrakott feltétek
      	xC = 27 + eval(tn);
      	tnn = eval(tn)+1;
    	       
    	  for (i=tnn;i<xC;i++) {    	    
    	   if (document.forms[0].elements[i].checked) { 
            pItem += '+' + document.forms[0].elements[i].value + ',';
          }	   
        }
        fId = cF.foodid.value;
        fSub =   cF.fsize.value + '|' + cF.price.value + '|' + xItem + pItem;        
        break;            
      
  }
  sendToCart('add',fId,fSub);
}

function refreshCart()
{
  if (xHRObject.readyState == 4 && xHRObject.status == 200)
  {
    var serverText = xHRObject.responseText;
    window.parent.document.getElementById('cart').innerHTML = serverText;
  }
}

function xgetList(id) {
    sendRequest(id);
}

function setOrder()
{
  if (xHRObject.readyState == 4 && xHRObject.status == 200)
  {
    var serverText = xHRObject.responseText;
    parent.document.getElementById('cart').innerHTML = serverText;
  }
}

function sendOrder(points,summa,modaddress,address,nr,floor,door,gatephone,needbill,coname,cocity,postcode,coaddress,conr,bn,on)
{
  params = "order.php?points=" + escape(points) + "&summa=" + escape(summa) + "&modaddress=" + escape(modaddress) + "&address=" + escape(address)+ "&nr=" + escape(nr)+ "&floor=" + escape(floor)+ "&door=" + escape(door)+ "&gatephone=" + escape(gatephone) + "&needbill=" + escape(needbill) + "&coname=" + escape(coname) + "&cocity=" + escape(cocity) + "&postcode=" + escape(postcode) + "&coaddress=" + escape(coaddress) + "&conr=" + escape(conr) + "&addressnote=" + escape(bn) + "&othernote=" + escape(on);
  xHRObject.open("POST", params, true);
  xHRObject.onreadystatechange = setOrder;
  xHRObject.send(null);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function calculate(balance,evalue,summa) {
  maxPoint = summa - 300;
  if (evalue > balance) {
    evalue = balance;
    document.getElementById("points").value = evalue;
  }
  divv = summa - evalue;
  if (evalue>maxPoint) {
    evalue = maxPoint;
    document.getElementById("points").value = evalue;
  }
  document.getElementById("totalprice").innerHTML = summa - evalue;
}

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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function shDiv(divName) {
    state = document.getElementById(divName).style.display;
    if (state=="none") {
        document.getElementById(divName).style.display='block';
    } else {
        document.getElementById(divName).style.display='none';
    }
}

function shDiv(id) {
	if(document.getElementById(id).style.display == 'none') document.getElementById(id).style.display = 'block';
	else document.getElementById(id).style.display = 'none';
	return false;
}

function setPageTitleFrame(ctitle) {
  window.parent.document.getElementById('ctitle').innerHTML = ctitle;
}

function setPageTitle(imgId) {
  window.parent.document.getElementById('titlepic').src = "templates/images/titles/" + imgId + ".gif";
}




function setActiveTab(tabId) {
  for (i=0;i<8;i++) {
    liId = 'a' + i;
    window.parent.document.getElementById(liId).className = "";
  }
  window.parent.document.getElementById(tabId).className = "current";
}

function makeSelection(selected1,selected2,size) {
  document.location='half.php?selected1=' + selected1 + '&selected2=' + selected2 + '&size=' + size;
}

function setPrice(eName,price) {
  
  switch (price)
  {
  case '32cm':
    toShow = '1490';
    break;
  case '45cm':
    toShow = '2590';
    break;
  case '60cm':
    toShow = '3790';
    break;        
  }    
  document.getElementById(eName).innerHTML = toShow;
  document.forms[0].price.value = toShow;
}

function setCPrice(eName,price) {
  
  switch (price)
  {
  case '32cm':
    toShow = document.getElementById('price1').value;
    break;
  case '45cm':
    toShow = document.getElementById('price2').value;
    break;
  case '60cm':
    toShow = document.getElementById('price3').value;
    break;        
  }    
  document.getElementById(eName).innerHTML = toShow;
  document.forms[0].price.value = toShow;
  //document.forms[0].size.value = price;
}


function bookmarksite(title, url){
  if (document.all)
  window.external.AddFavorite(url, title);
  else if (window.sidebar)
  window.sidebar.addPanel(title, url, "")
}

function showHideLegend(sOrHide) {
  parent.document.getElementById('legend').style.display = sOrHide;
}

function launchWindow () {
	
  
  //select all the a tag with name equal to modal

		//Cancel the link behavior

		//Get the A tag
		var id = $(this).attr('href');
	  var id = '#dialog' ;
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	

	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
  }			


