// This fuction sets the page to reload inUrl after inTime seconds
//top.gDebugWindow.document.writeln('Default [' + window.name + '] gLoadUrlMutex=0' );
var gLoadedDate = new Date();


function SetReload( inTime, inURL )
{
	var fnCall = "Reload('" + inURL + "')";
	setTimeout( fnCall, inTime * 1000 );
}

function LoadURLInNewWindow( inURL, urlName,inProperties )
{
	var properties = 'scrollbars=yes,toolbar=no,height=450,width=800,location=no,menubar=no,resizable=yes,status=yes';
	
	if (inProperties != null)
	{
		properties = properties + "," + inProperties;
	}
	var win = window.open(inURL, urlName, properties);
	win.focus();
	
	return win;
}


function Reload( inURL )
{
	// IF the initial URL has changed
	// THEN 
	//		someone else has already set the url
	//		so ignore the reload in this case.
	//top.gDebugWindow.document.writeln('Reload(' + window.name + ',' + inURL + ',' + gLoadedDate );
	if (inURL == null || inURL == "")
	{
		window.location.reload(true);	
	}
	else
	{
		window.location = inURL;	
	}
}

function WindowClose()
{
	top.close();
}

function LoadTopUrl( inUrl )
{
	//top.gDebugWindow.document.writeln('LoadTopUrl(' + inUrl + ')');
	top.location = inUrl;
}

function CloseWindow( inUrl )
{
	if (window.opener == null)
	{
		LoadTopUrl( inUrl );
	}
	else
	{
		window.close();
	}	
}

function LoadUrl( inUrl )
{
	//top.gDebugWindow.document.writeln('LoadUrl(' + inUrl + ')');
	self.location = inUrl;
}


function LoadUrlInFrame( inFrame, inUrl )
{
	//top.gDebugWindow.document.writeln('LoadUrlInFrame('+ inFrame.name + ', ' + inUrl + ')');
	inFrame.location = inUrl;
}
	
// This function requires the thin activeX component.
function WebArrowQuit(inThinActiveX)
{
	if (inThinActiveX != null && inThinActiveX.IsRunning == true)
	{
		inThinActiveX.SetCommandLine("-s /q");
		inThinActiveX.Stop();
	}
}

	 
// Used for returning a status code from the service string
function GetServiceCode(inServiceStr,inService)
{
	var index = inServiceStr.indexOf(inService);
	if (index >= 0)
	{
		index++;
		var retCode  = parseInt(inServiceStr.substr( index,2 ));
		
		if ( retCode.toString() == "NaN" )
		{
			return 0;
		}
		
		return retCode;		
	}
	
	return 0;
}

function IsWebArrowConnected(inServiceStr)
{
	if ( GetServiceCode(inServiceStr,'c') == 1 ||
	     GetServiceCode(inServiceStr,'v') == 1 ||	 
	     GetServiceCode(inServiceStr,'p') == 1 ||	 
	     GetServiceCode(inServiceStr,'d') == 1 ||
	     GetServiceCode(inServiceStr,'f') == 1 ||
	     GetServiceCode(inServiceStr,'s') == 1 )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function IsWebArrowTerminated(inStatus)
{
    if( inStatus == '' )
    {
		return true;
	}
	else
	{
		return false;
	}
}

// Create a cookie with the specified name and value.
// The cookie expires at the end of the 20th century.
function SetCookie(sName, sValue)
{
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + ";";
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}
// Delete the cookie with the specified name.
function DelCookie(sName)
{
  var sDate = new Date(1970,1,1);
  document.cookie = sName + "=; expires=" + sDate.toGMTString() + ";";
}

/////////////////////////////////////////
// GENERAL ROUTINES FOR USE WITH FORMS
////////////////////////////////////////
function ResetForm(inForm)
{

	for (var i=0, j=inForm.elements.length; i<j; i++) 
	{    
		myType = inForm.elements[i].type;
		
		if (myType == 'checkbox')
		{
			 inForm.elements[i].checked = inForm.elements[i].defaultChecked;         		
		}
		else if (myType == 'text' || myType == 'textarea')
		{        
		 	inForm.elements[i].value = inForm.elements[i].defaultValue;            	
		}
	}
	
}

function SelectBoxSet( inSelectObj, inValue )
{
	if ( inSelectObj == null || inSelectObj.type != "select-one" )
		return;
	
	for (i=0;i<inSelectObj.options.length;i++)
	{    	
		if (inSelectObj.options[i].value == inValue)
		{
			inSelectObj.options[i].selected = true;
			return;
		}
	}		
}

function IsNumber( inStr, inMin, inMax )
{
	//-- Not an object
	if ( inStr == null )
		return 0;
		
	//-- Not a number
	if ( isNaN(inStr) )
		return 0;
	
	var val = parseInt(inStr);
	
	if (inMin != null && !isNaN(inMin) )
	{
		var min = parseInt(inMin);
		
		//-- number < min
		if ( val < min )
			return 0;
	} 
	
	if (inMax != null && !isNaN(inMax) )
	{
		var max = parseInt(inMax);
		
		//-- number < min
		if ( val > max )
			return 0;
	} 
	
	return 1;
}

function IsDigit( inChar )
{
	if ( inChar >= '0' && inChar <= '9' )
	{
		return true;
	}
	
	return false;
}

function IsLetterOrNumber( inChar )
{
	if ( IsDigit(inChar) || IsLetter(inChar))
	{
		return true;
	}
	
	return false;
}

function IsLetter( inChar )
{
	if ( (inChar >= 'A' && inChar <= 'Z')  ||
	     (inChar >= 'a' && inChar <= 'z') )
	{
		return true;
	}
	
	return false;
}

// Added as fix for Bug 1676 - 19 June 2003 [JMR]
function IsValidDomainName( inText )
{
	var i = 0;
	var state = 0; 
	for( i=0; i<inText.length; i++)
	{	
		var character = inText.charAt(i);
		
		// Domain Name consists of <subdomain> | ""
		// where <subdomain> 	= <label> | <subdomain> "." <label>
		//       <label>     	= <letter> [ [ <ldh-str> ] <let-dig> ]
		//       <ldh-str>   	= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
		//       <let-dig-hyp>	= <let-dig> | "-"
		//       <let-dig>   	= <letter> | <digit>

		// Start of <label>
		if ( state == 0 && IsLetterOrNumber(character) )
		{
			state = 1;
		}
		// In <label>
		else if ( (state == 1 || state == 2) && IsLetterOrNumber(character) )
		{
			state = 1;
		}
		// In <label> see '-'
		else if ( (state == 1 || state == 2) && character == '-' )
		{
			state = 2;
		}
		// In <label> but see '.'
		else if ( state == 1 && character == '.' )
		{
			state = 3;
		}
		else if ( state == 3 && IsLetterOrNumber(character) )
		{
			state = 1;
		}
		else
		{
   			return false;
		}
	}
	
	if( state == 1 )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function ValidateParticipant(inText)
{
	if (IsAlphanumeric(inText) || ValidateEmail(inText))
	{
		return true;
	}
	
	return false;
}
	
function ValidateUserid(inText)
{
	if (ValidateEmail(inText))
	{
		return true;
	}
	
	// Userid must be min 2 letters
	// Start and end with a character or number
	// and may contain any letter, number or '_-' in between 
	var pat = /^[A-Za-z0-9][A-Za-z0-9_-]*[A-Za-z0-9]$/
	
	return pat.test(inText);
}

function IsAlphanumeric(inText)
{
	var i = 0;
	for (i=0; i<inText.length; i++)
	{
		if ( !((inText.charAt(i) >= 'A' && inText.charAt(i) <= 'Z' ) ||
		       (inText.charAt(i) >= 'a' && inText.charAt(i) <= 'z' ) ||
		       (inText.charAt(i) >= '0' && inText.charAt(i) <= '9' ) ||
		       (inText.charAt(i) == '_' )) )
		{
			return false;
		}
	}
	
	return true;
}
// aug 15, 03 bug 1570 - Check for empty space. 
function TrimFormFields(inForm)
{
	frm=inForm;
	
	if (frm==null)
	{
		frm=document.Form1;
	}
	
	if (frm != null)
	{
		for (var i=0, j=frm.elements.length; i<j; i++) 
		{
			myType = frm.elements[i].type;
			
			if (myType == 'text')
			{        
				frm.elements[i].value = Trim(frm.elements[i].value);  
			}
		}
	}
}
/******************************************************************************* 
TRIM IN A VALUE AND A SELECTBOX AND SELECTS THE GIVEN VALUE FROM THE LIST
*******************************************************************************/
function Trim(strText) 
{ 
    // this will get rid of leading spaces 
    
    while (strText.substring(0,1) == ' ') 
    {
        strText = strText.substring(1, strText.length);
	}
    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
     {
        strText = strText.substring(0, strText.length-1);
        
     }
   return strText;
}

function AwayMessageCounter(field,maxlimit)
{	
	if (field.value.length > maxlimit) 
	{ 
		field.value = field.value.substring(0, maxlimit);			
	}	
}

function ValidateEmail(str)
{
    var pat1 = /^\s*(".*")\s*<\s*([A-Za-z0-9][\w-]*(\.[A-Za-z0-9][\w-]*)*@[A-Za-z0-9][\w-]*(\.[A-Za-z0-9][\w-]*)*)\s*>\s*$/;
    var res = pat1.test(str);

	if (!res)
	{
    	var pat2 = /^\s*([A-Za-z0-9][\w-]*(\.[A-Za-z0-9][\w-]*)*@[A-Za-z0-9][\w-]*(\.[A-Za-z0-9][\w-]*)*)\s*$/;
    	res = pat2.test(str);
	}
	
	if (!res)
	{
    	var pat3 = /^[^\[]*\[\s*([A-Za-z0-9][\w-]*(\.[A-Za-z0-9][\w-]*)*@[A-Za-z0-9][\w-]*(\.[A-Za-z0-9][\w-]*)*)\s*\]\s*$/;
    	res = pat3.test(str);
	}
	
    return res;
}

function ValidateAlph(theStr)
{
 	var i = 0; 
 	
	for( i=0; i<theStr.length; i++)
	{
		if ( (theStr.charAt(i) < '0' || theStr.charAt(i) > '9') && 
		     (theStr.charAt(i) < 'a' || theStr.charAt(i) > 'z') &&
		     (theStr.charAt(i) < 'A' || theStr.charAt(i) > 'Z') &&
		     (theStr.charAt(i) != '.' ) && (theStr.charAt(i) != '-' )
		     && (theStr.charAt(i) != '_' )
		      )
		{
			
			return false;
		}		
	}
	return true;
}

// replace an occurence of 'text' with 'by'

function Replace(string,text,by) 
{
// Replaces text with by in string
   
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) 
    {
    	return string;
    }

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength)))
    {
    	return string;
    }
    if (i == -1)
    {
    	return string;
    }

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
    {
        newstr += Replace(string.substring(i+txtLength,strLength),text,by);
    }
    return newstr;
}

function FindStyleObj(styleName) 
{
  for (i = 0; i < document.styleSheets.length; i++) 
  { 
    for (j = 0; j < document.styleSheets(i).rules.length; j++) 
    {
      if (document.styleSheets(i).rules(j).selectorText == styleName) 
      {
        return document.styleSheets(i).rules(j).style;
      }
    }     
  }
  
  return null;
}

function CreateActiveXObject(id)
{
  var error;
  var control = null;

  try
  {
    if (window.ActiveXObject)
    {
      control = new ActiveXObject(id);
    }
  }
  catch (error)
  {
    ;
  }
  return control;
}

function altHideAndShowObj(inId) 
{
	var obj = document.getElementById(inId);
	
	if (obj == null || obj.style == null)
	{
	    return;
	}
	
	if (obj.style.display == '') 
	{
		obj.style.display = 'none';
	} 
	else
	{
		obj.style.display = '';
	}
}
