/* Установка страницы в качестве стартовой */
function set_start_page(page, site) { 
	if (( navigator.appVersion.indexOf('MSIE 5.') != -1) || (navigator.appVersion.indexOf('MSIE 6.') != -1)) { 
		page.style.behavior='url(#default#homepage)'; 
		page.setHomePage(site); 
	} else { location.href='/wellcms-faq/startpage/' }
}
/* Поместить страницу в избранное */
function set_favourite(page, title) { 
	if (( navigator.appVersion.indexOf('MSIE 5.') != -1) || (navigator.appVersion.indexOf('MSIE 6.') != -1)) { 
		window.external.addFavorite(page, title);
	} else { location.href='/wellcms-faq/favourite/' }
}

function sall(sitem){
  if( !sitem ) return;
  if( !sitem.length ) sitem.checked = true;
  else
    for(var i=0;i<sitem.length;i++)
      sitem[i].checked = true;
}
function desall(sitem){
  if( !sitem ) return;
  if( !sitem.length ) sitem.checked = false;
  else
    for(var i=0;i<sitem.length;i++)
      sitem[i].checked = false;
}
function inverts(sitem){
  if( !sitem ) return;
  if( !sitem.length ) 
    sitem.checked = sitem.checked ? false : true;
  else
    for(var i=0;i<sitem.length;i++)
      sitem[i].checked = sitem[i].checked ? false : true;
}




function deleteconf(message)
 {if(!message) message="Вы действительно хотите удалить этот элемент?";
  if (confirm(message))  return true;
  return false;
 }

function open_win(url, width, height) 
{
 width=width || 500;
 height=height || 400;
 window.open(url, "newwin", "width="+width+",height="+height+",resizable,scrollbars,status");
}





//В будущем использовать именно эту функци
function checkAll(oForm, cbName, checked)
{
for (var i=0; i < oForm[cbName].length; i++) oForm[cbName][i].checked = checked;
}

function select_all(fname, name, value) {
  formblock = document.getElementById(fname);
  forminputs = formblock.getElementsByTagName('input');
  for (i = 0; i < forminputs.length; i++) {
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) {
      if (value == '1') {
        forminputs[i].checked = true;
      } else if (value == '0') {
        forminputs[i].checked = false;
      } else if (value == 'invert') {
        forminputs[i].checked = forminputs[i].checked ? false : true;
      }
    }
  }
}
function dont_del(name) {
  name.checked = false;
}
//Проверка email-адреса с помощью регулярного выражения
function isValidEmail (email)
{
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

//конвертация десятичного числа в шестнадцатеричное и обратно
function decToHex(n){ return Number(n).toString(16); }
function hexTodec (hex){ return parseInt(hex,16); }

//Как преобразовать значение цвета из rgb(n,n,n) в #HEX
function getHexRGBColor(color)
{
  var hexColor = '';

  if (Number(color))
  {
    hexColor = Number(color).toString(16);
    for (;hexColor.length<6; hexColor='0'+hexColor);
  }
  else
  {
    color = color.replace(/\s/g,"");
    var aRGB = color.match(/^rgb\((\d{1,3}[%]?),(\d{1,3}[%]?),(\d{1,3}[%]?)\)$/i);

    if(aRGB)
      for (var i=1;  i<=3; i++) hexColor += Math.round((aRGB[i][aRGB[i].length-1]=="%"?2.55:1)*parseInt(aRGB[i])).toString(16).replace(/^(.)$/,'0$1'); 
    else
      color = color.replace(/^#([\dabcdef])([\dabcdef])([\dabcdef])$/i, '#$1$1$2$2$3$3');
  } 

  return hexColor?'#'+hexColor:color;
}

//Как получить отдельно значение красного (Red), зеленого (Green), синего (Blue) цветов из RGB значения цвета
function splitRGB(color)
{
  color = getHexRGBColor(color); 
  var matches = color.match(/^#([\dabcdef]{2})([\dabcdef]{2})([\dabcdef]{2})$/i);
    
  if (!matches) return false;
  
  for (var i=1, rgb = new Array(3);  i<=3; i++) rgb[i-1] = parseInt(matches[i],16);
  
  return rgb;
}

//Генерация случайного числа в Javascript
function randomNumber (m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}

//Функциии Javascript и значения по умолчанию...
function myFunc(s)
{
 s = s || 'default value for s';
 alert(s);
}













/*
// 2005-10-06
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/

function cmnInformation(){
        var eHTML = document.getElementsByTagName( 'html' )[0];
        this.sUser_agent = navigator.userAgent.toLowerCase();
        if( window.opera ){
                this.bOpera = true; cmnSet_class( eHTML, 'Opera');
        }
        if( !this.bOpera && this.sUser_agent.indexOf( 'msie' ) != -1 ){
                this.bIE = true; cmnSet_class( eHTML, 'IE');
        }else if( navigator.product == 'Gecko' ){
                this.bGecko = true; this.bMozilla = true; cmnSet_class( eHTML, 'Mozilla');
        }
        this.bMAC = ( this.sUser_agent.indexOf( 'mac' ) != -1 );
        this.sLanguage = null;
        this.bHTTP = null;
        this.bHandheld = false;
        return this;
}

function cmnPlaceholder( eThis, sText, sClass_on_empty ){
        eThis.onfocus = function(){ if( eThis.value.length && eThis.value == sText ){ eThis.value = ''; } cmnRemove_class( this, sClass_on_empty ); eThis.select(); }
        eThis.onblur = function(){ if( !this.value.length ){ cmnSet_class( this, sClass_on_empty ); this.value = sText; } }
        if( !eThis.value.length ){ eThis.onblur(); }
        if( !eThis.getAttribute( 'placeholder' ) ){ eThis.setAttribute( 'placeholder', sText ); }
}

var cmn_aPopup_defaults = [
        ["width", 540],
        ["height", 600],
        ["location", "no"],
        ["menubar", "no"],
        ["toolbar", "no"],
        ["resizable", "yes"],
        ["scrollbars", "yes"],
        ["status", "yes"]
]
var cmn_aPopup = new Array();
function cmnPopup( sURL, sName, sFeatures, bReplace ){
        var sTarget = sName;
        if( !sURL.length ){
                if( sURL && sURL.tagName.toLowerCase() == "a" ){
                        if( !sName ){ sName = ( sURL.id ) ? sURL.id : "_blank"; }
                        sURL = sURL.href;
                }else{
                        return false;
                }
        }else if( !sName ){
                sName = "_blank";
        }
        for( var i = 0 ; i < cmn_aPopup_defaults.length ; i++ ){
                if( !cmnPairs_string_get_value( sFeatures, cmn_aPopup_defaults[i][0], "=", "," ) ){
                        sFeatures = cmnPairs_string_set_value( sFeatures, cmn_aPopup_defaults[i][0], cmn_aPopup_defaults[i][1], "=", "," );
                }
        }

        var iWidth = cmnPairs_string_get_value( sFeatures, "width", "=", "," );
        var iHeight = cmnPairs_string_get_value( sFeatures, "height", "=", "," );
        if( screen ){
                var iScreen_height = screen.height ? screen.height - 150 : 0;
                var iScreen_width = screen.width ? screen.width - 100 : 0;
                var bScroll = false;
                if( iScreen_height < iHeight ){ bScroll = true; iHeight = iScreen_height; sFeatures = cmnPairs_string_set_value( sFeatures, "height", iHeight, "=", "," ); }
                if( iScreen_width < iWidth ){ bScroll = true; iWidth = iScreen_width; sFeatures = cmnPairs_string_set_value( sFeatures, "width", iWidth, "=", "," ); }
                if( bScroll ) {
                        sFeatures = cmnPairs_string_set_value( sFeatures, "scrollbars", "yes", "=", "," );
                }
                sFeatures = cmnPairs_string_set_value( sFeatures, "top", Math.round( ( iScreen_height - iHeight ) / 2 ), "=", "," );
                sFeatures = cmnPairs_string_set_value( sFeatures, "left", Math.round( ( iScreen_width - iWidth ) / 2 ), "=", "," );
        }

        if( sURL.match(/\.(gif|jpe?g|png)$/i) ){
                cmn_aPopup[sName] = window.open( "", sName, sFeatures );
                if( cmn_aPopup[sName] ){
                        var sTitle = unescape( cmnPairs_string_get_value( sFeatures, "title", "=", "," ) );
                        sTitle = sTitle.replace( /<\/?\w[^>]*>/g, " " ).replace( /</g, "&lt;" ).replace( /</g, "&lt;" ).replace( /"/g, "&quot;" );
                        cmn_aPopup[sName].document.open();
                        cmn_aPopup[sName].document.write('<html><head><title>'
                                + sTitle
                                + '</title></head><body bgcolor="white" style="margin: 0px; padding: 0px;"><table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%"><tr><td align="center"><img src="'
                                + sURL + '" alt="'
                                + sTitle + '" /></td></tr></table></body></html>');
                        cmn_aPopup[sName].document.close();
                }
        }else{
                var bWas_open = false;
                try{ bWas_open = ( cmn_aPopup[sName] && cmn_aPopup[sName].location.href != sURL ) }catch(e){}
                if( !cmn_aPopup[sName] || cmn_aPopup[sName].closed || !bWas_open ){
                        if( sTarget && sTarget.match( /^_/ ) ){
                                cmn_aPopup[sName] = window.open( sURL, sTarget );
                        }else{
                                cmn_aPopup[sName] = window.open( sURL, sName, sFeatures, bReplace );
                        }
                }
        }
        if( sName != "_blank" ){
                cmn_aPopup[sName].focus();
        }

        return false;
}

var cmn_oInformation = new cmnInformation();

function cmnInit_Information(){
        cmn_oInformation.sLanguage = ( document.body && document.body.getAttribute( "lang" ) != "" ) ? document.body.getAttribute( "lang" ) : "ru";
        cmn_oInformation.bHTTP = ( document.location.href.indexOf("http://") == 0 ) ? true : false;
}

cmnAdd_event( window, "load", cmnInit_Information );

function cmnFlash(){
        var aeFlash = new Array();
        this.build = function( vEnabled, vDisabled, vNone, iVersion  ){
                var vVersion = this.get_version( iVersion );
                if( vVersion.toString() != "false" ){
                        this.enable();
                        vVersion = this.get_version( iVersion );
                }
                if( vVersion ){
                        if( vEnabled.nodeType ) vEnabled.style.display = "block";
                        else document.write( build_object(vEnabled) );
                        if( vDisabled.nodeType ) vDisabled.style.display = "none";
                        if( vNone.nodeType ) vNone.style.display = "none";
                }else{
                        if( vEnabled.nodeType ) vEnabled.style.display = "none";
                        if( vDisabled.nodeType ) vDisabled.style.display = "block";
                        else if( vDisabled ) document.write( vDisabled );
                        if( parseInt(vVersion) == 0 );
                                if( vNone.nodeType ) vNone.style.display = "block";
                                else if( vNone ) document.write( vNone );
                }
        }
        this.get_version = function( iVersion ){
                var vVersion = cmnGet_cookie( "flash" );
                if( !vVersion ){
                        vVersion = check_flash_version( iVersion );
                        set_flash_cookie( vVersion );
                        return vVersion;
                }else if( iVersion && iVersion > vVersion ){
                        vVersion = false;
                }
                return ( vVersion == "false" ) ?  false : vVersion;
        }
        this.disable = function(){
                set_flash_disabled( true );
        }
        this.enable = function(){
                set_flash_disabled( false );
        }
        this.set_disabled = function( bDisabled ){
                set_flash_disabled( bDisabled );
        }

        return this;

        function build_object( sOptions ){
                var asOptions = sOptions.split( "," );
                for( var i = 0 ; i < asOptions.length ; i++){
                        asPair = asOptions[i].split( "=" );
                        asOptions[asPair[0]] = unescape( asPair[1] );
                }
                var sHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
                sHTML += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' + 'width="' + asOptions.width + '" height="' + asOptions.height + '" align="middle">' + '<param name="allowScriptAccess" value="sameDomain">' + '<param name="movie" value="' + asOptions.source + '">' + '<param name="quality" value="high">';
                if( asOptions.base ){ sHTML += '<param name="base" value="' + asOptions.base + '">'; }
                if( asOptions.background ){ sHTML += '<param name="bgcolor" value="' + asOptions.background + '">'; }
                if( asOptions.transparent ){ sHTML += '<param name="wmode" value="transparent">' }
                if( asOptions.flashvars ){ sHTML += '<param name="flashvars" value="' + asOptions.flashvars + '">'; }
                sHTML += '<embed quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"' + ' pluginspage="http://www.macromedia.com/go/getflashplayer"' + ' src="' + asOptions.source + '" width="' + asOptions.width + '" height="' + asOptions.height + '"';
                if( asOptions.base ){ sHTML += ' base="' + asOptions.base + '"'; }
                if( asOptions.background ){ sHTML += ' bgcolor="' + asOptions.background + '"'; }
                if( asOptions.transparent ){ sHTML += ' wmode="transparent"' }
                if( asOptions.flashvars ){ sHTML += ' flashvars="' + asOptions.flashvars + '"'; }
                sHTML += '></embed></object>';
                return sHTML;
        }
        function set_flash_disabled( bDisabled ){
                set_flash_cookie( ( bDisabled ) ?  "false" : "" );
        }
        function check_flash_version( iVersion ){
                if( navigator.plugins && navigator.plugins['Shockwave Flash'] ){
                        var sVersion = ( !iVersion ) ? "\\d+" : "[" + iVersion + "-9]\\d*";
                        sVersion = parseInt( navigator.plugins['Shockwave Flash'].description.replace( new RegExp( "^[^\\d]+(" + sVersion + ").*$"), "$1" ) );
                        return ( isNaN( sVersion ) ) ? ( !iVersion ) ? false : 0 : sVersion;
                }else if( cmn_oInformation.bIE && !cmn_oInformation.bMAC ){
                        var oTemp, iMax = 20;
                        iVersion = ( !iVersion ) ? 3 : iVersion;
                        for( var i = iVersion ; i < iMax ; i++ ){
                                try{
                                        oTemp = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i );
                                        iVersion = i;
                                }catch(e){
                                        return ( i > iVersion ) ? iVersion : 0;
                                }
                        }
                        return iVersion;
                }else{
                        return ( !iVersion ) ? false : 0;
                }
        }
        function set_flash_cookie( sCookie_value ){
                var dNow = new Date();
                var dExpires = new Date();
                dExpires.setTime( dNow.getTime() + 1000 * 60 * 60 * 24 * 365 );
                cmnSet_cookie( "flash", ( !sCookie_value ) ? "" : sCookie_value, dExpires );
        }
}

function cmnSet_cookie( sName, vValue, dExpires ){
   document.cookie = sName + "=" + escape( vValue )
           + ( dExpires == null ? "" : ( "; expires=" + dExpires.toGMTString() ) )
           + "; path=/";
}

function cmnGet_cookie( sName ){
        return cmnPairs_string_get_value( document.cookie, sName );
}

function cmnSwitch_class( eOn, sClass_name, sInstead ){
        if( cmnMatch_class( eOn, sClass_name ) ){
                cmnSet_class( eOn, sInstead, sClass_name );
        }else{
                cmnSet_class( eOn, sClass_name, sInstead );
        }
}

function cmnRemove_class( eOn, sClass_name ){
        cmnSet_class( eOn, "", sClass_name );
}

function cmnSet_class( eOn, sClass_name, sInstead ){
        if( eOn ){
                sClass_name = ( sClass_name.length ) ? sClass_name.replace( /(^\s+|\s+$)/, "" ) : "";
                if( eOn.className.length ){
                        var sOld = sClass_name;
                        if( sInstead && sInstead.length ){
                                sInstead = sInstead.replace( /\s+(\S)/g, "|$1" );
                                if( sOld ){
                                        sOld += "|";
                                }
                                sOld += sInstead;
                        }
                        eOn.className = eOn.className.replace( new RegExp("(^|\\s+)(" + sOld +")($|\\s+)", "g"), "$1" );
                }
                eOn.className += ( eOn.className.length && sClass_name ? " " : "" ) + sClass_name;
        }
}

function cmnMatch_class( eOn, sClass_name ){
        return ( sClass_name && eOn.className && eOn.className.length && eOn.className.match( new RegExp("(^|\\s+)(" + sClass_name +")($|\\s+)") ) );
}

function cmnPairs_string_get_value( sText, sName, sFrom, sBefore ){
        var sValue = "";
        if( sText ){
                if( !sFrom ) sFrom = "=";
                if( !sBefore ) sBefore = ";";
                sText = sText.replace( new RegExp( "(" + sBefore + ")\\s+", "g" ), "$1" );
                var iStart = sText.indexOf( sBefore + sName + sFrom );
                if( iStart >= 0 ){
                        iStart += ( sBefore.length + sName.length + sFrom.length );
                }else{
                        iStart = sText.indexOf( sName + sFrom );
                        if( iStart == 0 ){
                                iStart += ( sName.length + sFrom.length );
                        }else{
                                iStart = -1;
                        }
                }
                if( iStart >= 0 ){
                        var iEnd = sText.indexOf( sBefore, iStart );
                        if( iEnd < 0 ){
                                iEnd = sText.length;
                        }
                        sValue = sText.substring( iStart, iEnd );
                }
        }
        return sValue;
}

function cmnPairs_string_set_value( sText, sName, sValue, sFrom, sBefore ){
        if( !sFrom ) sFrom = "=";
        if( !sBefore ) sBefore = ";";
        var iStart = -1;
        if( sText ){
                sText = sText.replace( new RegExp( "\\s+(" + sBefore + "|" + sFrom + ")\\s+", "g" ), "$1" );
                iStart = sText.indexOf( sBefore + sName + sFrom );
                if( iStart >= 0 ){
                        iStart += ( sBefore.length + sName.length + sFrom.length );
                }else{
                        iStart = sText.indexOf( sName + sFrom );
                        if( iStart == 0 ){
                                iStart += ( sName.length + sFrom.length );
                        }else{
                                iStart = -1;
                        }
                }
                if( iStart >= 0 ){
                        var iEnd = sText.indexOf( sBefore, iStart );
                        if( iEnd < 0 ){
                                iEnd = sText.length;
                        }
                        sText = sText.substring( 0, iStart ) + sValue + sText.substr( iEnd );
                }
        }
        if( iStart < 0 ){
                if( sText && sText.lastIndexOf( sBefore ) != ( sText.length - sBefore.length ) ){
                        sText += sBefore;
                }
                sText += sName + sFrom + sValue + sBefore;
        }
        return sText;
}

function cmnAdd_button_events( eOn ){
        cmnAdd_hover( eOn );
        cmnAdd_event( eOn, "mousedown", function(){ cmnSet_class( eOn, "mousedown" ) } );
        cmnAdd_event( eOn, "mouseup", function(){ cmnSwitch_class( eOn, "mousedown", "mouseup" ) } );
}

function cmnAdd_hover( eOn ){
        cmnAdd_event( eOn, "mouseover", function(){ cmnSet_class( eOn,    "hover" ) } );
        cmnAdd_event( eOn, "mouseout",  function(){ cmnRemove_class( eOn, "hover" ) } );
}

function cmnAdd_events( eOn, asEvent_type, ptrFunction ){
        for( var sName in asEvent_type ){
                cmnAdd_event(eOn, asEvent_type[sName], ptrFunction);
        }
}

function cmnAdd_event( eOn, sEvent_type, ptrFunction ){
        if( eOn.addEventListener ){  
                eOn.addEventListener( sEvent_type, ptrFunction, false );
        }else if(eOn.attachEvent){
                eOn.attachEvent( 'on' + sEvent_type, ptrFunction );
        }
}

function cmnRemove_event( eOn, sEvent_type, ptrFunction ){
        if( eOn.removeEventListener ){  
                eOn.removeEventListener( sEvent_type, ptrFunction, false );
        }else if( eOn.detachEvent ){
                eOn.detachEvent( 'on' + sEvent_type, ptrFunction );
        }
}

function cmnInit_tels(){
        var aeNOBR = document.getElementsByTagName( 'nobr' );
        for( var i = 0 ; i < aeNOBR.length ; i++)
                if( cmnMatch_class( aeNOBR[i], 'phone' ) )
                        aeNOBR[i].innerHTML = '<a href="tel:' + aeNOBR[i].innerHTML.replace( /(<\/?\w[^>]*>|[\s\,\.\;\-\(\)]+)/g, '' ) + '">' + aeNOBR[i].innerHTML + '</a>';
}

