var def_ajax = "1.1";										// The version of ajax.js

var cpaint_loader = null;

function loadScripts()
{
  if( ajax_oldload != null && typeof( ajax_oldload ) != "undefined" )
  {
    ajax_oldload();
  }

  if( typeof( cpaint ) == "undefined" )
  {
    addScript( "/js/ajax/cpaint2.inc.compressed.js" );
    addScript( "/js/ajax/tinyxmlsax.js" );
    addScript( "/js/ajax/tinyxmlw3cdom.js" );
  }
}

function addScript( scriptSrc )
{
  var scriptTag  = document.createElement( "script" );
  scriptTag.type = "text/javascript";
  scriptTag.src  = scriptSrc;

  var theHead = document.getElementsByTagName( "head" )[0];

  if( typeof( theHead ) != "undefined" )
  {
    theHead.appendChild( scriptTag );
  }
}

function testAjax()
{
  if( typeof( cpaint_call ) != "undefined" )
  {
    var cpc = new cpaint_call( 0 );

    return cpc.test_ajax_capability();
  }

  return false;
}

function createLoader()
{
  if( cpaint_loader == null )
  {
    cpaint_loader = new cpaint();

    cpaint_loader.set_async( true );
    cpaint_loader.set_persistent_connection( false );
    cpaint_loader.set_debug( 0 );
    cpaint_loader.set_use_cpaint_api( false );
  }
}

function callRemote( url, callBack, responseType )
{
  if( typeof( responseType ) == "undefined" )
  {
    responseType = "text";
  }

  var cb = Math.round( Math.random() * 20000 );

  url += ( ( url.indexOf( "?" ) == -1 ) ? "?" : "&" ) + "__cb=" + cb;

  createLoader();

  cpaint_loader.set_response_type( responseType );

  cpaint_loader.call( url, '', callBack );
}

function getSubItem( item, valueName, attribName, attribValue )
{
  var subItems = item.getElementsByTagName( valueName );

  if( subItems.length > 0 )
  {
    if( typeof( attribName ) == "undefined" )
    {
      return subItems.item( 0 );
    }
    else
    {
      for( var i = 0 ; i < subItems.length ; i++ )
      {
        var itemValue = subItems.item( i ).getAttribute( attribName );

        if( itemValue == attribValue )
        {
          return subItems.item( i );
        }
      }
    }
  }

  return null;
}

function getSubItemValue( item, valueName, attribName, attribValue )
{
  var subItem = getSubItem( item, valueName, attribName, attribValue );

  if( subItem != null )
  {
    return getItemValue( subItem );
  }

  return null;
}

function getItemValue( item )
{
  if( item != null && typeof( item.getFirstChild() ) != "undefined" && item.getFirstChild() != null )
  {
    return item.getFirstChild().getNodeValue();
  }

  return null;
}

function getQueryString( theForm )
{
  var theElem;
  var queryString = "";

  for( var i = 0 ; i < theForm.elements.length ; i++ )
  {
    theElem = theForm.elements[i];

    if( theElem.name == "" || theElem.disabled ) continue;

    switch( theElem.type )
    {
      case "radio":
      case "checkbox":
        if( theElem.checked )
        {
          queryString += ( ( queryString == "" ) ? "" : "&" ) + escape( theElem.name ) + "=" + escape( theElem.value );
        }

        break;

      case "select-one":
      case "select-multiple":
        for( var j = 0 ; j < theElem.options.length ; j++ )
        {
          if( theElem.options[j].selected )
          {
            queryString += ( ( queryString == "" ) ? "" : "&" ) + escape( theElem.name ) + "=" + escape( ( theElem.options[j].value == "" ) ? theElem.options[j].text : theElem.options[j].value );
          }
        }

        break;

      default:
        queryString += ( ( queryString == "" ) ? "" : "&" ) + escape( theElem.name ) + "=" + escape( theElem.value );
    }
  }

  return queryString;
}

function getElemValue( theElem )
{
  var elemValue = "";

  switch( theElem.type )
  {
    case "radio":
    case "checkbox":
      if( theElem.checked )
      {
        elemValue += theElem.value;
      }

      break;

    case "select-one":
    case "select-multiple":
      for( var j = 0 ; j < theElem.options.length ; j++ )
      {
        if( theElem.options[j].selected )
        {
          elemValue += ( ( elemValue == "" ) ? "" : "," ) + ( ( theElem.options[j].value == "" ) ? theElem.options[j].text : theElem.options[j].value );
        }
      }

      break;

    default:
      elemValue += theElem.value;
  }

  return elemValue;
}

function setElemValue( theElem, elemValue )
{
  switch( theElem.type )
  {
    case "radio":
    case "checkbox":
      if( theElem.value == elemValue )
      {
        theElem.checked = true;
      }

      break;

    case "select-one":
      for( var j = 0 ; j < theElem.options.length ; j++ )
      {
        if( ( theElem.options[j].value == "" && theElem.options[j].text == elemValue ) || theElem.options[j].value == elemValue )
        {
          theElem.selectedIndex = j;
          break;
        }
      }

      break;

    case "select-multiple":
      for( var j = 0 ; j < theElem.options.length ; j++ )
      {
        theElem.options[j].selected = ( ( theElem.options[j].value == "" ) ? theElem.options[j].text == elemValue : theElem.options[j].value == elemValue );
      }

      break;

    default:
      theElem.value = elemValue;
  }
}

var ajax_oldload = window.onload;
window.onload = loadScripts;

