// JavaScript Document
function EvalScripts(html) 
{
	var script;
	var reg = /(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/gi;

	while(script = reg.exec(html)) 
	{
		if(!script[1])
			continue;
			
		if (window.execScript) 
		{   
			// Internet Explorer
            execScript (script[1], "JavaScript");
        }
        else 
		{
			// Internet Explorer, Firefox, Opera, Safari
			setTimeout(script[1], 0);
		}
	}
}

function backorderAlert(addToBasketText, removeFromBackorderText, closeText)
{
	var container = document.createElement('div');
	container.id = 'winPopupContainer';
	container.name = 'winPopupContainer';
	
	var html = '<style>';
	html += '.popup_bg_cell';
	html += '{';
	html += '	filter: alpha(opacity=50); ';
	html += '	-khtml-opacity: 0.5;    ';
	html += '	-moz-opacity: 0.5;     ';
	html += '	opacity: 0.5;      ';  
	html += '	';
	html += '	background-color:#FFF;';
	html += '}';
	html += '';
	html += '.popup_bg_main';
	html += '{';
	html += '	border:5px solid #CCC;';
	html += '	background-color:#FFF;';
	html += '	width:10%;';
	html += '	height:10%;';
	html += '}';
	html += '</style>';
	html += '<table style="position:fixed; left:0; right:0; top:0; margin-left:0px; margin-right:0px; height:100%; padding:0" align="center" width="100%" height="100%" border="0" cellspacing="0" cellpadding="2">';
	html += '  <tr>';
	html += '    <td class="popup_bg_cell" colspan="3">&nbsp;</td>';
	html += '  </tr>';
	html += '  <tr>';
	html += '    <td class="popup_bg_cell">&nbsp;</td>';
	html += '    <td class="popup_bg_main" >';
	html += '    <div id="popupInternal" name="popupInternal" ><img style="padding:80px 130px 80px 130px;" src="style/default/loading.gif" width="64" height="64" /></div>';
	html += '    <br />';
	html += '    <button onClick="document.documentElement.removeChild(document.getElementById(\'winPopupContainer\'));" style="float:right; margin: 4px 4px 4px 4px; padding:3px 7px 3px 7px">' + closeText + '</button>';
	html += '    <button onClick="window.location = \'basket.php?action=removebackorder\';" style="float:right; padding:3px 7px 3px 7px; margin: 4px 4px 4px 4px;">' + removeFromBackorderText + '</button>';
	html += '    <button onClick="window.location = \'basket.php?action=usebackorder\';" style="float:right; padding:3px 7px 3px 7px; margin: 4px 4px 4px 4px;">' + addToBasketText + '</button>';
	html += '    </td>';
	html += '    <td class="popup_bg_cell">&nbsp;</td>';
	html += '  </tr>';
	html += '  <tr>';
	html += '    <td class="popup_bg_cell" colspan="3">&nbsp;</td>';
	html += '  </tr>';
	html += '</table>';
	html += '</html>';
	
	container.innerHTML = html;

	document.documentElement.appendChild(container);
	
	var xhr;
	if(window.XMLHttpRequest) 
	{
		xhr = new XMLHttpRequest(); //FireFox, Safari, Chrome
	}
	else if(window.ActiveXObject) 
	{
		try	
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP"); //Msxml2.XMLHTTP
		}
		catch(e) 
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer 
		}
	}
	

	
	xhr.onreadystatechange = function()
	{
		
		if(xhr.readyState == 4 && xhr.status >= 200 && xhr.status < 300)
		{
			try
			{
				if(window.opera)
				{
					document.getElementById('popupInternal').innerHTML = 'BackOrder Alert';
				}
				else
				{
					document.getElementById('popupInternal').innerHTML = ' ' + xhr.responseText;
					EvalScripts( document.getElementById('popupInternal').innerHTML ); 
				}
			}
			catch(err)
			{
				alert(err);
			}
		}
	}
	xhr.open('GET', 'backorder-popup.php', false, '', '');
	
	xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
	xhr.setRequestHeader("If-Modified-Since", "Sat, 11 Jan 1977 00:00:00 GMT");
	
	xhr.send('');
}

function addToCart(url, basketText, closeText)
{
	var container = document.createElement('div');
	container.id = 'winPopupContainer';
	container.name = 'winPopupContainer';
	
	var html = '<style>';
	html += '.popup_bg_cell';
	html += '{';
	html += '	filter: alpha(opacity=50); ';
	html += '	-khtml-opacity: 0.5;    ';
	html += '	-moz-opacity: 0.5;     ';
	html += '	opacity: 0.5;      ';  
	html += '	';
	html += '	background-color:#FFF;';
	html += '}';
	html += '';
	html += '.popup_bg_main';
	html += '{';
	html += '	border:5px solid #CCC;';
	html += '	background-color:#FFF;';
	html += '	width:10%;';
	html += '	height:10%;';
	html += '}';
	html += '</style>';
	html += '<table style="position:fixed; left:0; right:0; top:0; margin-left:0px; margin-right:0px; height:100%; padding:0" align="center" width="100%" height="100%" border="0" cellspacing="0" cellpadding="2">';
	html += '  <tr>';
	html += '    <td class="popup_bg_cell" colspan="3">&nbsp;</td>';
	html += '  </tr>';
	html += '  <tr>';
	html += '    <td class="popup_bg_cell">&nbsp;</td>';
	html += '    <td class="popup_bg_main" >';
	html += '    <div id="popupInternal" name="popupInternal" ><img style="padding:80px 130px 80px 130px;" src="style/default/loading.gif" width="64" height="64" /></div>';
	html += '    <br />';
	html += '    <button onClick="document.documentElement.removeChild(document.getElementById(\'winPopupContainer\')); " style="float:right; margin: 4px 4px 4px 4px; padding:3px 7px 3px 7px">' + closeText + '</button>';
	html += '    <button onClick="window.location = \'basket.php\';" style="float:right; padding:3px 7px 3px 7px; margin: 4px 4px 4px 4px;">' + basketText + '</button>';
	html += '    </td>';
	html += '    <td class="popup_bg_cell">&nbsp;</td>';
	html += '  </tr>';
	html += '  <tr>';
	html += '    <td class="popup_bg_cell" colspan="3">&nbsp;</td>';
	html += '  </tr>';
	html += '</table>';
	html += '</html>';
	
	container.innerHTML = html;

	document.documentElement.appendChild(container);
	
	var xhr;
	if(window.XMLHttpRequest) 
	{
		xhr = new XMLHttpRequest(); //FireFox, Safari, Chrome
	}
	else if(window.ActiveXObject) 
	{
		try	
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP"); //Msxml2.XMLHTTP
		}
		catch(e) 
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer 
		}
	}
	

	
	xhr.onreadystatechange = function()
	{
		
		if(xhr.readyState == 4 && xhr.status >= 200 && xhr.status < 300)
		{
			try
			{
				if(window.opera)
				{	
					document.getElementById('popupInternal').innerHTML = 'Product has been added to Shopping Cart';
				}
				else
				{
					document.getElementById('popupInternal').innerHTML = xhr.responseText;
					EvalScripts( document.getElementById('popupInternal').innerHTML );
				}
			}
			catch(err)
			{
				alert(err);
			}
		}
	}
	xhr.open('GET', url, true, '', '');
	
	xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
	xhr.setRequestHeader("If-Modified-Since", "Sat, 11 Jan 1977 00:00:00 GMT");
	
	xhr.send('');
}

function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
	// the highlightStartTag and highlightEndTag parameters are optional
	if ((!highlightStartTag) || (!highlightEndTag)) 
	{
		highlightStartTag = "<font style='color:red; background-color:yellow;'>";
		highlightEndTag = "</font>";
	}
  
	// find all occurences of the search term in the given text,
	// and add some "highlight" tags to them (we're not using a
	// regular expression search, because we want to filter out
	// matches that occur within HTML tags and script blocks, so
	// we have to do a little extra validation)
	var newText = "";
	var i = -1;
	var lcSearchTerm = searchTerm.toLowerCase();
	var lcBodyText = bodyText.toLowerCase();
	
	var thesame = new Array();
	thesame['ć'] = 'c';
	thesame['ś'] = 's';
	thesame['ł'] = ';';
	thesame['ń'] = 'n';
	thesame['ü'] = 'u';

	for(var key in thesame)
	{
		lcSearchTerm = lcSearchTerm.replace(key, thesame[key]);
		lcBodyText = lcBodyText.replace(key, thesame[key]);
	}
    
	while (bodyText.length > 0) 
	{
		i = lcBodyText.indexOf(lcSearchTerm, i+1);
		if (i < 0) 
		{
			newText += bodyText;
			bodyText = "";
		} 
		else 
		{
			// skip anything inside an HTML tag
			if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) 
			{
				// skip anything inside a <script> block
				if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) 
				{
					newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
					bodyText = bodyText.substr(i + searchTerm.length);
					lcBodyText = bodyText.toLowerCase();
					i = -1;
				}
			}
		}
	}
  
	return newText;
}

function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
	if(searchText.length < 2)
		return;
		
	var bodyText = document.getElementById('articlesbox').innerHTML;
	
	// if the treatAsPhrase parameter is true, then we should search for 
	// the entire phrase that was entered; otherwise, we will split the
	// search string so that each word is searched for and highlighted
	// individually
	if (treatAsPhrase)
	{
		searchArray = [searchText];
	}
	else
	{
		searchArray = searchText.split(" ");
	}
	
	if (!document.body || typeof(document.body.innerHTML) == "undefined")
	{
		if (warnOnFailure) 
		{
			alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
		}
		return false;
	}
	
	for (var i = 0; i < searchArray.length; i++)
	{
		bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
	}
	
	document.getElementById('articlesbox').innerHTML = bodyText;
	return true;
}
