/* AJAX UPLOADING STATUS BAR */
function UAgent(){
	this.ver    = navigator.appVersion;
	this.agent  = navigator.userAgent;
	this.dom    = document.getElementById ? 1 : 0;
	this.opera5 = this.agent.indexOf("Opera 5") >= 0 ? 1 : 0;
	this.ie5    = (this.ver.indexOf("MSIE 5") >= 0 && this.dom && !this.opera5) ? 1 : 0;
	this.ie6    = (this.ver.indexOf("MSIE 6") >= 0 && this.dom && !this.opera5) ? 1 : 0;
	this.ie4    = (document.all && !this.dom && !this.opera5) ? 1 : 0;
	this.ie     = this.ie4 || this.ie5 || this.ie6;
	this.mac    = this.agent.indexOf("Mac") >= 0;
	this.ns6    = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0;
	this.ns4    = (document.layers && !this.dom) ? 1 : 0;
	this.bw     = (this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);
  return this;
}

function request(url){

    debug = 0;

    agent =  new UAgent();
    try{
    	if(agent.ie){
    		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.async=false;
    		xmlDoc.load(url);
    		if(xmlDoc.readyState != 4){
    			if(debug) alert('Error[load]['+this.id+']: ReadyState: '+xmlDoc.readyState);
    			return false;
    		}
    	}else{
    		objHTTP = new XMLHttpRequest();
    		objHTTP.open("GET", url, false);
    		objHTTP.send(null);
    		if(objHTTP.readyState != 4){
    			if(debug) alert('Error[load]['+this.id+']: ReadyState: '+objHTTP.readyState);
    			return false;
    		}
    		xmlDoc = objHTTP.responseXML;
    	}
    } catch(err) {
        if(debug) alert('Error[loadPage]: '+err);
        return false;
    }
    if (!xmlDoc) return false;
    return xmlDoc.documentElement;
}

/* Function printf(format_string,arguments...)
 * Javascript emulation of the C printf function (modifiers and argument types
 *    "p" and "n" are not supported due to language restrictions)
 *
 * Copyright 2003 K&L Productions. All rights reserved
 * http://www.klproductions.com
 *
 * Terms of use: This function can be used free of charge IF this header is not
 *               modified and remains with the function code.
 *
 * Legal: Use this code at your own risk. K&L Productions assumes NO resposibility
 *        for anything.
 ********************************************************************************/
function printf(fstring)
  { var pad = function(str,ch,len)
      { var ps='';
        for(var i=0; i<Math.abs(len); i++) ps+=ch;
        return len>0?str+ps:ps+str;
      }
    var processFlags = function(flags,width,rs,arg)
      { var pn = function(flags,arg,rs)
          { if(arg>=0)
              { if(flags.indexOf(' ')>=0) rs = ' ' + rs;
                else if(flags.indexOf('+')>=0) rs = '+' + rs;
              }
            else
                rs = '-' + rs;
            return rs;
          }
        var iWidth = parseInt(width,10);
        if(width.charAt(0) == '0')
          { var ec=0;
            if(flags.indexOf(' ')>=0 || flags.indexOf('+')>=0) ec++;
            if(rs.length<(iWidth-ec)) rs = pad(rs,'0',rs.length-(iWidth-ec));
            return pn(flags,arg,rs);
          }
        rs = pn(flags,arg,rs);
        if(rs.length<iWidth)
          { if(flags.indexOf('-')<0) rs = pad(rs,' ',rs.length-iWidth);
            else rs = pad(rs,' ',iWidth - rs.length);
          }
        return rs;
      }
    var converters = new Array();
    converters['c'] = function(flags,width,precision,arg)
      { if(typeof(arg) == 'number') return String.fromCharCode(arg);
        if(typeof(arg) == 'string') return arg.charAt(0);
        return '';
      }
    converters['d'] = function(flags,width,precision,arg)
      { return converters['i'](flags,width,precision,arg);
      }
    converters['u'] = function(flags,width,precision,arg)
      { return converters['i'](flags,width,precision,Math.abs(arg));
      }
    converters['i'] =  function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = ((Math.abs(arg)).toString().split('.'))[0];
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        return processFlags(flags,width,rs,arg);
      }
    converters['E'] = function(flags,width,precision,arg)
      { return (converters['e'](flags,width,precision,arg)).toUpperCase();
      }
    converters['e'] =  function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        if(isNaN(iPrecision)) iPrecision = 6;
        rs = (Math.abs(arg)).toExponential(iPrecision);
        if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs.replace(/^(.*)(e.*)$/,'$1.$2');
        return processFlags(flags,width,rs,arg);
      }
    converters['f'] = function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        if(isNaN(iPrecision)) iPrecision = 6;
        rs = (Math.abs(arg)).toFixed(iPrecision);
        if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs + '.';
        return processFlags(flags,width,rs,arg);
      }
    converters['G'] = function(flags,width,precision,arg)
      { return (converters['g'](flags,width,precision,arg)).toUpperCase();
      }
    converters['g'] = function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        absArg = Math.abs(arg);
        rse = absArg.toExponential();
        rsf = absArg.toFixed(6);
        if(!isNaN(iPrecision))
          { rsep = absArg.toExponential(iPrecision);
            rse = rsep.length < rse.length ? rsep : rse;
            rsfp = absArg.toFixed(iPrecision);
            rsf = rsfp.length < rsf.length ? rsfp : rsf;
          }
        if(rse.indexOf('.')<0 && flags.indexOf('#')>=0) rse = rse.replace(/^(.*)(e.*)$/,'$1.$2');
        if(rsf.indexOf('.')<0 && flags.indexOf('#')>=0) rsf = rsf + '.';
        rs = rse.length<rsf.length ? rse : rsf;
        return processFlags(flags,width,rs,arg);
      }
    converters['o'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = Math.round(Math.abs(arg)).toString(8);
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        if(flags.indexOf('#')>=0) rs='0'+rs;
        return processFlags(flags,width,rs,arg);
      }
    converters['X'] = function(flags,width,precision,arg)
      { return (converters['x'](flags,width,precision,arg)).toUpperCase();
      }
    converters['x'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        arg = Math.abs(arg);
        var rs = Math.round(arg).toString(16);
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        if(flags.indexOf('#')>=0) rs='0x'+rs;
        return processFlags(flags,width,rs,arg);
      }
    converters['s'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = arg;
        if(rs.length > iPrecision) rs = rs.substring(0,iPrecision);
        return processFlags(flags,width,rs,0);
      }
    farr = fstring.split('%');
    retstr = farr[0];
    fpRE = /^([-+ #]*)(\d*)\.?(\d*)([cdieEfFgGosuxX])(.*)$/;
    for(var i=1; i<farr.length; i++)
      { fps=fpRE.exec(farr[i]);
        if(!fps) continue;
        if(arguments[i]!=null) retstr+=converters[fps[4]](fps[1],fps[2],fps[3],arguments[i]);
        retstr += fps[5];
      }
    return retstr;
  }


  function vote(master_type, master_id, rate){

    var rating;

    new Ajax.Request(url_vote_add, {
        method: 'post',
        parameters: {vote_master_type: master_type, vote_master_id: master_id, vote_rate: rate},
        onSuccess: function(transport){

            data = eval( '(' + transport.responseText + ')' );
            rating = data.rating;

            view = document.getElementById( master_type + '_' + master_id);
            if (!view) return false;
            if (!rating) return false;
            view.innerHTML = rating;
            return true;
        }
    });

}

function post_comment(name){
    f_name = 'comment_' + name ;
    f = document[f_name];
    if (!f) return false;

    indicator = document.getElementById('indicator_' + name);

    show(indicator);
    document.body.style.cursor = 'progress';

    f.submit.blur();

    comment_username = f.comment_username.value;
    comment_title = f.comment_title.value;
    comment_text = f.comment_text.value;
    comment_master_type = f.comment_master_type.value;
    comment_master_id = f.comment_master_id.value;
    comment_count = f.count.value;

    try {
        comment_list = document.getElementById('comment_list');
    } catch(e) {}

    new Ajax.Request(url_comment_add, {
        method: 'post',
        parameters: {
            comment_master_type: comment_master_type,
            comment_master_id: comment_master_id,
            comment_username: comment_username,
            comment_title: comment_title,
            comment_text: comment_text,
            count: comment_count
        },
        onSuccess: function(transport){
            new_comment = transport.responseText;
            comment_list.innerHTML = new_comment;
            f.comment_text.value = '';
            f.comment_text.focus();
            hide(indicator);
            document.body.style.cursor = '';
            return true;
        }
    });

    return false;
}

function show(element) {
//    if (!element) return false;
//    if (!element.style) return false;
    element.style.display = "";
}

function hide(element){
//    if (!element) return false;
//    if (!element.style) return false;
    element.style.display = "none";
}

function showComment(id) {
    comment_text = document.getElementById('comment_text_' + id);
    comment_text.style.display = 'block';

    document.getElementById('comment_voting_' + id).style.display = '';
    document.getElementById('comment_hidden_' + id).style.display = 'none';
    return true;
}