// Created: 30/12/2009 Adrianw
// Last updated: 31/01/2010 Adrianw
// Load all button images for main navigation into client cache for smooth display
function preload_buttons()
{
    var button_pics = new Array("index","contact","resources", "what_is_smart","when_and_where","index_sel","contact_sel","resources_sel", "what_is_smart_sel","when_and_where_sel","index_hov","contact_hov","resources_hov", "what_is_smart_hov","when_and_where_hov");
    var preload = new Array();
    for(var imgld = 0; imgld < button_pics.length; imgld++ )
    {
        preload[ imgld ] = new Image();
        preload[ imgld ].src = 'images/buttons/btn_' + button_pics[ imgld ] + '.png';
    }
}

// Select which button to dislay on main navigation depending on whether selected, background or hovered over
function imgswap(imgname, n, thispage)
{
    var imgstat = 'images/buttons/btn_';
    
    switch(n){
        case 0:
            if (imgname == thispage){
                imgstat = imgstat + imgname + '_sel.png';
            }
            else
            {
                imgstat = imgstat + imgname + '.png';
            }
            break;
        case 1:
            imgstat = imgstat + imgname + '_hov.png';
            break;
    }
    document.images(imgname).src = imgstat;
}

// Build an email address to hopefully avoid robot spam
function mail_address(strMsg)
{
    var ad1 = "mailto:";
    var ad2 = "enquiries";
    var ad3 = "@";
    var ad4 = "smartkendal";
    var ad5 = ".org.uk";
    document.write('<a href="' + ad1 + ad2 + ad3 + ad4 + ad5 + '">' + strMsg + '</a>');
}

// Client side form validation for contact page prior to submission
function validate_contact(thisForm){
    with (thisForm){
        // Validate name
        if (validate_field(name)==false){
            name.focus();
            name.select();
            return false;
        }
        // Validate phone number
        if (validate_field(telno)==false){
            telno.focus();
            telno.select();
            return false;
        }
        // Validate the area
        var areaOption = -1;
        for (i=0; i<area.length; i++){
            if (area[i].checked){
                areaOption = i;
            }
        }
        if (areaOption == -1){
            alert('Please select an area (Barrow in Furness, Kendal or Neither at this stage)');
            return false;            
        }
    }
    // Display the form contents to the user prior to submission
    if (confirmcontents(thisForm)) return true; else return false;
}

// Validate form content using regular expressions
function validate_field(field){
    var strMsg;
    var re;
    
    switch(field.name){
        case "name":
            re = /^([a-zA-Z]{3,})+[a-zA-Z-\s\']*$/
            strMsg = 'contact name. It must be at least 3 characters and can only contain characters, spaces, hyphen and apostrophe.';
            break;
        case "telno":
            re = /^[0]+([0-9]{10,25})+$/
            strMsg = 'telephone number. It must be digits only (no spaces) and include the STD.';
            break;
        default:
        
    }
    with (field){
        if (re.test(value)){
            return true;
        }
        else{
            alert('Please provide us with a valid ' + strMsg);
            return false;            
        }
    }
}

// Display the form contents to the user
function confirmcontents(thisForm){
    var strMsg = 'Please confirm the data you wish to submit:\n\n';
    with (thisForm){
        with (name){
            if (value != ''||value !=null){
                strMsg += 'Contact name: ' + value + '\n\n'; 
            }
        }
        with (telno){
            if (value != ''||value !=null){
                strMsg += 'Contact number: ' + value + '\n\n';
            }
        }
        with (area){
            if (value !=''||value !=null){
                strMsg += 'Area: ' + value + '\n\n';
            }
        }
        with (cont_time){
            if (value !=''||value !=null){
                strMsg += 'Contact time: ' + value + '\n\n';
            }        
        }
        with (notes){
            if (value !=''||value !=null){
                strMsg += 'Notes: ' + value + '\n';
            }        
        }
    }
    var ask = window.confirm(strMsg);
    if(ask){
        return true;
    }
    else {
        return false;
    }
}

// Delay following msg display then redirect to home page
function redir_home(){
    // Delay prior to redirect to home page of 7 seconds (7000 milliseconds)
    setTimeout("window.location=\"index.php\"",7000);
}
