// JavaScript Document

$(function() {
   $('#slider').cycle({ fx:     'scrollRight',  speed:   300,  timeout: 5000,   next:   '#slider',  pause:   1	 });
});


function grabobj(objname)
{
	if (document.getElementById) return document.getElementById(objname);
	if (document.all) return document.all[objname];
	if (document.layers) return document.layers[objname];
}

var prevobj='';
var prevobj2='';

function selectMan ()
{
	var obj=grabobj('manID');
	grabobj('pfind').value='';
	if (prevobj) prevobj.style.display='none';
	if (prevobj2) prevobj2.style.display='none';
	if (obj.options[obj.selectedIndex].value!="")
	{
		var val=obj.options[obj.selectedIndex].value;
		grabobj('opt_man_'+val).style.display='';
		prevobj=grabobj('opt_man_'+val);
	}
}

function selectMod (obj)
{
	if (prevobj2) prevobj2.style.display='none';
	if (obj.options[obj.selectedIndex].value!="")
	{
		var val1=grabobj('manID').options[grabobj('manID').selectedIndex].value;
		var val2=grabobj(val1+'_modID').options[grabobj(val1+'_modID').selectedIndex].value;
		grabobj('opt_ser_'+val1+'_'+val2).style.display='';
		grabobj(val1+'_'+val2+'_serID').selectedIndex=0;
		grabobj('pfind').value=val1+"\\"+val2;
	} else {
		grabobj('pfind').value='';
	}
}
function selectSer (obj)
{
	if (obj.options[obj.selectedIndex].value!="")
	{
		var val1=grabobj('manID').options[grabobj('manID').selectedIndex].value;
		var val2=grabobj(val1+'_modID').options[grabobj(val1+'_modID').selectedIndex].value;
		var val3=grabobj(val1+'_'+val2+'_serID').options[grabobj(val1+'_'+val2+'_serID').selectedIndex].value;
		grabobj('pfind').value=val1+"\\"+val2+"\\"+val3;
		prevobj2=obj;
	} else {
		var val1=grabobj('manID').options[grabobj('manID').selectedIndex].value;
		selectMod (grabobj(val1+'_modID'));
	}
}

function checkQuickFinder ()
{
	if (grabobj('pfind').value=='')
	{
		alert("Please enter both a manufacturer and model");
		return false;
	}
	return true;
}


function ajaxObject(url, callbackFunction) {
	var that=this;      
	this.updating = false;
	this.abort = function() {
		if (that.updating) {
			that.updating=false;
			that.AJAX.abort();
			that.AJAX=null;
		}
	}
	this.update = function(passData,postMethod) { 
		if (that.updating) { return false; }
		that.AJAX = null;                          
		if (window.XMLHttpRequest) {              
			that.AJAX=new XMLHttpRequest();
		} else {
			try {
				that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				that.AJAX=new IFrameXMLHttpRequest();
			}
		}                                             
		if (that.AJAX==null) {                             
			return false;                               
		} else {
			that.AJAX.onreadystatechange = function() {  
				if (that.AJAX.readyState==4) {             
					that.updating=false;
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
					that.AJAX=null;                                         
				}                                                      
			}                                                        
			that.updating = new Date();        

			if ($('ajaxcall')) $('ajaxcall').innerHTML=new Date();
			if (/post/i.test(postMethod)) {
				var uri=urlCall+'?'+that.updating.getTime();
				that.AJAX.open("POST", uri, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				that.AJAX.send(passData);
			} else {
				var uri=urlCall+'?'+passData+'&stamp='+(that.updating.getTime()); 
				that.AJAX.open("GET", uri, true);                             
				that.AJAX.send(null);                                         
			}              
			return true;                                             
		}
	}
	var urlCall = url;
	this.callback = callbackFunction || function () { };
}
function reloadCaptcha(imgId,folderLoc,prevIds) {
	var f=function(newImg) {
		if (document.getElementById(imgId)) {
			document.getElementById(imgId).src=folderLoc+newImg+'.gif?r='+Math.random();
		} else {
			for(var i=1; document.getElementById(imgId+'_'+i); i++) {
				document.getElementById(imgId+'_'+i).src=folderLoc+newImg+'.gif?r='+Math.random();
			}
		}
	}
	var ajax=new ajaxObject('/captcha-refresh.htm',f);
	ajax.update('?prevIds='+prevIds,'POST');
}
