var auto_complete_selected_id = 0;
var delayFlag = false;


function switchSplash(divtoShow)
{
	var listarray = new Array('introduction','shopping','desktops','repairs','support','contact');
	
	for (i = 0; i< listarray.length; i++)
	{
		if (listarray[i] == divtoShow)
		{
			showdiv(listarray[i] + 'Splash')
			document.getElementById(listarray[i] + 'Link').className = "over";
		}
		else
		{
			hidediv(listarray[i] + 'Splash');
			document.getElementById(listarray[i] + 'Link').className = "";
		}
	}
	
}







function switchRepair(divtoShow)
{
	var listarray = new Array('formatreinstall','cleanup','troubleshooting','upgrades');
	
	for (i = 0; i< listarray.length; i++)
	{
		if (listarray[i] == divtoShow)
		{
			showdiv(listarray[i] + 'Repair')
			document.getElementById(listarray[i] + 'Link').className = "over";
		}
		else
		{
			hidediv(listarray[i] + 'Repair');
			document.getElementById(listarray[i] + 'Link').className = "";
		}
	}
	
}






function switchInternet(divtoShow)
{
	var listarray = new Array('webdesign','ecommerce','update','domains_hosting','portfolio','quote','contact');
	
	for (i = 0; i< listarray.length; i++)
	{
		if (listarray[i] == divtoShow)
		{
			showdiv(listarray[i] + 'Internet')
			document.getElementById(listarray[i] + 'Link').className = "over";
		}
		else
		{
			hidediv(listarray[i] + 'Internet');
			document.getElementById(listarray[i] + 'Link').className = "";
		}
	}
	
}




//Get URL Parameter value
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}




function hidediv(divname)
{
	var obj = document.getElementById(divname);
	if(obj != null) { obj.style.display = 'none'; }
}

function showdiv(divname)
{
	var obj = document.getElementById(divname);
	if(obj != null) { obj.style.display = 'block'; }	
}
function flashNav(frameNumber) {
	New.GotoFrame(frameNumber);
	New.Play();
}






//AUTO COMPLETE
/* Displays auto complete box for search function */
function showautocomplete(textbox,charCode,type)
{

	var searchtext = textbox.value;
	var autocompletediv = document.getElementById("autocompletediv");
	
	var leftpos;
	var toppos;

	if(autocompletediv != null)
	{
		if(charCode == 27) //If ESC was pressed
		{
			hidediv('autocompletediv');
			auto_complete_selected_id= 0;
		}
		else
		{
			showdiv('autocompletediv');
		}		
	}
	else
	{
		var autocompletediv = document.createElement('div');
		autocompletediv.className = "autocompletediv";
		autocompletediv.id = "autocompletediv";
		
		textboxpos = findPos(textbox);
		autocompletediv.style.top = textboxpos['top'] + 28 + 'px';
		autocompletediv.style.left = textboxpos['left'] + 'px';
		
		var docbody = document.getElementsByTagName('body');
		docbody[0].appendChild(autocompletediv);
	}		

	if(searchtext.length > 2)
	{		
		if(type == "product")
		{
			var url = "http://www.bluedrop.com.au/includes/productsearch.php";
			formdata = 'searchtext=' + searchtext + '&date=' + Date();
		}
		
	
		var request = initializeXMLHTTP(); //initialize the request object 
		request.open("POST",url,true); 
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
		request.send(formdata);		
		
		request.onreadystatechange = function () {	
		if (request.readyState == 4) 
		{				
			
			var response = request.responseXML; //Retrieve XML from php function			
			var resultslist = response.getElementsByTagName("result"); //get list of category items from xml									
			
			if(resultslist.length == 0)
			{
				emptydivcontents(autocompletediv);
				autocompletediv.appendChild(document.createTextNode('No Products Found'));
			}
			else
			{
				emptydivcontents(autocompletediv);

				if(charCode == 38) //If UP arrow was pressed
				{
					if(auto_complete_selected_id == 1 || auto_complete_selected_id == 0)
					{
						auto_complete_selected_id = resultslist.length;	
					}
					else
					{
						auto_complete_selected_id--;
					}
				}
				else if(charCode == 40) //If DOWN arrow was pressed
				{
					if(auto_complete_selected_id == resultslist.length)
					{
						auto_complete_selected_id = 1;	
					}
					else
					{
						auto_complete_selected_id++;
					}
				}

				var newtable = buildtable(autocompletediv);

				for (var i=0;i<resultslist.length;i++)
				{	
					//Store item information in variables
					var productname = getXMLvalue("name",i,response);
					var productid = response.getElementsByTagName("id")[i].firstChild.nodeValue;
					var productcode = getXMLvalue("code",i,response);

					var newitem = document.createElement('tr');
					newtable.appendChild(newitem);
					newitem.className = "unselected";
					newitem.alt = productid;
					newitem.onclick = function () 
					{
						textbox.value = "";
						location.href = 'http://www.bluedrop.com.au/shop/?item=' + productid;					
					};	
					newitem.onmouseover = function() 
					{
						var container = document.getElementById('autocompletediv');
						var nodelist = container.childNodes;
						
						for(i=0;i<nodelist.length;i++)
						{
							if(nodelist[i].className == "selected")
							{
								nodelist[i].className = "unselected";	
							}
						}
						
						this.className = "selected";
					};
					newitem.onmouseout = function() { this.className = "unselected"; auto_complete_selected_id = 0; };

					newitem.onclick = function() { location.href = 'http://www.bluedrop.com.au/shop/?item=' + this.alt; };							

					var position = i+1;
					if(position == auto_complete_selected_id)
					{
						if(charCode == 13) //If ENTER was pressed
						{
							location.href = 'http://www.bluedrop.com.au/shop/?item=' + productid;
							
							hidediv('autocompletediv');
							textbox.value = "";
						}

						newitem.className = "selected";
					}
					
					var namecell = document.createElement('td');
					newitem.appendChild(namecell);

					var regesp = RegExp(searchtext,"i");
					var search_pos = 0;
					var search_string = productname;
					
					while(search_pos != productname.length)
					{
						var temppos = search_string.search(regesp);

						if(temppos == -1)
						{
							namecell.appendChild(document.createTextNode(productname.substr(search_pos,search_string.length)));
							search_pos = productname.length;							
						}
						else
						{
							namecell.appendChild(document.createTextNode(productname.substr(search_pos,temppos)));
							var strong = document.createElement('strong');
							strong.appendChild(document.createTextNode(productname.substr(temppos,searchtext.length)));
							namecell.appendChild(strong)
							search_pos = temppos + searchtext.length;														
							search_string = search_string.substr(search_pos,search_string.length);

						}
						
					}

					var search_pos = 0;
					var search_string = productcode;
		

				}
			}
		}};		
	}
	else
	{
		hidediv("autocompletediv");
	}	
}





function getformdata(formname)
{
	var theform = document.getElementById(formname);
	var getstr = "";
	
	for (i=0; i<theform.elements.length; i++) 
	{
		if(theform.elements[i] != null)
		{		
		 if (theform.elements[i].tagName == "INPUT") 
		 {			   
			if (theform.elements[i].type == "checkbox") 
			{
			   if (theform.elements[i].checked) 
			   {
				  getstr += theform.elements[i].id + "=" + encodeURIComponent(theform.elements[i].value) + "&";
			   } else {
				  getstr += theform.elements[i].id + "=&";
			   }
			}
			else if (theform.elements[i].type == "radio") 
			{
			   if (theform.elements[i].checked) 
			   {
				  getstr += theform.elements[i].id + "=" + encodeURIComponent(theform.elements[i].value) + "&";
			   }
			}
			else
			{
				getstr += theform.elements[i].id + "=" + encodeURIComponent(theform.elements[i].value) + "&";	
			}
		 }   
		 else if (theform.elements[i].tagName == "SELECT") 
		 {
			var sel = theform.elements[i];
			getstr += sel.name + "=" + encodeURIComponent(sel.options[sel.selectedIndex].value) + "&";
		 } 
		 else
		 {
			 getstr += theform.elements[i].id + "=" + encodeURIComponent(theform.elements[i].value) + "&";	
		 }
      }
	}
	   
	return getstr;			
}




function findPos(obj)
{
 var curleft = 0;
 var curtop = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft-obj.scrollLeft;
   curtop += obj.offsetTop-obj.scrollTop;
   var position='';
   if (obj.style&&obj.style.position) position=obj.style.position.toLowerCase();
   if ((position=='absolute')||(position=='relative')) break;
   while (obj.parentNode!=obj.offsetParent) {
    obj=obj.parentNode;
    curleft -= obj.scrollLeft;
    curtop -= obj.scrollTop;
   }
   obj = obj.offsetParent;
  }
 }
 else {
     if (obj.x)
      curleft += obj.x;
  if (obj.y)
      curtop += obj.y;
    }
	
 return {left:curleft,top:curtop};
}





function initializeXMLHTTP()
{
	
	if(typeof xmlhttp=="undefined")var xmlhttp=false; 
	
	/* running locally on IE6,IE7 */                      ;/*Gotta love IE's support for Conditional Compilation*/; /*@cc_on 
	  if(location.protocol=="file:"){ 
	   if(!xmlhttp)try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;} 
	   if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;} 
	  }                                                                                                             @cc_off @*/ 
	
	/* IE7, Firefox, Safari, Konqueror, Opera... the conventional/standard way */ 
	//Yes, standard: http://www.w3.org/TR/XMLHttpRequest/ 
	  if(!xmlhttp)try{ xmlhttp=new XMLHttpRequest(); }catch(e){xmlhttp=false;} 
	
	/* IE5, IE6 */ 
	  if(typeof ActiveXObject != "undefined"){ 
	   if(!xmlhttp)try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;} 
	   if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;} 
	  } 
	
	/* IceBrowser */ 
	  if(!xmlhttp)try{ xmlhttp=createRequest(); }catch(e){xmlhttp=false;} 
// Note IceBrowser does not support responseXML

	return xmlhttp;
}




function emptydivcontents(container)
{

	while(container.firstChild)
	{
	
		container.removeChild(container.firstChild);
	}

}




function buildtable(container)
{
                //Create table to store list
                var newtable = document.createElement('table');
                newtable.width = "100%";
                newtable.cellSpacing = "1";
                newtable.cellPadding = "0";
                container.appendChild(newtable);
                
                newtablebody = document.createElement('tbody');
                newtable.appendChild(newtablebody);               
                
                return newtablebody;
}



function getXMLvalue(tagname,i,response)
{
                var tag = response.getElementsByTagName(tagname)[i];
                
                if(tag.hasChildNodes())
                {
                                return tag.firstChild.nodeValue;
                }
                else
                {
                                return "";             
                }
}



function createproductautocomplete()
{
	
	var productsearch = document.getElementById('productsearch');
	
	productsearch.onkeyup = function(e)
	{
					var e=(e)?e:(window.event)?window.event:null;

					var charCode = e.which || e.keyCode;                   
					showautocomplete(this,charCode,'product');
	};
}

function delayStart(function_name,parameter_name)
{

	delayFlag = parameter_name;
	function_name = function_name + "('" + parameter_name + "')";
	setTimeout("if(delayFlag == '" + parameter_name + "') { " + function_name + "}",500);

}

function disableDelayFlag() {
	
	delayFlag = 0;
	
}