var trimchars = {
    1 : "\n",
    2 : "\r",
    3 : " "
}

function htmlspecialchars (string, quote_style, charset, double_encode) {
    // http://kevin.vanzonneveld.net
    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      input by: felix
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: charset argument not supported
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    // *     example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);
    // *     returns 2: 'ab"c&#039;d'
    // *     example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
    // *     returns 3: 'my &quot;&entity;&quot; is still here'

    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined' || quote_style === null) {
        quote_style = 2;
    }
    string = string.toString();
    if (double_encode !== false) { // Put this first to avoid double-encoding
        string = string.replace(/&/g, '&amp;');
    }
    string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');

    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/'/g, '&#039;');
    }
    if (!noquotes) {
        string = string.replace(/"/g, '&quot;');//"
    }

    return string;
}

function nl2br (str, is_xhtml) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli ??r
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'

    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

function trimcheckchar(ch)
{
    var i;
    for (i in trimchars)
    {
        if(trimchars[i] == ch)
        {
            return true;
        }
    }
    return false;
}

function altrim(str)
{
    var v_length = str.length;
    if(v_length < 1)
    {
        return "";
    }
    
    var strTemp = "";
    
    var iTemp = 0;
    
    while(iTemp < v_length)
    {
        if(trimcheckchar(str.charAt(iTemp)))
        {
        }
        else
        {
            strTemp = str.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
}

function artrim(str)
{
    var v_length = str.length;
    if(v_length < 1)
    {
        return "";
    }
    
    var strTemp = "";   
    var iTemp = v_length -1;
    
    while(iTemp > -1)
    {
        if(trimcheckchar(str.charAt(iTemp)))
        {
        }
        else
        {
            strTemp = str.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
    
    } //End While
    return strTemp;
}


function atrim(str)
{
    str = altrim(str);
    str = artrim(str);
    return str;
}


function doSend()
{
    var contunit = "word";
    var contlen = 10;
    var text = null;
    if (navigator.appName.indexOf("Netscape")!=-1 && eval(navigator.appVersion.substring(0,1))<5) {
      alert("Ваш браузер не поддерживает эту возможность!");
      return;
    }   
    var selection = null;
    
    if (window.getSelection) {
      text = window.getSelection();
    } else if (document.getSelection) {
      text = document.getSelection();
    } else {
      selection = document.selection;
    }
    
    if (selection) {
      var r = selection.createRange(); if (!r) return;
      text = r.text;
      var s = 0; 
      while (text.charAt(s)==" " || text.charAt(s)=="\n") s++;
      var e = 0; 
      while (text.charAt(text.length-e-1)==" " || text.charAt(text.length-e-1)=="\n") e++;
      var rngA = selection.createRange();
      rngA.moveStart(contunit, -contlen);
      rngA.moveEnd("character", -text.length+s);
      var rngB = selection.createRange();
      rngB.moveEnd(contunit, contlen);
      rngB.moveStart("character", text.length-e);
      text     = text.substring(s, text.length-e);
    }
    //*
    if (text == null) { 
      alert("Ваш браузер не поддерживает эту возможность!"); 
      return; 
    }//*/
    
    var url = '';
    if(tp_url)
    {
    url = tp_url;
    }
    else
    {
    url = document.location.href;
    }
    var scroll = "no";
    var height = 250;
    var width = 450;
    var top=0, left=0;
    if(width > screen.width-10 || height > screen.height-28) scroll = "yes";
    if(height < screen.height-28) top = Math.floor((screen.height - height)/2-14);
    if(width < screen.width-10) left = Math.floor((screen.width - width)/2-5);
    width = Math.min(width, screen.width-10);
    height = Math.min(height, screen.height-28);
    var wnd = window.open("","","scrollbars="+scroll+",resizable=yes,width="+width+",height="+height+",left="+left+",top="+top);
    wnd.document.write("<html><head>\n");
    wnd.document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">");
    wnd.document.write("<link rel=\"stylesheet\" href=\"/bitrix/templates/tc/template_styles.css\" type=\"text/css\">");
    wnd.document.write("<title>Сообщение об ошибке</title></head>\n");
    wnd.document.write("<body topmargin=\"0\" leftmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\n");
    wnd.document.write("<form id='f1' action='/senderror.php' method='POST'><table border='0' width='98%'>");
    wnd.document.write("<tr><td>");
    wnd.document.write("<input type='hidden' id=\"f_error\" name='error' value='"+htmlspecialchars(text)+"'>");
    wnd.document.write("<input type='hidden' id=\"f_url\" name='url' value='"+htmlspecialchars(url)+"'>");
if($.browser.msie)
wnd.document.write("<input type='hidden' name='is_msie' value='1'>");
if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
wnd.document.write("<input type='hidden' name='is_chrome' value='1'>");
    if(text == "")
    {
        wnd.document.write("Опишите, пожалуйста, в чем заключается неточность:");
    }
    else
    {
        wnd.document.write("Найдена неточность:<br><b>" + nl2br(htmlspecialchars(text)));
        wnd.document.write("</b>");
        wnd.document.write("<br><br>Прокомментируйте, если сочтете необходимым");
    }
    wnd.document.write("</td></tr>");
    wnd.document.write("<tr><td align='center'><textarea name=\"message\" cols=\"20\" rows=\"6\" style=\"width: 100%\" class=\"inputtextarea\"></textarea></td></tr>");
    wnd.document.write("<tr><td align='right'><input type='submit' value='Отправить'></td></tr>");
    wnd.document.write("</table></form>");
    wnd.document.write("</body>");
    wnd.document.write("</html>");
    wnd.document.close();
    var f = wnd.document.forms['f1'];
    if(isMozilla)
    {
    //alert(wnd.document.forms[0]);
        //alert(wnd.document.getElementById('f_error'));
        /*
        qqq = wnd.document.forms;
        zzz = qqq.item(0);
        var q = "";
        for (i in zzz)
        {
        q += i + " ::: " ; //+ wnd.document[i] + "\n";
        }
        alert(q);
        alert(qqq.item(0));
        */
    }
    //if(!f) f = wnd.document.getElementById('f1');
    //alert(f);
    f.error.value = text;
    f.url.value = url;
}

function doSendPrice(item, url, price, contact, email)
{
    var scroll = "no";
    var height = 250;
    var width = 450;
    var top=0, left=0;
    if(width > screen.width-10 || height > screen.height-28) scroll = "yes";
    if(height < screen.height-28) top = Math.floor((screen.height - height)/2-14);
    if(width < screen.width-10) left = Math.floor((screen.width - width)/2-5);
    width = Math.min(width, screen.width-10);
    height = Math.min(height, screen.height-28);
    var wnd = window.open("","","scrollbars="+scroll+",resizable=yes,width="+width+",height="+height+",left="+left+",top="+top);
    wnd.document.write("<html><head>\n");
    wnd.document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">");
    wnd.document.write("<link rel=\"stylesheet\" href=\"/bitrix/templates/tc/template_styles.css\" type=\"text/css\">");
    wnd.document.write("<title>Комментарий на цену</title></head>\n");
    wnd.document.write("<body topmargin=\"0\" leftmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\n");
    wnd.document.write("<form id='f1' action='/pricecomplain.php' method='POST'><table border='0' width='98%'>");
    wnd.document.write("<tr><td>");
    wnd.document.write("<input type='hidden' id=\"f_item\" name='item' value='"+htmlspecialchars(item)+"'>");
    wnd.document.write("<input type='hidden' id=\"f_url\" name='url' value='"+htmlspecialchars(url)+"'>");
    wnd.document.write("<input type='hidden' id=\"f_price\" name='price' value='"+htmlspecialchars(price)+"'>");
    wnd.document.write("<input type='hidden' id=\"f_email\" name='email' value='"+htmlspecialchars(email)+"'>");
if($.browser.msie)
wnd.document.write("<input type='hidden' name='is_msie' value='1'>");
if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
wnd.document.write("<input type='hidden' name='is_chrome' value='1'>");
    wnd.document.write("</td></tr>");
    wnd.document.write("<tr><td>Оставьте, пожалуйста, ваш контакт:</td></tr>");
    wnd.document.write("<tr><td align='center'><input name='contact' value='"+htmlspecialchars(contact)+"' size='50' style='width: 100%' class='inputtext'></td></tr>");
    wnd.document.write("<tr><td>Комментарий:</td></tr>");
    wnd.document.write("<tr><td align='center'><textarea name='message' cols='50' rows='6' style='width: 100%' class='inputtextarea'></textarea></td></tr>");
    wnd.document.write("<tr><td align='right'><input type='submit' value='Отправить'></td></tr>");
    wnd.document.write("</table></form>");
    wnd.document.write("</body>");
    wnd.document.write("</html>");
    wnd.document.close();
    var f = wnd.document.forms['f1'];
    f.error.value = text;
    f.url.value = url;
}

function tcokp(e)
{
    var pressed=0;
    var we = null;
    if (window.event) we = window.event;
    else if (parent && parent.event) we = parent.event;
    if (we) {
      // IE & Opera
      pressed = we.keyCode==10 ||  // IE
        (we.keyCode == 13 && we.ctrlKey); // Opera 
    } else if (e) {
      // NN
      pressed = 
        (e.which==10 && e.modifiers==2) || // NN4
        (e.keyCode==0 && e.charCode==106 && e.ctrlKey) ||
        (e.keyCode==13 && e.ctrlKey) // Mozilla
    }
    if (pressed) {
      doSend();
      return false;
    }
}

document.onkeypress = function(e) { return tcokp(e) };