// website.js

// ===================================================================
// Site functions.

// Returns a string that can be used to call a plug function, 
// <func>, in api dll, through website dll, with args <args>.
// ARG: func: string with name of the function.
// ARG: args: strin with list of args.
// RET: string with cgi url for the specified call.
function PlugCall(func,args)
{ return SiteCall("EPAC_DllCall",Arg("FUNC",func)+args); }

// Logs in a user into the website.
// ARG: usr: the user id.
// ARG: pwd: the password.
// ARG: type: the user type.
// RET: an url.
function Login(usr,pwd,type,forcelogin)
{ return SiteCall("EPAC_LoginUser",Arg("USER",usr)+Arg("PWD",pwd)+Arg("USERTYPELEVEL",type)+Arg("FORCELOGIN",forcelogin)); }

// Logs in an admin into the website.
// ARG: usr: the user id.
// ARG: pwd: the password.
// ARG: type: the user type.
// RET: an url.
function LoginAdm(usr,pwd,type,forcelogin)
{ return SiteCall("EPAC_LoginUser",Arg("USER",usr)+Arg("PWD",pwd)+Arg("USERTYPE","2")+Arg("USERTYPELEVEL",type)+Arg("FORCELOGIN",forcelogin)); }

// Logs out this user from the website.
// ARG: type: the user type.
// RET: an url.
function Logout()
{ return SiteCall("EPAC_Logout", ""); }

// ===================================================================
// Site functions.

// Returns a string that can be used to call an isapi function, 
// <func> in the website dll, with args <args>.
// ARG: siteFuncName: the name of the isapi function.
// ARG: args: string with list of arguments for the function.
// RET: an url.
function SiteCall(func,args)
{
	return "/elkedjan_site.dll?" + func + "&FORCE=" + 
		String(Math.random()).substr(2,4) + args;
}

// 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);
}

// 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 = escape(str);
	// These chars are not escaped by escape() and must be escaped manually. 
	// Theoretically only "+" should have to be escaped but now we're safe.
	cgi = StrReplace(cgi,"*","%2A");
	cgi = StrReplace(cgi,"+","%2B");
	cgi = StrReplace(cgi,"-","%2D");
	cgi = StrReplace(cgi,".","%2E");
	cgi = StrReplace(cgi,"/","%2F");
	cgi = StrReplace(cgi,"@","%40");
	cgi = StrReplace(cgi,"_","%5F");
	return cgi;
}

// 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)
{
	return str.replace(/oldstr/g,newstr);
}
