
var EMAIL_SIGN = "@";

/*=====================================*/
/*      CALENDAR                       */
/*=====================================*/

function calendarInit(dayInputId, monthInputId, yearInputId)
{
    calendarInit(dayInputId, monthInputId, yearInputId, false);
}

function calendarInit(dayInputId, monthInputId, yearInputId, showEmpty)
{
    var dayInput = document.getElementById(dayInputId);
    var monthInput = document.getElementById(monthInputId);
    var yearInput = document.getElementById(yearInputId);
    
    var curDate = new Date();
    
    var startIndex = (showEmpty) ? 1 : 0;
    var monthIndex = startIndex;
    
     if(monthInput.options.length == startIndex)
     {
        monthInput.options[monthIndex++] = new Option('Январь', 1);
        monthInput.options[monthIndex++] = new Option('Февраль', 2);
        monthInput.options[monthIndex++] = new Option('Март', 3);
        monthInput.options[monthIndex++] = new Option('Апрель', 4);
        monthInput.options[monthIndex++] = new Option('Май', 5);
        monthInput.options[monthIndex++] = new Option('Июнь', 6);
        monthInput.options[monthIndex++] = new Option('Июль', 7);
        monthInput.options[monthIndex++] = new Option('Август', 8);
        monthInput.options[monthIndex++] = new Option('Сентябрь', 9);
        monthInput.options[monthIndex++] = new Option('Октябрь', 10);
        monthInput.options[monthIndex++] = new Option('Ноябрь', 11);
        monthInput.options[monthIndex++] = new Option('Декабрь', 12);
    }
    
    if(startIndex > 0)
        setStyleForOptions(monthInput);
    
    var curMonth = curDate.getMonth(); 
    //monthInput.selectedIndex = curMonth; 
    
    var year = curDate.getYear();
    if (year < 1900) { year+=1900; } 
    var yearIndex = startIndex;
    if(yearInput.options.length == startIndex)
    {
        yearInput.options[yearIndex++] = new Option(year, year);
        yearInput.options[yearIndex++] = new Option(year+1, year+1);
   }
   
   if(startIndex > 0)
       setStyleForOptions(yearInput);

    var monthDayCount = new Array(31,((curDate.getFullYear() - 2000) % 4 ? 28 : 29),31,30,31,30,31,31,30,31,30,31);
    
    //for(var j = 1; j <= monthDayCount[curMonth]; j++)
    for(var j = 1; j <= 31; j++)
    {
        dayInput.options[j - 1 + startIndex] = new Option(j, j);
    }
    //dayInput.selectedIndex = curDate.getDay(); 
    
    if(startIndex > 0)
        setStyleForOptions(dayInput);
}

/* It is implemented only for month 
 * month - number of month which will be added to current date
*/
function calendarSetDefaultDate(dayInputId, monthInputId, yearInputId, month)
{
    var dayInput = document.getElementById(dayInputId);
    var monthInput = document.getElementById(monthInputId);
    var yearInput = document.getElementById(yearInputId);
    
    var curDate = new Date();
    var nMonth = curDate.getMonth() + month; 
    //var nYear = curDate.getYear(); 
    //if (nYear < 1900) { nYear+=1900; } 
    var nYear = 0;
    if(nMonth > 11)
    {
        nYear += nMonth / 12;
        nMonth = nMonth % 12;
    }
    
    var startIndex = (monthInput.options[0].value == 1) ? 0 : 1; 
    dayInput.selectedIndex = curDate.getDate() - 1 + startIndex; 
    monthInput.selectedIndex = nMonth + startIndex;
    yearInput.selectedIndex = nYear + startIndex;
}

function calendarSetDate(dayInputId, monthInputId, yearInputId, day, month, year)
{
    var dayInput = document.getElementById(dayInputId);
    var monthInput = document.getElementById(monthInputId);
    var yearInput = document.getElementById(yearInputId);
    
    var startIndex = (monthInput.options[0].value == 1) ? 0 : 1; 
    dayInput.selectedIndex = day - 1 + startIndex; 
    monthInput.selectedIndex = month -1 + startIndex;
    yearInput.selectedIndex = year - yearInput.options[startIndex].value + startIndex;
}

function calendarMonthChanged(dayInputId, monthInputId, yearInputId)
{

    var dayInput = document.getElementById(dayInputId);
    var monthInput = document.getElementById(monthInputId);
    var yearInput = document.getElementById(yearInputId);
    
    var startIndex = (monthInput.options[0].value == 1) ? 0 : 1; 

    var monthDayCount = new Array(31,((yearInput.value - 2000) % 4 ? 28 : 29),31,30,31,30,31,31,30,31,30,31);
    var selectedMonth = monthInput.value - 1;
    var selectedDay = (dayInput.value >  monthDayCount[selectedMonth]) ? monthDayCount[selectedMonth] : dayInput.value;
    selectedDay = (selectedDay >= 0) ? selectedDay : 0;
    
    if(monthDayCount[selectedMonth] > dayInput.options.length - startIndex)
    {
        for(var j = dayInput.options.length - startIndex; j <= monthDayCount[selectedMonth]; j++)
        {
            dayInput.options[j-1] = new Option(j, j);
            setStyleForOption(dayInput.options[j-1]);
        }
    }
    else
    {
        for(var k = dayInput.options.length-1; k >= monthDayCount[selectedMonth] + startIndex; k--)
        {
            dayInput.options.remove(k);
        }
    }
    dayInput.selectedIndex = selectedDay - 1 + startIndex;
}

function calendarYearChanged(dayInputId, monthInputId, yearInputId)
{
    var monthInput = document.getElementById(monthInputId);
    if(monthInput.value == 2)
    {
        var dayInput = document.getElementById(dayInputId);
        var yearInput = document.getElementById(yearInputId);
        
        var startIndex = (monthInput.options[0].value == 1) ? 0 : 1; 
        var monthDayCount = (yearInput.value - 2000) % 4 ? 28 : 29;
        if(dayInput.options.length - startIndex > monthDayCount)
        {
            if(dayInput.value == 29)
                dayInput.selectedIndex = 27 + startIndex;
            dayInput.options.remove(28 + startIndex);
        }
        else if(dayInput.options.length - startIndex < monthDayCount)
        {
            dayInput.options[28 + startIndex] = new Option(29, 29);
            setStyleForOption(dayInput.options[28 + startIndex]);
        }
    }
}


/*=====================================*/


/*=====================================*/
/*===== DROP DOWN TOOL TIP     ========*/
/*=====================================*/

function setStyleForOptions(dropdown)
{
    for(var i = 1; i < dropdown.options.length; i++)
    {
        setStyleForOption(dropdown.options[i]);
    }
}

function setStyleForOption(option)
{
    option.style.color='black';
    //option.style.fontStyle='normal';
}

function hideDropDownToolTip(obj)
{
    if(obj.options.length > 1 && obj.className == 'dropDownToolTip')
    {
        //obj.className = '';
        //obj.style.color='black';
        //obj.style.fontStyle ='normal';
        //obj.remove(0);
        //obj.click();
        //obj.onmousedown();
    }
}

function setDropDownToolTipStyle(obj)
{
    if(obj.selectedIndex == 0)
    {
        //obj.className = 'dropDownToolTip';
        obj.style.color='gray';
        //obj.style.fontStyle ='italic';
    }
    else
    {
        obj.style.color='black';
        //obj.style.fontStyle ='normal';
    }
}

/*=====================================*/

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

var userAgent = navigator.userAgent.toLowerCase();

function isClienChrome()
{
    return /chrome/.test(userAgent);
}

function isClientIE()
{
    return /msie/.test( userAgent ) && !/opera/.test( userAgent );
}

//help window
var newWin = null;
function help(strURL, strHeight, strWidth) {

    strHeight = strHeight || 400;
    strWidth = strWidth || 400;

    if (newWin != null && !newWin.closed)
        newWin.close();
   var strOptions="";
        strOptions="toolbar=yes,menubar=no,scrollbars=yes,"+
        "resizable=yes,location=no,height="+
        strHeight+",width="+strWidth;
        newWin = window.open(strURL, 'newWin', strOptions);
        if (window.focus) {newWin.focus()}
            return false;
}


