function id$(x){return document.getElementById(x);}

function getEvent(e){
  if (window.event && window.event.srcElement)return(window.event.srcElement);
  else if(e && e.target)return(e.target);
  else return;
};

function canEvent(e){
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.stopPropagation && e.preventDefault) {
      e.stopPropagation();
      e.preventDefault();
    }
};

function find_a_node(x){
	if(x.parentNode.tagName.toLowerCase()=='a')return x.parentNode;
	else return find_a_node(x);
};

function addListeners() 
{
	if (!document.getElementById)return;
	
	addEvent( id$('search_form') , 'submit', bSubmit, false);
	addEvent( id$('adsl') , 'click', ChCatalogLinkBw, false);
	addEvent( id$('dial_up') , 'click', ChCatalogLinkBw, false);
	var all_links = id$('vsh3').getElementsByTagName('a');
	for (var i = 0; i < all_links.length; i++)
	{
		addEvent(all_links[i], 'click', handleLink, false);
	}
};


function test1234(e)
{
	alert(123);
	canEvent(e);
}

function handleLink(e) 
{
	var el=getEvent(e);
	if(el.tagName.toLowerCase()!='a')el=find_a_node(el);
	switch(true)
	{
		case( 'video'===el.id ):
		case( 'catalog'===el.id ):
			btnPos(el.id);
			canEvent(e);
		break;
		default:
			return;
		break;
	}
	
};

function ChCatalogLinkBw(e)
{
	var el=getEvent(e);
	var bw =( el.id == 'dial_up' )?'l':'h';
	var all_links = document.getElementsByTagName('a');
	for (var i = 0; i < all_links.length; i++)
	{
		if(all_links[i].href.match(/\/adsl\//i))
		{
			all_links[i].href = all_links[i].href.replace(/\/adsl\//ig, "/dial_up/");
		}
		else if(all_links[i].href.match(/\/dial_up\//i))
		{
			all_links[i].href = all_links[i].href.replace(/\/dial_up\//ig, "/adsl/");
		}
	}	
}



function bSubmit(e)
{
	var el=getEvent(e);
	if( ''===id$('sword').value )
	{
		canEvent(e);
	}
	else
	{
		yCookie.set( "sword_input" , id$('sword').value , { path: "/", domain:"machine-catalog.com" } );
	}
}

function init_cookie()
{

	if( 'video' === yCookie.get("btnPos")  ||  'catalog' === yCookie.get("btnPos"))
	{
		btnPos(yCookie.get("btnPos"));
	}
	if( null !== yCookie.get("sword_input") )
	{
		id$('sword').value=yCookie.get("sword_input");
	}
}

function btnPos(id)
{
	yCookie.set("btnPos",id,{ path: "/", domain:"machine-catalog.com" });
	id$('video').className='';
	id$('catalog').className='';
	id$(id).className='sf_clicked';
	id$('search_type').value = id;
}

function addEvent(elm, evType, fn, useCapture) {
  // cross-browser event handling for IE5+, NS6 and Mozilla 
  // By Scott Andrew 
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
};


function ajaxFunction()
{
	var xmlHttp;
	var x = new Array();
	x[0]="new ActiveXObject('Msxml2.XMLHTTP');";
	x[1]='new XMLHttpRequest();';
	x[2]="new ActiveXObject('Microsoft.XMLHTTP');";
	var i=0;
	while(i<x.length)
	{
		try{xmlHttp=eval(x[i]);break;
		}catch(e){}
		i++;
	}
	return xmlHttp;
}

var yCookie=YAHOO.util.Cookie;
addEvent(window, 'load', addListeners, false);
addEvent(window, 'load', init_cookie, false);
