
//window.onload = loadIndex;
//var d = new Date();
//var start_stime=d.getSeconds();
//var start_mtime=d.getMinutes();
//var start_smtime=d.getMilliseconds();

function loadIndex() { // load indexfile

try
	{
		if (window.ActiveXObject)
		{
			var errorHappendHere = "Check Browser and security settings";
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load("security_data.xml");
		}
		else if(window.XMLHttpRequest)
		{
			var errorHappendHere = "Error handling XMLHttpRequest request";
			var d = new XMLHttpRequest();
			d.open("GET", "security_data.xml", false);
			d.send(null);
			xmlDoc=d.responseXML;
		} else {
			var errorHappendHere = "Error.";
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.async=false;
			xmlDoc.load("security_data.xml");
		}
	}	
	catch(e)
	{
		alert(errorHappendHere);
	}


}

		

function searchIndex(searchterm) { // search the index (duh!)
	if (!xmlDoc) {
		loadIndex();}

	// get the search term from a form field with id 'searchme'
	if (searchterm != 'access')
	{
		var terms = searchterm;
	}
	else{  var terms = ["standalone", "capture", "camera", "lens", "bracket", "power", "cable", "kit"];}
	var	results = new Array;
	var allitems = xmlDoc.getElementsByTagName("ProductDescription");

	//check = new Boolean ();
	if (searchterm != 'access')
	{
   		for (var i=0;i<allitems.length;i++) {
		// see if the XML entry matches the search term,
		// and (if so) store it in an array
			var name = allitems[i].lastChild.nodeValue;
			var exp = new RegExp(searchterm,"i");
			if ( name.match(exp) != null) {
				results.push(allitems[i]);
			}
		}
	}
	else
	{
		for (var i=0;i<allitems.length;i++) {


// see if the XML entry matches the search term,
// and (if so) store it in an array
			var name = allitems[i].lastChild.nodeValue;
		
			for ( x=0; x<terms.length; x++)
			{
				var txt = new RegExp (terms[x], "i");
				if (name.match(txt) == null)
				{ check = true;}
				else { check = false;
				break;}
			}

			if (check)
			{results.push(allitems[i]);}

		}	
	}
	
	
			//if (check)
			//{results.push(allitems[i]);}

			//var exp = new RegExp(searchterm,"i");
			//if ( name.match(exp) != null) {
			//	results.push(allitems[i]);
			//}

// send the results to another function that displays them to the user
	//showResults(results, searchterm);
	
	showResults(results, searchterm);
}

function searchCameras(searchterm) { // search the index (duh!)
	if (!xmlDoc) {
		loadIndex();}

	// get the search term from a form field with id 'searchme'
	var	resultsCamera = new Array;
var	results = new Array;
var allitems = xmlDoc.getElementsByTagName("ProductDescription");

	//check = new Boolean ();
     for (var i=0;i<allitems.length;i++) {
		// see if the XML entry matches the search term,
		// and (if so) store it in an array
			var name = allitems[i].lastChild.nodeValue;
			var exp = new RegExp("camera","i");
			if ( name.match(exp) != null) {
				resultsCamera.push(allitems[i]);
			}
		}
		
		
if (searchterm == "ip cam")
{
	for (var i=0;i<resultsCamera.length;i++) {
		// see if the XML entry matches the search term,
		// and (if so) store it in an array
			var name = resultsCamera[i].lastChild.nodeValue;
			var exp = new RegExp("\\Wip","i");
			if ( name.match(exp) != null) {
				results.push(resultsCamera[i]);
			}
		}
	
	}
else
	{
		for (var i=0;i<resultsCamera.length;i++) {


// see if the XML entry matches the search term,
// and (if so) store it in an array
			var name = resultsCamera[i].lastChild.nodeValue;
		
			for ( x=0; x<resultsCamera.length; x++)
			{
				var txt = new RegExp ("ip", "i");
				if (name.match(txt) == null)
				{ check = true;}
				else { check = false;
				break;}
			}

			if (check)
			{results.push(resultsCamera[i]);}

		}	
	}
	
	
			//if (check)
			//{results.push(allitems[i]);}

			//var exp = new RegExp(searchterm,"i");
			//if ( name.match(exp) != null) {
			//	results.push(allitems[i]);
			//}

// send the results to another function that displays them to the user
	//showResults(results, searchterm);
	
	showResults(results, searchterm);
}



// Write search results to a table
function showResults(results, searchterm) {
	
// if there are any results, write them to a table 
		clear_previous_search(searchterm);
		var display_title;
		switch (searchterm)
		{
			case 'standalone':
			display_title = 'Standalone DVRs';
			break;
			case 'capture':
			display_title = 'Capture Cards';
			break;
			case 'lens':
			display_title = 'Lens';
			break;
			case 'ip cam':
			display_title = 'IP Cameras';
			break;
			case 'analog_cam':
			display_title = 'Analog Cameras';
			break;
			case 'bracket':
			display_title = 'Bracket Housing';
			break;
			case 'cable':
			display_title = 'Cables';
			break;
			case 'access':
			display_title = 'Accessories';
			break;
			case 'power':
			display_title = 'Power';
			break;
			case 'kit':
			display_title = 'Surveillance Kits';
			break;
			case 'dlink':
			display_title = 'D-Link';
			break;
			case 'trendnet':
			display_title = 'Trendnet';
			break;
			case 'avermedia':
			display_title = 'Avermedia';
			break;
			case 'axis':
			display_title = 'Axis';
			break;
			case 'sony':
			display_title = 'Sony';
			break;
			case 'vonnic':
			display_title = 'Vonnic';
			break;
			default: display_title ='';
			break;
		}
			
  		var result_div = document.getElementById(searchterm);
        var result_container = document.createElement("div");
        result_container.id = "result";
		result_container.setAttribute("width","815");
        result_container.style.margin = "0px";
  		result_container.style.top="0";
    	result_container.style.left="0";

		if (results.length > 0) {
 
		 var actual_result="";
			 var status;
            for(var i=0; i<results.length; i++) {
				if (results[i].getAttribute("Status") == null) { status="Active";}
				else { status = results[i].getAttribute("Status");}
		     actual_result= actual_result+'<tr><td>' + results[i].getAttribute("ProductID") + '</td><td>' + results[i].lastChild.nodeValue + '</td></tr>';
			}
			result_container.innerHTML ='<strong style="font-size:24px;">'+display_title+'</strong><br /><br /><table border="0" style="width: 815px;"><tr><th>SKUs</th><th>Descriptions</th></tr>'+ actual_result+'</table><div style="width:100%; text-align:right"><a href="#top" style="font-weight:bold;">&uArr;Top</a></div>';
            result_div.appendChild(result_container);
		
		
	} else {
// else tell the user no matches were found
	
			result_container.innerHTML ='<br />';
			 result_div.appendChild(result_container);
	}
}



function clear_previous_search (div_name)
{
	
		var d = document.getElementById(div_name);
         d.innerHTML="";

}

 
