var xml = null;
var stateList = null;
var cityList = null;
var smallSize = 0;
var postFix = '';
var loadingMessages = null;


function ajax ( url , arr , cb )
{
   if (xml != null && xml.readyState != 0 && xml.readyState != 4) 
      xml.abort ();

   try { xml = new XMLHttpRequest (); }
   catch ( error )
   {
      try { xml = new ActiveXObject ( "Microsoft.XMLHTTP" ); }
      catch ( error )
      {
         xml = null;
         return false;
      }
   }

   var data = "";

   for ( i in arr ) 
      data = data + i + "=" + arr [i] + "&";

   xml.onreadystatechange = cb;
   xml.open ( "POST" , url );
   xml.setRequestHeader ( "If-Modified-Since" , "Sat, 1 Jan 2000 00:00:00 GMT" );
   xml.setRequestHeader ( "Content-type" , "application/x-www-form-urlencoded" );
   xml.setRequestHeader ( "Connection" , "close" );
   xml.setRequestHeader ( "Content-length" , data.length );

   xml.send ( data );

}




function getStates ( c , pf )
{
   stateList = document.getElementById ( 'state' + pf );
   stateList.options.length = 0;

   cityList = document.getElementById ( 'city' + pf );
   cityList.options.length = 0;
   cityList.options [0] = new Option( "Select state" , 0 , false );

   var id = c.options[c.selectedIndex].value;

   if ( id )
   {
      var r = [];
      r ['a'] = 'getStates';
      r ['id'] = id;
      ajax ( '/ajax.php' , r , applyStates );
   }  
}



function applyStates ()
{
   if ( xml.readyState == 4 ) {
      try {
         if ( xml.status == 200 ) {

            if ( document.all ) 
            {
               stateList.innerHTML = "$";
               stateList.outerHTML = stateList.outerHTML.replace ( "$" , xml.responseText );
            }
            else
            {
               stateList.innerHTML = xml.responseText;
            }
         }
         else if (xml.status != 0) alert("There was an error while retrieving the URL: " + xml.statusText);
      }
      catch (error) { alert(error); }
   }
   return true;
}



function flagArticle ( id ) 
{
   ajax ( '/ajax.php' , { 'a': 'flagArticle' , 'id': id } , errorResp );
   document.getElementById ( 'flagLink' ).innerHTML = 'Thanks!';
   return false;
}



function delPic ( id )
{
   if ( id )
   {
      var r = [];
      r ['a'] = 'delPic';
      r ['id'] = id;
      ajax ( '/ajax.php' , r , errorResp );
   }
}


function picPrimary ( id )
{
   if ( id )
   {
      var r = [];
      r ['a'] = 'picPrimary';
      r ['id'] = id;
      ajax ( '/ajax.php' , r , errorResp );
   }  
}


function delUserPic ( id )
{
   if ( id )
   {
      var r = [];
      r ['a'] = 'delUserPic';
      r ['id'] = id;
      ajax ( '/ajax.php' , r , errorResp );
   }  
}


function upicPrimary ( id )
{
   if ( id )
   {
      var r = [];
      r ['a'] = 'upicPrimary';
      r ['id'] = id;
      ajax ( '/ajax.php' , r , errorResp );
   }  
}

function errorResp ()
{
   if ( xml.readyState == 4 ) {
      try {
         if ( xml.status == 200 ) {
            if ( xml.responseText != "" )
               alert ( xml.responseText );
         }
         else if (xml.status != 0) alert("There was an error while retrieving the URL: " + xml.statusText);
      }
      catch (error) { alert(error); }
   }
   return true;
}





function getCities ( c , pf )
{
   cityList = document.getElementById ( 'city' + pf );
   cityList.options.length = 0;

   var id = c.options[c.selectedIndex].value;

   if ( id )
   {
      var r = [];
      r ['a'] = 'getCities';
      r ['id'] = id;
      ajax ( '/ajax.php' , r , applyCities );
   }  
}



function applyCities ()
{
   if ( xml.readyState == 4 ) {
      try {
         if ( xml.status == 200 ) {
            if ( document.all ) 
            {
               cityList.innerHTML = "$";
               cityList.outerHTML = cityList.outerHTML.replace ( "$" , xml.responseText );
            }
            else
            {
               cityList.innerHTML = xml.responseText;
            }
         }
         else if (xml.status != 0) alert("There was an error while retrieving the URL: " + xml.statusText);
      }
      catch (error) { alert(error); }
   }
   return true;
}




var savedRatingWidth = 0;
var savedNode = null;


         function pp(obj, objName)
         {
             var result = "";
             for (var i in obj)
                 result += objName + "." + i + " = " + obj[i] + "<br>\n";
             document.write(result);
         }



         function getAbsolutePosition(element) 
         {
            var x = element.offsetLeft;
            if ( element.offsetParent ) x += getAbsolutePosition ( element.offsetParent );
            return x;
         };



         function getStars ( o , e , b )
         {
            var stars = 0;
            var offset = 0;
            var innerDiv = o.children ? o.children[0] : o.childNodes[1];

            // for FF
            if ( ! o.children )
               offset = e.clientX - getAbsolutePosition(o);
            else
               offset = e.offsetX;

            if ( b ) 
               stars = Math.round ( ( offset + 7 ) / 16 );
            else
               stars = Math.round ( ( offset + 5 ) / 10 );

            if ( stars > 5 ) stars = 5;
            if ( stars < 1 ) stars = 1;

            return stars;           
         }



         function rateArt ( o , e , b )
         {
            submitRating ( o , e , b , 1 );
            
         }

         function rate ( o , e , b )
         {
            submitRating ( o , e , b , 0 );
         }

         function submitRating ( o , e , b , isArt )
         {
            var stars = getStars ( o , e , b );
            var innerDiv = o.children ? o.children[0] : o.childNodes[1];
            var r = [];

            savedNode = o;

            r ['a'] = 'setRating';
            r ['rating'] = stars;

            if ( isArt ) r ['article_id'] = o.id;
               else r ['studio_id'] = o.id;

            ajax ( '/ajax.php' , r , applyRating );
            smallSize = !b;
         }



         function applyRating ()
         {
            if ( xml.readyState == 4 ) {
               try {
                  if ( xml.status == 200 ) {

                     o = savedNode;
         
                     var rating = 0.0;
                     var innerDiv = o.children ? o.children[0] : o.childNodes[1];
                     var size = 53;
                     if ( !smallSize ) size = 80;

                     rating = parseFloat ( xml.responseText );

                     if ( rating == NaN )
                     {
                        alert ( xml.responseText );
                     }
                     else
                     {
                        savedRatingWidth = rating * size / 5;
                        innerDiv.style.width = savedRatingWidth;
                        //innerDiv.style.zIndex = savedRatingWidth;
                     }
                     o.style.cursor = "";
                     o.onclick  = "";
                     o.onmouseover = "";
                     o.onmousemove  = "";
                     o.onmouseout = "";
                  }
               }
               catch (error) { alert(error); }
            }
            return true;
         }
         
         
         function rateOver ( o , e , b )
         {
            var width = 0;
            var stars = getStars ( o , e , b );
            var innerDiv = o.children ? o.children[0] : o.childNodes[1];
            
            if ( !b )
            {
               width = stars * 10;
               if ( stars == 3 ) width = 31;
               if ( stars == 4 ) width = 42;
               if ( stars == 5 ) width = 53;
            }
            else
            {
               width = 16;
               if ( stars == 2 ) width = 32;
               if ( stars == 3 ) width = 48;
               if ( stars == 4 ) width = 64;
               if ( stars == 5 ) width = 80;
            }

            innerDiv.style.width = width + "px";
         }

         
         function rateEnd ( o , e )
         {
            var innerDiv = o.children ? o.children[0] : o.childNodes[1];
            innerDiv.style.width = innerDiv.style.zIndex;
         }





function cc ( o ) 
{
   if ( _htvars ) 
   {
      if ( o.value == _htvars [o.name] )
         o.value = "";
   }
}


function iblur ( o )
{
   if ( _htvars ) 
   {
      if ( o.value == "" ) // if empty, set default value
         o.value = _htvars [o.name];
   }
}






var mutexPass = 0;


function passMask ( id )
{
   mutexPass = 1;
   var pass = document.getElementById ( id );
   var pass_mask = document.getElementById ( id + '_mask' );
   if ( pass ) 
   {
      pass_mask.style.display = "none";
      pass.style.display = "inline";
      pass.focus ();
   }
   mutexPass = 0;
}


function passUnMask ( id )
{
   if ( mutexPass ) return;
   var pass = document.getElementById ( id );
   var pass_mask = document.getElementById ( id + '_mask' );
   if ( pass.value == "" && pass_mask ) 
   {
      pass.style.display = "none";
      pass_mask.style.display = "inline";
   }
}



function suggestCity()
{
   var cityC = document.getElementById ( 'cityC' );
   var cityT = document.getElementById ( 'cityT' );

   if ( cityC && cityT )
   {
      cityC.style.display = "none";
      cityT.style.display = "inline";
      cityT.focus();
   }
   return false;
}





function showMessageForm ()
{
   var smf = document.getElementById ( 'userMessageForm' );
   var usb = document.getElementById ( 'userSendButton' );
   if ( smf )
   {
      smf.style.display = "block";
      usb.style.display = "none";
      var um = document.getElementById ( 'userMessage' );
      if ( um ) um.focus();
   }
   return false;
}


function userMessageHide ()
{
   var smf = document.getElementById ( 'userMessageForm' );
   var msgSent = document.getElementById ( 'userMessageSent' );
   if ( smf )
   {
      smf.style.display = "none";
      msgSent.style.display = "block";
//      var um = document.getElementById ( 'userMessage' );
//      if ( um ) um.focus();
   }

   return false;
}



function userSendMessage ( id )
{
   var msgArea = document.getElementById ( 'userMessage' );
   if ( msgArea )
      ajax ( '/ajax.php' , { 'a': 'sendMessage' , 'id': id , 'message' : msgArea.value } , userMessageSent );
   
   return false;
}



function userEmailMessage ( id )
{
   var msgArea = document.getElementById ( 'userMessage' );
   if ( msgArea )
      ajax ( '/ajax.php' , { 'a': 'emailMessage' , 'id': id , 'message' : msgArea.value } , userMessageSent );
   
   return false;
}



function userMessageSent ()
{
   if ( xml.readyState == 4 ) {
      try {
         if ( xml.status == 200 ) {
            if ( xml.responseText != "" )
               alert ( xml.responseText );
            else
               userMessageHide ();
         }
         else if (xml.status != 0) alert("There was an error while retrieving the URL: " + xml.statusText);
      }
      catch (error) { alert(error); }
   }
   return true;
}




function userSendReply ( id )
{
   var msgArea = document.getElementById ( 'userMessage' );
   if ( msgArea )
      ajax ( '/ajax.php' , { 'a': 'sendMessage' , 'id': id , 'message' : msgArea.value } , userReplySent );

   return false;

}


function userReplySent ()
{
   if ( xml.readyState == 4 ) {
      try {
         if ( xml.status == 200 ) {
            if ( xml.responseText != "" )
               alert ( xml.responseText );
            else
               userShowMessages ( loadingMessages );
         }
         else if (xml.status != 0) alert("There was an error while retrieving the URL: " + xml.statusText);
      }
      catch (error) { alert(error); }
   }
   return true;
}




function userShowMessages ( id )
{
   if ( loadingMessages != null )
   {
      var d = document.getElementById ( "msgList_" + loadingMessages );
      if ( d ) 
      {
         d.innerHTML = '';
         d.style.display = 'none';
      }
   }

   loadingMessages = id;
   ajax ( '/ajax.php' , { 'a': 'showMessages' , 'src_id': id } , userDisplayMessages );
      
   return false;
}


function userDisplayMessages ()
{
   if ( xml.readyState == 4 ) {
      try {
         if ( xml.status == 200 ) {
            var d = document.getElementById ( "msgList_" + loadingMessages );
            if ( d ) 
            {
               d.innerHTML = xml.responseText;
               d.style.display = 'block';
            }
//            loadingMessages = null;
         }
         else if (xml.status != 0) alert("There was an error while retrieving the URL: " + xml.statusText);
      }
      catch (error) { alert(error); }
   }
   return true;
}





/*      <a href="#" onClick="return showMessageForm();">Send Message</a>

      <div class="send_message" id="userMessageForm">
         <textarea id="userMessage"></textarea>
         <br>
         
         <ul class="bt bb">
            <li class="l"></li>
            <li class="m"><a href="#" onClick="return userSendMessage();">Send</a></li>
            <li class="r"></li>
            <li>&nbsp;</li>
         </ul>

      </div>
*/