var AjaxObj = false; // make global variables
var objHTML = false
var SecretCode = 0;

function BuyNowInstructions() {
	Str  = '<p class="Paypal_Instructions">';
	Str += '	<b>Please Read!</b><br />';
	Str += '	You will receive an	order confirmation email with download instructions as soon as we receive your order from PayPal.';
	Str += '	If you do not receive the download instructions, please check the spam folder on your email program. </font></p>';
	Str += '</p>';

	Str += '<p class="Guarantee">';
	Str += '	<b>30 Day Satisfaction Guarantee:</b>&nbsp;<br />';
	Str += '	If you are not satisfied with your plans for any reason, you may email us within 30 days <br />';
	Str += '	of purchase. We will promptly refund your full purchase price. <br /><br />';

	Str += '	If you successfully placed your order, but did not receive <br />';
	Str += '	download instructions, please email us (with your phone number).<br />';
	Str += '	<a href="mailto:webmaster@bobsplans.com">webmaster@bobsplans.com</a></font>';
	Str += '</p>';
	document.write(Str);
 }

function ClearBkg(ElementName) {
	document.getElementById(ElementName).style.background="white";
}

function CodeStr(Str) {
  var RetStr = '';
  for (i=0; i<Str.length; i++) {
    RetStr +=  Str.charCodeAt(i) + '.' ;
//		if(!confirm('Ascii Chr: ' + Str.substr(i) + '\nUnicode: ' + Str.charCodeAt(i) + 'RetStr: ' + RetStr)) {break;}
  }
  return RetStr;
}

function ContactUs() { // Called from Send button onClick event
//alert('ContactUs()');
	var FormMsg = '';
	var Name 		= document.getElementById('tName').value;
	var Subject = document.getElementById('tSubject').value;
	var Email 	= document.getElementById('tEmail').value;
	var Email2 	= document.getElementById('tEmail2').value;
	var Message = document.getElementById('tMessage').value;
	var FormIsGood = true;
	var URL = 'Editors/MessageServer.php';
// Ensure each field is properly filled out and exit function after alerting user.
	if (Name == "") { document.getElementById('tName').style.background="#ff8072";
		FormMsg += 'Please enter a name.\n\n'; }
	if (Email == "" || Email2 == "") { document.getElementById('tEmail').style.background="#ff8072";document.getElementById('tEmail2').style.background="#ff8072";
		FormMsg += 'Email fields may not be empty. Please double check both email fields.\n\n'; }
	if(Email.indexOf('@')== -1) {
		FormMsg += 'Email Address must contain an "@".\n\n';}
	if(Email.indexOf('.')== -1) {
		FormMsg += 'Email Address must contain a domain name after a "." \n\n';}
	if (Email != Email2) { document.getElementById('tEmail').style.background="#ff8072";document.getElementById('tEmail2').style.background="#ff8072";
		FormMsg += 'Email fields do not match. Please double check both email fields.\n\n'; }

//	alert('No ampersand\n'+'Email.indexOf: '+Email.indexOf('@'));
	if (Subject == "") { document.getElementById('tSubject').style.background="#ff8072";
		FormMsg += 'Please enter a Subject.\n\n';}
	if (Message == "") { document.getElementById('tMessage').style.background="#ff8072";
		FormMsg += 'Please enter a Message.\n\n'; }
	if (FormMsg != '') { alert(FormMsg);
		return;}
	NameValuePairs  = 'name='    				+ Name ;
	NameValuePairs += '&subject=' 			+ Subject;
	NameValuePairs += '&email_address=' + Email;
	NameValuePairs += '&message=' 			+ Message;
	AjaxMsgObj = GetAjaxObj();
	if(!AjaxMsgObj)	{ alert('Could not create AjaxMsgObj.'); return; }
	AjaxMsgObj.onreadystatechange = function() {
		if (AjaxMsgObj.readyState == 4 && AjaxMsgObj.status == 200) {
			alert(AjaxMsgObj.responseText);
		}
	}
	AjaxMsgObj.open("POST", URL, true);
	AjaxMsgObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	AjaxMsgObj.setRequestHeader("Content-length", NameValuePairs.length);
	AjaxMsgObj.send(NameValuePairs);
}

function DownloadButton(PathOffset, PlanName, PdfFile){
// This function displays a download button for the PDF file
	sDownloadURL = PathOffset + 'FreeDownloads/' + PdfFile;
	sEvent = 'onclick=\'DownLoadPDF("'+PathOffset + '","' + sDownloadURL + '", "' + PdfFile + '")\' ';
	sStr  = '<div class="download">'
	sStr += '<p class="color_bold_center_italic">';
	sStr += 'Click the Download button for your free '+ PlanName +' plans.</p>';
	sStr += '<input type="button" name="download" value="Download"';
	sStr += sEvent  + '><br /><br />';
	sStr += 'When the plans open in your PDF viewer, be sure to save the file to your hard drive. ';
	sStr += '</div>	';
	document.write(sStr);
//	alert(sDownloadURL);
}

function divMouseDn(){
// This function changes the border to 0
	document.getElementById("divDnldBtn").style.border="1px solid #104377";
}

function divMouseUp(){
// This function changes the border to 0
	document.getElementById("divDnldBtn").style.border="3px outset #104377";

}

function DownloadInstructions(sPlanName) {

	sMsg  = '<p style="font-style: italic; color:red; font-weight:bold">';
	sMsg += 'Please Note: <br /><p>';
	sMsg += '<p style="font-style: italic;  color:red; margin: -15 0 0 0">';
	sMsg += 'These plans are provided as downloadable PDF files. ';
	sMsg += 'After your order has been completed by Paypal, you will receive instructions ';
	sMsg += 'for downloading your ' + sPlanName + ' plans';
	sMsg += '</p>';

document.getElementById('download_instructions').innerHTML = sMsg ;
}

function DownLoadPDF(PathOffset, DownloadURL, PdfFile) {
	// This function updates the PdfViews table, then implements downloading of the pdf file.
	sTrackingURL  = PathOffset + 'Admin/UpdatePageViews.php?PdfFile=' + PdfFile;
//	alert(sTrackingURL);
	// The following code sends the above URL to the php script which updates the PdfViews table.
	AjaxObj = GetAjaxObj();
	if(AjaxObj)	{
		AjaxObj.onreadystatechange = function()
			{
				if (AjaxObj.readyState == 4 && AjaxObj.status == 200) {
//		alert(AjaxObj.responseText);
				}
			}
		AjaxObj.open("GET", sTrackingURL, true);
		AjaxObj.send(null);
	}
	// The following line implements the download using the URL passed to the function the
	window.open(DownloadURL,"Download");
}

function DownloadPreview(PathOffset, PlanName, PdfFile){
// This function displays a download button for the PDF file
	sDownloadURL = PathOffset + 'FreeDownloads/' + PdfFile;
	sEvent = 'onclick=\'DownLoadPDF("'+PathOffset + '","' + sDownloadURL + '", "' + PdfFile + '")\' ';
	sStr  = '<div class="download">'
	sStr += '<p class="color_bold_center_italic">';
	sStr += 'Click the Download button for your free Preview.</p>';
	sStr += '<input type="button" name="download" value="Download"';
	sStr += sEvent  + '><br /><br />';
	sStr += 'When the plans open in your PDF viewer, be sure to save the file to your hard drive. ';
	sStr += '</div>	';
	document.write(sStr);
//	alert(sDownloadURL);
}

function Footer(Date, Filename) {
	var Year = Date.substring(6,19);
	var divFooter  = '<div style="margin-top:75px; text-align:center; font-size:10pt; color:#333333; " >'
			divFooter += '<p style="font-size: 8pt; color:#555555; " ><a href="#Top">Back to Top</a></p>';
			divFooter += '<p style="font-size: 8pt; color:#333333; " > 	Copyright &copy; ' + Year + ' by Robert E. Reedy</p>';
			divFooter += '<p style="font-size: 8pt; color:#555555; " >' + Filename + '&nbsp;&nbsp;-&nbsp;&nbsp;' + Date + '</p>';
			divFooter += '<br /><br />';

document.write(divFooter);

}


function GetAjaxObj (){
	var AjaxObj = false;
	if (window.XMLHttpRequest)
	{
		AjaxObj = new XMLHttpRequest();
	}
		else if (window.ActiveXObject)
		{
		AjaxObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	return AjaxObj;
}

function GetMessages(Period) {
//alert('GetMessages('+Period+')');
	var URL = 'MessageServer.php?period=' + Period;
	var ResponseText = '';
	AjaxMessagesObj = GetAjaxObj();
	if(AjaxMessagesObj)	{
		AjaxMessagesObj.onreadystatechange = function() {
			if (AjaxMessagesObj.readyState == 4 && AjaxMessagesObj.status == 200) {
				document.getElementById('divMessages').innerHTML = AjaxMessagesObj.responseText;
				document.getElementById('divSelectedMessage').innerHTML = '<p style="text-align:center;">No Message Selected</p>';
			}
		}
	}
	AjaxMessagesObj.open("POST", URL, true);
	AjaxMessagesObj.send(null);
}

function SelectMessage(RecNo) {
//alert('SelectMessage('+RecNo+')');
	var URL = 'MessageServer.php?RecNo=' + RecNo;
	AjaxSelectMessageObj = GetAjaxObj();
	if(AjaxSelectMessageObj)	{
		AjaxSelectMessageObj.onreadystatechange = function() {
			if (AjaxSelectMessageObj.readyState == 4 && AjaxSelectMessageObj.status == 200) {

//alert(URL + '\n' + AjaxSelectMessageObj.status+ '\n' + AjaxSelectMessageObj.readyState);
				ResponseText = AjaxSelectMessageObj.responseText;
				document.getElementById('divSelectedMessage').innerHTML = ResponseText;
			}
		}
	}
	AjaxSelectMessageObj.open("POST", URL, true);
	AjaxSelectMessageObj.send(null);
}




function GetReferringURL(){
	sStr = document.referrer.replace('http://','');
	if (sStr.indexOf('/')) {
		nOffset = sStr.indexOf('/');
		sStr = sStr.substring(0,nOffset);
	}
	return sStr;
}

function HeaderLeft(PathOffset) {
	var LeftImage   = '<img src="'+PathOffset+'Images/Pictures/RouterTable150x123.gif" alt="Router Table" ';
			LeftImage  += 'style="padding: 8px 10px; text-align:left;  width:110px;	height:90px; " />';
	document.write(LeftImage);
}

function HeaderRight(PathOffset) {
	var RightImage  = '<img src="'+PathOffset+'Images/Thumbnails/GWorkbench250x150.gif" alt="Workbench Plans" ';
			RightImage += 'style="padding: 3px 6px; text-align:right; width:150px; height:90px; " />';
	document.write(RightImage);
}

function LargeImage(imgFile) {
	document.getElementById('Div-LargeImage').style.display = 'inline';
	if (imgFile=='Hide') {
		document.getElementById('Div-LargeImage').style.display = 'none';
		return;
	}
}

function Nav_Bar(Offset) {
	var	span = '<td class="tdNav"><a class= "aNav" href="' + Offset;
	var NavHTML = '<div class= "divNav">';

	NavHTML += '<table class="tblNav" > <tr>';
	NavHTML += span + 'index.htm"> Home </a> </span></a>';
	NavHTML += span + 'FreePlans/FreePlans.htm"> Free Plans </span></a>';
	NavHTML += span + 'FreeJigPlans/FreeJigPlans.htm"> Free Jig Plans </span></a>';
	NavHTML += span + 'forum/index.php"> Forum </span></a>';
	NavHTML += span + 'Admin/ContactUs.htm"> Contact Us </span></a>';
	NavHTML += span + 'Admin/AboutUs.htm"> About Us </span></a>';
	NavHTML += span + 'TopSites/TopSites.htm"> Links </span></a>';
	NavHTML += '</tr> </table>';
	NavHTML += '</div>';
	document.write(NavHTML);

}

function PageTracker(PathOffset, pFilename) {
	sReferrer = GetReferringURL();
	sURL  = PathOffset + 'Admin/UpdatePageViews.php';
	sURL += '?page=' + pFilename;
	sURL += '&referrer=' + sReferrer;
//alert(sURL );
	AjaxObj = GetAjaxObj();
	if(AjaxObj)	{
		AjaxObj.onreadystatechange = function()
			{
				if (AjaxObj.readyState == 4 && AjaxObj.status == 200) {
//alert(AjaxObj.responseText);
				}
			}
		AjaxObj.open("GET", sURL, true);
		AjaxObj.send(null);
	}

}

function SetCode(element) {
//alert('SetCode; '+element);
	var Password = '';
	switch (element) {
		case 'body': SecretCode = 0;
			break;
		case 'Field': SecretCode = 0;
			break;
		case 'Contact Us': if(SecretCode==0) {SecretCode = 1;} else {SecretCode = 0;}
			break;
		case 'Message': if(SecretCode==1){SecretCode = 3;} else {SecretCode = 0;}
		break;
		case 'Name': if(SecretCode==3){SecretCode = 6;} else {SecretCode = 0;}
			break;
		default: SecretCode = 0;
			break;
	}
	if(SecretCode ==6 ) {
		window.location="Editors/Messages.htm";
	}

}

