// ARG Class ===================================================================
/**
* CArg class -- argument of the URL
*    parameters:
*      name      -- name of the arg
*      value     -- value of the arg
*      def_value -- default value of the arg
*
* @param string name name of the arg
* @param def_value default value
* @param class cUrl cUrl class of the URL string. May be null, otherwise defualt value will be searched in the cUrl class
*/
function CArg(name, def_value, cUrl)
{
	this.name = name;
	this.value = def_value;
   this.def_value = def_value;

   if (cUrl) // if exists global cUrl class
   {
   	cnt = cUrl.arg_list.length;
   	for (i=0; i<cnt; i++)
   	   if (cUrl.arg_list[i].name == name) // this arg exists in a global array
            this.value = cUrl.arg_list[i].value
   } // if
} // class CArg
//========================================================================

// URL Class =============================================================
/**
* cUrl class -- collection arguments of the URL string
*    parameters:
*      arg_list -- array of arguments. cArg class
*
* @param string url parsed URL string
*/
function CUrl(url)
{
	this.arg_list = new Array();

	strUrl = new String(url);
	strUrl = strUrl.replace('http://', '');

   arUrl = new Array();
   arUrl = strUrl.split('?', 2);

   if (arUrl[1]) // exists Uri
   {
      arUri = new Array();
   	arUri = arUrl[1].split('&');

      cnt = arUri.length
      for (i=0; i<cnt; i++)
      {
         arg = arUri[i].split('=', 2);
         if (arg.length>1)
         	this.arg_list[i] = new CArg(arg[0], arg[1], null);
      } // for
   } // if
} // class CUrl
//========================================================================

// Query Class ===========================================================
/**
* CQuery class
*   parameters:
*      arg_list -- collection of the arguments. cArg class
*      page     -- page name
*   methods:
*      addArg   -- add new argument. cArg class
*      getArg   -- return argument class
*      setArgValue -- set new value to the argument
*      setDefaults -- set all arguments to the default value
*      getUrl   -- build URL string
*      getUri   -- build URI string
*
* @param string page Name of the page
*/
function CQuery(page)
{
	this.page_name = page;
	this.arg_list = new Array();

	// methods
/*	this.addArg = addArg;

	this.getArg = getArg;
	this.getArgValue = getArgValue;
	this.setArgValue = setArgValue;
	this.setDefaults = setDefaults;

	this.getUrl = getUrl;
	this.getUri = getUri;
*/
} // class CQuery

/**
* Adds new argument to the class.
*
* @param class arg new argument. cArg class
*/
CQuery.prototype.addArg = function(arg)
{
	cnt = this.arg_list.length;
	this.arg_list[cnt] = arg;
} // function addArg

/**
* Returns CArg class of the 'code' arg
*
* @param string code name of the arg
* @return class
*/
CQuery.prototype.getArg = function(code)
{
	cnt = this.arg_list.length;
	if (cnt)
	{
	   for (i=0; i<cnt; i++)
	   {
	   	if (this.arg_list[i].name == code)
	   	   return this.arg_list[i];
	   } // for
	} // if

	return 0;
} // funciton getArg

/**
* Returns arg 'code' current value
*
* @param string code
* @return string
*/
CQuery.prototype.getArgValue = function(code)
{
	cnt = this.arg_list.length;
	if (cnt)
	{
	   for (i=0; i<cnt; i++)
	   {
	   	if (this.arg_list[i].name == code)
	   	   return this.arg_list[i].value;
	   } // for
	} // if

	return 0;
} // funciton getArgValue

/**
* Sets arg 'code' value 'value'
*
* @param string code name of the arg
* @param string value new value
* @return bool if operation success
*/
CQuery.prototype.setArgValue = function(code, value)
{
	cnt = this.arg_list.length;
	if (cnt)
	{
	   for (i=0; i<cnt; i++)
	   {
	   	if (this.arg_list[i].name == code)
	   	{
	   	   this.arg_list[i].value = value;
	   	   return true;
	   	}
	   } // for
	} // if

	return false;
} // function setArgValue

/**
* Sets all arguments to the default value
*
* @param array except except parameters
*/
CQuery.prototype.setDefaults = function(except)
{
   if (except)
   {
   	if (typeof(except)=='object')
   	   exc = true;
   	else
   	   exc = false;
   } // if
   else
      exc = false;

	cnt = this.arg_list.length;

	if (exc)
   	exc_cnt = except.length;

	if (cnt)
	{
	   for (i=0; i<cnt; i++)
	   {
	      if (exc)
	      {
	      	except_flag = false;
	      	for (j=0; j<exc_cnt; j++)
	      	{
	      		if (except[j] == this.arg_list[i].name)
   	      		except_flag = true;
	      	} // for

	      	// if this arg not in except array
	      	if (!except_flag)
   	   	   this.arg_list[i].value = this.arg_list[i].def_value;
	      } // if
         else
	   	   this.arg_list[i].value = this.arg_list[i].def_value;

	   } // for
	} // if

} // function setDefaults

/**
* Build URL string
*
* @return string URL-string
*/
CQuery.prototype.getUrl = function()
{
   strUrl = new String(this.page_name);
   strUri =  new String();

	cnt = this.arg_list.length;
	if (cnt)
	{
	   strUri =  '?';
	   strUri += this.getUri();
	} // if

	return strUrl + strUri;
} // function getUrl

/**
* Build URI string
*
* @return string URI-string
*/
CQuery.prototype.getUri = function()
{
   strUri =  new String();

	cnt = this.arg_list.length;
	if (cnt)
	{
	   for (i=0; i<cnt; i++)
	   {
	      strUri += this.arg_list[i].name + '=' + this.arg_list[i].value + '&';
	   } // for

	   strUri = strUri.substring(0, strUri.length - 1);
	} // if

	return strUri;
} // function getUri
//========================================================================

