function clearField( element )
{
    if ( !$(element).hasClass('cleared') )
    {
        element.addClass('cleared');
        element.set('value','');
    }
}

function sendPassword( element )
{
    var form = $(element).getParent('form');
    var email = form.elements['Email'].value;
    if ( !email )
    {
        alert( "Please type your email address into the field above.\n\nNote : if you have multiple email addresses, only the one originally used for your guntrader account will work." );
    }
    else
    {
        location.href = "https://"+window.location.host+"/YourAccount/index.php?Action=EmailPassword&Email="+encodeURIComponent(email);
    }
    return( false );
}

function validPhone( phone )
{
    return( phone.match( /^\+?[\d -]+$/ ) );
}

function validEmail( email )
{
    return( email.match( /^([\w\-\+\.]+)@([\w\-\+\.]+\.[\w\-\+]+)$/ ) );
}

function validUrl( url )
{
    return( url.match( /^((http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/ ) );
}

function validPassword( password )
{
    return( true );
}

function validateLoginForm( form )
{
    var email = form.elements['Email'].value;
    if ( !validEmail( email ) )
    {
        alert( "Please enter a valid email address." );
        return( false );
    }
    var password = form.elements['Password'].value;
    if ( !(email && password) )
    {
        alert( "Both Email address and Password are required for login." );
        return( false );
    }
    return( true );
}

function submitLogin( form )
{
    //var form = element.getParent('form');
    if ( validateLoginForm( form ) )
    {
        form.submit();
    }
    return( false );
}

function submitLogout( form )
{
    //var form = element.getParent('form');
    form.submit();
    return( false );
}

function yourAccount()
{
    window.location = 'https://'+window.location.host+'/YourAccount/';
}

function helpPhone() {
    var win = window.open("/Shared/Help/HelpPhone.php","HelpPhone","resizable,width=330,height=250");
}

function viewGun( id )
{
    //var winUrl = '/GunsForSale/detail.php?NewGunID='+id;
    var winUrl = '/GunsForSale/'+id;
    window.location = winUrl;
    return( false );
    var winName = 'gun'+id;
    var winWidth = 800;
    var winHeight = 640;
    var newWin = window.open( winUrl, winName, 'resizable,scrollbars,width='+winWidth+',height='+winHeight );
    return( false );
}

function gunDetail( id )
{
    viewGun( id );
    return( false );
}

function gunLink( id )
{
    viewGun( id );
    return( false );
}

function viewEqp( id )
{
    var winUrl = '/EquipmentForSale/detail.php?EquipmentID='+id;
    window.location = winUrl;
    return( false );
    var winName = 'eqp'+id+'';
    var winWidth = 800;
    var winHeight = 640;
    var newWin = window.open( winUrl, winName, 'resizable,scrollbars,width='+winWidth+',height='+winHeight );
    return( false );
}

function newGunAdvert()
{
    window.location = 'https://'+window.location.host+'/YourAccount/SellingGuns/advert.php';
    exit;
    var Win = window.open(
        'https://'+window.location.host+'/YourAccount/SellingGuns/GunAdvert.php',
        'GunAdvert',
        'resizable,scrollbars,status,width=720,height=600'
    );
}

function gunAdvert( id )
{
    var url = '/YourAccount/SellingGuns/detail.php'
    if ( id )
        url += '?NewGunID='+id;
    var name = 'gunAd'+id;
    var width = 720;
    var height = 640;
    var win = window.open( url, name, 'resizable,scrollbars,width='+width+',height='+height );
    return( false );
}

function newEqpAdvert()
{
    window.location = 'https://'+window.location.host+'/YourAccount/SellingEquipment/advert.php';
    exit;
    var Win = window.open(
        'https://'+window.location.host+'/YourAccount/SellingEquipment/NewEquipmentAdvert.php',
        'EqpAdvert',
        'resizable,scrollbars,status,width=650,height=515'
    );
}

function eqpAdvert( id )
{
    var url = '/YourAccount/SellingEquipment/detail.php'
    if ( id )
        url += '?EquipmentID='+id;
    var name = 'eqpAd'+id;
    var width = 720;
    var height = 640;
    var win = window.open( url, name, 'resizable,scrollbars,width='+width+',height='+height );
    return( false );
}

function newWantedGun()
{
    var Win = window.open(
        'https://'+window.location.host+'/YourAccount/WantedGuns/NewGunAdvert.php',
        'WantedGunAdvert',
        'resizable,scrollbars,status,width=650,height=515'
    );
}

function dealerGuns( userId )
{
    var url = '/DealerSearch/dealerGuns.php?UserID='+userId;
    window.location = url;
    //var win = window.open( url, userId+'Guns', 'resizable,scrollbars,width=900,height=540' );
    return( false );
}

function dealerImages( userId )
{
    var url = '/DealerSearch/dealerImages.php?UserID='+userId;
    window.location = url;
    //var win = window.open( url, userId+'Images', 'resizable,scrollbars,width=560,height=465' );
    return( false );
}

function dealerWebsite( url )
{
    window.open( 'http://'+url );
    return( false );
}

function emailSeller( gunId )
{
    var url = '/Shared/EmailSeller.php?AdvertID='+gunId;
    //window.location = url;
    var win = window.open( url, 'gtEmailSeller', 'resizable,scrollbars,width=560,height=465' );
    return( false );
}

function closeWindow()
{
    window.close();
}

function closeWithReload()
{
    window.opener.location.reload();
    closeWindow();
}

function sendEmail()
{
    var email = window.open( "/FriendEmail/","FriendEmail","non-resizable,width=450,height=320");
}

function addIEFavorite()
{
    if ( Browser.Engine.trident && window.external ) {
        external.AddFavorite("http://www.guntrader.co.uk", "guntrader.co.uk");
    }
}

function initHomeLink()
{
    if ( $('header') )
        if ( $('header').getElement('h1') )
            $('header').getElement('h1').addEvent('click',function() { window.location = "/"; } );
}

window.addEvent( 'domready', initHomeLink );

