var win = null;

function NewWindow(mypage,myname,w,h,scroll){
    if(win) { 
        win.close();
    }
    LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-h)/2 : 0;  
   settings = (h>0?'height='+h+',':'') + (w>0?'width='+w+',':'') + 'top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=1,menubar=1'
    win = window.open(mypage,myname,settings)
}
function SameWindow(url)
{
    location = url;
}

/*
 status  The status bar at the bottom of the window.
toolbar  The standard browser toolbar, with buttons such as Back and Forward.
location  The Location entry field where you enter the URL.
menubar  The menu bar of the window
directories  The standard browser directory buttons, such as What's New and What's Cool
resizable Allow/Disallow the user to resize the window.
scrollbars  Enable the scrollbars if the document is bigger than the window
height Specifies the height of the window in pixels. (example: height='350')
width  Specifies the width of the window in pixels.

 */

function PhotoDescriptionWindow(filePath)
{
    if(win) {
        win.close();
    }
    LeftPosition = (screen.width) ? (screen.width-450)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-400)/2 : 0;
    settings =
    'height=400,width=450,top='+TopPosition+',left='+LeftPosition+',scrollbars=0,resizable=0,status=0,toolbar=0,menubar=0';
    win = window.open('/public/photo_description_form.php?file=' + filePath,'',settings)
}

function DeleteAlert(message,returnUrl)
{
    if(confirm(message))
    {
        location = returnUrl;
    }
    return true;
}
function ChangeScreen(index,len)
{
    for(var x=0;x<len;x++)
    {
        document.getElementById('screen'+x).style.display='none';
        document.getElementById('tab'+x).style.fontWeight='normal';
    }
    document.getElementById('screen'+index).style.display='block';
    document.getElementById('tab'+index).style.fontWeight='bold';
    return false;
}

function FilterView(returnUrl)
{
    var filter = document.getElementById('FilterSelect').options[document.getElementById('FilterSelect').selectedIndex].value;
    location = returnUrl + '&show=' + filter;
}

function SwitchCalendar(direction,filter, tab)
{
    var currentUrl = location.href;
    var l = currentUrl.indexOf('?');
    if(l > -1)
    {
        currentUrl=currentUrl.substr(0,l);
    }
    var cal = calendarMonth;
    if(direction==-1)
    {
        cal = lastMonth;
    }
    else if(direction==1)
    {
        cal = nextMonth;
    }
    location = currentUrl + '?show=' + filter + '&tab=' + tab + '&cal=' + cal;
}

function RemoveAnchors(htmlString)
{
    var outString = htmlString.replace(/<A.*?>/g,'').replace(/<\/A>/g,'').replace(/<a.*?>/g,'').replace(/<\/a>/g,'');
    return outString;
}

function ValidateDate(field)
{
    if(field.value.length > 0)
    {
        if (!(field.value.match(/(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/)))
        {
            alert('Date is not formatted correctly\nThe correct format is "mm/dd/yyyy".');
            field.focus();
            return false;
        }
    }
    return true;
}

function  ValidateMonthYear(fileName)
{
    var monthArray = new Array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
    for(var x=0;x<12;x++)
    {
        if(fileName.indexOf('_' + monthArray[x] + '_') > -1)
        {
            if (fileName.match(/(19|20)\d\d/))
            //            var y = fileName.indexOf('20');
            //           if((y+4) < fileName.length && isFinite(fileName.substr(y,4)))
            {
                return true;
            }
        }
    }
    return false;
}

function ValidatePhone(field)
{
    if(field.value.length > 0)
    {
        var phone=field.value.replace(/[\.\(\)\/\-]/g,'');
        field.value=phone;
        if(phone.length < 10)
        {
            alert('Phone number is incorrect.');
            field.focus();
            return false;
        }
    }
    return true;
}
function ValidateMemberPhotoName(memberPhotoFileName)
{
    if(memberPhotoFileName.indexOf('_')>-1 && memberPhotoFileName.indexOf('.jpg') > -1)
    {
        return true;
    }
    alert('Member Photo Name is not formatted correctly\n"lastname_firstname.jpg" lowercase only.');
    return false;
}

function ValidateNewsletterName(newsletterFileName)
{
    if(newsletterFileName.indexOf('newsletter_')>-1 && (newsletterFileName.indexOf('.pdf') > -1 || newsletterFileName.indexOf('.doc') > -1))
    {
        if(ValidateMonthYear(newsletterFileName)){
            return true;
        }
    }
    alert('Newsletter Name is not formatted correctly\n"newsletter_mmm_yyyy.(pdf | doc)" lowercase only.');
    return false;
}

function ValidatePhotoName(photoFileName)
{
    if(photoFileName.indexOf('.jpg') > -1)
    {
        return true;
    }
    alert('Photo Name is not formatted correctly\n"photo_name.jpg" lowercase only.');
    return false;
}

function ValidateFileName(fileName)
{
    if(fileName.indexOf('.pdf') > -1 || fileName.indexOf('.doc') > -1|| fileName.indexOf('.zip') > -1 || fileName.indexOf('.txt') > -1 || fileName.indexOf('.xml') > -1 || fileName.indexOf('.gif') > -1 || fileName.indexOf('.jpg') > -1)
    {
        return true;
    // if(ValidateMonthYear(fileName)){return true;};
    }
    alert('File Name is not formatted correctly\n"file_name.(pdf | doc | zip | txt | xml | gif | jpg)" lowercase only.');
    return false;
}
