//*********************************************************************************
// Open a popup dialog style window, having no menubar or toolbar
//*********************************************************************************
function popupWindow( url, x, y, cx, cy) {
   window.open(url,null,"top="+y+",left=" + x + ",height=" + cy + ",width=" + cx +",status=no,toolbar=no,menubar=no,location=no");
}
//** formatZIPCode ****************************************************************
// Autoformat a ZIP/Postal code field
//*********************************************************************************
function New_Window(page) {
    OpenWin = window.open(page, "CtrlWindow", "toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=yes, width=200, height=100");
}

function formatZIPCode( field )
{
    var i, nd, out, c;
    var out = ""
    var nd = 0
    var ndigits = 0;
    var nalpha = 0;
    
    var str = field.value;
    str = str.toUpperCase();
    nd = str.length;
    
    // Count digits and letters
    for(i = 0; i < nd; i++)
     {
        c = str.charAt(i);
        if( c >= "0" && c <= "9")  
          ndigits++;
        else if( c >= "A" && c <= "Z") 
          nalpha++;
        else  if( c != "-" && c != " ") {
               alert("Invalid ZIP/Postal Code:\n" + str );
               field.focus();
               field.select();
               return;
           }
     }
    
    if( (ndigits != 3 && nalpha != 0) || (nalpha != 0 && nalpha != 3) )
    {
     alert("Invalid ZIP/Postal code\n");
     field.focus();
     field.select();
     return;
    }
    
    if( ndigits > 5 && ndigits != 9 )
    {
     alert("Invalid ZIP/Postal code.\n");
     field.focus();
     field.select();
     return;
    }
    
    var nc = 0;
    for(i = 0; i < nd; i++)
     {
     c = str.charAt(i);
     if( !((c >= "0" && c <= "9") || (c >="A" && c <="Z")) ) continue;
     out = out+c;
     nc++;
     if( nc == 3 && nalpha || nc == 5 && ndigits > 5)
    	{
    	out = out + "-";
    	nc++;
    	}
     }
     field.value = out;

}

//*** formatPhoneNumber  ***********************************************************
// Put a phone number into standard format. This routine
// allows phone number field to be empty, or it must
// contain exactly 10 digits. Allowed characters are
// 0,1,..9,-(). Non-digits are removed and the result
// is formatted as "nnn-nnn-nnnn"
// call formatPhoneNumber(elementReference) or
// formatPhoneNumber(elementReference, defaultAreaCode)
//*********************************************************************************
function formatPhoneNumber( field )
{
    var i, nd, out, c, digits;
    var argv = formatPhoneNumber.arguments;
    var argc = argv.length;
    var areaCode = null;
    
    if(argc > 1 )
    {
    	areaCode = argv[1];
    }
    
    out = "";
    nd = 0;
    ndigits = 0;
    digits = "";
    
    if( field.value.length == 0 )
        return;
        
    for(i = 0; i < field.value.length; i++)
     {
     c = field.value.charAt(i);
     if( c >= "0" && c <= "9")
     {
     	digits += c;
    	ndigits++;
     }
     else if( c != '(' && c != ')' && c != '-' && c != ' ' && "ext".indexOf( c ) < 0)
    	{
    	 alert("Invalid phone number format\nMust be 10 digits\nExample: 519-555-1234");
    	 field.focus();
    	 field.select();
    	 return false;
    	}
     }
    
    if( ndigits == 7 && areaCode != null )
    	{
    	digits = areaCode + digits;
    	ndigits += 3;
    	}
    
    if( digits.length < 10)
    {
     alert("Invalid phone number format\nMust be 10 digits\nExample: 519-555-1234");
     field.focus();
     field.select();
     return false;
    }
    
    out = "(" + digits.substring(0,3) + ") " + digits.substring(3,6) + "-" + digits.substring(6,10);
    if( digits.length > 10 )
    	out += " x" + digits.substring(10, digits.length);
     field.value = out;
     return true;
}

function checkInputFields( obj ) {
	if ( obj.value.indexOf( "\"" ) != -1 ) {
		alert( "You cannot enter quotes in text boxes, they will not be displayed properly." );
		obj.focus();	
		obj.select();
	}
	return;
}


/**
* If text box is empty, sets value to strValue parameter.
* Selects the text for editing.
*/
function onFocusInit( elem, strValue )
{
	if( elem.value == "" )
		elem.value = strValue;
	elem.select();
	return;
}

/**
* If text box is empty, sets value to strValue parameter.
*/
function onBlurInit( elem, strValue )
{
	if( elem.value == "" )
		elem.value = strValue;
	return;

}

// String format for assigning to document.cookie is
// name=value[; expires=GMTString][; path=pathVal][; domain=domainName][; secure]
function SetCookie(name, value)
{
var argv = SetCookie.arguments;
var argc = argv.length;

expires = null;
if( argc > 2)
	expires = argv[2]

str = name + "=" + escape(value)

if( expires != null )
	str += "; expires=" + expires;

str += "; path=/"
document.cookie = str;
}

function GetCookie( name )
{
	var cookies = document.cookie;
	i = cookies.indexOf(name+"=")
	if( i < 0 )
		return null;

	offset = i+name.length+1
	j = cookies.indexOf(";", offset);
	if( j < 0 )
		j = cookies.length;
	return unescape( cookies.substring(offset, j) );
}

function DeleteCookie( name )
{
	var exp = new Date();
	exp.setTime( exp.getTime()-1);
	var cookie = GetCookie( name );
	if( cookie != null )
	{
		SetCookie( name, cookie, exp.toGMTString() )
	}
}
