﻿// website.js
// Copyright © 2003-2004, Intellecta Systems AB.
// Encoded as UTF-8.


// ===================================================================
// Call functions.

// -------------------------------------------------------------------
// Returns a string that can be used as a url.
// ARG: page - the path of the page.
// ARG: func [opt] - the name of the function.
// ARG: args - string with list of arguments for the function.
// RET: an url.
// -------------------------------------------------------------------
function UrlCall(page, func, args)
{
	// If func is left out args will be func ..
	if( args==null || typeof(args)=="undefined" )
	{
		args = func; // .. so move to args.
		func = null;
	}

	var hasArgs = args!=null && typeof(args)!="undefined";
	var hasFunc = func!=null && typeof(func)!="undefined";
	var url = page;
	
	if( hasFunc )
		url += "?" + StrToCgi(func);
	else if( hasArgs )
		url += "?x";
	
	if( hasArgs )
		return url + args;
	else
		return url;
}

// ===================================================================
// Arg functions.

// -------------------------------------------------------------------
// Returns an cgi arg string.
// ARG: argName - the name of the cgi argument. If <argName> is of the
//  type Number then the argument name is PARAMn where n is the number.
// ARG: argValue - the value of the argument.
// RET: string with a cgi argument.
// -------------------------------------------------------------------
function Arg(argName,argValue)
{
	if( typeof(argName)=="number" )
		return "&PARAM" + String(argName) + "=" + StrToCgi(argValue);
	else
		return "&" + StrToCgi(argName) + "=" + StrToCgi(argValue);
}

// -------------------------------------------------------------------
// Returns an cgi arg named FORCE with a random value.
// -------------------------------------------------------------------
function ForceArg()
{
	return Arg("FORCE", String(Math.random()).substr(2,4));
}

// -------------------------------------------------------------------
// Escapes a javascript string to a cgi encoded string.
// ARG: str - the javascript string.
// RET: string with cgi encoded version of <str>.
// -------------------------------------------------------------------
function StrToCgi(str)
{
	var cgi = "";
	for( var i=0; i<str.length; i++ )
	{
		var unicode = str.charCodeAt(i);
		if( unicode==0x20 )
		{
			cgi += "+"; // Space.
		}
		else if( unicode>127 )
		{
			cgi += "%u"+StrPadBeg(StrHex(unicode),"0",4);
		}
		else if( unicode<0x30 || unicode==0x7E || unicode==0x60
			|| (unicode>=0x3A && unicode<=0x40) )
		{
			cgi += "%"+StrPadBeg(StrHex(unicode),"0",2);
		}
		else
			cgi += str.charAt(i);
	}
	
	return cgi;
}

// -------------------------------------------------------------------
// Pads a string at the beginning until it reaches a certain length.
// ARG: str - string to pad.
// ARG: pad - string to add at the beginning.
// ARG: padLen - the length of the final string.
// RET: the padded string.
// -------------------------------------------------------------------
function StrPadBeg(str, pad, padLen)
{
	while( str.length<padLen )
	{
		str = pad + str;
		if( str.length>padLen )
			str = str.substr(str.length-padLen, padLen);
	}
	return str;
}

// -------------------------------------------------------------------
// Converts a integer number into a hex string.
// ARG: d - the integer number.
// RET: hex string.
// -------------------------------------------------------------------
function StrHex(d)
{
	var str = 
		StrHexByte((d>>24))+
		StrHexByte((d>>16)&255)+
		StrHexByte((d>>8)&255)+
		StrHexByte(d&255);
		
	while( str.length>0 && str.charAt(0)=='0' )
		str = str.substr(1, str.length-1);
	
	return str;
}

// -------------------------------------------------------------------
// Gets the nibble hex string of a integer number.
// ARG: d - integer number in range [0,15].
// RET: hex nibble string. ["0","F"].
// -------------------------------------------------------------------
function StrHexNibble(d) 
{
  if( d<10 )  
		return String(d); 
	else 
		return String.fromCharCode(65+d-10);
}

// -------------------------------------------------------------------
// Gets the byte hex string of a integer number.
// ARG: d - integer number in range [0,255].
// RET: hex nibble string. ["00","FF"].
// -------------------------------------------------------------------
function StrHexByte(d) 
{
	return StrHexNibble(d>>4)+StrHexNibble(d&15);
}

// -------------------------------------------------------------------
// Replaces the occurance of a substring in a string
// with a new substring.
// ARG: str - the string.
// ARG: oldstr - old sub string.
// ARG: newstr - new sub string.
// RET: new string.
// -------------------------------------------------------------------
function StrReplace(str, oldstr, newstr)
{
	if( str==null || oldstr==null || oldstr.length==0 )
		return str;
	if( newstr==null )
		newstr = '';

	var ret = '';
	var pos = 0;
	while( pos<str.length )
	{
		var next = str.indexOf(oldstr, pos);
		if( next==-1 )
		{
			ret += str.substr(pos);
			break;
		}
		else
		{
			var len = next-pos;
			if( len>0 )
				ret += str.substr(pos, len);

			ret += newstr;
			pos = next+oldstr.length;
		}
	}
		
	return ret;
}


// ===================================================================
// Time in/out functions.

// -------------------------------------------------------------------
// Used to timeout function calls for a period
// so users cant call them to often.
// ARG: expr - expression that must evaluate to a global variable.
// ARG: delay - timeout duration in ms.
// -------------------------------------------------------------------
function TimeOut(expr,delay)
{
	eval(expr+" = true;");
	setTimeout("TimeIn('"+expr+"')",delay);
}

// -------------------------------------------------------------------
// Call this function to "time in" whats been timed out
// with TimeOut().
// ARG: expr - expression that must evaluate to a global variable.
// -------------------------------------------------------------------
function TimeIn(expr)
{
	eval(expr+" = false;");
}


// ===================================================================
// Page functions.

// -------------------------------------------------------------------
// Checks if a page has the name <name>. Use together with the 
//  PageNameJS propery in /Code/EjendalsPage.
// ARG: frame - browser frame to check in.
// ARG: name - name to check for in the loaded document.
// RET: true if document in frame has the name <name>.
// -------------------------------------------------------------------
function PageHasName(frame, name)
{
	if( frame.GetPageName && frame.GetPageName()==name )
		return true;
	else
		return false;
}


