var b_isIE=(navigator.appName =='Microsoft Internet Explorer');
var b_isNS=(navigator.appName =='Netscape');

function checkemail( str )
{
	var testresults;
	var filter=/^\s*(\w+(\.\w+)*@(\w+\.)+\w{2,3})\s*$/

	if(filter.test(str))
	{
		testresults=true;
	} else {
		alert("Check E-mail address!");
		testresults=false;
	}
	return (testresults);
} 

function verifyForm( f )
{
	var o;

	o = document.getElementById('EMail');
	if( o!=null && !checkemail( o.value ) )
	{
		o.focus();
		return false;
	}
	o = document.getElementById('from');
	if( o!=null && TrimString( o.value ) == "" )
	{
		o.value = orig;
		o.focus();
		return false;
	}
	return true;
}

function TrimString(sInString)
{
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function getContent( o )
{
	if( b_isNS )
	{
		return TrimString( o.textContent );
	} else {
		return TrimString( o.innerText );
	}
}

var locateTagLevel=0;

function locateTag( p, tagName )
{
	var i;
	for( i=0; i < p.childNodes.length; i++ )
	{
		var o=p.childNodes[i];
		if( o.nodeName != null && o.nodeName == tagName )
			return o;
		if( o.tagName != null && o.tagName == tagName )
			return o;
		v = locateTag( o, tagName );
		if( v != null )
			return v;
	}
	return null;
}
function locateParentTag(oIn,tagName)
{
	var o = oIn;
	if( b_isNS )
	{
		while( o.nodeName != tagName )
		{
			o = o.parentNode;
			if( o == null )
				return null;
		}
	} else {
		while( o.tagName != tagName )
		{
			o = o.parentElement;
			if( o == null )
				return null;
		}
	}
	return o;
}
/* Used from articles */
function sendToFriendPage( user, oIn )
{
	var o = oIn;
	var p;
	var i;
	var txt;
	var idx;
	var subj;
	var oInTxt;
	var uri  = document.location.href;
	var tdSubj, tdBody;

	var table = locateParentTag( o, "TABLE" );
	if( table == null )
	{
		reportFailure( uri, "Unable to prepare an email. Error:001");
		return false;
	}
	if( getContent( table.rows[0] ) == 'Editorial' 
	 && getContent( table.rows[2] ) == 'Go to our forums to discuss this article' )
	{
		tdSubj=1;
		tdBody=3;
	}
	else
	{
		reportFailure( uri, "Unable to prepare an email. Error:002\n:" + getContent( table.rows[0] ) + ":" + getContent( table.rows[2] ) + ":");
		return false;
	}

	o = table.rows[tdSubj].cells[0];
	p = locateTag( o, "B" );
	subj = getContent( p );

	var sSplit = subj.split( "\n" );
	subj="";
	for( key in sSplit )
	{
		var st = TrimString( sSplit[key] );
		if( st.substr( 0, 3) == "by " )
			break;
		subj = subj + st + " ";
	}
	subj = TrimString(subj);

	// Now the text
	// ----------------------------------------------------------
	txt = getContent( table.rows[tdBody] );

	// Let see if we have extra table in the content
	p = table.rows[tdBody].cells[0].getElementsByTagName("TABLE");
	if( p != null )
	{
		for (i=0; i<p.length; i++) 
		{
			oInTxt=getContent( p[i] );
			var oInTxtLen = oInTxt.length;
			if( oInTxt.substr(0,oInTxtLen) == txt.substr(0,oInTxtLen) )
			{
				txt = TrimString( txt.substr( oInTxt.length ) );
				break;
			}
		}
	}
	idx = txt.substr( 200 ).indexOf( "\n" );
	if( idx > 0 && idx < 50 )
	{
		txt = txt.substr( 0, 201+idx );
	} else {
		idx = txt.substr( 200 ).indexOf( "." )
		if( idx > 0 )
		{
			txt = txt.substr( 0, 201+idx );
		} else {
			txt = TrimString( txt.substr( 0, 300 ));
		}
	}
	return sendToFriendCommon( user, subj, txt, uri )
}
function sendToFriendTag( user )
{
	var subj, txt, uri

	subj = document.getElementsByTagName("theSubj")[0].textContent;
	txt  = document.getElementsByTagName("theText")[0].textContent;
	uri  = document.location.href;
	return sendToFriendCommon( user, subj, txt, uri );
}
function sendToFriend( user, subj, body, uri )
{
	var txt
	txt = body

	idx = txt.substr( 200 ).indexOf( "\n" );
	if( idx > 0 && idx < 50 )
	{
		txt = txt.substr( 0, 201+idx );
	} else {
		idx = txt.substr( 200 ).indexOf( "." )
		if( idx > 0 )
		{
			txt = txt.substr( 0, 201+idx );
		} else {
			txt = TrimString( txt.substr( 0, 300 ));
		}
	}
	return sendToFriendCommon( user, subj, txt, uri )
}
/* Used from rumorsDB.asp & hotnews.asp */
function sendToFriendBody( user, tID, uri )
{
	var txt, subj
	subj = document.getElementsByTagName("ART"+tID)[0].textContent;
	txt  = document.getElementsByTagName("ARB"+tID)[0].textContent;

	idx = txt.substr( 200 ).indexOf( "\n" );
	if( idx > 0 && idx < 50 )
	{
		txt = txt.substr( 0, 201+idx );
	} else {
		idx = txt.substr( 200 ).indexOf( "." )
		if( idx > 0 )
		{
			txt = txt.substr( 0, 201+idx );
		} else {
			txt = TrimString( txt.substr( 0, 300 ));
		}
	}
	return sendToFriendCommon( user, subj, txt, uri )
}

function sendToFriendCommon( user, subj, txt, uri )
{
	var w = window.open('','','width=620,height=500');
	w.document.write('<html><head>');
	w.document.write('<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">');
	w.document.write('<title>Send to friend...</title>');
	w.document.write('<script language="Javascript">');
	w.document.write('function checkemail( str ) {var testresults;var filter=/^\\s*(\\w+(\\.\\w+)*@(\\w+\\.)+\\w{2,3})\\s*$/;if(filter.test(str)){testresults=true;} else {alert("Check E-mail address!");testresults=false;}return (testresults);}');
	w.document.write('function verifyForm( f ){var o;o = document.getElementById("EMail");if( o!=null && !checkemail( o.value ) ){o.focus();return false;}o = document.getElementById("from");if( o!=null && TrimString( o.value ) == "" ){o.value = orig;o.focus();return false;}return true;}');
	w.document.write('function TrimString(sInString){sInString = sInString.replace( /^\\s+/g, "" );return sInString.replace( /\\s+$/g, "" );}');
	w.document.write('</script>');

	w.document.write('<script language="Javascript">var orig="'+user+'";</script>');
	w.document.write('</head><body onload="document.getElementById('+"'"+"EMail"+"'"+').focus()">' );
	w.document.write('<table style="background-repeat:no-repeat;background-image:url(/Images/Pagestuff/BannerMain.jpg);"><tr><td><img src="/Images/PageStuff/null_dot.gif" width=565px height=92px></td></tr></table>');
	w.document.write('<form method="POST" action="/asp-bin/sendToFriend.asp" onsubmit="return verifyForm();">');
	w.document.write('<table cellspacing=5px>');
	w.document.write('<tr><td>To:</td><td><input id=EMail type="text" name="EMail" size="48"></td><td nowrap>Example: YourFriend@aol.com</td></tr>');
	w.document.write('<tr><td>From:</td><td colspan=2><input id=from type="text" name="from" style="background-color:white;color:black;width:100%" value="'+user+'"></td></tr>');
	w.document.write('<tr><td>Subject:</td><td colspan=2 style="border:solid black 1px"><input type="hidden" name="subject" value="AutoRacing1.com: '+subj+'">AutoRacing1.com: '+subj+'</td></tr>');
	w.document.write('<tr><td colspan=3>Add a message to your friend below:</td></tr>');
	w.document.write('<tr><td colspan=3><table style="border:solid black 1px"><tr><td><textarea id=txt1 rows="4" name="txt1" style="border:0px;width:100%"></textarea><br>');
	w.document.write( '<hr>'+txt );
	w.document.write('</td></tr></td></tr></table>');
	w.document.write('</table>');
	w.document.write('<input type="submit" value="Send" name="B1"></p>');
	w.document.write('<input type="hidden" name=url value="'+uri+'">');
	w.document.write('<input type="hidden" name=txt2 value="'+txt+'">');
	w.document.write('</form></body></html>');
	w.document.close();
	return false;
}

function reportFailure( uri, text )
{
	var w = window.open('','','width=580,height=500');
	w.document.write('<html><head><title>Failure...</title></head><body>');
	w.document.write('<table style="background-repeat:no-repeat;background-image:url(/Images/Pagestuff/BannerMain.jpg);">');
	w.document.write('<tr><td><img src="/Images/PageStuff/null_dot.gif" width="565px" height="92px"></td></tr></table>');
	w.document.write('<form method="POST" action="/asp-bin/sendToFriend.asp" style="text-align: center">');
	w.document.write('Unfortunately we are unable to complete your request at this time.<p align="center">We are sorry for the inconvenience.<br>');
	w.document.write('Please click on the button "Report" below to report this small incident to us.</p>');
	w.document.write('<p align="center">Thank you for your interest in AutoRacing1.com</p>');
	w.document.write('<p align="center"><input type="submit" id=Report value="Report" name="B1"></p>');
	w.document.write('<input type="hidden" name="EMail" value="mail@zdima.net">');
	w.document.write('<input type="hidden" name="url" value="'+uri+'"></p>');
	w.document.write('<input type="hidden" name="txt1" value="'+text+'">');
	w.document.write('<input type="hidden" name="txt2" value="">');
	w.document.write('<input type="hidden" name="subject" value="Send To Friend failure">');
	w.document.write('</form></body></html>');
	w.document.close();
	w.document.getElementById("Report").focus();
	return false;
}
