﻿function ChangeReceivalMethod()
{
    $('loader').style.display = 'block';
    
   new Ajax.Request('/Pages/ReceiptType/' + $F('id'),
        {
            method:'get', 		
            parameters: {receipt: $F('receive-method')},						
            
            onSuccess: function(transport)
            {
                $('dwelling-box').update( transport.responseText);
                $('address-box').update( '' );
                $('loader').style.display = 'none';
            }
        });
}

function ChangeCity()
{
    $('loader').style.display = 'block';
    
   new Ajax.Request('/Parts/GetSuburbs/',
        {
            method:'get', 		
            parameters: {city_id: $F('available-cities')},						
            
            onSuccess: function(transport)
            {
                $('suburbs-box').update( transport.responseText);
                $('loader').style.display = 'none';
            }
        });
}

function ChangeDwellingType()
{
    $('loader').style.display = 'block';
    
   new Ajax.Request('/Pages/DwellingType/' + $F('id'),
        {
            method:'get', 		
            parameters: {dwelling: $F('dwelling-type')},						
            
            onSuccess: function(transport)
            {
                $('address-box').update( transport.responseText);
                $('loader').style.display = 'none';
            }
        });
}

Event.observe(window, 'load', function() 
{
    if($('signup-form') != null)
        Event.observe('signup-form', 'submit', createAccount, false);
});

function createCollectionAccount(form)
{
    new Ajax.Request('/account/signup',
    {
        method:'post', 			
        parameters: 
        {
            firstname: form['firstname'].value,
            lastname: form['lastname'].value,
            email_address: form['email-address'].value,
            contact_number: form['contact-number'].value,
            password: form['password'].value,
            confirm_password: form['confirm-password'].value,
            type: 'collection' 
         },					
        
        onSuccess: function(transport) { 
            successFunc(transport); },		
        onFailure: function() { 
            failureFunc(); },
        onException: function(requester, exception){ 
            exceptionFunc(exception.message);}
    });
}

function createNonEstateAccount(form)
{
    new Ajax.Request('/account/signup',
    {
        method:'post', 			
        parameters: 
        {
            firstname: form['firstname'].value,
            lastname: form['lastname'].value,
            email_address: form['email-address'].value,
            contact_number: form['contact-number'].value,
            password: form['password'].value,
            confirm_password: form['confirm-password'].value,
            suburb: form['available-suburbs'].value,
            city: form['available-cities'].value,
            street_name: form['street-name'].value,
            number: form['house-number'].value,
            type: 'nonestate' 
         },					
        
        onSuccess: function(transport) { 
            successFunc(transport); },		
        onFailure: function() { 
            failureFunc(); },
        onException: function(requester, exception){ 
            exceptionFunc(exception.message);}
    });
}

function createEstateAccount(form)
{
    new Ajax.Request('/account/signup',
    {
        method:'post', 			
        parameters: 
        {
            firstname: form['firstname'].value,
            lastname: form['lastname'].value,
            email_address: form['email-address'].value,
            contact_number: form['contact-number'].value,
            password: form['password'].value,
            confirm_password: form['confirm-password'].value,
            suburb: form['available-suburbs'].value,
            city: form['available-cities'].value,
            street_name: form['estate-street-name'].value,
            number: form['unit-number'].value,
            estate_name: form['estate-name'].value,
            internal_street_name: form['internal-street-name'].value,
            type: 'estate' 
         },					
        
        onSuccess: function(transport) { 
            successFunc(transport); },		
        onFailure: function() { 
            failureFunc(); },
        onException: function(requester, exception){ 
            exceptionFunc(exception.message);}
    });
}

function createAccount(e)
{      
    try
    {
        $('loader').style.display = 'block';
        
        $('sign-up-box').addClassName('status');
        $('sign-up-box').update('Please wait while we sign you up ...');

        _form = $('signup-form');

        if( _form['receive-method'].value != "-1")
        {
            if( _form['receive-method'].value == "1")
            {
                    if( _form['dwelling-type'].value != "-1")
                    {
                        switch(_form['address-type'].value)
                        {
                            case 'de':
                                createEstateAccount(_form);
                                break;
                            case 'dp':
                                createNonEstateAccount(_form);
                                break;
                        }
                     }
                    else
                    {
                        errorFunc('Please select either private residence or estate/complex.');
                    }
            }
            else
            {
                if(_form['address-type'].value == 'co')
                {
                    createCollectionAccount(_form);
                }
            }
        }
        else
        {
            errorFunc('Please select either delivery or collection.');
        }

        Event.stop(e);
    }
    catch(err)
    {
        exceptionFunc(err);
       
        Event.stop(e);
    }
}

function successFunc(result)
{
    if( result.responseText.include('id="exception"'))
    {
        errorFunc(result.responseText);
    }
    else
    {
        $('signup-form').update(result.responseText);
        
        if($("loader") != null)
            $('loader').style.display = 'none';
    }
}

function failureFunc()
{
    errorFunc('An error occurred while attemping to sign up. ');
}

function exceptionFunc(exception)
{
    errorFunc('An error occurred while attemping to sign up. ' + exception);
}

function errorFunc(message)
{
    $('error').className = 'error';
    $('error').update('<ul class="error"><li>' + message + '</li></ul>');
    
    $('sign-up-box').className = '';
    $('sign-up-box').update('<button id="sign-up-button" type="submit" tabindex="9">sign up</button>');
    
    if($("loader") != null)
            $('loader').style.display = 'none';
}
