// original code by Stuart Langridge 2003-11
// with additions to the code by other good people
// http://www.kryogenix.org/code/browser/nicetitle/
// thank you, sir

// modified by Peter Janes 2003-03-25
// http://peterjanes.ca/blog/archives/2003/03/25/nicetitles-for-ins-and-del
// added in ins and del tags

// modified by Dunstan Orchard 2003-11-18
// http://www.1976design.com/blog/
// added in accesskey information
// tried ever-so-hard, but couldn't work out how to do what Ethan did

// final genius touch by by Ethan Marcotte 2003-11-18
// http://www.sidesh0w.com/
// worked out how to delay showing the popups to make them more like the browser's own

// start everything off on window load
addEvent(window, 'load', makeNiceTitles);

// set the namespace
var XHTMLNS = 'http://www.w3.org/1999/xhtml';
var CURRENT_NICE_TITLE;

// browser sniff
var browser = new Browser();



// determine browser and version.
function Browser()
	{
	var ua, s, i;

	this.isIE = false;
	this.isNS = false;
	this.version = null;

	ua = navigator.userAgent;

	s = 'MSIE';
	if ((i = ua.indexOf(s)) >= 0)
		{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
		}

	s = 'Netscape6/';
	if ((i = ua.indexOf(s)) >= 0)
		{
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
		}

	// treat any other 'Gecko' browser as NS 6.1.
	s = 'Gecko';
	if ((i = ua.indexOf(s)) >= 0)
		{
		this.isNS = true;
		this.version = 6.1;
		return;
		}
	}



// 2003-11-19 sidesh0w
// set delay vars to emulate normal hover delay
var delay;
var interval = 0;



// this function runs on window load
// it runs through all the links on the page as starts listening for actions
function makeNiceTitles()
	{
	if (!document.createElement || !document.getElementsByTagName) return;
	if (!document.createElementNS)
		{
		document.createElementNS = function(ns, elt)
			{
			return document.createElement(elt);
			}
		}

	// do regular links
	if (!document.links)
		{
		document.links = document.getElementsByTagName('a');
		}
	for (var ti=0; ti<document.links.length; ti++)
		{
		var lnk = document.links[ti];
		if (lnk.title)
			{
			lnk.setAttribute('nicetitle', lnk.title);
			lnk.removeAttribute('title');
			addEvent(lnk, 'mouseover', showDelay);
			addEvent(lnk, 'mouseout', hideNiceTitle);
			addEvent(lnk, 'focus', showDelay);
			addEvent(lnk, 'blur', hideNiceTitle);
			}
		}

	// 2003-03-25 Peter Janes
	// do ins and del tags
	var tags = new Array(2);
	tags[0] = document.getElementsByTagName('ins');
	tags[1] = document.getElementsByTagName('del');
	for (var tt=0; tt<tags.length; tt++)
		{
		if (tags[tt])
			{
			for (var ti=0; ti<tags[tt].length; ti++)
				{
				var tag = tags[tt][ti];
				if (tag.dateTime)
					{
					var strDate = tag.dateTime;
					// HTML/ISO8601 date: yyyy-mm-ddThh:mm:ssTZD (Z, -hh:mm, +hh:mm)
					var month = strDate.substring(5,7);
					var day = strDate.substring(8,10);
					if (month[0] == '0')
						{
						month = month[1];
						}
					if (day[0] == '0')
						{
						day = day[1];
						}
					var dtIns = new Date(strDate.substring(0,4), month-1, day, strDate.substring(11,13), strDate.substring(14,16), strDate.substring(17,19));
					tag.setAttribute('nicetitle', (tt == 0 ? 'Added' : 'Deleted') + ' on ' + dtIns.toString());
					addEvent(tag, 'mouseover', showDelay);
					addEvent(tag, 'mouseout', hideNiceTitle);
					addEvent(tag, 'focus', showDelay);
					addEvent(tag, 'blur', hideNiceTitle);
					}
				}
			}
		}
	}



// by Scott Andrew
// add an eventlistener to browsers that can do it somehow.
function addEvent(obj, evType, fn)
	{
	if (obj.addEventListener)
		{
		obj.addEventListener(evType, fn, true);
		return true;
		}
	else if (obj.attachEvent)
		{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
		}
	else
		{
		return false;
		}
	}



function findPosition(oLink)
	{
	if (oLink.offsetParent)
		{
		for (var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent)
			{
			posX += oLink.offsetLeft;
			posY += oLink.offsetTop;
			}
		return [posX, posY];
		}
	else
		{
		return [oLink.x, oLink.y];
		}
	}



function getParent(el, pTagName)
	{
	if (el == null)
		{
		return null;
		}
	// gecko bug, supposed to be uppercase
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
		{
		return el;
		}
	else
		{
		return getParent(el.parentNode, pTagName);
		}
	}



// 2003-11-19 sidesh0w
// trailerpark wrapper function
function showDelay(e)
	{
    if (window.event && window.event.srcElement)
		{
        lnk = window.event.srcElement
		}
	else if (e && e.target)
		{
        lnk = e.target
		}
    if (!lnk) return;

	// lnk is a textnode or an elementnode that's not ins/del
    if (lnk.nodeType == 3 || (lnk.nodeType == 1 && lnk.tagName.toLowerCase() != 'ins' && lnk.tagName.toLowerCase() != 'del'))
		{
		// ascend parents until we hit a link
		lnk = getParent(lnk, 'a');
		}
	
	delay = setTimeout("showNiceTitle(lnk)", interval * 1000);
	}



// build and show the nice titles
function showNiceTitle(link)
	{
    if (CURRENT_NICE_TITLE) hideNiceTitle(CURRENT_NICE_TITLE);
    if (!document.getElementsByTagName) return;

    nicetitle = lnk.getAttribute('nicetitle');
    
    var d = document.createElementNS(XHTMLNS, 'div');
    d.className = 'nicetitle';
    tnt = document.createTextNode(nicetitle);
    pat = document.createElementNS(XHTMLNS, 'p');
    pat.className = 'titletext';
    pat.appendChild(tnt);

	// 2003-11-18 Dunstan Orchard
	// added in accesskey info
	if (lnk.accessKey)
		{
        axs = document.createTextNode(' [' + lnk.accessKey + ']');
		axsk = document.createElementNS(XHTMLNS, 'span');
        axsk.className = 'accesskey';
        axsk.appendChild(axs);
		pat.appendChild(axsk);
		}
    d.appendChild(pat);

    if (lnk.href)
		{
        tnd = document.createTextNode(lnk.href);
        pad = document.createElementNS(XHTMLNS, 'p');
        pad.className = 'destination';
        pad.appendChild(tnd);
        // d.appendChild(pad);
		}
    
    STD_WIDTH = 200;

	if (lnk.href)
		{
        h = lnk.href.length;
		}
	else
		{
		h = nicetitle.length;
		}
	
    if (nicetitle.length)
		{
		t = nicetitle.length;
		}
	
    h_pixels = h*6;
	t_pixels = t*10;
    
    if (h_pixels > STD_WIDTH)
		{
        w = STD_WIDTH;
		}
	else if ((STD_WIDTH>t_pixels) && (t_pixels>h_pixels))
		{
        w = t_pixels;
		}
	else if ((STD_WIDTH>t_pixels) && (h_pixels>t_pixels))
		{
        w = h_pixels;
		}
	else
		{
        w = STD_WIDTH;
		}
        
    d.style.width = w + 'px';    

    mpos = findPosition(lnk);
    mx = mpos[0];
    my = mpos[1];
    
    d.style.left = (mx+15) + 'px';
    d.style.top = (my+35) + 'px';

    if (window.innerWidth && ((mx+w) > window.innerWidth))
		{
        d.style.left = (window.innerWidth - w - 25) + 'px';
		}
    if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth))
		{
        d.style.left = (document.body.scrollWidth - w - 25) + 'px';
		}
    
    document.getElementsByTagName('body')[0].appendChild(d);

    CURRENT_NICE_TITLE = d;
	}




function hideNiceTitle(e)
	{
	// 2003-11-19 sidesh0w
	// clearTimeout 
	if (delay) clearTimeout(delay);
	if (!document.getElementsByTagName) return;
	if (CURRENT_NICE_TITLE)
		{
		document.getElementsByTagName('body')[0].removeChild(CURRENT_NICE_TITLE);
		CURRENT_NICE_TITLE = null;
		}
	}

function openTutorial(doc) {
  var html="/support/web-hosting-tutorials/"+doc+".htm"
  newWin=window.open(html,"pop_document","width=800,height=450,scrollbars=no")
}

function openDoc(doc) {
  var html="http://www.nexpoint.net/popup/"+doc+".html"
  newWin=window.open(html,"pop_document","width=400,height=400,scrollbars=no")
}

function newbm(url,title){
var newurl=url;
var newtitle=title;
if (document.all)
window.external.AddFavorite(newurl,newtitle);
}

function TestForm(form) { 
if ( !TestWhois(form) ) 
return false; 
return true; 
} 

function TestWhois(form) { 
str = document.form.domain.value;

if ( str.indexOf('www.', 0) != -1) 
{ 
alert("\nDo not enter the www.  when choosing a domain name.\n\n"); 
form.domain.value = ""; 
document.form.domain.focus(); 
return false; 
}

if ( str.indexOf('.', 0) == -1) { 
alert("\nYou MUST enter the domain extension.  Some examples are .com, .net, .org, .info, and .biz.\n\n"); 
document.form.domain.focus();
return false; 
}

if ( str == "" ) { 
alert("\nThe Domain Name field is blank.  Please enter a valid Top Level Domain Name.  An example would be: mydomain.com\n\n"); 
document.form.domain.focus(); 
return false; 
}

for ( var i = 0; i < str.length; i++ ) 
{ 
var ch = str.substring(i, i + 1); 

if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch < "0" || "9" < ch)) && ch != ('-') && ch != ('.')) {
alert("The domain has illegal characters! Use only letters and numbers or a '-'"); 
document.form.domain.focus();
return false; 
} 
} 
return true; 
}

function TestEmail(form) { 
if ( !TestEmailAddy(form) ) 
return false; 
return true; 
} 
function TestEmailAddy(form) { 
	str1 = document.form.cemail.value;
	str1a = document.form.cfname.value;
	str1g = document.form.clname.value;
	str1b = document.form.caddress.value;
	str1d = document.form.ccity.value;
	str1e = document.form.cstate.value;
	str1f = document.form.czip.value;
	str1c = document.form.cwork.value;
	if ( str1.indexOf('.', 0) == -1) 
		{ 
		alert("\nYou MUST enter a VALID E-Mail Address.\n\n"); 
		document.form.cemail.focus();
		return false; 
		}
	if ( str1.indexOf('@', 0) == -1) 
		{ 
		alert("\nYou MUST enter a VALID E-Mail Address.\n\n"); 
		document.form.cemail.focus();
		return false; 
		}
	if ( str1 == "" ) 
		{ 
		alert("\nYou MUST enter a VALID E-Mail Address.\n\n"); 
		document.form.cemail.focus(); 
		return false; 
		}
	if ( str1a == "" ) 
		{ 
		alert("\nYou MUST enter a Contact First Name.\n\n"); 
		document.form.cfname.focus(); 
		return false; 
		}
	if ( str1g == "" ) 
		{ 
		alert("\nYou MUST enter a Contact Last Name.\n\n"); 
		document.form.clname.focus(); 
		return false; 
		}
	if ( str1b == "" ) 
		{ 
		alert("\nYou MUST enter a Contact Address.\n\n"); 
		document.form.caddress.focus(); 
		return false; 
		}
	if ( str1d == "" ) 
		{ 
		alert("\nYou MUST enter a Contact City.\n\n"); 
		document.form.ccity.focus(); 
		return false; 
		}
	if ( str1e == "" ) 
		{ 
		alert("\nYou MUST enter a Contact State.\n\n"); 
		document.form.cstate.focus(); 
		return false; 
		}
	if ( str1f == "" ) 
		{ 
		alert("\nYou MUST enter a Contact Zip.\n\n"); 
		document.form.czip.focus(); 
		return false; 
		}
	if ( str1c == "" ) 
		{ 
		alert("\nYou MUST enter a Contact Work Phone.\n\n"); 
		document.form.cwork.focus(); 
		return false; 
		}
	return true; 
	}

function TestbEmail(form) { 
	if ( !TestbEmailAddy(form) ) 
	return false; 
	return true; 
} 

function TestbEmailAddy(form) { 
	str2 = document.form.bemail.value;
	str2a = document.form.bfname.value;
	str2d = document.form.blname.value;
	str2b = document.form.baddress.value;
	str2c = document.form.bwork.value;
	str2g = document.form.bcity.value;
	str2e = document.form.bstate.value;
	str2f = document.form.bzip.value;

	if ( str2.indexOf('.', 0) == -1) { 
		alert("\nYou MUST enter a VALID E-Mail Address.\n\n"); 
		document.form.bemail.focus();
		return false; 
		}
	if ( str2 == "" ) { 
		alert("\nYou MUST enter a VALID E-Mail Address.\n\n"); 
		document.form.bemail.focus(); 
		return false; 
		}
	if ( str2a == "" ) 
		{ 
		alert("\nYou MUST enter a Billing First Name.\n\n"); 
		document.form.bfname.focus(); 
		return false; 
		}
	if ( str2d == "" ) 
		{ 
		alert("\nYou MUST enter a Billing Last Name.\n\n"); 
		document.form.blname.focus(); 
		return false; 
		}
	if ( str2b == "" ) 
		{ 
		alert("\nYou MUST enter a Billing Address.\n\n"); 
		document.form.baddress.focus(); 
		return false; 
		}
	if ( str2c == "" ) 
		{ 
		alert("\nYou MUST enter a Billing Work Phone.\n\n"); 
		document.form.bwork.focus(); 
		return false; 
		}
	if ( str2g == "" ) 
		{ 
		alert("\nYou MUST enter a Billing City.\n\n"); 
		document.form.bcity.focus(); 
		return false; 
		}
	if ( str2e == "" ) 
		{ 
		alert("\nYou MUST enter a Billing State.\n\n"); 
		document.form.bstate.focus(); 
		return false; 
		}
	if ( str2f == "" ) 
		{ 
		alert("\nYou MUST enter a Billing Zip Code.\n\n"); 
		document.form.bzip.focus(); 
		return false; 
		}
	return true; 
	}
	
function disableForm(form) 
{

	if ( !TestCCNum(form) || !TestEmailAddy(form) || !TestbEmailAddy(form)) 
	return false; 
	 

	if (document.all || document.getElementById) {
		for (i = 0; i < form.length; i++) 
		{
		var tempobj = form.elements[i];
		if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
		tempobj.disabled = true;
		}
	return true;
	}
	else 
	{
		alert("Thank you for your order.   It may take a few minutes for your order to process.  Please do not resubmit your order this may incur additional charges");
		return true;
	}
	return true;
	
}

function TestCCNum(form) 
{ 
	str3 = document.form.bcc.value;
	str3a = document.form.bcardcode.value;
	str3b = document.form.bexpmonth.value;
	if ( str3.indexOf('-', 0) != -1) { 
		alert("\nPlease DO NOT Enter dashes (-) or spaces in your credit card number.\n\n"); 
		document.form.bcc.focus();
		return false; 
		}
	if ( str3.indexOf(' ', 0) != -1) { 
		alert("\nPlease DO NOT Enter dashes (-) or spaces in your credit card number.\n\n"); 
		document.form.bcc.focus();
		return false; 
		}
	if ( str3 == "" ) { 
		alert("\nPlease enter a credit card number.  Please DO NOT Enter dashes (-) or spaces in your credit card number.\n\n"); 
		document.form.bcc.focus(); 
		return false; 
		}
	if ( str3a == "" ) 
		{ 
		alert("\nYou MUST enter a card code.  If you credit card does not have a CVV/CVC Code please enter 000.\n\n"); 
		document.form.bcardcode.focus(); 
		return false; 
		}
	if ( str3b == "" ) 
		{ 
		alert("\nYou MUST enter a Expiration Month for your credit card.   Example:  02.\n\n"); 
		document.form.bexpmonth.focus(); 
		return false; 
		}
	return true; 
}