var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);

function getElement(id)
{
 if (isDOM) { return document.getElementById(id); }
 if (isIE4) { return document.all[id]; }
 if (isNS4) { return document.layers[id]; }
}

var selectedPageSheet=null;
var mode=0;

function onMainPageLoad(p,pmode)
{
 var _q=getElement('q');
 if(_q){_q.focus();}
 try{

 if (p==0) {selectedPageSheet = getElement('web_center');}
 if (p==1) {selectedPageSheet = getElement('ebay_center');}
 if (p==2) {mode=pmode;selectedPageSheet = getElement(pmode+'_center');}
 if (p==3) {selectedPageSheet = getElement('clickbank_center');}
 if (p==4) {selectedPageSheet = getElement('news_center');}
 if (p==6) {selectedPageSheet = getElement('youtube_center');}
 if (selectedPageSheet!=null) pageSheetClick(selectedPageSheet);
 } catch(e){};

}

function pageSheetClick(pageSheet)
{
	if ( pageSheet && pageSheet.id )
	{
		try{
		if ( selectedPageSheet && selectedPageSheet.id )
		{
			selectedPageSheet.className = 'bookmarkCenter';
			pId = selectedPageSheet.id.split('_');
			var pLeft = getElement(pId[0] + '_left');
			if ( pLeft ) { pLeft.className = 'bookmarkLeft'; }
			var pRight = getElement(pId[0] + '_right');
			if ( pRight ) { pRight.className = 'bookmarkRight'; }
		}
	        } catch(e){};
		mode=0;
		pageSheet.className = 'bookmarkCenterSel';
		var pId = pageSheet.id.split('_');
		var pLeft = getElement(pId[0] + '_left');
		if ( pLeft ) { pLeft.className = 'bookmarkLeftSel'; }
		var pRight = getElement(pId[0] + '_right');
		if ( pRight ) { pRight.className = 'bookmarkRightSel'; }
		selectedPageSheet = pageSheet;
		var pMode = getElement('mode');
		if ( pMode ) { pMode.value = pId[0]; }
	}
}

function CheckKey(e){

if( !e ) {
//if the browser did not pass the event information to the
//function, we will have to obtain it from the event register
if( window.event ) {
//Internet Explorer
e = window.event;
} else {
//total failure, we have no way of referencing the event
return;
}
}
if( typeof( e.keyCode ) == 'number' ) {
//DOM
e = e.keyCode;
} else if( typeof( e.which ) == 'number' ) {
//NS 4 compatible
e = e.which;
} else if( typeof( e.charCode ) == 'number' ) {
//also NS 6+, Mozilla 0.9+
e = e.charCode;
} else {
//total failure, we have no way of obtaining the key code
return;
}

if (e == 13){
changePage(getElement('q').value);
}
}

function changePage(findtext)
{
  var tmp=findtext;
    tmp=tmp.toLowerCase();
    tmp=trim(tmp);
	tmp=ltrim(tmp);
/*   alert(tmp); */
while (tmp.indexOf('"')!=-1)
  tmp=tmp.replace('"',' ');
while (tmp.indexOf("'")!=-1)
  tmp=tmp.replace("'",' ');
while (tmp.indexOf("\\")!=-1)
  tmp=tmp.replace("\\",' ');
//while (tmp.indexOf('-')!=-1)
//  tmp=tmp.replace('-','-');
while (tmp.indexOf('+')!=-1)
  tmp=tmp.replace('+',' ');
// while (tmp.indexOf('.')!=-1)
  // tmp=tmp.replace('.',' ');
while (tmp.indexOf(';')!=-1)
  tmp=tmp.replace(';',' ');
while (tmp.indexOf('*')!=-1)
  tmp=tmp.replace('*',' ');
while (tmp.indexOf('&')!=-1)
  tmp=tmp.replace('&',' ');
while (tmp.indexOf('^')!=-1)
  tmp=tmp.replace('^',' ');
while (tmp.indexOf('%')!=-1)
  tmp=tmp.replace('%',' ');
while (tmp.indexOf('(')!=-1)
  tmp=tmp.replace('(',' ');
while (tmp.indexOf(']')!=-1)
  tmp=tmp.replace(']',' ');
while (tmp.indexOf(')')!=-1)
  tmp=tmp.replace(')',' ');
while (tmp.indexOf('{')!=-1)
  tmp=tmp.replace('{',' ');
while (tmp.indexOf('[')!=-1)
  tmp=tmp.replace('[',' ');
while (tmp.indexOf('}')!=-1)
  tmp=tmp.replace('}',' ');
while (tmp.indexOf('$')!=-1)
  tmp=tmp.replace('$',' ');
while (tmp.indexOf('#')!=-1)
  tmp=tmp.replace('#',' ');
while (tmp.indexOf('!')!=-1)
  tmp=tmp.replace('!',' ');
while (tmp.indexOf('~')!=-1)
  tmp=tmp.replace('~',' ');
while (tmp.indexOf('`')!=-1)
  tmp=tmp.replace('`',' ');
while (tmp.indexOf('?')!=-1)
  tmp=tmp.replace('?',' ');
while (tmp.indexOf('  ')!=-1)
  tmp=tmp.replace('  ',' ');
while (tmp.indexOf(' ')!=-1)
  tmp=tmp.replace(' ','-');
//alert('*'+tmp);

  //if(tmp.lastIndexOf("maresme")<0) {tmp='maresme-'+tmp;}
  if (mode==0) {
  pId = selectedPageSheet.id.split('_');
  var html="A_"+tmp+","+pId[0]+".html";
  if (pId[0]=='web')
      html=tmp+".html";
  if (pId[0]=='ebay')
      html="E_"+tmp+".html";
  if (pId[0]=='clickbank')
      html="C_"+tmp+".html";
  if (pId[0]=='news')
      html="N_"+tmp+".html";
  if (pId[0]=='youtube')
      html="Y_"+tmp+".html";
  } else html="A_"+tmp+","+mode+".html";
  if (tmp!="")
  window.location=html;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
