<?php
class Address extends AppModel {

    var $name = 'Address';

    var $validate = array(
	    'address' => array(
			    'rule' => array('notEmpty'),
			    'message' => 'Please Enter the Addess',
			    'required' => true,
			    'allowEmpty' => false
	    ),

	    'city' => array(
			    'rule' => array('notEmpty'),
			    'message' => 'Please Enter the City',
			    'required' => true,
			    'allowEmpty' => false
	    ),
	    'state_id' => array(
			    'rule' => array('notEmpty'),
			    'message' => 'Please select the State',
			    'required' => true,
			    'allowEmpty' => false
	    ),
	    'postcode' => array(
			    'rule' => array('notEmpty'),
			    'message' => 'Please enter the Post Code',
			    'required' => true,
			    'allowEmpty' => false
	    ),
	    'country_id' => array(
			    'numeric' => array(
					    'rule' => 'numeric',
					    'required' => true,
					    'message' => 'Please choose a Country for this Address',
					    'on' => 'create'
			    ),
			    'checkStates' => array(
					    'rule' => array('checkStates'),
					    'message' => ' Australian States can only be chosen if Australia is selected. Otherwise choose "Overseas" for other Countries ',
					    'on' => 'create')),


	    'type' => array(
			    'rule' => array('notEmpty'),
			    'message' => 'Please select the Type of Address',
			    'required' => true,
			    'allowEmpty' => false
	    ),
    );



    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
	    'Customer' => array('className' => 'Customer',
			    'foreignKey' => 'customer_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''
	    ),

	    'State' => array('className' => 'State',
			    'foreignKey' => 'state_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''),

	    'Country' => array('className' => 'Country',
			    'foreignKey' => 'country_id',
			    'conditions' => '',
			    'fields' => '',
			    'order' => ''


    ));


    /*Custom Validation Rule to Check that Address with Australian States are assigned to Australia.
	 * and that Overseas enquiries are NOT assigned to Australia.
	 * Not Portable at all. This code is only for CMC usage
	 * Should probably change this to use RegExes.
    */

    function checkStates($data) {
	if($this->data['Address']['country_id'] != 1) { //This Enquiry is not for Australia. State must be set to overseas.
	    if($this->data['Address']['state_id'] != 9) { //The State isn't set to Overseas. Return false
		return FALSE;
	    }
	    else {
		return TRUE;
	    }
	}
	if($this->data['Address']['country_id'] == 1) { //This enquiry is for Australia. State must be for an Australian State
	    if($this->data['Address']['state_id'] == 9) {
		return FALSE;
	    }
	    else {
		return TRUE;
	    }
	}
    }

}
?>
