// WSW-specific code

function preloadImages() { //v2.0
  if (document.images) {
    var imgFiles = preloadImages.arguments;
    if (document.preloadArray==null) document.preloadArray = new Array();
    var i = document.preloadArray.length;
    with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){
      preloadArray[i] = new Image;
      preloadArray[i++].src = imgFiles[j];
  } }
}

function swapImgRestore() { //v2.0
  if (document.swapImgData != null)
    for (var i=0; i<(document.swapImgData.length-1); i+=2)
      document.swapImgData[i].src = document.swapImgData[i+1];
}

function swapImage() { //v2.0
  var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.swapImgData;
  for (i=0; i < (swapImage.arguments.length-2); i+=3) {
    objStr = swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
    if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
        (objStr.indexOf('document.all[')   ==0 && document.all   ==null))
      objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
    obj = eval(objStr);
    if (obj != null) {
      swapArray[j++] = obj;
      swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
      obj.src = swapImage.arguments[i+2];
  } }
  document.swapImgData = swapArray; //used for restore
}

// launch new small window from selection.
// u is start of url
// sel is select widget.
// selected option's value is added to u to get URL to launch
// w, h, are width and height of popup window
function        launch(uu, ss, ww, hh)
{
        if (ss) v = ss.options[ss.selectedIndex].value;
	else v = "";
//alert("launch|" + uu + "|" + v + "|" + ww + "|" + hh);
	uu = uu + escape(v);
	window.open(uu,"popup",
		"width=" + ww + "," +
		"height=" + hh + "," +
		"resizable=yes," +
		"location=no," +
		"toolbar=no," +
		"status=no," +
		"menubar=no");
return true;
}


function wswpopup(f, u, w, h)	{
	//	Wrapper to new 'stormnow' popup function replacing wswpopup code
    // launch new small window from selection.
    // f is the form
    // w, h, are width and height of popup window
	popup( u, h, w, f, '', false );
	return true;
}


function	load(url)
{
	window.location = url;
}

//	StormNow code
function popup( url, height, width, form, newAction, revert )	{
	var oldTarget;
	var win = window.open( url, 'popupWin', 'width='+width+',height='+height+',toolbar=no,resizable=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes');
	if( form )	{
		var oldAction = form.action;
		oldTarget = form.target;
		form.target = 'popupWin';
		form.action = newAction;
		form.submit();
		if( revert )	{
			form.action = oldAction;
			form.target = oldTarget;
		}
	}
	if( win )	{
		win.focus();
	}
	return;
}

function checkDate( month, day, year, min_date, max_date, check_future, check_boundary )																							{

	var i;
	var now = new Date;
	var this_date;

	if( !checkNumber( month ) )																									{
		return -1;
	}
	if( !checkNumber( day ) )																									{
		return -2;
	}
	if( !checkNumber( year ) )																									{
		return -3;
	}																

    now = new Date;
    this_date = new Date( year, month-1, day, 0, 0, 0 );

    // Verify Month

    if ( ( month < 1 ) || ( month > 12 ) )          																			{
        return -1;
    }
    else                                                    {

        // Verify Day

        switch( month )             {
            case "09":
            case "06":
            case "04":
            case "11":
                if( ( day < 1 ) || ( day > 30 ) )            {
                    return -2;
                }
                break;
            case "02":
                if( ( year % 400 == 0 ) || ( ( year % 100 != 0 ) && ( year % 4 == 0 ) ) )   {
                    max_day = 29;
                }
                else                                                        {
                    max_day = 28;
                }
                if( ( day < 1 ) || ( day > max_day ) )        {
                    return -2;
                }
                break;
            default:
                if( ( day < 1 ) || ( day > 31 ) )            {
                    return -2;
                }

        }

		if( check_future )																						{

        	//
    	    //  Verify Year
	        //

        	if ( this_date.getYear() < ( now.getYear() ) )  {
    	        return -3;
			}

 	       //  
            //  Make sure the date specified is in the future
            //

            if( this_date.getYear() == now.getYear() )    {

                if ( ( this_date.getMonth() < ( now.getMonth() + 1 ) ) || ( ( this_date.getMonth() == ( now.getMonth() + 1 ) ) && ( this_date.getDate() <= now.getDate() ) ) )  {
                        return -4;
                }
            }

		}

		//	Make sure that the date falls in between the minimum and maximum dates.

		if( check_boundary )																					{

			if( ( this_date < min_date ) || ( this_date > max_date ) )										{
				return -5;
			}

		}

    	return 0;

    }

}

function checkCurrency( value )																										{

	var i = 0;
	for( i = 0; i< value.length; i++ )																							{
		if( ( value.charAt(i) < "0" || value.charAt(i) > "9" ) && ( value.charAt(i) != "." ))								{
			return false;
		}
	}

	if( value.indexOf( "." ) != -1 )																							{
	
		//	Make sure there's only 2 digits on the right of the decimal point

		if ( ( value.length - 1 ) - value.indexOf( "." ) > 2 )																	{		
			return false
		}

	}

	if( value.length == 0 )																										{
		return false;
	}

	return true;
}

function checkNumber( value	)																										{
	var i = 0;
	if( value )	{
    	for( i = 0; i< value.length; i++ ){
    		if( ( value.charAt(i) < "0" || value.charAt(i) > "9" ))	{
    			return false;
    		}
    	}	
		return true;
	}
	else {
		return false;	
	}
}

function stripSpaces( value )																										{

    //  remove leading blanks from the value

	if( value ){

        while(  ( value.charAt(0) == " " ) ||
                ( value.charAt(0) == "\u000A" ) ||
                ( value.charAt(0) == "\u000D" ) ||
                ( value.charAt(0) == "\u000B" ) ||
                ( value.charAt(0) == "\u000C" ) ||
                ( value.charAt(0) == "\u0009" ) )          {

            value = value.substring( 1, value.length );

        }

        //  remove trailing blanks from the value

    	if( value.length > 1 )																										{

            while( ( value.charAt(value.length-1) == " " ) ||
                    ( value.charAt(value.length-1) == "\u000A" ) ||
                    ( value.charAt(value.length-1) == "\u000D" ) ||
                    ( value.charAt(value.length-1) == "\u000B" ) ||
                    ( value.charAt(value.length-1) == "\u000C" ) ||
                    ( value.charAt(value.length-1) == "\u0009" ) )                    {

                value = value.substring( 0, value.length-1 );

            }
    	}
	}
	return value;
}

function removeCharacters( value, removeString )	{

	var i;
	var newValue = "";
	if( value.length > 0 )	{
		for( i=0; i<value.length; i++ )	{
			if( removeString.indexOf( value.charAt(i) ) < 0 )	{
				newValue = newValue + value.charAt(i);
			}
		}
	}
	return newValue;
}
			

function upperCase( value )		{
	var newvalue = "";																									
	//	convert value to uppercase
	for( var i=0; i<value.length; i++ )																					{
		newvalue = newvalue + value.charAt(i).toUpperCase();
	}
	return newvalue;
}

function errorHandler( message, object )																					{

	//	
 	//	Pops up a JavaScript alert window that displays the error message
	//	and gives focus to the errant form object.
	//

	window.alert( message );
	object.focus();
	return true;
}

function checkEmail( email_address )																						{

    var i = 0;
    var temp_email_address = "";
    var good_characters = "@!#$%&\'*\+-.\/0123456789=\?ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{\|}~";

    //  Strip off any spaces
    
    for ( i=0; i < email_address.length; i++ )                  {

        if( email_address.charAt(i) != " " )                                {
            temp_email_address = temp_email_address + email_address.substring( i, i+1);
        }

    }

    //  Verify length and characters

    if ( temp_email_address.length > 5 )                                        {

        for( i=0; i < temp_email_address.length; i++ )            {

            if( good_characters.indexOf(temp_email_address.charAt(i)) == -1 )    {
                return false;
            }

        }
    }
    else                                                                        {

        return false;   

    }         

    //  Verify the form of the email address 

    if ( ( temp_email_address.indexOf("@") == -1 ) ||   
        ( temp_email_address.indexOf(".") == -1 ) )       {

        return false;

    }

    //  All checks passed, email address okay.
    return true;
}

function autosetTZ( form )	{
    temp_state=form.state.options[form.state.options.selectedIndex].value;    
    if (temp_state == '--') {}
    if (temp_state == 'AL') {tz=-5;dst=1}
    if (temp_state == 'AK') {tz=-9;dst=1}
    if (temp_state == 'AR') {tz=-6;dst=1}
    if (temp_state == 'AZ') {tz=-7;dst=0}
    if (temp_state == 'CA') {tz=-8;dst=1}
    if (temp_state == 'CO') {tz=-7;dst=1}
    if (temp_state == 'CT') {tz=-5;dst=1}
    if (temp_state == 'DE') {tz=-5;dst=1}
    if (temp_state == 'FL') {tz=-5;dst=1}
    if (temp_state == 'GA') {tz=-5;dst=1}
    if (temp_state == 'HI') {tz=-11;dst=0}
    if (temp_state == 'ID') {tz=-7;dst=1}
    if (temp_state == 'IL') {tz=-6;dst=1}
    if (temp_state == 'IN') {tz=-5;dst=1}
    if (temp_state == 'IA') {tz=-6;dst=1}
    if (temp_state == 'KS') {tz=-6;dst=1}
    if (temp_state == 'KY') {tz=-5;dst=1}
    if (temp_state == 'LA') {tz=-6;dst=1}
    if (temp_state == 'ME') {tz=-5;dst=1}
    if (temp_state == 'MD') {tz=-5;dst=1}
    if (temp_state == 'MA') {tz=-5;dst=1}
    if (temp_state == 'MI') {tz=-5;dst=1}
    if (temp_state == 'MN') {tz=-6;dst=1}
    if (temp_state == 'MS') {tz=-6;dst=1}
    if (temp_state == 'MO') {tz=-6;dst=1}
    if (temp_state == 'MT') {tz=-7;dst=1}
    if (temp_state == 'NE') {tz=-6;dst=1}
    if (temp_state == 'NV') {tz=-8;dst=1}
    if (temp_state == 'NH') {tz=-5;dst=1}
    if (temp_state == 'NJ') {tz=-5;dst=1}
    if (temp_state == 'NM') {tz=-7;dst=1}
    if (temp_state == 'NY') {tz=-5;dst=1}
    if (temp_state == 'NC') {tz=-5;dst=1}
    if (temp_state == 'ND') {tz=-6;dst=1}
    if (temp_state == 'OH') {tz=-5;dst=1}
    if (temp_state == 'OK') {tz=-6;dst=1}
    if (temp_state == 'OR') {tz=-8;dst=1}
    if (temp_state == 'PA') {tz=-5;dst=1}
    if (temp_state == 'RI') {tz=-5;dst=1}
    if (temp_state == 'SC') {tz=-5;dst=1}
    if (temp_state == 'SD') {tz=-6;dst=1}
    if (temp_state == 'TN') {tz=-6;dst=1}
    if (temp_state == 'TX') {tz=-6;dst=1}
    if (temp_state == 'UT') {tz=-7;dst=1}
    if (temp_state == 'VT') {tz=-5;dst=1}
    if (temp_state == 'VA') {tz=-5;dst=1}
    if (temp_state == 'WA') {tz=-8;dst=1}
    if (temp_state == 'WV') {tz=-5;dst=1}
    if (temp_state == 'WI') {tz=-6;dst=1}
    if (temp_state == 'WY') {tz=-7;dst=1}
    if (temp_state == 'NU') {}
    
    for (x=0;x<=form.timezone.options.length-1;x++)	{
        if (form.timezone.options[x].value == tz)	{
           form.timezone.options[x].selected=1;
        }
    }
        
    for (x=0;x<=form.daylight.options.length-1;x++)	{
        if (form.daylight.options[x].value == dst)	{
            form.daylight.options[x].selected=1;
        }
    }
}

function selectAllCheckboxes( object )	{
	if( object.length )	{
    	for( var i=0; i<object.length; i++ ){
    		object[i].checked = true;
    	}
	}
	else {
		object.checked = true;
	}
	return;
}	

function deselectAllCheckboxes( object )	{
	if( object.length )	{
    	for( var i=0; i<object.length; i++ ){
    		object[i].checked = false;
    	}
	}
	else {
		object.checked = false;
	}
	return;
}	

function validServerAddress( server )	{
	if( ( server.indexOf( '.' ) < 0 ) )	{
		return false;
	}
	server = removeCharacters( stripSpaces( server ), '.' );
	if( ( server.length == 0 ) )	{
		return false;
	}
	//	if server consists of all numbers, verify the value is valid
	if( checkNumber( server ) )	{
		if( ( server + 0 == 0 ) || ( server == '255255255255' ) || ( server.length > 12 ) )	{
			return false;
		}
		else	{
			return true;
		}
	}
	return true;
}

function checkHexColor( color )	{
	var validHexValues = "ABCDEF0123456789abcdef";
	if( color.length < 7 )	{
		return false;
	}
	if( color.charAt(0) != "#" ) {
		return false;
	}
	for( var i=1; i<color.length; i++ )	{
		if( validHexValues.indexOf( color.charAt(i) ) < 0 )	{
			return false;
		}
	}
	return true;
}

function checkURL( url, checkLength ) {
	if( checkLength )	{
	    if( ( !url ) || ( url.length == 0 ) )	{ 
    		return false;
    	}
	}
    if( url.substring( 0, 7 ) != 'http://' )	{
    	return false;
    }	
	return true
}