// JavaScript Document
var kolekcja='';
var filename='';
var sizex='';
var sizey='';
var typk='';

function createPNGImage(src, width, height) {
    if (navigator.userAgent.indexOf("MSIE") != -1) {
        var element = document.createElement("div");
	element.style.filter = "progid:DXImageTransform.Microsoft." +
			       "AlphaImageLoader(src='" + src + "')";
    } else {
        var element = document.createElement("img");
        element.src = src;
    }
    element.style.width = width + "px";
    element.style.height = height + "px";
    return element;
}
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
  var xmlHttp;
  if(window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  else {
    try {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

function AN_select_product(prodid,language,kol,fn,sx,sy,typ) {
  kolekcja = kol;	
  filename = fn;
  sizex =sx;
  sizey = sy;
  typk = typ;
  
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "ajax_response.php?action=select_product&prodid=" + prodid + "&language=" + language, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_select_product(prodid,language,kol,fn,sx,sy,typ)', 1000);
  }
}

function AN_basket_promo(typ,language) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "ajax_response.php?action=change_promo&typ=" + typ + "&language=" + language, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
    // make the server request
    xmlHttp.send(null);
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_basket_promo(typ,language)', 1000);
  }
}


function AN_view_basket(language) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "ajax_response.php?action=view_basket&language=" + language, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
    // make the server request
    xmlHttp.send(null);
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_view_basket(language)', 1000);
  }
}

function AN_order_form(language) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("POST", "ajax_response.php?action=order_form&language=" + language, true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.setRequestHeader('Pragma', 'no-cache');
    xmlHttp.setRequestHeader('Cache-Control', 'must-revalidate');
    xmlHttp.setRequestHeader('Cache-Control', 'no-cache');
    xmlHttp.setRequestHeader('Cache-Control', 'no-store');
    var nowypost = '';
    xmlHttp.send(nowypost);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_order_form(language)', 1000);
  }
}

function AN_preview_form(language,serialized) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("POST", "ajax_response.php?action=preview_form&language=" + language, true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send(serialized);	
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
    // make the server request
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_preview_form(language,serialized)', 1000);
  }
}


function AN_change_basket(prodid,newcount,language) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "ajax_response.php?action=change_basket&language=" + language + "&prodid=" + prodid + "&newcount=" + newcount, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
    // make the server request
    xmlHttp.send(null);
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_change_basket(prodid,newcount,language)', 1000);
  }
}

function AN_add_to_basket(prodid,language) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "ajax_response.php?action=add_to_basket&language=" + language + "&id=" + prodid, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
    // make the server request
    xmlHttp.send(null);
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_add_to_basket(prodid,language)', 1000);
  }
}
function AN_clear_basket(language) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    //name = encodeURIComponent(document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "ajax_response.php?action=clear_basket&language=" + language, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse2;
    // make the server request
    xmlHttp.send(null);
  } else {
    // if the connection is busy, try again after one second
    setTimeout('AN_clear_basket(language)', 1000);
  }
}

// executed automatically when a message is received from the server
function handleServerResponse() {
  var textLoader = '<br><br><br><center><img src="images/ajax-loader.gif" border="0"></center>';
  if (xmlHttp.readyState == (1 || 0 || 3 || 2)) {
	document.getElementById('produkt').innerHTML = textLoader; 
  } else if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
		response = xmlHttp.responseText;
		document.getElementById("produkt").innerHTML = response;
		var element = createPNGImage("images/elementy/" + kolekcja + "/" + typk + "_elementy/" + filename + "_normal.png", sizex, sizey);
        document.getElementById("output").appendChild(element);
    } else {
        alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function handleServerResponse2() {
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) {
      // extract the XML retrieved from the server
    //  xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
//	  xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
//	  response = xmlDocumentElement.firstChild.data;
	  response = xmlHttp.responseText;
	  // update the client display using the data received from the server
		document.getElementById("produkt").innerHTML = response;
// restart sequence
      //setTimeout('process()', 1000);
    }
    // a HTTP status different than 200 signals an error
    else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
