var dtCh= "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s)
{
    var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")))
            return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
    var i;
    var returnString = "";
    // Search through strings characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1)
            returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n)
{
    for (var i = 1; i <= n; i++)
    {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11)
            {this[i] = 30};
        if (i==2)
            {this[i] = 29};
    } 
    return this
}

function isDate(dtStr)
{
    var daysInMonth = DaysArray(12);
    var pos1 = dtStr.indexOf(dtCh);
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
    var strMonth = dtStr.substring(0, pos1);
    var strDay = dtStr.substring(pos1 + 1, pos2);
    var strYear = dtStr.substring(pos2 + 1);
    strYr = strYear;
    if (strDay.charAt(0) == "0" && strDay.length > 1)
        strDay = strDay.substring(1);
    if (strMonth.charAt(0) == "0" && strMonth.length > 1)
        strMonth = strMonth.substring(1);
    for (var i = 1; i <= 3; i++)
    {
        if (strYr.charAt(0) == "0" && strYr.length > 1)
            strYr = strYr.substring(1);
    }
    month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYr);
    if (pos1 == -1 || pos2 == -1)
    {
        // alert("The date format should be : mm/dd/yyyy");
        return false;
	}
    if (strMonth.length < 1 || month < 1 || month > 12)
    {
        // alert("Please enter a valid month");
        return false;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
    {
        // alert("Please enter a valid day");
        return false;
	}
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
    {
        // alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear);
        return false;
	}
    if (dtStr.indexOf(dtCh,pos2+1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
    {
        // alert("Please enter a valid date");
        return false;
    }
    return true;
}

function DoingValidateDate(source, arguments)
{
    var edtBirthdayDay = GetDropDownValue(BirthdayDay, '');
    var edtBirthdayMonth = GetDropDownValue(BirthdayMonth, '');
    var edtBirthdayYear = GetDropDownValue(BirthdayYear, '');
    arguments.IsValid = (edtBirthdayDay == '-' && edtBirthdayMonth == '-' && edtBirthdayYear == '-') || isDate(edtBirthdayMonth + '/' + edtBirthdayDay + '/' + edtBirthdayYear);
}

function DoingValidateCountry(source, arguments)
{
    var edtCountryList = GetDropDownValue(CountryList, '');
    arguments.IsValid = edtCountryList != '0';
}

function DoingValidateTerms(source, arguments)
{
    var chkTerms = GetCheckboxValue(Terms);
    arguments.IsValid = chkTerms == '1';
}

function DoingValidateSecurity(source, arguments)
{
    var chkSecurity = GetCheckboxValue(Security);
    arguments.IsValid = chkSecurity == '1';
}

function AddNewFriend(TextField, FriendField, ReleaseControlPrefix)
{
    var Question = GetInputValue(TextField);
    if (Question == '')
        ShowHideObject('RequiredFriendMessage', 'block');
    else
    {
        var FriendID = GetInputValue(FriendField);
        var Releases = BuildRelease(ReleaseControlPrefix);
        ExecuteInfoPopup('entrynewfriend', FriendID, Question, Releases, '');
    }
}

function ToggleProfilRightGroup(Button, GroupName)
{
    var ImageBase = '/images/button/';
    var Container = GetElement(GroupName);
    if (Button != null && Container != null)
    {
        if (Container.style.display == 'block')
        {
            Container.style.display = 'none';
            Button.src = ImageBase + 'ButtonExpand.jpg';
        }
        else
        {
            Container.style.display = 'block';
            Button.src = ImageBase + 'ButtonClose.jpg';
        }
    }
}

function SetCheckBoxValue(ControlPrefix, CheckName, ValueCheck)
{
    var CheckField = GetElement(ControlPrefix + '_' + CheckName);
    if (CheckField != null)
        CheckField.checked = ValueCheck.checked;
}

function CheckAllRights(Todo, ControlPrefix)
{
    var SelectedCheck = '';
    switch (Todo)
    {
        case 'addresses':
            SelectedCheck = 'chkAddressAll';
            break;
        case 'phones':
            SelectedCheck = 'chkPhonesAll';
            break;
        case 'internet':
            SelectedCheck = 'chkInternetAll';
            break;
        case 'messaging':
            SelectedCheck = 'chkMessagingAll';
            break;
    }
    if (SelectedCheck != '')
    {
        var SelectedValue = GetElement(ControlPrefix + '_' + SelectedCheck);
        if (SelectedValue != null)
        {
            switch (Todo)
            {
                case 'addresses':
                    SetCheckBoxValue(ControlPrefix, 'chkAddress', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkZIP', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkCity', SelectedValue);
                    break;
                case 'phones':
                    SetCheckBoxValue(ControlPrefix, 'chkTelephone', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkMobilphone', SelectedValue);
                    break;
                case 'internet':
                    SetCheckBoxValue(ControlPrefix, 'chkEMail', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkHomepage', SelectedValue);
                    break;
                case 'messaging':
                    SetCheckBoxValue(ControlPrefix, 'chkICQ', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkSkype', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkMSN', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkAIM', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkYahoo', SelectedValue);
                    SetCheckBoxValue(ControlPrefix, 'chkJabber', SelectedValue);
                    break;
            }
        }       
    }
}

function AddToParameters(ParamStr, Value)
{
    if (ParamStr != '')
        ParamStr = ParamStr + '~';
    ParamStr = ParamStr + Value;
    
    return ParamStr;
}

function BuildRelease(ControlPrefix)
{
    var ParamStr = '';

    ParamStr = AddToParameters(ParamStr, 'realname=' + GetCheckboxValue(ControlPrefix + '_chkRealname'));
    ParamStr = AddToParameters(ParamStr, 'address=' + GetCheckboxValue(ControlPrefix + '_chkAddress'));
    ParamStr = AddToParameters(ParamStr, 'zip=' + GetCheckboxValue(ControlPrefix + '_chkZIP'));
    ParamStr = AddToParameters(ParamStr, 'city=' + GetCheckboxValue(ControlPrefix + '_chkCity'));
    ParamStr = AddToParameters(ParamStr, 'birthday=' + GetCheckboxValue(ControlPrefix + '_chkBirthday'));
    ParamStr = AddToParameters(ParamStr, 'telephone=' + GetCheckboxValue(ControlPrefix + '_chkTelephone'));
    ParamStr = AddToParameters(ParamStr, 'mobilephone=' + GetCheckboxValue(ControlPrefix + '_chkMobilphone'));
    ParamStr = AddToParameters(ParamStr, 'email=' + GetCheckboxValue(ControlPrefix + '_chkEMail'));
    ParamStr = AddToParameters(ParamStr, 'homepage=' + GetCheckboxValue(ControlPrefix + '_chkHomepage'));
    ParamStr = AddToParameters(ParamStr, 'icq=' + GetCheckboxValue(ControlPrefix + '_chkICQ'));
    ParamStr = AddToParameters(ParamStr, 'skype=' + GetCheckboxValue(ControlPrefix + '_chkSkype'));
    ParamStr = AddToParameters(ParamStr, 'msn=' + GetCheckboxValue(ControlPrefix + '_chkMSN'));
    ParamStr = AddToParameters(ParamStr, 'aim=' + GetCheckboxValue(ControlPrefix + '_chkAIM'));
    ParamStr = AddToParameters(ParamStr, 'yahoo=' + GetCheckboxValue(ControlPrefix + '_chkYahoo'));
    ParamStr = AddToParameters(ParamStr, 'jabber=' + GetCheckboxValue(ControlPrefix + '_chkJabber'));
    
    return ParamStr;
}

function ReleaseNewFriend(TextField, ReleaseControlPrefix)
{
    var Answer = GetInputValue(TextField);
    var FriendID = GetInputValue(ReleaseControlPrefix + '_FriendName');
    var FriendRights = BuildRelease(ReleaseControlPrefix);
    ExecuteInfoPopup('acceptnewfriend', FriendID, FriendRights, Answer); 
}

function RefreshLists(RefreshApproved, RefreshNotApproved, RefreshInquiry, ApprovedGrid, NotApprovedGrid, InquiryGrid)
{
    if (RefreshApproved)
        ApprovedGrid.callback();
    if (RefreshNotApproved)
        NotApprovedGrid.callback();
    if (RefreshInquiry)
        InquiryGrid.callback();
}

function HandleTheFriend(Todo, SEOName, Callback)
{
    Callback.Callback(Todo, SEOName);
}