//###################################
// toggle the visibility of a layer
//###################################
function ToggleLayer(layerID)
{
    layer = $(layerID);

    if( layer.style.display == 'none' || layer.style.display == '' )
        layer.style.display = 'block';
    else
        layer.style.display = 'none';
}

//###################################
// used to auto-tab through the phone number fields
//###################################
var phone_field_length=0;
function TabNext( obj, event, len, _next_field )
{
	var ret = false;
	var num = NumbersOnly(obj);
	next_field = $(_next_field);
	
	if (num)
	{
		if (event == "down")
			phone_field_length = obj.value.length;
		else if (event == "up")
		{
			if (obj.value.length != phone_field_length)
			{
				phone_field_length=obj.value.length;
				if (phone_field_length == len)
				{
					ret = true;
					next_field.focus();
				}
			}
		}
	}
	
	return ret;
}

//###################################
// used to restrict text field user input to alpha chars only
//###################################
function AlphaOnly(f)
{
    var re = /^[a-zA-Z]*$/;
    if (!re.test(f.value))
    {
        f.value = f.value.replace(/[^a-zA-Z]/g,"");
        return false;
    }
    
    return true;
}

//###################################
// used to restrict text field user input to numeric chars only
//###################################
function NumbersOnly(f)
{
    var re = /^[0-9]*$/;
    if (!re.test(f.value))
    {
        f.value = f.value.replace(/[^0-9]/g,"");
        return false;
    }
    
    return true;
} 

//###################################
// used to restrict text field user input to numeric chars only
//###################################
function CurrencyOnly(f)
{
    var re = /^\$?\-?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}\d*(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;
    if (!re.test(f.value))
        return false;
    
    return true;
} 

//###################################
// open up a centered pop-up window
//###################################
function popWin(windowURL, windowWidth, windowHeight)
{
    var _windowLeft = (screen.width - windowWidth) / 2;
    var _windowTop = (screen.height - windowHeight) / 2;
    var windowProperties = 'height=' + windowHeight + ',width=' + windowWidth + ',top=' + _windowTop + ',left=' + _windowLeft + ',scrollbars=0,resizable=1,menubar=0,toolbar=0,location=0,fullscreen=0';
    var obj_window = window.open(windowURL, '', windowProperties)
    
    // if possible, focus the window
    if (parseInt(navigator.appVersion) >= 4)
    {
        obj_window.window.focus();
    }
}

//################################
// IMAGE POP-UP
//################################

// Set the horizontal and vertical position for the popup
PositionX = (screen.width) ? (screen.width-150)/2 : 0;
PositionY = (screen.height) ? (screen.height-150)/2 : 0;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)
defaultWidth = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var autoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4)
{
	var isNN=(navigator.appName=="Netscape")?1:0;
	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;

function popImage(imageURL)
{
	var imgWin;

	if (isIE)
	{
        if( imageURL.indexOf(".zip") != -1 )
		    imgWin=window.open(imageURL,'',optIE);

		imgWin=window.open('about:blank','',optIE);
	}
	else
	{
        if( imageURL.indexOf(".zip") != -1 )
		    imgWin=window.open(imageURL,'',optNN);

		imgWin=window.open('about:blank','',optNN);
	}

    if( imageURL.indexOf(".zip") == -1 )
    {
	    with (imgWin.document)
	    {
		    writeln('<html><head><title>Landtec Image</title><style>body{margin:0;}</style>');
		    writeln('<sc'+'ript>');
		    writeln('var isNN,isIE;');
		    writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		    writeln('isNN=(navigator.appName=="Netscape")?1:0;');
		    writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		    writeln('function resizeToimage(){');
		    writeln('if (isIE){');
		    writeln('window.resizeTo(100,100);');
		    writeln('width = 100-(document.body.clientWidth-document.images[0].width);');
		    writeln('height = 100-(document.body.clientHeight-document.images[0].height);');
		    writeln('window.resizeTo(width,height);}');
		    writeln('if (isNN){');
		    writeln('window.innerWidth = document.images["smf"].width;');
		    writeln('window.innerHeight = document.images["smf"].height;}');
		    writeln('}');
		    writeln('</sc'+'ript>');
		    if (!autoClose) writeln('</head><body bgcolor=FFFFFF scroll="no" onload="resizeToimage();self.focus()">')
		    else writeln('</head><body bgcolor=FFFFFF scroll="no" onload="resizeToimage();self.focus()" onblur="self.close()">');
		    writeln('<a href="javascript:self.close()"><img id="smf" name="smf" src="'+imageURL+'" style="display:block" border="0"/></a></body></html>');
		    close();
	    }
    }
} 

//###################################
// FORM FIELD UTILITIES
//###################################
function checkPhone (strng)
{
    var error = "";
    if (strng == "")
        error = "Please enter a phone number.\n";

    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters and whitespace
    if (isNaN(parseInt(stripped)))
        error = "The phone number contains illegal characters.";

    if (!(stripped.length == 10))
        error = "The phone number is the wrong length. Make sure you included an area code.\n";

    if( error != "" )
    {
        alert( error );
        return false;
    }
    else
        return true;
}

function checkPassword(val)
{
    var error = "";
    if (val == "")
        error = "You didn't enter a password.\n";

    var strng = val.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters and whitespace
    //alert( strng );

    var illegalChars = /[\W_]+/; // check for non-alphanumeric chars
    var alphaChars = /[a-zA-Z]+/; // check for letters and numbers
    var numericChars = /[0-9]+/; // check for letters and numbers
    if (strng.length < 7)
    {
        error = "Your password must be at least 7 characters long.\n";
    }
    else if (!illegalChars.test(strng))
    {
        error = "Your password must contain at least one non-alphanumeric character (!, @, _).\n";
    }
    else if (!alphaChars.test(strng))
    {
        error = "Your password must contain at least one letter.\n";
    }
    else if (!numericChars.test(strng))
    {
        error = "Your password must contain at least one number.\n";
    }
    /*
    else if (!((strng.search(/^[0-9a-zA-Z]+$/) > -1)))
        //&& (strng.search(/[A-Z]+/) > -1)
        //&& (strng.search(/[.!@#$*_-+]+/) > -1)
        //&& (strng.search(/[0-9]+/) > -1)))
    {
        error = "The password must be a combination of letters, numbers and characters.\n";
    }
    */
    
    if( error != "" )
    {
        alert( error );
        return false;
    }
    else
        return true;
}
  
function checkEmail(e, t)
{	
	if (e == "")
	{	alert("Please fill in your "+ t +".");
		return false; 
	}
	else
	{
		var emailStr = e;
			
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)<>@,;:\*\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="([^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + specialChars + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=emailStr.match(emailPat);
		if (matchArray==null)
		{	alert("Your "+ t +" seems incorrect. Please check @ and .'s!");
			return false;
		}
		
		var user=matchArray[1];
		var domain=matchArray[2];
		if (user.match(userPat)==null)
		{	alert("Please do not use any symbols (ie. quotes or asterisks) other than @ in your "+ t +".");
			return false;
		}
		
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null)
		{  for (var i=1;i<=4;i++)
			  {	if (IPArray[i]>255)
			  	{	alert("Destination IP address is invalid!");
					return false;
				}
			}
		}
		
		var domainArray=domain.match(domainPat)
		if (domainArray==null)
		{	alert("Your "+ t +" domain name doesn't seem to be valid.");
			return false;
		}			

		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
		{	alert("Your "+ t +" must end in a three-letter domain, or two letter country.");
			return false;
		} 

		var Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		if (domArr[domArr.length-1].length == 2 || domArr[domArr.length-1].length == 3)
		{	for (var i = 0; i < domArr[domArr.length-1].length; i++)
			{	if (Chars.indexOf(domArr[domArr.length-1].charAt(i)) == -1)
				{	alert("The domain name can only contain letters.");
					return false;
				}
			}
		}
		
		if (len<2 || len>3)
		{   alert("Your "+ t +" is missing a hostname!");
		   return false;
		}
	}
	return true;
}

function checkNums(n, t)
{	if (n == "")
	{	alert("Please enter a number in "+ t +".")
		return false;
	}
	else
	{	var Chars = "0123456789";
		for (var i = 0; i < n.length; i++)
		{	if (Chars.indexOf(n.charAt(i)) == -1)
			{	alert ("Please enter only numbers in "+ t +".")
				return false
			}
		}
	}
	
	return true;
}

function checkName(n, t)
{	if (n == "")
	{	alert("Please enter your "+ t +".")
		return false;
	}
	else
	{	var Chars = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
		for (var i = 0; i < n.length; i++)
		{	if (Chars.indexOf(n.charAt(i)) == -1)
			{	alert ("Please enter a valid "+ t +".")
				return false
			}
		}
	}
	
	return true;
}

function checkZip(z, t) {
	if (z == "")
	{	alert("Please fill in your "+ t +".");
		return false; 
	}
	else
	{	// check to see that the value contains at least 5 numbers
		if (z.length < 5)
		{	alert ("Please enter at least 5 numbers into the "+ t +" field.");
			return false;
		}
		// check to see that the value contains either 5 or 10 numbers
		if (z.length > 5 && z.length < 10)
		{	alert ("Please enter either your 5-digit "+ t +" OR your 10-digit "+ t +" including the 4 digit extension (12345-1234) into the "+ t +" field.");
			return false;
		}
		// check to see that the value contains only numbers and possibly a hyphen
		var nums = "0123456789-";
		for (var i = 0; i < z.length; i++)
		{	if (nums.indexOf(z.charAt(i)) == -1)
			{	alert ("Please enter only numbers into the "+ t +" field.");
				return false;
			}
		}
		// check to see if the 6th character is a hyphen
		if (z.length == 10)
		{	if (z.charAt(5) != "-")
			{	alert("Please make sure that your "+ t +" is properly formatted. (12345-1234)");
				return false;
			}
		}
	}
	return true;
}
